Pretrained Imagenet


In [1]:
import warnings
warnings.filterwarnings('ignore')

In [2]:
%matplotlib inline
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [3]:
import matplotlib.pylab as plt
import numpy as np

In [4]:
from distutils.version import StrictVersion

In [5]:
import sklearn
print(sklearn.__version__)

assert StrictVersion(sklearn.__version__ ) >= StrictVersion('0.18.1')


0.19.0

In [6]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)

assert StrictVersion(tf.__version__) >= StrictVersion('1.1.0')


1.2.1

In [7]:
import keras
print(keras.__version__)

assert StrictVersion(keras.__version__) >= StrictVersion('2.0.0')


Using TensorFlow backend.
2.0.4

In [8]:
import pandas as pd
print(pd.__version__)

assert StrictVersion(pd.__version__) >= StrictVersion('0.20.0')


0.20.3

Main


In [16]:
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')


Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5

In [21]:
def predict(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    preds = model.predict(x)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    print('Predicted:', decode_predictions(preds, top=3)[0])

In [22]:
!curl -O https://djcordhose.github.io/ai/img/squirrels/original/Michigan-MSU-raschka.jpg
predict(img_path = 'Michigan-MSU-raschka.jpg')


Predicted: [('n02356798', 'fox_squirrel', 0.99809438), ('n02361337', 'marmot', 0.0017146758), ('n02137549', 'mongoose', 3.9444629e-05)]

In [28]:
!curl -O https://djcordhose.github.io/ai/img/squirrels/original/Black_New_York_stuy_town_squirrel_amanda_ernlund.jpeg


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  302k  100  302k    0     0   433k      0 --:--:-- --:--:-- --:--:--  449k

In [30]:
predict(img_path = 'Black_New_York_stuy_town_squirrel_amanda_ernlund.jpeg')


Predicted: [('n02487347', 'macaque', 0.26802596), ('n01883070', 'wombat', 0.24108711), ('n02112018', 'Pomeranian', 0.059472464)]

In [31]:
!curl -O https://djcordhose.github.io/ai/img/squirrels/original/london.jpg


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  494k  100  494k    0     0   854k      0 --:--:-- --:--:-- --:--:--  954k    0     0   5565      0  0:01:30 --:--:--  0:01:30  6578

In [33]:
predict(img_path = 'london.jpg')


Predicted: [('n02321529', 'sea_cucumber', 0.21771204), ('n02356798', 'fox_squirrel', 0.17864239), ('n03223299', 'doormat', 0.12914015)]