Using a Network trained on Mnist with python

133 views
Skip to first unread message

Avaj

unread,
Dec 21, 2015, 11:40:28 AM12/21/15
to Caffe Users
Hey there,

I'm playing with caffe and python  in the Moment but I have got a problem.

I try to implement a Network trained with the Mnist dataset in python but I get an error.

Here is my code:



import sys
import caffe
import cv2
import Image
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


matplotlib
.rcParams['backend'] = "Qt4Agg"
import numpy as np
import lmdb

from PIL import Image


caffe_root
= '../'

MODEL_FILE
= '../examples/mnist/lenet.prototxt'
PRETRAINED
= '../examples/mnist/lenet_iter_10000.caffemodel'

net
= caffe.Net(MODEL_FILE, PRETRAINED,caffe.TEST)
caffe
.set_mode_cpu()
# Test self-made image


img
= caffe.io.load_image('../images/five.jpg', color=False)
img
= img.astype(np.uint8)
out = net.forward_all(data=np.asarray([img.transpose(2,0,1)]))
print out['prob'][0]











based on this post: https://groups.google.com/forum/#!topic/caffe-users/KHCU6Ti5gSQ

When i run this I get this error:


File "Knn.py", line 29, in <module>
   
out = net.forward_all(data=np.asarray([img.transpose(2,0,1)]))
 
File "/caffe/python/caffe/pycaffe.py", line 176, in _Net_forward_all
    outs
= self.forward(blobs=blobs, **batch)
 
File "/caffe/python/caffe/pycaffe.py", line 103, in _Net_forward
   
self.blobs[in_].data[...] = blob
ValueError: could not broadcast input array from shape (64,3,28,28) into shape (64,1,28,28)



And I think I know whit is the problem. I'm loading the image as and RGB but I need a grayscaled image. I read something that I have to change the code in io.py.but I don't know what to change.

I'm sorry asking about this easy stuff but I haven't found an good documentation about caffe and python.
Thank you for your help

Frank Liu

unread,
Dec 22, 2015, 12:34:38 AM12/22/15
to Caffe Users
Setting color=False won't return a grayscale image if the input is already RGB. This will probably work for you:
img = skimage.img_as_float(skimage.io.imread('../images/five.jpg', as_grey=True)).astype(np.float32)
Reply all
Reply to author
Forward
Message has been deleted
0 new messages