caishanli / pyncnn

python wrapper of ncnn with pybind11

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

关于数据预处理部分

lawo123 opened this issue · comments

感谢大佬的分享,但是我有一个疑问,我把数据处理完毕带进ncnn出来的结果不正确,但是把预处理部分放进ncnn去处理,出来的结果是正确的,两种方式结果不应该是一样的吗?
下面分别是两种方式的代码
方式一:
`import time
import ncnn
from PIL import Image
import cv2
import numpy as np
from torchvision.transforms import functional as F
from torchvision import transforms

def data_transform(img, im_size):
MEAN = [0.485, 0.456, 0.406]
STD = [0.229, 0.224, 0.225]
img = img.resize(im_size, Image.BILINEAR)
img = F.to_tensor(img) # convert to tensor (values between 0 and 1)
img = F.normalize(img, MEAN, STD) # normalize the tensor
return img

net = ncnn.Net()
net.load_param("espnetv2.param")
net.load_model("espnetv2.bin")
imagepath = '8.jpg'
img = cv2.imread(imagepath)
n_mat = ncnn.Mat(w=512, h=512, c=3)
w, h = img.shape[:2]
out_mat = ncnn.Mat()
mat_in = ncnn.Mat.from_pixels_resize(img, ncnn.Mat.PixelType.PIXEL_BGR2RGB,h,w,512,512)
mean_vals = [0.485255, 0.456255, 0.406*255];
mean_stds = [1/0.229/255, 1/0.224/255, 1/0.225/255];
mat_in.substract_mean_normalize(mean_vals, mean_stds)
ex = net.create_extractor()
ex.input("input.1", mat_in)
mat_out = ncnn.Mat()
ex.extract("1294", mat_out)
out = np.array(mat_out)
img_out = np.argmax(out,axis=0)

img_out = Image.fromarray(np.uint8(img_out*255))
img_out.show()`

方式二:
`import time
import ncnn
from PIL import Image
import cv2
import numpy as np
from torchvision.transforms import functional as F
from torchvision import transforms

def data_transform(img, im_size):
MEAN = [0.485, 0.456, 0.406]
STD = [0.229, 0.224, 0.225]
img = img.resize(im_size, Image.BILINEAR)
img = F.to_tensor(img) # convert to tensor (values between 0 and 1)
img = F.normalize(img, MEAN, STD) # normalize the tensor
return img
net = ncnn.Net()
net.load_param("espnetv2.param")
net.load_model("espnetv2.bin")

imagepath = '8.jpg'
img = Image.open(imagepath).convert('RGB')
w, h = img.size
img = data_transform(img, (512,512))
in_mat = ncnn.Mat(w=512, h=512, c=3)
img_numpy = img.numpy()
out_mat = ncnn.Mat()
mat_in = ncnn.Mat.from_pixels(img_numpy, ncnn.Mat.PixelType.PIXEL_RGB,h,w)
ex = net.create_extractor()
ex.input("input.1", mat_in)
mat_out = ncnn.Mat()
ex.extract("1294", mat_out)
out = np.array(mat_out)
img_out = np.argmax(out,axis=0)

img_out = Image.fromarray(np.uint8(img_out*255))
img_out.show()
`
data.zip

找到原因了,数据预处理在前的话就不用ncnn.Mat.from_pixels处理了,直接ncnn.Mat()把numpy转成mat作为输入就对了。