Output image is weired while trying to inference esrgan, Someone help me please.
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- android, java
- Domain
- machine-learning, mobile-dev
Research direction
Start by reproducing the output using the ImageInference and MainActivity snippets with realesr-general-x4v3-fp32.onnx and LR.png. Inspect the bitmap-to-tensor and tensor-to-bitmap paths, then compare the reported input and output shapes with the model requirements. Done means the Android example produces a correctly displayed ESRGAN output and documents the required preprocessing or postprocessing.
Written by the indexing model from the issue text.
Description
I'm trying to inference real esrgan, but cant able to get output correctly.
I'm using com.microsoft.onnxruntime:onnxruntime-android:1.17.3
Model Link: Google Drive
Inference Class:
public class ImageInference {
private static final String TAG = "ImageInference";
// Load the image from assets
public static Bitmap loadImageFromAssets(Context context, String fileName) throws IOException {
Log.d(TAG, "Loading image from assets: " + fileName);
InputStream is = context.getAssets().open(fileName);
Bitmap image = BitmapFactory.decodeStream(is);
if (image != null) {
Log.d(TAG, "Image loaded successfully: " + fileName);
} else {
Log.d(TAG, "Failed to load image: " + fileName);
}
return image;
}
// Convert Bitmap to FloatBuffer for ONNX Runtime
public static FloatBuffer bitmapToFloatBuffer(Bitmap bitmap, float mean, float std) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.d(TAG, "Preparing to convert bitmap to FloatBuffer. Width: " + width + ", Height: " + height);
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
FloatBuffer buffer = FloatBuffer.allocate(width * height * 3);
for (final int val : pixels) {
buffer.put(((val >> 16) & 0xFF) / 255.f - mean / std); // RED
buffer.put(((val >> 8) & 0xFF) / 255.f - mean / std); // GREEN
buffer.put((val & 0xFF) / 255.f - mean / std); // BLUE
}
buffer.flip(); // Prepare buffer for reading
Log.d(TAG, "Bitmap successfully converted to FloatBuffer.");
return buffer;
}
public static Bitmap tensorToBitmap(float[][][][] tensor) {
// Assume tensor dimensions are [1][3][height][width]
int height = tensor[0][0].length;
int width = tensor[0][0][0].length;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = (int) (tensor[0][0][y][x] * 255);
int g = (int) (tensor[0][1][y][x] * 255);
int b = (int) (tensor[0][2][y][x] * 255);
pixels[y * width + x] = 0xFF000000 | (r << 16) | (g << 8) | b;
}
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static float[][][][] runInference(Context context, String modelPath, Bitmap image) throws OrtException, IOException {
InputStream is = context.getAssets().open(modelPath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = is.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
byte[] modelBytes = bos.toByteArray();
Log.d(TAG, "Starting inference with model: " + modelPath);
OrtEnvironment env = OrtEnvironment.getEnvironment();
InputStream modelInputStream = context.getAssets().open(modelPath);
OrtSession session = env.createSession(modelBytes, new OrtSession.SessionOptions());
Log.d(TAG, "Model and Session created successfully." );
try {
int width = image.getWidth();
int height = image.getHeight();
FloatBuffer inputBuffer = bitmapToFloatBuffer(image, 0f, 1f);
OnnxTensor tensor = OnnxTensor.createTensor(env, inputBuffer, new long[]{1, 3, height, width});
OrtSession.Result results = session.run(Collections.singletonMap("input", tensor));
float[][][][] output = (float[][][][]) results.get(0).getValue();
tensor.close();
Log.d(TAG, "Inference completed successfully.");
return output;
} finally {
session.close();
modelInputStream.close();
env.close();
Log.d(TAG, "Cleaned up ONNX resources.");
}
}
}
Main Activity
public class MainActivity extends Activity {
private ImageView orginalImageView;
private ImageView outputImageView;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orginalImageView = findViewById(R.id.orginalImage);
outputImageView = findViewById(R.id.outputImage);
textViewResult = findViewById(R.id.textViewResult);
try {
Bitmap image = ImageInference.loadImageFromAssets(this, "LR.png");
orginalImageView.setImageBitmap(image);
float[][][][] output = ImageInference.runInference(this, "realesr-general-x4v3-fp32.onnx", image);
if (output != null && output.length > 0 && output[0].length > 0 && output[0][0].length > 0) {
int height = output[0][0].length;
int width = output[0][0][0].length;
Bitmap outputImage = ImageInference.tensorToBitmap(output);
outputImageView.setImageBitmap(outputImage); // Display the output image
textViewResult.setText("Inference complete with output shape: " + "Height " + height + " Width " + width);
} else {
textViewResult.setText("Inference complete but no valid output!");
}
} catch (Exception e) {
textViewResult.setText("Inference failed: " + e.getMessage());
e.printStackTrace();
}
}
- Dominant language
- C++
- Stars
- 1.7k
- Forks
- 417
- PR merge metrics
- No merged PRs in 30d
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/onnxruntime-inference-examples
-
Replace deprecated light-server with lite-server for onnxruntime-webPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Minor: typoPossibly taken @Hashim1999164 claimed this 66 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
microsoft/onnxruntime-inference-examples#124 · 1 reaction ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 38/100
-
Package "onnxruntime-react-native" does not contain a valid config pluginPossibly taken @reginaldalfret claimed this 24 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 42/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
All issues in microsoft/onnxruntime-inference-examples
Similar issues
-
Web3 RPC: one-element batch request returns an object instead of an array (breaks ethers.js v6)Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
FISCO-BCOS/FISCO-BCOS#5706 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
521xueweihan/HelloGitHub#4011 ·
-
docs(simulation): Quickstart publishes UDP 14550, which the SITL images' own docs say not to doOpenstatus:needs-triage
Difficulty 1/5 1-3 hours Newbie friendliness 78/100
PX4/PX4-Autopilot#29292 · 1 comment ·
Maintainers usually reply within 1 day
-
bug core p3
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
RunanywhereAI/runanywhere-sdks#1142 ·
Maintainers usually reply within 1 day
-
I/O internal correctness medium priority rt_lib
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day