Keras image loading vs opencv imread function

199 views
Skip to first unread message

omar.m...@gmail.com

unread,
Jul 7, 2017, 3:42:14 PM7/7/17
to Keras-users
Hi All,


I'm trying to use a keras model in production within an opencv application

I've a very weird observation when i load the image using opencv imread and preprocess it, the output is different from using keras, i expected the output to be slightly different but it's 10% difference in accuracy

for file in files:
    srcfile
= root + '/' + file
    img
= cv2.imread(root + '/' + file)
    img
= scipy.misc.imresize(img, (224, 224), 'bilinear')
    new_im
= np.zeros((3,224,224),dtype = np.float32)
    new_im
[0,:,:] = img[:,:,2]
    new_im
[1,:,:] = img[:,:,1]
    new_im
[2,:,:] = img[:,:,0]
    img
= new_im.copy()
   
   
#second way
    im
= image.load_img(srcfile,target_size = (224,224))
    im
= np.asarray(im,dtype=np.uint8)
    im
= im.transpose((2,0,1))
    im
= np.expand_dims(im, axis= 0)
    img
= np.expand_dims(img,axis= 0)
    preds
, n , m=  vgg.predict(img)
    preds2
, n2,m2 =  vgg.predict(im)


after looping through all images the classification performance, is a little bit different, can anyone guide me to the reason?

classification performance is totally different
image


omar.m...@gmail.com

unread,
Jul 8, 2017, 6:51:30 AM7/8/17
to Keras-users, omar.m...@gmail.com
Thank you guys

I found the problem, it was in the resize funtcion, opencv uses bilinear interpolation and keras use nearest neighbor

that's why the preprocessing is different
Reply all
Reply to author
Forward
0 new messages