Need help for super-resolution for onnx like tflite implemention (Not issue)
md-rifatkhan opened this issue · 0 comments
md-rifatkhan commented
I know this is tflite community but actually i need help, struggling over 2 week for upscale but none helped :),
I can successfullly upscale with tflite example,
now I've tried to inference onnx version of esrgan (1, 3, height, width) - tflite was (1, height, width, 3),
The problem is output color is none and image showing 9 time in frame
Future<void> processImage() async {
if (imagePath != null) {
// Read image bytes from file
final imageData = await rootBundle.load(imagePath!);
// Decode image using package:image/image.dart (https://pub.dev/image)
final image = img.decodeImage(imageData.buffer.asUint8List())!;
// Resize the image to the required input size [50, 50]
final resizedImage = img.copyResize(image, width: 50, height: 50);
// Get image matrix representation [3, 50, 50]
final imageMatrix = List.generate(
3,
(c) => List.generate(
resizedImage.height,
(y) => List.generate(
resizedImage.width,
(x) {
final pixel = resizedImage.getPixel(x, y);
return c == 0
? pixel.r / 255.0
: c == 1
? pixel.g / 255.0
: pixel.b / 255.0;
},
),
),
);
// Run model inference
await runInference(imageMatrix);
}
}
// Run inference
Future<void> runInference(
List<List<List<double>>> imageMatrix,
) async {
// Set tensor input [1, 50, 50, 3]
final int height = imageMatrix[0].length;
final int width = imageMatrix[0][0].length;
final Float32List floatImageMatrix = Float32List(3 * height * width);
int index = 0;
for (int c = 0; c < 3; c++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
floatImageMatrix[index++] = imageMatrix[c][y][x].toDouble();
}
}
}
// Set tensor input [1, 3, 50, 50]
final inputShape = [1, 3, 50, 50];
final inputTensor = onnx.OrtValueTensor.createTensorWithDataList(floatImageMatrix, inputShape);
// Create input map
final inputs = {'input': inputTensor};
onnx.OrtRunOptions ortRunOptions = onnx.OrtRunOptions();
// Perform inference
print("Running inference");
final outputs = await session.runAsync(ortRunOptions, inputs);
print("Inference complete");
// Get the output tensor
final gg = outputs?[0]?.value;
final result = gg as List<List<List<List<double>>>>;
final outputMatrix = result.first;
print("0 ${outputMatrix[0].length}");
print("1 ${outputMatrix[1].length}");
print("2 ${outputMatrix[2].length}");
// Convert output matrix to image
final buffer = Uint8List.fromList(outputMatrix
.expand(
(col) => col.expand(
(pixel) => pixel.map((e) => (e * 255).toInt()),
),
)
.toList());
// Build image from matrix
final outputImage = img.Image.fromBytes(
width: 200,
height: 200,
bytes: buffer.buffer,
numChannels: 3,
);
// Encode image in jpeg format
imageResult = img.encodeJpg(outputImage);
setState(() {});
}
