Same preprocess got different result on python and C++

username: jinfagang
cooked: <p>Hello, communities!</p>
<p>I got a preprocess script:</p>
<pre><code class="lang-auto">class BaseTransform(object):
    """Defines the transformations that should be applied to test PIL image
        for input into the network

    dimension -&gt; tensorize -&gt; color adj

    Arguments:
        resize (int): input dimension to SSD
        rgb_means ((int,int,int)): average RGB of the dataset
            (104,117,123)
        swap ((int,int,int)): final order of channels
    Returns:
        transform (transform) : callable transform to be applied to test/val
        data
    """
    def __init__(self, resize, rgb_means, swap=(2, 0, 1)):
        self.means = rgb_means
        self.resize = resize
        self.swap = swap

    # assume input is cv2 img for now
    def __call__(self, img):

        interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
        interp_method = interp_methods[0]
        img = cv2.resize(np.array(img), (self.resize,
                                         self.resize),interpolation = interp_method).astype(np.float32)
        img -= self.means
        img = img.transpose(self.swap)
        return torch.from_numpy(img)
</code></pre>
<p>This will preprocess from a cv2.imread() image and return a tensor which input to a model.</p>
<p>And I did same thing in C++:</p>
<pre><code class="lang-auto">void detect() {
cv::Mat in_img;
  preprocess(in_img, frame);

  at::Tensor tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, torch::kByte);
  tensor_image = tensor_image.permute({0, 3, 1, 2}).to(torch::kFloat32).to(device_type);
  cout &lt;&lt; "tensor_image: " &lt;&lt; tensor_image &lt;&lt; endl;
}

// method
void Detector::preprocess(cv::Mat &amp;image, const cv::Mat &amp;input_image) {
  cv::resize(input_image, image, cv::Size(image_size_, image_size_));
  cv::subtract(image, cv::Scalar(103.94, 116.78, 123.68), image);
}
</code></pre>
<p>As you can see, the operation is exactly same!!! But I got really different result!!!Same model, same image input:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/266d56b9ee58d091991e5f857608eeb42bff32e2.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/266d56b9ee58d091991e5f857608eeb42bff32e2" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/2/266d56b9ee58d091991e5f857608eeb42bff32e2_2_452x500.jpeg" alt="image" data-base62-sha1="5tWoRUpZdF3kp12QtWohBAZEIuu" width="452" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/2/266d56b9ee58d091991e5f857608eeb42bff32e2_2_452x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/2/266d56b9ee58d091991e5f857608eeb42bff32e2_2_678x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/2/266d56b9ee58d091991e5f857608eeb42bff32e2.jpeg 2x" data-dominant-color="7B796B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">733×810 260 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Does anybody could help out ??? I am stucked here, cause I can not see what’s the wrong with my C++ preprocess…</p>

username: ptrblck
cooked: <p>If you subtract the mean in your C++ approach, is <code>image</code> transformed to a <code>float32</code> image or is it still a <code>uint8</code> image and thus clipped to <code>[0, 255]</code>?</p>
<p>As far as I know, OpenCV loads the image in BGR, so you might want to permute the mean vector (since your comment says it’s in RGB).</p>

username: jinfagang
cooked: <p>thanks!!! it worked!!!</p>