In [3]:
%%bash
rm -rf deep-learning-models
git clone --depth=1 https://github.com/fchollet/deep-learning-models.git


Cloning into 'deep-learning-models'...

In [4]:
%%bash
ls deep-learning-models


audio_conv_utils.py
imagenet_utils.py
inception_v3.py
LICENSE
music_tagger_crnn.py
README.md
resnet50.py
vgg16.py
vgg19.py
xception.py

In [6]:
import sys
sys.path.insert(0, 'deep-learning-models')

In [7]:
from resnet50 import ResNet50


Using Theano backend.
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-7-0f3378b5427b> in <module>()
----> 1 from resnet50 import ResNet50

/space/dl/deep-learning-models/resnet50.py in <module>()
     20 from keras.preprocessing import image
     21 import keras.backend as K
---> 22 from keras.utils.layer_utils import convert_all_kernels_in_model
     23 from keras.utils.data_utils import get_file
     24 from imagenet_utils import decode_predictions, preprocess_input

ImportError: cannot import name convert_all_kernels_in_model

In [3]:
%%bash
pip2 list|grep keras
pip2 install -U keras


Collecting keras
  Using cached Keras-1.1.1.tar.gz
Requirement already up-to-date: theano in /usr/local/lib/python2.7/dist-packages (from keras)
Collecting pyyaml (from keras)
  Using cached PyYAML-3.12.tar.gz
Requirement already up-to-date: six in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already up-to-date: numpy>=1.7.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Requirement already up-to-date: scipy>=0.11 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Building wheels for collected packages: keras, pyyaml
  Running setup.py bdist_wheel for keras: started
  Running setup.py bdist_wheel for keras: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/be/da/07/f1f583eb4ee0fba7b79afe86ba7495792ef60a63bfc6870c90
  Running setup.py bdist_wheel for pyyaml: started
  Running setup.py bdist_wheel for pyyaml: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/2c/f7/79/13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc
Successfully built keras pyyaml
Installing collected packages: pyyaml, keras
  Found existing installation: PyYAML 3.11
    Uninstalling PyYAML-3.11:
      Successfully uninstalled PyYAML-3.11
  Found existing installation: Keras 1.0.6
    Uninstalling Keras-1.0.6:
      Successfully uninstalled Keras-1.0.6
Successfully installed keras-1.1.1 pyyaml-3.12
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

In [5]:
import sys
sys.path.insert(0, 'deep-learning-models')

In [32]:
import IPython.display as display

In [6]:
from resnet50 import ResNet50


Using TensorFlow backend.

In [9]:
import numpy as np
from keras.preprocessing import image
from imagenet_utils import preprocess_input, decode_predictions

model = ResNet50(weights='imagenet')

img_path = 'out.png'
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)
print('Predicted:', decode_predictions(preds))


K.image_dim_ordering: tf
Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
40960/35363 [==================================] - 1s 
('Predicted:', [[(u'n03991062', u'pot', 0.11102176), (u'n03642806', u'laptop', 0.078045845), (u'n04476259', u'tray', 0.072275393), (u'n13133613', u'ear', 0.054994311), (u'n12144580', u'corn', 0.044427935)]])

In [39]:
img_path = 'images/SNC12011.JPG'
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)
decoded_preds = decode_predictions(preds)

for pred in decoded_preds:
    for p in pred:
        print '%30s\t%05.2f%%' % (p[1], p[2] * 100.0)


                      fountain	41.63%
                        valley	29.49%
                           dam	15.44%
                        geyser	04.97%
                         cliff	02.76%

In [38]:
#IMG_1825.JPG
img_path = 'images/IMG_1825.JPG'
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)
decoded_preds = decode_predictions(preds)

for pred in decoded_preds:
    for p in pred:
        print '%30s\t%05.2f%%' % (p[1], p[2] * 100.0)


                        valley	68.94%
                       volcano	11.69%
                      lakeside	10.65%
                           dam	02.76%
                     boathouse	01.33%

In [41]:
from vgg16 import VGG16
from keras.preprocessing import image
from imagenet_utils import preprocess_input

model = VGG16(weights='imagenet', include_top=False)

img_path = 'images/SNC12011.JPG'
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)

features = model.predict(x)


K.image_dim_ordering: tf
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
58875904/58889256 [============================>.] - ETA: 0s

In [44]:
features.shape


Out[44]:
(1, 7, 7, 512)

In [ ]:
from vgg19 import VGG19
from keras.preprocessing import image
from imagenet_utils import preprocess_input
from keras.models import Model

base_model = VGG19(weights='imagenet')
model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output)

img_path = 'images/SNC12011.JPG'
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)

block4_pool_features = model.predict(x)


K.image_dim_ordering: tf
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels.h5
  8011776/574710816 [..............................] - ETA: 4148s

In [ ]: