Issue
i created a model with Keras and have
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16,(3,3), activation = 'relu', input_shape=(size,size,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32,(3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128,(3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation = 'relu'),
tf.keras.layers.Dense(512, activation = 'relu'),
tf.keras.layers.Dense(2, activation='softmax',name='predictions')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
my predict in Python:
print(model.predict(x))
return:
[[1. 0.]]
and convert model to tflite:
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
And when I implement this in kotlin to app:
val resizeBitmap: Bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, true)
val model = com.example.iscanner.ml.Model.newInstance(context)
val theBuffer = TensorImage.fromBitmap(resizeBitmap)
val byteBuffer = theBuffer.buffer
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 150, 150, 3), DataType.FLOAT32)
inputFeature0.loadBuffer(byteBuffer)
val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer
val tab = outputFeature0.floatArray
model.close()
Log.d("Tab_test[0]", " "+ tab[0])
Log.d("Tab_test[1]", " "+ tab[1])
Have this:
D/Tab_test[0]: NaN
D/Tab_test[1]: NaN
Anyone know where the problem could be? And how do I fix it?
It implements a different network and the type is DataType.UINT8 and here is DataType.FLOAT32 Could it be because of this?
Solution
its help, everything works correctly
val resizeBitmap: Bitmap = Bitmap.createScaledBitmap(bitmap, 150, 150, true)
val model = com.example.iscanner.ml.Model.newInstance(context)
val bytebuffer = ByteBuffer.allocateDirect(4*150*150*3)
bytebuffer.order(ByteOrder.nativeOrder())
val intValues = IntArray(150*150)
bitmap.getPixels(intValues,0,resizeBitmap.width,0,0,resizeBitmap.width,resizeBitmap.height)
var pixel = 0
for( i in 0 .. 149){
for(j in 0..149){
val tmpVal = intValues[pixel++]
bytebuffer.putFloat(((tmpVal shr 16) and 0xFF)*(1.0f/1))
bytebuffer.putFloat(((tmpVal shr 8) and 0xFF)*(1.0f/1))
bytebuffer.putFloat((tmpVal and 0xFF)*(1.0f/1))
}
}
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 150, 150, 3), DataType.FLOAT32)
inputFeature0.loadBuffer(bytebuffer)
val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer
val tab = outputFeature0.floatArray
model.close()
Answered By - macekmacek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.