我正在尝试在颤振中使用放大模型来推断图像。但输出图像不符合预期。 它似乎在标准化或输出到图像方面存在问题。
原图:https://i.sstatic.net/AJX5gj48.png
输出图像:https://i.sstatic.net/A2u3ahd8.png
模型形状:
Model input shape: ['batch_size', 3, 'width', 'height']
Model output shape: ['batch_size', 3, 'width', 'height']
日志:
flutter: Is normalized: true
flutter: Image normalized successfully.
flutter: Input tensor created successfully.
flutter: Width: 1428, Height: 804, Channel: 3
我尝试了这段代码,但现在超出了范围。
Future<void> inference() async {
if (selectedImage == null) {
debugPrint('No image selected');
return;
}
if (selectedImage != null) {
Float32List? floatData;
try {
img.Image normalizedImage =
img.normalize(selectedImage!, min: 0, max: 255);
Uint8List imageData = normalizedImage.getBytes(order: img.ChannelOrder.rgb);
floatData = Float32List.fromList(
imageData.map((byte) => byte / 255.0).toList());
debugPrint("Is normalized: ${isNormalized(floatData)}");
} catch (e) {
debugPrint("Error during normalization: $e");
}
final shape = [1, 3, selectedImage!.width, selectedImage!.height];
debugPrint('Image normalized successfully.');
final inputOrt =
OrtValueTensor.createTensorWithDataList(floatData!, shape);
final inputs = {'input': inputOrt};
debugPrint('Input tensor created successfully.');
final runOptions = OrtRunOptions();
final outputs = await ortSession.runAsync(runOptions, inputs);
inputOrt.release();
runOptions.release();
outputs?.forEach((element) {
final outputValue = element?.value;
if (outputValue is List<List<List<List<double>>>>) {
img.Image generatedImage = generateImageFromOutput(outputValue);
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
child: SizedBox(
width: generatedImage.width.toDouble(),
height: generatedImage.height.toDouble(),
child: Image.memory(
Uint8List.fromList(img.encodePng(generatedImage)),
fit: BoxFit.contain,
),
),
);
},
);
} else {
debugPrint("Output is of unknown type");
}
element?.release();
});
}
}
img.Image generateImageFromOutput(
List<List<List<List<double>>>> outputValue) {
int width = outputValue[0][0].length;
int height = outputValue[0][0][0].length;
int channel = outputValue[0].length;
print("Width: $width, Height: $height, Channel: $channel");
// Create the image
img.Image generatedImage = img.Image(width: width, height: height);
// Set pixel values
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Extract RGB values from the output tensor data
int r = (outputValue[0][0][x][y] * 255).toInt().clamp(0, 255);
int g = (outputValue[0][1][x][y] * 255).toInt().clamp(0, 255);
int b = (outputValue[0][2][x][y] * 255).toInt().clamp(0, 255);
// Set pixel value in the generated image
generatedImage.setPixelRgb(x, y, r, g, b);
}
}
return generatedImage;
}
尝试更新内部 for 循环。
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Extract RGB values from the output tensor data
double r = outputValue[0][0][0][y * width + x];
double g = outputValue[0][0][1][y * width + x];
double b = outputValue[0][0][2][y * width + x];
// Scale and clamp the values
int r_scaled = (r.clamp(0, 1) * 255).toInt();
int g_scaled = (g.clamp(0, 1) * 255).toInt();
int b_scaled = (b.clamp(0, 1) * 255).toInt();
// Set pixel value in the generated image
generatedImage.setPixel(x, y, img.getColor(r_scaled, g_scaled, b_scaled));
}
}