Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Dnn forward result has rows = -1, size = -1

Hi there,

I'm trying to translate this WORKING Python code for Neural Transfer Style on Java (Android). (the original work is from https://www.pyimagesearch.com/2018/08/27/neural-style-transfer-with-opencv/).

In python the test image shape is uint8 (479, 700, 3) and the output of the DNN is of shape float32 (1,3,480,700).

When I try converting the code to Java (see below), the resulting matrix I get is Mat [ -1*-1*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x75cf3fd9e0, dataAddr=0x75cea5e000 ], with rows = -1 and columns = -1. It also seems from CV_32FC1 that the output in Java has 1 float32 channel, while on python there are 3 I think. Maybe there's something I'm missing, but I don't know how to solve it.

I post below my working code in python and my initial tentative in Java.

I've searched a lot these days to find a solution, but I cannot find it, please help me!!! Thank you

PYTHON CODE

      import cv2
      import base64
      import numpy as np

    // read image directly from file, shape is uint8 (479, 700, 3)
    img = cv2.imread("./testImages/bike.jpg")
    // load dnn model
    net = cv2.dnn.readNetFromTorch("./models/instance_norm/mosaic.t7")
    // resize images if width > 700
    h, w = img.shape[:2]
    width = 700
    if w <= width:
        dim = (w, h)
    else:
        ratio = width / float(w)
        dim = (width, int(h * ratio))
        img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    // Feed the neural net
    blob = cv2.dnn.blobFromImage(img, 1.0, dim,
        (103.939, 116.779, 123.680), swapRB=False, crop=False)
    net.setInput(blob)
    // Run neural network, out shape is float32 (1,3,480,700)
    out = net.forward()
    // Add back in the mean subtraction
    // output shape is now float32 (3, 480, 700)
    out = out.reshape((3, out.shape[2], out.shape[3]))
    out[0] += 103.939
    out[1] += 116.779
    out[2] += 123.680
   // output shape is now float32 (480, 700, 3)
    out = out.transpose(1,2,0)
    // convert image from BGR to RGB and avoid artifacts. output shape is now uint8 (480, 700, 3)
    out = cv2.convertScaleAbs(cv2.cvtColor(out, cv2.COLOR_BGR2RGB))

JAVA CODE

// read image directly from file 
Mat image = Imgcodecs.imread(filePath);
// load dnn model 
Net net = Dnn.readNetFromTorch(modelPath);
// resize images if width > 700
int h = image.height();
int w = image.width();
int width = 700;
int height;
Mat resized;
if(w <= width) {
    width = w;
    height = h;
    resized = image;
} else {
    float ratio = width / (float) w;
    height = (int)(h * ratio);
    resized = new Mat();
    Imgproc.resize(image, resized, new Size(width, height), 0, 0, Imgproc.INTER_AREA);
}

// Feed the neural net
net.setInput(Dnn.blobFromImage(resized,
                               1.0,
                               new Size(width, height),
                               new Scalar(103.939, 116.779, 123.680),
                               false,
                               false));

// I have problems here with the resulting matrix
Mat stylizedImage = net.forward();
Core.add(stylizedImage, new Scalar(103.939, 116.779, 123.680), stylizedImage);

// ....

Dnn forward result has rows = -1, size = -1

Hi there,

I'm trying to translate this WORKING Python code for Neural Transfer Style on Java (Android). (the original work is from https://www.pyimagesearch.com/2018/08/27/neural-style-transfer-with-opencv/).

In python the test image shape is uint8 (479, 700, 3) and the output of the DNN is of shape float32 (1,3,480,700).

When I try converting the code to Java (see below), the resulting matrix I get is Mat [ -1*-1*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x75cf3fd9e0, dataAddr=0x75cea5e000 ], with rows = -1 and columns = -1. It also seems from CV_32FC1 that the output in Java has 1 float32 channel, while on python there are 3 I think. Maybe there's something I'm missing, but I don't know how to solve it.

I post below my working code in python and my initial tentative in Java.

I've searched a lot these days to find a solution, but I cannot find it, please help me!!! Thank you

PYTHON CODE

      import cv2
      import base64
      import numpy as np

    // read image directly from file, shape is uint8 (479, 700, 3)
    img = cv2.imread("./testImages/bike.jpg")
    // load dnn model
    net = cv2.dnn.readNetFromTorch("./models/instance_norm/mosaic.t7")
    // resize images if width > 700
    h, w = img.shape[:2]
    width = 700
    if w <= width:
        dim = (w, h)
    else:
        ratio = width / float(w)
        dim = (width, int(h * ratio))
        img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    // Feed the neural net
    blob = cv2.dnn.blobFromImage(img, 1.0, dim,
        (103.939, 116.779, 123.680), swapRB=False, crop=False)
    net.setInput(blob)
    // Run neural network, out shape is float32 (1,3,480,700)
    out = net.forward()
    // Add back in the mean subtraction
    // output shape is now float32 (3, 480, 700)
    out = out.reshape((3, out.shape[2], out.shape[3]))
    out[0] += 103.939
    out[1] += 116.779
    out[2] += 123.680
   // output shape is now float32 (480, 700, 3)
    out = out.transpose(1,2,0)
    // convert image from BGR to RGB and avoid artifacts. output shape is now uint8 (480, 700, 3)
    out = cv2.convertScaleAbs(cv2.cvtColor(out, cv2.COLOR_BGR2RGB))

JAVA CODE

// read image directly from file 
Mat image = Imgcodecs.imread(filePath);
// load dnn model 
Net net = Dnn.readNetFromTorch(modelPath);
// resize images if width > 700
int h = image.height();
int w = image.width();
int width = 700;
int height;
Mat resized;
if(w <= width) {
    width = w;
    height = h;
    resized = image;
} else {
    float ratio = width / (float) w;
    height = (int)(h * ratio);
    resized = new Mat();
    Imgproc.resize(image, resized, new Size(width, height), 0, 0, Imgproc.INTER_AREA);
}

// Feed the neural net
net.setInput(Dnn.blobFromImage(resized,
                               1.0,
                               new Size(width, height),
                               new Scalar(103.939, 116.779, 123.680),
                               false,
                               false));

// I have problems here with the resulting matrix
Mat stylizedImage = net.forward();
Core.add(stylizedImage, new Scalar(103.939, 116.779, 123.680), stylizedImage);

// ....
click to hide/show revision 3
retagged

updated 2018-12-23 08:50:34 -0600

berak gravatar image

Dnn forward result has rows = -1, size = -1

Hi there,

I'm trying to translate this WORKING Python code for Neural Transfer Style on Java (Android). (the original work is from https://www.pyimagesearch.com/2018/08/27/neural-style-transfer-with-opencv/).

In python the test image shape is uint8 (479, 700, 3) and the output of the DNN is of shape float32 (1,3,480,700).

When I try converting the code to Java (see below), the resulting matrix I get is Mat [ -1*-1*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x75cf3fd9e0, dataAddr=0x75cea5e000 ], with rows = -1 and columns = -1. It also seems from CV_32FC1 that the output in Java has 1 float32 channel, while on python there are 3 I think. Maybe there's something I'm missing, but I don't know how to solve it.

I post below my working code in python and my initial tentative in Java.

I've searched a lot these days to find a solution, but I cannot find it, please help me!!! Thank you

PYTHON CODE

      import cv2
      import base64
      import numpy as np

    // read image directly from file, shape is uint8 (479, 700, 3)
    img = cv2.imread("./testImages/bike.jpg")
    // load dnn model
    net = cv2.dnn.readNetFromTorch("./models/instance_norm/mosaic.t7")
    // resize images if width > 700
    h, w = img.shape[:2]
    width = 700
    if w <= width:
        dim = (w, h)
    else:
        ratio = width / float(w)
        dim = (width, int(h * ratio))
        img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    // Feed the neural net
    blob = cv2.dnn.blobFromImage(img, 1.0, dim,
        (103.939, 116.779, 123.680), swapRB=False, crop=False)
    net.setInput(blob)
    // Run neural network, out shape is float32 (1,3,480,700)
    out = net.forward()
    // Add back in the mean subtraction
    // output shape is now float32 (3, 480, 700)
    out = out.reshape((3, out.shape[2], out.shape[3]))
    out[0] += 103.939
    out[1] += 116.779
    out[2] += 123.680
   // output shape is now float32 (480, 700, 3)
    out = out.transpose(1,2,0)
    // convert image from BGR to RGB and avoid artifacts. output shape is now uint8 (480, 700, 3)
    out = cv2.convertScaleAbs(cv2.cvtColor(out, cv2.COLOR_BGR2RGB))

JAVA CODE

// read image directly from file 
Mat image = Imgcodecs.imread(filePath);
// load dnn model 
Net net = Dnn.readNetFromTorch(modelPath);
// resize images if width > 700
int h = image.height();
int w = image.width();
int width = 700;
int height;
Mat resized;
if(w <= width) {
    width = w;
    height = h;
    resized = image;
} else {
    float ratio = width / (float) w;
    height = (int)(h * ratio);
    resized = new Mat();
    Imgproc.resize(image, resized, new Size(width, height), 0, 0, Imgproc.INTER_AREA);
}

// Feed the neural net
net.setInput(Dnn.blobFromImage(resized,
                               1.0,
                               new Size(width, height),
                               new Scalar(103.939, 116.779, 123.680),
                               false,
                               false));

// I have problems here with the resulting matrix
Mat stylizedImage = net.forward();
Core.add(stylizedImage, new Scalar(103.939, 116.779, 123.680), stylizedImage);

// ....