Beatmup
classifier.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 #include "model.h"
22 #include "inference_task.h"
23 #include "../bitmap/abstract_bitmap.h"
24 #include "softmax.h"
25 #include <vector>
26 #include <initializer_list>
27 
28 
29 namespace Beatmup {
30  namespace NNets {
31  /**
32  Image classifier base class.
33  Combines a runnable InferenceTask with a Model assuming Conv2D and Softmax are its first and last operations respectively.
34  */
35  class Classifier : public Model, public InferenceTask {
36  protected:
38 
39  public:
40  /**
41  Creates a Classifier instance.
42  \param context A context instance Classifier will use to store its internal data
43  \param data A ChunkCollection to access to the classifier model data
44  */
46  ~Classifier();
47 
48  /**
49  Classifies an image (blocking).
50  The very first call includes the model preparation and might be slow as hell. Subsequent calls only run the inference and are likely
51  much faster.
52  \param[in] input The input image
53  \return a vector of probabilities per class.
54  */
55  const std::vector<float>& operator()(AbstractBitmap& input);
56 
57  /**
58  Initiates the classification of a given image.
59  The call is non-blocking.
60  \param[in] input The input image
61  \return a job corresponding to the submitted task.
62  */
63  Job start(AbstractBitmap& input);
64 
65  /**
66  Returns the last classification results.
67  \return a vector of probabilities per class.
68  */
69  inline const std::vector<float>& getProbabilities() const {
70  return static_cast<const Softmax&>(getLastOperation()).getProbabilities();
71  }
72  };
73  }
74 }
A very basic class for any image.
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
Definition: chunkfile.h:36
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Image classifier base class.
Definition: classifier.h:35
const std::vector< float > & getProbabilities() const
Returns the last classification results.
Definition: classifier.h:69
Classifier(Context &context, ChunkCollection &data)
Creates a Classifier instance.
Definition: classifier.cpp:29
const std::vector< float > & operator()(AbstractBitmap &input)
Classifies an image (blocking).
Definition: classifier.cpp:43
Job start(AbstractBitmap &input)
Initiates the classification of a given image.
Definition: classifier.cpp:50
Task running inference of a Model.
Neural net model.
Definition: model.h:92
AbstractOperation & getLastOperation()
Definition: model.h:294
Softmax layer.
Definition: softmax.h:32
int Job
Definition: parallelism.h:69