У меня есть модель gan_generator.tflite. Я могу сделать вывод об этом и получить с его помощью правильный вывод в Python. Но я не могу получить тот же результат, что и растровое изображение в Android Kotlin, даже если я выполнил те же операции постобработки. Где я пропустил?
Код: Выделить всё
from model.srgan import generator
from utils import load_image, plot_sample
from model import resolve_single
import tensorflow as tf
model = generator()
model.load_weights('weights/srgan/gan_generator.h5')
# Create a TFLite converter from the Keras model
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Set conversion parameters (optional)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float32]
# Convert the model
tflite_model = converter.convert()
# Save the TFLite model to a file
with open('gan_generator.tflite', 'wb') as f:
f.write(tflite_model)
interpreter = tf.lite.Interpreter(model_path='gan_generator.tflite')
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
input_data = np.asarray(load_image('demo/0869x4-crop.png'), dtype=np.float32)
input_data = np.expand_dims(input_data, axis=0)
input_data = np.reshape(input_data, (1, input_data.shape[1], input_data.shape[2], 3))
# Set the input tensor
interpreter.resize_tensor_input(input_details[0]['index'], (1, input_data.shape[1], input_data.shape[2], 3),strict=True)
interpreter.allocate_tensors()
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
# Get the output
output_data = interpreter.get_tensor(output_details[0]['index'])
output_data = np.squeeze(output_data)
output_data = np.clip(output_data, 0, 255).astype(np.uint8)
# Create a PIL Image from the NumPy array
image = Image.fromarray(output_data)
image #output image created here successfully
Код: Выделить всё
val tensorImage = TensorImage(INPUT_IMAGE_TYPE).also { it.load(inputBitmap) }
val processedImage = imageProcessor.process(tensorImage)
//[1,4X,4X,3] Output
val outputBuffer = TensorBuffer.createFixedSize(
intArrayOf(
1,
processedImage.width * outputShapeForScaling,
processedImage.height * outputShapeForScaling,
3
),
OUTPUT_IMAGE_TYPE
)
// [1, None, None, 3] dynamic input
interpreter!!.resizeInput(
interpreter!!.getInputTensor(0).index(),
intArrayOf(1, processedImage.width, processedImage.height, 3)
)
interpreter!!.allocateTensors()
interpreter!!.run(processedImage.buffer, outputBuffer.buffer)
val processedOutput = processOutput(
outputBuffer,
width = processedImage.width * 4,
height = processedImage.height * 4
)
Код: Выделить всё
private fun processOutput(outputBuffer: TensorBuffer, width: Int, height: Int): Bitmap {
val data = outputBuffer.floatArray
// Check that the float array has the correct number of elements
if (data.size != width * height * 3) {
throw IllegalArgumentException("Data size does not match the expected image size.")
}
// Create an empty Bitmap
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
// Iterate through the float array and set pixels in the bitmap
var index: Int
for (y in 0 until height) {
for (x in 0 until width) {
index = (y * width + x) * 3
// Extract RGB values from the float array
val r = data[index].coerceIn(0f, 255f).toInt()
val g = data[index + 1].coerceIn(0f, 255f).toInt()
val b = data[index + 2].coerceIn(0f, 255f).toInt()
// Set the pixel color (we set alpha to 255 for full opacity)
bitmap.setPixel(x, y, Color.rgb(r, g, b))
}
}
return bitmap
}

Подробнее здесь: https://stackoverflow.com/questions/791 ... on-version