In [4]:
import PIL
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from keras.models import model_from_json
import cPickle

In [18]:
def extractCategories(path, file):
    f = open(path+file, 'rb')
    dict = cPickle.load(f)
    return dict['label_names']

categories = extractCategories("../data/CIFAR-10/cifar-10-batches-py/", "batches.meta")

def loadModel(json_desc, weights):
    # load json and create model
    json_file = open(json_desc, 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    loaded_model.load_weights(weights)
    return loaded_model

model = loadModel('model.json', 'model.h5')

def showImage(image):
    %matplotlib inline
    imgplot = plt.imshow(image)    

def predictImage(image):
    showImage(image)
    image = np.expand_dims(image, axis=0)
    result = model.predict(image)
    result = result[0].tolist()
    best_index=result.index(max(result))
    print ("prediction : "+categories[best_index])

img = Image.open('car3.png')
img = img.resize((32, 32), PIL.Image.ANTIALIAS)
img.save('test.png', 'PNG')

In [19]:
img = Image.open( 'test.png' )
img.load()
data = np.asarray( img, dtype="int32" )
data = np.reshape(data, (32, 32, 3))
print (data.shape)
predictImage(data)



ValueErrorTraceback (most recent call last)
<ipython-input-19-e13901a9c79c> in <module>()
      2 img.load()
      3 data = np.asarray( img, dtype="int32" )
----> 4 data = np.reshape(data, (32, 32, 3))
      5 print (data.shape)
      6 predictImage(data)

/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in reshape(a, newshape, order)
    277            [5, 6]])
    278     """
--> 279     return _wrapfunc(a, 'reshape', newshape, order=order)
    280 
    281 

/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in _wrapfunc(obj, method, *args, **kwds)
     49 def _wrapfunc(obj, method, *args, **kwds):
     50     try:
---> 51         return getattr(obj, method)(*args, **kwds)
     52 
     53     # An AttributeError occurs if the object does not have

ValueError: cannot reshape array of size 4096 into shape (32,32,3)

In [ ]:


In [ ]: