1

I am trying to classify some images from GoogleNet using Pycaffe everything is in its default state, the deploy.prototxt and also the pre-trained model. However, when I want to run the code, I get the following error:

ValueError: could not broadcast input array from shape (1,3,256,256) into shape (1,3,224,224)

which happens when I want to subtract my image from the mean file! This is the code I'm using:

   # Extract mean from the mean image file
    mean_blobproto_new = caffe.proto.caffe_pb2.BlobProto()
    f = open(args.mean, 'rb')
    mean_blobproto_new.ParseFromString(f.read())
    mean_image = caffe.io.blobproto_to_array(mean_blobproto_new)

for i, image, label in reader:
    image_caffe = image.reshape(1, *image.shape)

    out = net.forward(data=np.asarray([ image_caffe ])- mean_image)
    plabel = int(out['pred'][0].argmax(axis=0))

and this is the deploy.prototxt file, (the network was trained on 256x256 images cropped at 224x224 just like GoogleNet and GoogleNet mean-file is used as well): https://pastebin.com/2QEtEeHW

what is wrong here?
Shouldn't Caffe first subtract the image and then crops it so this error wouldn't happen? What should I do?

Hossein
  • 24,202
  • 35
  • 119
  • 224
  • doesn't googlenet subtract a mean value per channel (a 3-vector, instead of a "mean image")? – Shai Jul 24 '17 at 08:43
  • I used the files from this branch : https://github.com/mrgloom/kaggle-dogs-vs-cats-solution and got all the files + meanfile from their branch! The caffe branch which the previous link referenced and said they got their googlenet implementation from, used mean per channel though. – Hossein Jul 24 '17 at 09:19
  • 1
    It doesn't. You can look https://stackoverflow.com/questions/44119176/caffe-model-gives-same-output-for-every-image for how I do it. – Harjatin Jul 25 '17 at 21:29
  • @Harjatin: Thanks I saw that, but that did'nt solve my problem, nevertheless it was very helpful to me:) – Hossein Jul 26 '17 at 14:51

1 Answers1

0

For this to work I had 3 choices!

  1. Use transformer.preprocess() method, which was extremely slow!
  2. resize both the image and mean-file to the crop size and then use the mean file
  3. center crop just like Caffe and carry on!

here is the code I wrote for this, note that I was reading off a lmdb database so the samples were already transposed and channel swapped! :

ims=[]              
for x in range(len(batch)):
    img = batch[x][1]
    #img has c,h,w shape! its already gone through transpose and channel swap when it was being saved into lmdb!
    #method I: crop the both the image and mean file 
    #ims.append(img[:,0:224,0:224] - mean_image[0][:,0:224,0:224] )
    #Method II : resize the image to the desired size(crop size) 
    #img = caffe.io.resize_image(img.transpose(2,1,0), (224, 224))
    #Method III : use center crop just like caffe does in test time
    #center crop
    c,h,w = img.shape
    startx = h//2 - cropx//2
    starty = w//2 - cropy//2
    img = img[:, startx:startx + cropx, starty:starty + cropy]                  
    #transpose the image so we can subtract from mean, and in the meanwhile, change it to float!   
    img = np.array(img.transpose(2,1,0),dtype=np.float32)
    img -= mean_image[0].mean(1).mean(1)
    #transpose back to the original state
    img = img.transpose(2,1,0)
    ims.append(img)        

net1.blobs['data'].data[...] = ims[:]
Hossein
  • 24,202
  • 35
  • 119
  • 224