In [7]:
import keras
In [2]:
keras.__version__
Out[2]:
'1.2.2'
In [3]:
from googlenet_custom_layers import PoolHelper, LRN
from keras.models import model_from_json
In [8]:
model = model_from_json(open('googlenet_architecture.json').read(),
custom_objects={'PoolHelper': PoolHelper, 'LRN': LRN})
model.load_weights('googlenet_weights.h5')
In [9]:
model.summary()
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 3, 224, 224) 0
____________________________________________________________________________________________________
conv1/7x7_s2 (Convolution2D) (None, 64, 112, 112) 9472 input_1[0][0]
____________________________________________________________________________________________________
zeropadding2d_1 (ZeroPadding2D) (None, 64, 114, 114) 0 conv1/7x7_s2[0][0]
____________________________________________________________________________________________________
poolhelper_1 (PoolHelper) (None, 64, 114, 114) 0 zeropadding2d_1[0][0]
____________________________________________________________________________________________________
pool1/3x3_s2 (MaxPooling2D) (None, 64, 56, 56) 0 poolhelper_1[0][0]
____________________________________________________________________________________________________
pool1/norm1 (LRN) (None, 64, 56, 56) 0 pool1/3x3_s2[0][0]
____________________________________________________________________________________________________
conv2/3x3_reduce (Convolution2D) (None, 64, 56, 56) 4160 pool1/norm1[0][0]
____________________________________________________________________________________________________
conv2/3x3 (Convolution2D) (None, 192, 56, 56) 110784 conv2/3x3_reduce[0][0]
____________________________________________________________________________________________________
conv2/norm2 (LRN) (None, 192, 56, 56) 0 conv2/3x3[0][0]
____________________________________________________________________________________________________
zeropadding2d_2 (ZeroPadding2D) (None, 192, 58, 58) 0 conv2/norm2[0][0]
____________________________________________________________________________________________________
poolhelper_2 (PoolHelper) (None, 192, 58, 58) 0 zeropadding2d_2[0][0]
____________________________________________________________________________________________________
pool2/3x3_s2 (MaxPooling2D) (None, 192, 28, 28) 0 poolhelper_2[0][0]
____________________________________________________________________________________________________
inception_3a/3x3_reduce (Convolu (None, 96, 28, 28) 18528 pool2/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_3a/5x5_reduce (Convolu (None, 16, 28, 28) 3088 pool2/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_3a/pool (MaxPooling2D) (None, 192, 28, 28) 0 pool2/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_3a/1x1 (Convolution2D) (None, 64, 28, 28) 12352 pool2/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_3a/3x3 (Convolution2D) (None, 128, 28, 28) 110720 inception_3a/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_3a/5x5 (Convolution2D) (None, 32, 28, 28) 12832 inception_3a/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_3a/pool_proj (Convolut (None, 32, 28, 28) 6176 inception_3a/pool[0][0]
____________________________________________________________________________________________________
inception_3a/output (Merge) (None, 256, 28, 28) 0 inception_3a/1x1[0][0]
inception_3a/3x3[0][0]
inception_3a/5x5[0][0]
inception_3a/pool_proj[0][0]
____________________________________________________________________________________________________
inception_3b/3x3_reduce (Convolu (None, 128, 28, 28) 32896 inception_3a/output[0][0]
____________________________________________________________________________________________________
inception_3b/5x5_reduce (Convolu (None, 32, 28, 28) 8224 inception_3a/output[0][0]
____________________________________________________________________________________________________
inception_3b/pool (MaxPooling2D) (None, 256, 28, 28) 0 inception_3a/output[0][0]
____________________________________________________________________________________________________
inception_3b/1x1 (Convolution2D) (None, 128, 28, 28) 32896 inception_3a/output[0][0]
____________________________________________________________________________________________________
inception_3b/3x3 (Convolution2D) (None, 192, 28, 28) 221376 inception_3b/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_3b/5x5 (Convolution2D) (None, 96, 28, 28) 76896 inception_3b/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_3b/pool_proj (Convolut (None, 64, 28, 28) 16448 inception_3b/pool[0][0]
____________________________________________________________________________________________________
inception_3b/output (Merge) (None, 480, 28, 28) 0 inception_3b/1x1[0][0]
inception_3b/3x3[0][0]
inception_3b/5x5[0][0]
inception_3b/pool_proj[0][0]
____________________________________________________________________________________________________
zeropadding2d_3 (ZeroPadding2D) (None, 480, 30, 30) 0 inception_3b/output[0][0]
____________________________________________________________________________________________________
poolhelper_3 (PoolHelper) (None, 480, 30, 30) 0 zeropadding2d_3[0][0]
____________________________________________________________________________________________________
pool3/3x3_s2 (MaxPooling2D) (None, 480, 14, 14) 0 poolhelper_3[0][0]
____________________________________________________________________________________________________
inception_4a/3x3_reduce (Convolu (None, 96, 14, 14) 46176 pool3/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_4a/5x5_reduce (Convolu (None, 16, 14, 14) 7696 pool3/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_4a/pool (MaxPooling2D) (None, 480, 14, 14) 0 pool3/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_4a/1x1 (Convolution2D) (None, 192, 14, 14) 92352 pool3/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_4a/3x3 (Convolution2D) (None, 208, 14, 14) 179920 inception_4a/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_4a/5x5 (Convolution2D) (None, 48, 14, 14) 19248 inception_4a/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_4a/pool_proj (Convolut (None, 64, 14, 14) 30784 inception_4a/pool[0][0]
____________________________________________________________________________________________________
inception_4a/output (Merge) (None, 512, 14, 14) 0 inception_4a/1x1[0][0]
inception_4a/3x3[0][0]
inception_4a/5x5[0][0]
inception_4a/pool_proj[0][0]
____________________________________________________________________________________________________
inception_4b/3x3_reduce (Convolu (None, 112, 14, 14) 57456 inception_4a/output[0][0]
____________________________________________________________________________________________________
inception_4b/5x5_reduce (Convolu (None, 24, 14, 14) 12312 inception_4a/output[0][0]
____________________________________________________________________________________________________
inception_4b/pool (MaxPooling2D) (None, 512, 14, 14) 0 inception_4a/output[0][0]
____________________________________________________________________________________________________
inception_4b/1x1 (Convolution2D) (None, 160, 14, 14) 82080 inception_4a/output[0][0]
____________________________________________________________________________________________________
inception_4b/3x3 (Convolution2D) (None, 224, 14, 14) 226016 inception_4b/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_4b/5x5 (Convolution2D) (None, 64, 14, 14) 38464 inception_4b/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_4b/pool_proj (Convolut (None, 64, 14, 14) 32832 inception_4b/pool[0][0]
____________________________________________________________________________________________________
inception_4b_output (Merge) (None, 512, 14, 14) 0 inception_4b/1x1[0][0]
inception_4b/3x3[0][0]
inception_4b/5x5[0][0]
inception_4b/pool_proj[0][0]
____________________________________________________________________________________________________
inception_4c/3x3_reduce (Convolu (None, 128, 14, 14) 65664 inception_4b_output[0][0]
____________________________________________________________________________________________________
inception_4c/5x5_reduce (Convolu (None, 24, 14, 14) 12312 inception_4b_output[0][0]
____________________________________________________________________________________________________
inception_4c/pool (MaxPooling2D) (None, 512, 14, 14) 0 inception_4b_output[0][0]
____________________________________________________________________________________________________
inception_4c/1x1 (Convolution2D) (None, 128, 14, 14) 65664 inception_4b_output[0][0]
____________________________________________________________________________________________________
inception_4c/3x3 (Convolution2D) (None, 256, 14, 14) 295168 inception_4c/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_4c/5x5 (Convolution2D) (None, 64, 14, 14) 38464 inception_4c/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_4c/pool_proj (Convolut (None, 64, 14, 14) 32832 inception_4c/pool[0][0]
____________________________________________________________________________________________________
inception_4c/output (Merge) (None, 512, 14, 14) 0 inception_4c/1x1[0][0]
inception_4c/3x3[0][0]
inception_4c/5x5[0][0]
inception_4c/pool_proj[0][0]
____________________________________________________________________________________________________
inception_4d/3x3_reduce (Convolu (None, 144, 14, 14) 73872 inception_4c/output[0][0]
____________________________________________________________________________________________________
inception_4d/5x5_reduce (Convolu (None, 32, 14, 14) 16416 inception_4c/output[0][0]
____________________________________________________________________________________________________
inception_4d/pool (MaxPooling2D) (None, 512, 14, 14) 0 inception_4c/output[0][0]
____________________________________________________________________________________________________
inception_4d/1x1 (Convolution2D) (None, 112, 14, 14) 57456 inception_4c/output[0][0]
____________________________________________________________________________________________________
inception_4d/3x3 (Convolution2D) (None, 288, 14, 14) 373536 inception_4d/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_4d/5x5 (Convolution2D) (None, 64, 14, 14) 51264 inception_4d/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_4d/pool_proj (Convolut (None, 64, 14, 14) 32832 inception_4d/pool[0][0]
____________________________________________________________________________________________________
inception_4d/output (Merge) (None, 528, 14, 14) 0 inception_4d/1x1[0][0]
inception_4d/3x3[0][0]
inception_4d/5x5[0][0]
inception_4d/pool_proj[0][0]
____________________________________________________________________________________________________
inception_4e/3x3_reduce (Convolu (None, 160, 14, 14) 84640 inception_4d/output[0][0]
____________________________________________________________________________________________________
inception_4e/5x5_reduce (Convolu (None, 32, 14, 14) 16928 inception_4d/output[0][0]
____________________________________________________________________________________________________
inception_4e/pool (MaxPooling2D) (None, 528, 14, 14) 0 inception_4d/output[0][0]
____________________________________________________________________________________________________
inception_4e/1x1 (Convolution2D) (None, 256, 14, 14) 135424 inception_4d/output[0][0]
____________________________________________________________________________________________________
inception_4e/3x3 (Convolution2D) (None, 320, 14, 14) 461120 inception_4e/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_4e/5x5 (Convolution2D) (None, 128, 14, 14) 102528 inception_4e/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_4e/pool_proj (Convolut (None, 128, 14, 14) 67712 inception_4e/pool[0][0]
____________________________________________________________________________________________________
inception_4e/output (Merge) (None, 832, 14, 14) 0 inception_4e/1x1[0][0]
inception_4e/3x3[0][0]
inception_4e/5x5[0][0]
inception_4e/pool_proj[0][0]
____________________________________________________________________________________________________
zeropadding2d_4 (ZeroPadding2D) (None, 832, 16, 16) 0 inception_4e/output[0][0]
____________________________________________________________________________________________________
poolhelper_4 (PoolHelper) (None, 832, 16, 16) 0 zeropadding2d_4[0][0]
____________________________________________________________________________________________________
pool4/3x3_s2 (MaxPooling2D) (None, 832, 7, 7) 0 poolhelper_4[0][0]
____________________________________________________________________________________________________
inception_5a/3x3_reduce (Convolu (None, 160, 7, 7) 133280 pool4/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_5a/5x5_reduce (Convolu (None, 32, 7, 7) 26656 pool4/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_5a/pool (MaxPooling2D) (None, 832, 7, 7) 0 pool4/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_5a/1x1 (Convolution2D) (None, 256, 7, 7) 213248 pool4/3x3_s2[0][0]
____________________________________________________________________________________________________
inception_5a/3x3 (Convolution2D) (None, 320, 7, 7) 461120 inception_5a/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_5a/5x5 (Convolution2D) (None, 128, 7, 7) 102528 inception_5a/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_5a/pool_proj (Convolut (None, 128, 7, 7) 106624 inception_5a/pool[0][0]
____________________________________________________________________________________________________
inception_5a/output (Merge) (None, 832, 7, 7) 0 inception_5a/1x1[0][0]
inception_5a/3x3[0][0]
inception_5a/5x5[0][0]
inception_5a/pool_proj[0][0]
____________________________________________________________________________________________________
inception_5b/3x3_reduce (Convolu (None, 192, 7, 7) 159936 inception_5a/output[0][0]
____________________________________________________________________________________________________
inception_5b/5x5_reduce (Convolu (None, 48, 7, 7) 39984 inception_5a/output[0][0]
____________________________________________________________________________________________________
inception_5b/pool (MaxPooling2D) (None, 832, 7, 7) 0 inception_5a/output[0][0]
____________________________________________________________________________________________________
loss1/ave_pool (AveragePooling2D (None, 512, 4, 4) 0 inception_4a/output[0][0]
____________________________________________________________________________________________________
loss2/ave_pool (AveragePooling2D (None, 528, 4, 4) 0 inception_4d/output[0][0]
____________________________________________________________________________________________________
inception_5b/1x1 (Convolution2D) (None, 384, 7, 7) 319872 inception_5a/output[0][0]
____________________________________________________________________________________________________
inception_5b/3x3 (Convolution2D) (None, 384, 7, 7) 663936 inception_5b/3x3_reduce[0][0]
____________________________________________________________________________________________________
inception_5b/5x5 (Convolution2D) (None, 128, 7, 7) 153728 inception_5b/5x5_reduce[0][0]
____________________________________________________________________________________________________
inception_5b/pool_proj (Convolut (None, 128, 7, 7) 106624 inception_5b/pool[0][0]
____________________________________________________________________________________________________
loss1/conv (Convolution2D) (None, 128, 4, 4) 65664 loss1/ave_pool[0][0]
____________________________________________________________________________________________________
loss2/conv (Convolution2D) (None, 128, 4, 4) 67712 loss2/ave_pool[0][0]
____________________________________________________________________________________________________
inception_5b/output (Merge) (None, 1024, 7, 7) 0 inception_5b/1x1[0][0]
inception_5b/3x3[0][0]
inception_5b/5x5[0][0]
inception_5b/pool_proj[0][0]
____________________________________________________________________________________________________
flatten_1 (Flatten) (None, 2048) 0 loss1/conv[0][0]
____________________________________________________________________________________________________
flatten_2 (Flatten) (None, 2048) 0 loss2/conv[0][0]
____________________________________________________________________________________________________
pool5/7x7_s2 (AveragePooling2D) (None, 1024, 1, 1) 0 inception_5b/output[0][0]
____________________________________________________________________________________________________
loss1/fc (Dense) (None, 1024) 2098176 flatten_1[0][0]
____________________________________________________________________________________________________
loss2/fc (Dense) (None, 1024) 2098176 flatten_2[0][0]
____________________________________________________________________________________________________
flatten_3 (Flatten) (None, 1024) 0 pool5/7x7_s2[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout) (None, 1024) 0 loss1/fc[0][0]
____________________________________________________________________________________________________
dropout_2 (Dropout) (None, 1024) 0 loss2/fc[0][0]
____________________________________________________________________________________________________
dropout_3 (Dropout) (None, 1024) 0 flatten_3[0][0]
____________________________________________________________________________________________________
loss1/classifier (Dense) (None, 1000) 1025000 dropout_1[0][0]
____________________________________________________________________________________________________
loss2/classifier (Dense) (None, 1000) 1025000 dropout_2[0][0]
____________________________________________________________________________________________________
loss3/classifier (Dense) (None, 1000) 1025000 dropout_3[0][0]
____________________________________________________________________________________________________
activation_1 (Activation) (None, 1000) 0 loss1/classifier[0][0]
____________________________________________________________________________________________________
activation_2 (Activation) (None, 1000) 0 loss2/classifier[0][0]
____________________________________________________________________________________________________
prob (Activation) (None, 1000) 0 loss3/classifier[0][0]
====================================================================================================
Total params: 13,378,280
Trainable params: 13,378,280
Non-trainable params: 0
____________________________________________________________________________________________________
In [10]:
??model_from_json
In [12]:
??PoolHelper
In [35]:
from keras.layers import Merge
In [36]:
merge
Out[36]:
<function keras.engine.topology.merge>
In [58]:
from keras.models import Sequential
from keras.layers import Dense, merge
left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))
print(left_branch.output_shape)
right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))
print(right_branch.output_shape)
merged = Merge([left_branch, right_branch], mode='concat', concat_axis=1)
print(merged.output_shape)
(None, 32)
(None, 32)
(None, 64)
In [60]:
model.output_shape
Out[60]:
[(None, 1000), (None, 1000), (None, 1000)]
In [62]:
import cv2
img = imread
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-62-a0e6bed9ef3d> in <module>()
----> 1 import cv2
2 img = imread
ImportError: No module named 'cv2'
In [63]:
model
Out[63]:
<keras.engine.training.Model at 0x118b5b0b8>
In [64]:
dir(model)
Out[64]:
['__call__',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_fit_loop',
'_get_node_attribute_at_index',
'_make_predict_function',
'_make_test_function',
'_make_train_function',
'_output_mask_cache',
'_output_shape_cache',
'_output_tensor_cache',
'_predict_loop',
'_standardize_user_data',
'_test_loop',
'_updated_config',
'add_inbound_node',
'add_loss',
'add_update',
'add_weight',
'assert_input_compatibility',
'build',
'built',
'call',
'compile',
'compute_mask',
'constraints',
'container_nodes',
'count_params',
'create_input_layer',
'evaluate',
'evaluate_generator',
'fit',
'fit_generator',
'from_config',
'get_config',
'get_input_at',
'get_input_mask_at',
'get_input_shape_at',
'get_layer',
'get_losses_for',
'get_output_at',
'get_output_mask_at',
'get_output_shape_at',
'get_output_shape_for',
'get_updates_for',
'get_weights',
'inbound_nodes',
'input',
'input_layers',
'input_layers_node_indices',
'input_layers_tensor_indices',
'input_mask',
'input_names',
'input_shape',
'input_spec',
'inputs',
'internal_input_shapes',
'internal_output_shapes',
'layers',
'layers_by_depth',
'load_weights',
'load_weights_from_hdf5_group',
'load_weights_from_hdf5_group_by_name',
'losses',
'name',
'nodes_by_depth',
'non_trainable_weights',
'outbound_nodes',
'output',
'output_layers',
'output_layers_node_indices',
'output_layers_tensor_indices',
'output_mask',
'output_names',
'output_shape',
'outputs',
'predict',
'predict_generator',
'predict_on_batch',
'regularizers',
'reset_states',
'run_internal_graph',
'save',
'save_weights',
'save_weights_to_hdf5_group',
'set_weights',
'state_updates',
'stateful',
'summary',
'supports_masking',
'test_on_batch',
'to_json',
'to_yaml',
'train_on_batch',
'trainable',
'trainable_weights',
'updates',
'uses_learning_phase',
'weights']
In [65]:
type(model)
Out[65]:
keras.engine.training.Model
In [67]:
model.input_shape
Out[67]:
(None, 3, 224, 224)
In [81]:
from PIL import Image
import numpy as np
In [82]:
ls
170518-keras-mnist.ipynb googlenet_custom_layers.py
170519-lstm-text-generation.ipynb googlenet_weights.h5
170523-googlenet.ipynb keras_mlp.py
__pycache__/ tabby_cat.jpg
googlenet_architecture.json
In [106]:
img = Image.open('tabby_cat.jpg')
In [107]:
img = img.resize((224, 224))
In [108]:
img = np.array(img, dtype='float32')
In [109]:
img.shape
Out[109]:
(224, 224, 3)
In [110]:
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.779
img[:, :, 2] -= 103.939
In [111]:
img[:, :, [0, 1, 2]] = img[:, :, [2, 1, 0]] # RGB => BGR
In [112]:
img = img.transpose((2, 0, 1))
In [113]:
img.shape
Out[113]:
(3, 224, 224)
In [114]:
img = np.expand_dims(img, axis=0)
In [115]:
img.shape
Out[115]:
(1, 3, 224, 224)
In [118]:
output = model(img)
In [122]:
output[0]
Out[122]:
theano.tensor.var.TensorVariable
In [135]:
import theano
import keras.backend as K
In [131]:
model.input
Out[131]:
input_1
In [132]:
model.output
Out[132]:
[Softmax.0, Softmax.0, Softmax.0]
In [136]:
f = theano.function(inputs=[model.input, K.learning_phase()], outputs=model.output, allow_input_downcast=True)
In [140]:
K.learning_phase()
Out[140]:
keras_learning_phase
In [144]:
result = f(img, 0)
In [149]:
print(result[0].shape)
print(result[1].shape)
print(result[2].shape)
(1, 1000)
(1, 1000)
(1, 1000)
In [150]:
np.argmax(result[0])
Out[150]:
282
In [151]:
np.argmax(result[1])
Out[151]:
282
In [152]:
np.argmax(result[2])
Out[152]:
282
In [155]:
!wget 'https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json'
--2017-05-23 15:03:14-- https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
Resolving s3.amazonaws.com... 52.216.17.115
Connecting to s3.amazonaws.com|52.216.17.115|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35363 (35K) [application/octet-stream]
Saving to: ‘imagenet_class_index.json’
imagenet_class_inde 100%[===================>] 34.53K 107KB/s in 0.3s
2017-05-23 15:03:15 (107 KB/s) - ‘imagenet_class_index.json’ saved [35363/35363]
In [157]:
import json
classes = json.load(open('imagenet_class_index.json'))
In [160]:
classes['282']
Out[160]:
['n02123159', 'tiger_cat']
In [ ]:
Content source: aidiary/notebooks
Similar notebooks: