In [1]:
!pip install opencv-python wget matplotlib
!apt update && apt install -y libsm6 libxext6 libgtk2.0-dev


Requirement already satisfied: opencv-python in /usr/local/lib/python2.7/dist-packages (3.4.3.18)
Requirement already satisfied: wget in /usr/local/lib/python2.7/dist-packages (3.2)
Requirement already satisfied: matplotlib in /usr/local/lib/python2.7/dist-packages (2.2.3)
Requirement already satisfied: numpy>=1.11.1 in /usr/local/lib/python2.7/dist-packages (from opencv-python) (1.15.2)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python2.7/dist-packages (from matplotlib) (2.2.1)
Requirement already satisfied: backports.functools-lru-cache in /usr/local/lib/python2.7/dist-packages (from matplotlib) (1.5)
Requirement already satisfied: subprocess32 in /usr/local/lib/python2.7/dist-packages (from matplotlib) (3.5.2)
Requirement already satisfied: pytz in /usr/local/lib/python2.7/dist-packages (from matplotlib) (2018.5)
Requirement already satisfied: six>=1.10 in /usr/local/lib/python2.7/dist-packages (from matplotlib) (1.11.0)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python2.7/dist-packages (from matplotlib) (2.7.3)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python2.7/dist-packages (from matplotlib) (1.0.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python2.7/dist-packages (from matplotlib) (0.10.0)
Requirement already satisfied: setuptools in /usr/local/lib/python2.7/dist-packages (from kiwisolver>=1.0.1->matplotlib) (39.1.0)
You are using pip version 18.0, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Get:1 file:/var/nvinfer-runtime-trt-repo-4.0.1-ga-cuda9.0  InRelease
Ign:1 file:/var/nvinfer-runtime-trt-repo-4.0.1-ga-cuda9.0  InRelease
Get:2 file:/var/nvinfer-runtime-trt-repo-4.0.1-ga-cuda9.0  Release [574 B]
Get:2 file:/var/nvinfer-runtime-trt-repo-4.0.1-ga-cuda9.0  Release [574 B]
Hit:3 http://archive.ubuntu.com/ubuntu xenial InRelease                        
Hit:4 http://security.ubuntu.com/ubuntu xenial-security InRelease          
Hit:6 http://archive.ubuntu.com/ubuntu xenial-updates InRelease        
Hit:7 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
Ign:8 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64  InRelease
Ign:9 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64  InRelease
Hit:10 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64  Release
Hit:11 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64  Release
Reading package lists... Done[0m
Building dependency tree       
Reading state information... Done
15 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree        
Reading state information... Done
libsm6 is already the newest version (2:1.2.2-1).
libxext6 is already the newest version (2:1.3.3-1).
libgtk2.0-dev is already the newest version (2.24.30-1ubuntu1.16.04.2).
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.

In [53]:
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

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

images, labels = extractImagesAndLabels("../data/CIFAR-10/cifar-10-batches-py/", "test_batch")

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 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
    
model = loadModel('model.json', 'model.h5')

def showImage(id):
    image = getImage(images,id)
    %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)
    result = model.predict(image)
    result = result[0].tolist()
    best_index=result.index(max(result))
    print ("prediction : "+categories[best_index])

In [62]:
predictImage(19)


category : frog
prediction : frog

In [ ]: