Search code examples
c#opencvcaffe

Caffe reshape zeros


I'm new in Caffe, maybe I'm doing something wrong or understanding something else.

I'm creating my first script in Caffe, I tried to get the same input as output using reshape with same params, I'm getting the same size in output, but all are zeros.

I tried with this prototxt files:

name: "copying"
layer {
    name: "input"
    type: "Input"
    top: "input"
    input_param { shape: { dim: 1 dim: 1 dim: 2 dim: 2 } }
}
layer {
    name: "data"
    type: "Reshape"
    bottom: "input"
    top: "data"
    reshape_param { shape: { dim: 1 dim: 1 dim: 2 dim: 2 } }
}
name: "copying"
input: "input"
input_dim: 1
input_dim: 1
input_dim: 2
input_dim: 2
layer {
    name: "data"
    type: "Reshape"
    bottom: "input"
    top: "data"
    reshape_param { shape: { dim: 0 dim: 0 dim: 0 dim: 0 } }
}

This is my code:

var input = new Mat(new[] {1, 1, 2, 2}, MatType.CV_8S, new byte[] { 1, 2, 3, 4 });
using var net = OpenCvSharp.Dnn.Net.ReadNetFromCaffe("copying.prototxt");
net.SetInput(input);

using var output = net.Forward();

Also I tried using names

var input = new Mat(new[] {1, 1, 2, 2}, MatType.CV_8S, new byte[] { 1, 2, 3, 4 });
using var net = OpenCvSharp.Dnn.Net.ReadNetFromCaffe("copying.prototxt");
net.SetInput(input, "input");

using var output = net.Forward("data");

This is the input and the output

[1,2,3,4]
[0,0,0,0]

Solution

  • I found the problem, no matter that the input layer is a MatType.CV_8S the result is MatType.CV_32FC1, so when the result array is parse should be cast to float.