In [39]:
from __future__ import print_function
import numpy as np
import cv2
import cPickle
import matplotlib.pyplot as plt
from keras.models import model_from_json
import PIL
from PIL import Image

def extractImagesAndLabels(path, file):
    f = open(path+file, 'rb')
    dict = cPickle.load(f)
    images = dict['data']
    images = np.reshape(images, (10000, 3, 32, 32))
    labels = dict['labels']
    return images, labels

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

def getImage(images, id):
    image = images[id]
    image = image.transpose([1, 2, 0])
    image = image.astype('float32')
    image /= 255
    return image

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
    
def showImage(id):
    image = getImage(images,id)
    print (image.shape)
    %matplotlib inline
    imgplot = plt.imshow(image)
    labelid = labels[id]
    category = categories[labelid]
    print("category : "+category)    

def predictImage(id):
    image = getImage(images, id)
    showImage(id)
    image = np.expand_dims(image, axis=0)
    print (image.shape)
    print (image)
    result = model.predict(image)
    result = result[0].tolist()
    best_index=result.index(max(result))
    print ("prediction : "+categories[best_index])
    i=0
    while (i < 10):
        print (str(categories[i])+" : "+str(result[i]))
        i+=1
    
    
model = loadModel('models/allconv-model.json', 'models/allconv-weights-90.hdf5') 
images, labels = extractImagesAndLabels("data/CIFAR-10/cifar-10-batches-py/", "test_batch")
categories = extractCategories("data/CIFAR-10/cifar-10-batches-py/", "batches.meta")
print("done")


done

In [40]:
predictImage(129)


(32, 32, 3)
category : bird
(1, 32, 32, 3)
[[[[0.58431375 0.6        0.77254903]
   [0.6117647  0.6313726  0.8117647 ]
   [0.6666667  0.7176471  0.88235295]
   ...
   [0.67058825 0.7137255  0.9490196 ]
   [0.67058825 0.7137255  0.94509804]
   [0.6666667  0.70980394 0.9372549 ]]

  [[0.5254902  0.5019608  0.64705884]
   [0.5921569  0.58431375 0.7176471 ]
   [0.63529414 0.6392157  0.7647059 ]
   ...
   [0.6666667  0.69803923 0.9098039 ]
   [0.6666667  0.69411767 0.90588236]
   [0.65882355 0.69411767 0.89411765]]

  [[0.52156866 0.5137255  0.6117647 ]
   [0.5019608  0.46666667 0.54509807]
   [0.6313726  0.627451   0.7058824 ]
   ...
   [0.67058825 0.6901961  0.85882354]
   [0.67058825 0.6901961  0.85882354]
   [0.6666667  0.6862745  0.8509804 ]]

  ...

  [[0.76862746 0.6509804  0.5921569 ]
   [0.7607843  0.627451   0.5686275 ]
   [0.76862746 0.627451   0.5647059 ]
   ...
   [0.7372549  0.61960787 0.59607846]
   [0.6862745  0.5647059  0.54509807]
   [0.67058825 0.5411765  0.5176471 ]]

  [[0.76862746 0.6509804  0.6039216 ]
   [0.7607843  0.627451   0.5803922 ]
   [0.7647059  0.62352943 0.5803922 ]
   ...
   [0.72156864 0.60784316 0.59607846]
   [0.65882355 0.54509807 0.53333336]
   [0.6666667  0.54901963 0.5294118 ]]

  [[0.7607843  0.6392157  0.6039216 ]
   [0.75686276 0.61960787 0.58431375]
   [0.7607843  0.61960787 0.58431375]
   ...
   [0.6666667  0.6156863  0.6039216 ]
   [0.6        0.5137255  0.5019608 ]
   [0.64705884 0.52156866 0.50980395]]]]
prediction : bird
airplane : 0.00402062991634
automobile : 1.09006192961e-10
bird : 0.995978593826
cat : 1.01212432532e-11
deer : 9.76827397182e-14
dog : 1.37584063066e-13
frog : 4.19389458058e-13
horse : 6.11641898289e-15
ship : 7.24032986454e-07
truck : 1.87321647083e-09

In [73]:
#load image
import urllib
url1="http://www.locationsphotography.co.uk/wp-content/uploads/2017/07/Truck-Professional-Photography-Square-Tile2-400x400.jpg"
url2="http://p3ky.com/wp-content/uploads/2017/11/LWC-Truck_Square.jpg"
url3="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ7a70Fqqx6WaWOEUg1irrgzLQvJX0B7Bx8SQVStKwpjEe7-RVhpg"
url4="https://rlv.zcache.co.uk/semi_truck_cab_square_sticker-rfe992504321541beaefdc3263c2b65d1_v9wf3_8byvr_400.jpg"
url5="https://www.iotsolutionprovider.com/sites/iotsolutionprovider/files/articles/Fleet%20Management_square.jpg"
url6="https://rlv.zcache.com/red_dump_truck_boy_construction_birthday_party_square_sticker-r93c318e9d4f64dc3888bff21f6fa88d6_v9i40_8byvr_400.jpg"
url7="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQaqBfc5b78ED6bM8gN2yn47O1ph6JP5ix8OE82MP8z8klCHAtU"
url8="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQO4SYXHBQOcmWIS3DoQRvBN2EI9V_kGyUz74qs30LwIVqmTTya"
url9="https://www.trossachs-sar.com/pubd/images/square.58d8deb4-RN-Sea-king-6.jpg"
url10="https://www.picclickimg.com/d/l400/pict/123325311402_/New-Fall-Dog-Ride-old-Blue-Truck-wood.jpg"
url11="https://cdn.shopify.com/s/files/1/1132/0194/files/District_Mobile_Slider_truck_400x.jpg?v=1517957865"
url12="https://i.pinimg.com/736x/bb/46/1a/bb461a25fdcb613e88f4b130b17842d4--scottish-terrier-ranges.jpg"

url12="https://www.arlingtondogandcat.com/imagebank/eVetSites/DogCats/052016_EVS_CatDog5.jpg"

url = url12

img = Image.open(urllib.urlopen(url))
img = img.resize((32, 32), PIL.Image.ANTIALIAS)
img = img.convert('RGB')

#img.save('images/test.png', 'PNG')
#img = Image.open( 'images/test.png' )
#img.load()

#show image
%matplotlib inline
imgplot = plt.imshow(img)

#process image data
data = np.reshape(img, (1, 32, 32, 3))
data = data.astype('float32')
data /= 255

#make prediction
result = model.predict(data)
result = result[0].tolist()
best_index=result.index(max(result))

#print result
print ("prediction : "+categories[best_index])
i=0
while (i < 10):
    print (str(categories[i])+" : "+str(result[i]))
    i+=1


prediction : cat
airplane : 6.46880772866e-24
automobile : 1.68293218937e-23
bird : 1.0320243303e-19
cat : 0.999976754189
deer : 1.77428935998e-19
dog : 2.32851543842e-05
frog : 8.40160885822e-19
horse : 4.93272697402e-18
ship : 1.57753485041e-23
truck : 1.56264312859e-18

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: