Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.36k stars 4.16k forks source link

The way Pytorch reads the picture is inconsistent with that of Ncnn bitmap #2793

Open ciwei123 opened 3 years ago

ciwei123 commented 3 years ago

@nihui Thanks for your sharing. I want to read an image,and there are two methods: 1.in pytorch by opencv :

img = cv2.imread(img_path)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = np.transpose(img, (2, 0, 1)).astype(np.float32)
img = img.__div__(255)
img = torch.from_numpy(img)

2.in pytorch by PIL.Image

from torchvision.transforms.functional import resize, to_tensor
im = Image.open("/mnt/fhl/vmaf/dog.jpeg").convert('RGB')  #
im = to_tensor(im)

And I want to convert the pytorch model to ncnn, but the image type is bitmap:

open_image = getAssets().open(path);
Bitmap bitmap = BitmapFactory.decodeStream(open_image);

ncnn::Mat in = ncnn::Mat::from_android_bitmap(env, bitmap_in, ncnn::Mat::PIXEL_RGB);

And I find that the first method is consistend with the NCNN,but the second methos is inconsistend with the NCNN. Coud you tell me what is the correspoding NCNN code for the second method?Thank you very much!!

nihui commented 3 years ago

For this line

img = img.__div__(255)

in ncnn

const float norm_vals[3] = {1/255.f, 1/255.f, 1/255.f};
in.substract_mean_normalize(0, norm_vals);
ciwei123 commented 3 years ago

For this line

img = img.__div__(255)

in ncnn

const float norm_vals[3] = {1/255.f, 1/255.f, 1/255.f};
in.substract_mean_normalize(0, norm_vals);

@nihui Thanks for your reply . I know this convertion you said, but PIL.Image is different to Opencv when read an image, Opencv is consistent with bitmap, PIL.Image is inconsistent with bitmap. I want to know if I use PIL.Image in pytorch, what should I do in NCNN?Thank you very much!!