Beatmup
wrapper_nnets.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, 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 "wrapper.h"
20 
21 #include "include/Beatmup_NNets_AbstractOperation.h"
22 #include "include/Beatmup_NNets_DeserializedModel.h"
23 #include "include/Beatmup_NNets_InferenceTask.h"
24 #include "include/Beatmup_NNets_ImageSampler.h"
25 #include "include/Beatmup_NNets_Model.h"
26 #include "include/Beatmup_NNets_Softmax.h"
27 
28 #include "android/context.h"
29 
32 
33 #include <iostream>
34 #include <sstream>
35 
36 /////////////////////////////////////////////////////////////////////////////////////////////
37 // MODEL
38 /////////////////////////////////////////////////////////////////////////////////////////////
39 
40 JNIMETHOD(jstring, serializeToString, Java_Beatmup_NNets_Model, serializeToString)
41  (JNIEnv * jenv, jobject, jlong handle)
42 {
45  return jenv->NewStringUTF(model->serializeToString().c_str());
46 }
47 
48 
49 JNIMETHOD(jlong, countMultiplyAdds, Java_Beatmup_NNets_Model, countMultiplyAdds)
50  (JNIEnv * jenv, jobject, jlong handle)
51 {
54  return (jlong)model->countMultiplyAdds();
55 }
56 
57 
58 JNIMETHOD(jlong, getMemorySize, Java_Beatmup_NNets_Model, getMemorySize)
59  (JNIEnv * jenv, jobject, jlong handle)
60 {
63  return (jlong)model->getMemorySize();
64 }
65 
66 
67 JNIMETHOD(jlong, getNumberOfOperations, Java_Beatmup_NNets_Model, getNumberOfOperations)
68  (JNIEnv * jenv, jobject, jlong handle)
69 {
72  return (jlong)model->getNumberOfOperations();
73 }
74 
75 /////////////////////////////////////////////////////////////////////////////////////////////
76 // DESERIALIZED MODEL
77 /////////////////////////////////////////////////////////////////////////////////////////////
78 
79 JNIMETHOD(jlong, newDeserializedModel, Java_Beatmup_NNets_DeserializedModel, newDeserializedModel)
80  (JNIEnv * jenv, jclass, jobject jCtx, jstring str)
81 {
86  auto model = new Beatmup::NNets::DeserializedModel(*ctx, strStr);
87  $pool.addJavaReference(jenv, jCtx, model); // model needs context in destruction
88  return (jlong)model;
89  });
91 }
92 
93 /////////////////////////////////////////////////////////////////////////////////////////////
94 // ABSTRACT OPERATION
95 /////////////////////////////////////////////////////////////////////////////////////////////
96 
97 JNIMETHOD(jstring, getName, Java_Beatmup_NNets_AbstractOperation, getName)
98  (JNIEnv * jenv, jobject, jlong handle)
99 {
102  return jenv->NewStringUTF(op->getName().c_str());
103 }
104 
105 
106 JNIMETHOD(jlong, countMultiplyAdds, Java_Beatmup_NNets_AbstractOperation, countMultiplyAdds)
107  (JNIEnv * jenv, jobject, jlong handle)
108 {
111  return (jlong)op->countMultiplyAdds();
112 }
113 
114 
115 JNIMETHOD(jlong, getOperationFromModel, Java_Beatmup_NNets_AbstractOperation, getOperationFromModel)
116  (JNIEnv * jenv, jclass, jlong handle, jstring name)
117 {
121  try {
122  return (jlong)&model->getOperation(nameStr);
123  }
125  $pool.throwToJava(jenv, "java/lang/IllegalArgumentException", ex.what());
126  }
128 }
129 
130 /////////////////////////////////////////////////////////////////////////////////////////////
131 // SOFTMAX
132 /////////////////////////////////////////////////////////////////////////////////////////////
133 
134 JNIMETHOD(jfloatArray, getProbabilities, Java_Beatmup_NNets_Softmax, getProbabilities)
135  (JNIEnv * jenv, jclass, jlong handle)
136 {
139  const auto& proba = softmax->getProbabilities();
140  jfloatArray result = jenv->NewFloatArray(proba.size());
141  jfloat *outptr = jenv->GetFloatArrayElements(result, nullptr);
142  for (size_t i = 0; i < proba.size(); ++i)
143  outptr[i] = proba[i];
144  jenv->ReleaseFloatArrayElements(result, outptr, 0);
145  return result;
146 }
147 
148 /////////////////////////////////////////////////////////////////////////////////////////////
149 // IMAGE SAMPLER
150 /////////////////////////////////////////////////////////////////////////////////////////////
151 
152 JNIMETHOD(void, setRotation, Java_Beatmup_NNets_ImageSampler, setRotation)
153  (JNIEnv * jenv, jclass, jlong handle, jint rotation)
154 {
157  imageSampler->setRotation((int)rotation);
158 }
159 
160 
161 JNIMETHOD(jint, getRotation, Java_Beatmup_NNets_ImageSampler, getRotation)
162  (JNIEnv * jenv, jclass, jlong handle)
163 {
166  return (jint)imageSampler->getRotation();
167 }
168 
169 /////////////////////////////////////////////////////////////////////////////////////////////
170 // INFERENCE TASK
171 /////////////////////////////////////////////////////////////////////////////////////////////
172 
173 JNIMETHOD(jlong, newInferenceTask, Java_Beatmup_NNets_InferenceTask, newInferenceTask)
175 {
180  $pool.addJavaReference(jenv, jModel, task); // inference task needs model
181  $pool.addJavaReference(jenv, jData, task); // inference task needs data
182  return (jlong)task;
183 }
184 
185 
186 JNIMETHOD(void, connectByName, Java_Beatmup_NNets_InferenceTask, connectByName)
187  (JNIEnv * jenv, jobject, jlong handle, jobject jBitmap, jstring opName, jint index)
188 {
193  try {
194  task->connect(*bitmap, opNameStr, (int)index);
195  }
196  catch (Beatmup::InvalidArgument& ex) {
197  $pool.throwToJava(jenv, "java/lang/IllegalArgumentException", ex.what());
198  }
199 }
200 
201 
202 JNIMETHOD(void, connectByHandle, Java_Beatmup_NNets_InferenceTask, connectByHandle)
204 {
209  task->connect(*bitmap, *op, (int)index);
210 }
static void throwToJava(JNIEnv *jenv, const char *exceptionClass, const char *message)
Throws a specific exception.
Definition: objectpool.h:156
void addJavaReference(JNIEnv *jenv, jobject jobj, const Beatmup::Object *bobj)
Creates new global reference on a Java object to avoid garbage collecting.
Definition: objectpool.h:105
static const jlong INVALID_HANDLE
Definition: objectpool.h:61
A very basic class for any image.
Android context.
Definition: context.h:32
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
Definition: chunkfile.h:36
virtual const char * what() const NOEXCEPT override
Definition: exception.h:56
Abstract neural net operation (layer).
Definition: operation.h:46
Model reconstructed from a serialized representation.
Image preprocessing operation.
Definition: image_sampler.h:32
Task running inference of a Model.
void connect(AbstractBitmap &image, AbstractOperation &operation, int inputIndex=0)
Connects an image to a specific operation input.
Neural net model.
Definition: model.h:92
Softmax layer.
Definition: softmax.h:32
#define BEATMUP_ENTER
Definition: wrapper.h:35
#define JNIMETHOD(R, N, C, O)
Definition: wrapper.h:28
BeatmupJavaObjectPool $pool
return() jlong(listener)
Beatmup::Context * ctx
jlong jint op
JNIEnv jlong jstring name
JNIEnv jobject jobject jData
JNIEnv jlong jint rotation
JNIEnv jobject jstring str
JNIEnv jlong handle
JNIEnv * jenv
JNIEnv jclass
BEATMUP_CATCH({ auto model=new Beatmup::NNets::DeserializedModel(*ctx, strStr); $pool.addJavaReference(jenv, jCtx, model);return(jlong) model;})
JNIEnv jobject jModel
const auto & proba
jfloat * outptr
JNIEnv jlong jobject jBitmap
JNIEnv jobject jCtx
JNIEnv jlong jobject jstring jint index
JNIEnv jlong jobject jlong hOp
JNIEnv jlong jobject jstring opName
BEATMUP_OBJ(Beatmup::NNets::Model, model, handle)
Beatmup::NNets::InferenceTask * task
JNIEnv jobject
imageSampler setRotation((int) rotation)
jfloatArray result
task connect * bitmap(int) index
BEATMUP_STRING(str)