In [10]:
from keras.applications.vgg16 import VGG16 
model = VGG16()
print(model.summary())
Model: "vgg16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
predictions (Dense)          (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
None
In [11]:
from keras.preprocessing.image import load_img
# loads an image from file
image = load_img('cat1.jpg', target_size=(224, 224)) 
image
Out[11]:
In [12]:
from keras.preprocessing.image import img_to_array # converts the image pixels to a numpy array: 
image = img_to_array(image)
print(image.shape)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
print(image.shape)
(224, 224, 3)
(1, 224, 224, 3)
In [13]:
y_pred = model.predict(image) 
print(y_pred.shape)
from keras.applications.vgg16 import decode_predictions # convert the probabilities to class labels 
labels_pred = decode_predictions(y_pred) 
print(labels_pred)
(1, 1000)
[[('n02124075', 'Egyptian_cat', 0.6511454), ('n02123045', 'tabby', 0.2637198), ('n02123159', 'tiger_cat', 0.05041448), ('n03938244', 'pillow', 0.009114498), ('n04522168', 'vase', 0.004020794)]]
In [ ]: