Beatmup
Beatmup::NNets::Classifier Class Reference

Image classifier base class. More...

#include <classifier.h>

Inheritance diagram for Beatmup::NNets::Classifier:
Beatmup::NNets::Model Beatmup::NNets::InferenceTask Beatmup::GL::ProgramBank Beatmup::GpuTask Beatmup::BitmapContentLock Beatmup::Object Beatmup::AbstractTask Beatmup::Object

Public Member Functions

 Classifier (Context &context, ChunkCollection &data)
 Creates a Classifier instance. More...
 
 ~Classifier ()
 
const std::vector< float > & operator() (AbstractBitmap &input)
 Classifies an image (blocking). More...
 
Job start (AbstractBitmap &input)
 Initiates the classification of a given image. More...
 
const std::vector< float > & getProbabilities () const
 Returns the last classification results. More...
 
- Public Member Functions inherited from Beatmup::NNets::Model
 Model (Context &context, std::initializer_list< AbstractOperation * > ops)
 Instantiates a model from a list of operations interconnecting them in a feedforward fashion. More...
 
 Model (Context &context)
 Instantiates an empty model. More...
 
 ~Model ()
 
void append (AbstractOperation *newOp, bool connect=false)
 Adds a new operation to the model. More...
 
void append (std::initializer_list< AbstractOperation * > newOps, bool connect=false)
 Adds new operations to the model. More...
 
void addOperation (const std::string &opName, AbstractOperation *newOp)
 Adds a new operation to the model before another operation in the execution order. More...
 
void addOperation (const AbstractOperation &operation, AbstractOperation *newOp)
 
void addConnection (const std::string &sourceOpName, const std::string &destOpName, int output=0, int input=0, int shuffle=0)
 Adds a connection between two given ops. More...
 
void addOutput (const std::string &operation, int output=0)
 Enables reading output data from the model memory through getOutputData(). More...
 
void addOutput (const AbstractOperation &operation, int output=0)
 
const float * getOutputData (size_t &numSamples, const std::string &operation, int output=0) const
 Reads data from the model memory. More...
 
const float * getOutputData (size_t &numSamples, const AbstractOperation &operation, int output=0) const
 
virtual void prepare (GraphicPipeline &gpu, ChunkCollection &data)
 Prepares all operations: reads the model data from chunks and builds GPU programs. More...
 
bool isReady () const
 
void execute (TaskThread &thread, GraphicPipeline *gpu)
 Runs the inference. More...
 
bool isOperationInModel (const AbstractOperation &operation) const
 Checks if a specific operation makes part of the model. More...
 
AbstractOperationgetFirstOperation ()
 
AbstractOperationgetLastOperation ()
 
const AbstractOperationgetFirstOperation () const
 
const AbstractOperationgetLastOperation () const
 
size_t getNumberOfOperations () const
 
template<class OperationClass = AbstractOperation>
OperationClass & getOperation (const std::string &operationName)
 Retrieves an operation by its name. More...
 
const ProgressTrackinggetPreparingProgress () const
 Returns model preparation progress tracking. More...
 
const ProgressTrackinggetInferenceProgress () const
 Returns inference progress tracking. More...
 
unsigned long countMultiplyAdds () const
 Provides an estimation of the number of multiply-adds characterizing the model complexity. More...
 
unsigned long countTexelFetches () const
 Provides an estimation of the total number of texels fetched by all the operations in the model per image. More...
 
size_t getMemorySize () const
 Returns the amount of texture memory in bytes currently allocated by the model to run the inference. More...
 
Listing serialize () const
 Returns serialized representation of the model as a Listing. More...
 
std::string serializeToString () const
 Returns serialized representation of the model as a string. More...
 
void setProfiler (Profiler *profiler)
 Attaches a profiler instance to meter the execution time per operation during the inference. More...
 
- Public Member Functions inherited from Beatmup::GL::ProgramBank
 ProgramBank (Context &context)
 
 ~ProgramBank ()
 
GL::RenderingProgramoperator() (GraphicPipeline &gpu, const std::string &code, bool enableExternalTextures=false)
 Provides a program given a fragment shader source code. More...
 
void release (GraphicPipeline &gpu, GL::RenderingProgram *program)
 Marks a program as unused any more. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 
- Public Member Functions inherited from Beatmup::NNets::InferenceTask
 InferenceTask (Model &model, ChunkCollection &data)
 
void connect (AbstractBitmap &image, AbstractOperation &operation, int inputIndex=0)
 Connects an image to a specific operation input. More...
 
void connect (AbstractBitmap &image, const std::string &operation, int inputIndex=0)
 

Protected Attributes

Contextcontext
 
- Protected Attributes inherited from Beatmup::NNets::Model
std::vector< AbstractOperation * > ops
 model operations More...
 
ProgressTracking preparingProgress
 model preparation progress More...
 
ProgressTracking inferenceProgress
 inference progress More...
 
bool ready
 if true, ops are connected to each other and storages are allocated More...
 
- Protected Attributes inherited from Beatmup::GL::ProgramBank
Contextcontext
 
- Protected Attributes inherited from Beatmup::NNets::InferenceTask
ChunkCollectiondata
 
Modelmodel
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 
- Protected Member Functions inherited from Beatmup::NNets::Model
void freeMemory ()
 Frees all allocated storages. More...
 
StorageallocateStorage (GraphicPipeline &gpu, const Size size, bool forGpu=true, bool forCpu=false, const int pad=0, const int reservedChannels=0)
 Allocates a new storage. More...
 
StorageallocateFlatStorage (GraphicPipeline &gpu, const int size)
 Allocates a new flat storage. More...
 
GL::VectorallocateVector (GraphicPipeline &gpu, const int size)
 Allocates a vector that can be used as operation input or output. More...
 
InternalBitmapallocateTexture (GraphicPipeline &gpu, const Size size)
 Allocates a texture that can be used as operation input or output. More...
 
bool isPreceding (const AbstractOperation &first, const AbstractOperation &second) const
 Checks whether an operation goes before another operation in the model according the ops execution order. More...
 
AbstractOperationoperator[] (const std::string &operationName)
 
const AbstractOperationoperator[] (const std::string &operationName) const
 
void addConnection (AbstractOperation &source, AbstractOperation &dest, int output=0, int input=0, int shuffle=0)
 

Detailed Description

Image classifier base class.

Combines a runnable InferenceTask with a Model assuming Conv2D and Softmax are its first and last operations respectively.

Definition at line 35 of file classifier.h.

Constructor & Destructor Documentation

◆ Classifier()

Classifier::Classifier ( Context context,
ChunkCollection data 
)

Creates a Classifier instance.

Parameters
contextA context instance Classifier will use to store its internal data
dataA ChunkCollection to access to the classifier model data

Definition at line 29 of file classifier.cpp.

29  :
30  Model(context),
31  InferenceTask(*this, data),
33 {}
InferenceTask(Model &model, ChunkCollection &data)
Model(Context &context, std::initializer_list< AbstractOperation * > ops)
Instantiates a model from a list of operations interconnecting them in a feedforward fashion.
Definition: model.cpp:27

◆ ~Classifier()

Classifier::~Classifier ( )

Definition at line 36 of file classifier.cpp.

36  {
37  for (auto op : ops)
38  delete op;
39  ops.clear();
40 }
std::vector< AbstractOperation * > ops
model operations
Definition: model.h:122
jlong jint op

Member Function Documentation

◆ operator()()

const std::vector< float > & Classifier::operator() ( AbstractBitmap input)

Classifies an image (blocking).

The very first call includes the model preparation and might be slow as hell. Subsequent calls only run the inference and are likely much faster.

Parameters
[in]inputThe input image
Returns
a vector of probabilities per class.

Definition at line 43 of file classifier.cpp.

43  {
44  connect(input, *ops[0], 0);
45  context.performTask(*this);
46  return getProbabilities();
47 }
float performTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Performs a given task.
Definition: context.cpp:240
const std::vector< float > & getProbabilities() const
Returns the last classification results.
Definition: classifier.h:69
void connect(AbstractBitmap &image, AbstractOperation &operation, int inputIndex=0)
Connects an image to a specific operation input.

◆ start()

Job Classifier::start ( AbstractBitmap input)

Initiates the classification of a given image.

The call is non-blocking.

Parameters
[in]inputThe input image
Returns
a job corresponding to the submitted task.

Definition at line 50 of file classifier.cpp.

50  {
51  connect(input, *ops[0], 0);
52  return context.submitTask(*this);
53 }
Job submitTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Adds a new task to the jobs queue.
Definition: context.cpp:248

◆ getProbabilities()

const std::vector<float>& Beatmup::NNets::Classifier::getProbabilities ( ) const
inline

Returns the last classification results.

Returns
a vector of probabilities per class.

Definition at line 69 of file classifier.h.

69  {
70  return static_cast<const Softmax&>(getLastOperation()).getProbabilities();
71  }
AbstractOperation & getLastOperation()
Definition: model.h:294

Member Data Documentation

◆ context

Context& Beatmup::NNets::Classifier::context
protected

Definition at line 37 of file classifier.h.


The documentation for this class was generated from the following files: