Beatmup
binding_tools.cpp
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 #include "binding_tools.hpp"
20 
21 
22 py::object Beatmup::Python::getModelOutputDataByOp(NNets::Model& model, const NNets::AbstractOperation& operation, int output) {
23  size_t numSamples;
24  auto data = model.getOutputData(numSamples, operation, output);
25  auto size = operation.getOutputSize(output);
26  if (!data)
27  return py::none();
28  if (size[0] == 1 && size[2] == 1)
29  // column vector got, return flat
30  return py::array_t<float>({ size[1] }, { sizeof(float) }, data);
31  else
32  return py::array_t<float>(
33  { size[1], size[0], size[2] }, // "H, W, C"
34  { size[0] * size[2] * sizeof(float), size[2] * sizeof(float), sizeof(float) },
35  data
36  );
37 }
38 
39 
40 py::object Beatmup::Python::getModelOutputDataByName(NNets::Model& model, const std::string& opName, int output) {
41  return getModelOutputDataByOp(model, model.getOperation(opName), output);
42 }
Abstract neural net operation (layer).
Definition: operation.h:46
virtual Size getOutputSize(int outputIndex=0) const
Returns full size of a specific operation output.
Definition: operation.cpp:32
Neural net model.
Definition: model.h:92
OperationClass & getOperation(const std::string &operationName)
Retrieves an operation by its name.
Definition: model.h:303
const float * getOutputData(size_t &numSamples, const std::string &operation, int output=0) const
Reads data from the model memory.
Definition: model.cpp:125
py::object getModelOutputDataByOp(NNets::Model &model, const NNets::AbstractOperation &operation, int output)
py::object getModelOutputDataByName(NNets::Model &model, const std::string &opName, int output)
jlong jobject size
JNIEnv jlong jobject jstring opName