Repository: opencv
Issue: DNN Input Remains The Same Even After Setting New Input
Labels: bug,category: java bindings,category: dnn

### System Information

OpenCV Java Version 4.10.0
Operating System: Fedora 39
Java Version 17


### Detailed description

I am successfully loading ResNet50 into a `Net` object with OpenCV's DNN module and running inference on an image. However, when i set the input again with the `setInput()` method, the output of the model does not change. Moreover, if I reload the model before setting the input again, the output does change and I get the expected results.

In this example, the `main()` method is calculating the cosine similarity. The similarity will be 1.0, as the input remains the same on the model. However, if the `model` object is re-instantiated after the first call to `embed()`, then the cosine similarity will be a more reasonable number such as 0.5.

Another way I was able to resolve this issue was by calling `model.setInput(new Mat())`, setting the input to an empty mat, before setting the new input. This doesn't seem right though and I am wondering if there is a bug in the module.

### Steps to reproduce


Code:
```java
package com.sadcaptcha.opencv_dnn;

import java.io.File;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ResNet {

	private static int OUT_FEATURES = 1000;
	private static int INPUT_WIDTH = 224;
	private static int INPUT_HEIGHT = 224;
	private static float SCALE_FACTOR = 1.0f / 255.0f;

	private static Net model;

	public static void main(String[] args) {
		String filename = new File("src/main/resources/opencv_4100.so").getAbsolutePath();
		System.load(filename);

		model = Dnn.readNetFromONNX(new File("src/main/resources/resnet50-v2-7.onnx").getAbsolutePath());

		Mat out = embed("src/test/resources/dug1.jpg");
		Mat out1 = embed("src/test/resources/dug2.jpg");

		System.out.println(cosineSimilarity(matToArray(out), matToArray(out1)));

	}

	private static double[] matToArray(Mat mat) {
		double[] array = new double[mat.size(1)];
		for (int i = 0; i < mat.cols(); i++) {
			array[i] = mat.get(0, i)[0];
		}
		return array;
	}

	private static Mat embed(String filename) {
		Mat input = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
		Mat blob = preprocess(input);
		model.setInput(blob);
		Mat out = model.forward();
		return out;
	}

	/**
	 * https://docs.opencv.org/4.x/dc/d70/pytorch_cls_tutorial_dnn_conversion.html
	 * regarding the mean/std:
	 * https://discuss.pytorch.org/t/how-to-preprocess-input-for-pre-trained-networks/683/30
	 */
	private static Mat preprocess(Mat input) {
		input.convertTo(input, CvType.CV_32F);
		Imgproc.resize(input, input, new Size(256, 256));
		Scalar mean = new Scalar(0.485 * 255, 0.456 * 255, 0.406 * 255);
		Size size = new Size(INPUT_WIDTH, INPUT_HEIGHT);
		Mat blob = Dnn.blobFromImage(input, SCALE_FACTOR, size, mean, true, true);
		Scalar std = new Scalar(0.229f, 0.224f, 0.225f);
		Core.divide(blob, std, blob);
		return blob;
	}

	private static void printMatInfo(Mat mat) {
		for (int i = 0; i < mat.dims(); i++) {
			System.out.println("Dim %d size: %s".formatted(i, mat.size(i)));
		}
	}

	private static double cosineSimilarity(Mat vectorA, Mat vectorB) {
		if (vectorA.size(0) != 1 || vectorB.size(0) != 1)
			throw new IllegalArgumentException("vectorA and vectorB must be 1d arrays");
		if (vectorA.size(1) != vectorB.size(1))
			throw new IllegalArgumentException("vectorA and vectorB must be same length");
		double dotProduct = 0.0;
		double normA = 0.0;
		double normB = 0.0;
		for (int i = 0; i < vectorA.size(1); i++) {
			dotProduct += vectorA.get(0, i)[0] * vectorB.get(0, i)[0];
			normA += Math.pow(vectorA.get(0, i)[0], 2);
			normB += Math.pow(vectorB.get(0, i)[0], 2);
		}
		return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
	}

	private static double cosineSimilarity(double[] vectorA, double[] vectorB) {
		if (vectorA.length != vectorB.length)
			throw new IllegalArgumentException("vectorA and vectorB must be same length");
		double dotProduct = 0.0;
		double normA = 0.0;
		double normB = 0.0;
		for (int i = 0; i < vectorA.length; i++) {
			dotProduct += vectorA[i] * vectorB[i];
			normA += Math.pow(vectorA[i], 2);
			normB += Math.pow(vectorB[i], 2);
		}
		return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
	}

}
``` 

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)