Using Pretrained CNN Models

Econ 425T / Biostat 203B

Author

Dr. Hua Zhou @ UCLA

Published

February 22, 2023

Display system information for reproducibility.

import IPython
print(IPython.sys_info())
{'commit_hash': 'add5877a4',
 'commit_source': 'installation',
 'default_encoding': 'utf-8',
 'ipython_path': '/Users/huazhou/opt/anaconda3/lib/python3.9/site-packages/IPython',
 'ipython_version': '8.8.0',
 'os_name': 'posix',
 'platform': 'macOS-10.16-x86_64-i386-64bit',
 'sys_executable': '/Users/huazhou/opt/anaconda3/bin/python3',
 'sys_platform': 'darwin',
 'sys_version': '3.9.12 (main, Apr  5 2022, 01:56:13) \n[Clang 12.0.0 ]'}
sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur ... 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9        here_1.0.1        lattice_0.20-45   png_0.1-8        
 [5] withr_2.5.0       rprojroot_2.0.3   digest_0.6.29     grid_4.2.2       
 [9] jsonlite_1.8.0    magrittr_2.0.3    evaluate_0.15     rlang_1.0.4      
[13] stringi_1.7.8     cli_3.4.1         rstudioapi_0.13   Matrix_1.5-1     
[17] reticulate_1.27   rmarkdown_2.14    tools_4.2.2       stringr_1.4.0    
[21] htmlwidgets_1.6.1 xfun_0.31         yaml_2.3.5        fastmap_1.1.0    
[25] compiler_4.2.2    htmltools_0.5.4   knitr_1.39       

Load some libraries.

# Load the pandas library
import pandas as pd
# Load numpy for array manipulation
import numpy as np
# Load seaborn plotting library
import seaborn as sns
import matplotlib.pyplot as plt
import os

# Set font sizes in plots
sns.set(font_scale = 1.2)
# Display all columns
pd.set_option('display.max_columns', None)

# Load Tensorflow and Keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
library(keras)
library(jpeg)
library(imager)

In this example, we usea a CNN (convolution neural network) pretrained on the ImageNet database to classify natural images.

1 Prepare data

Read in images from the /book_images folder, convert them to arrays of size 224 x 224 x 3, and preprocess them using the preprocess_input function in keras.

img_dir = "book_images"
image_names = os.listdir(img_dir)
num_images = len(image_names)
x = np.empty((num_images, 224, 224, 3))
for i in range(num_images):
  img_path = img_dir + '/' + image_names[i]
  img = keras.utils.load_img(img_path, target_size = (224, 224))
  plt.imshow(img)
  x[i] = keras.utils.img_to_array(img)
  plt.show(x[i] / 255)

x = keras.applications.imagenet_utils.preprocess_input(x)
img_dir <- "book_images"
image_names <- list.files(img_dir)
num_images <- length(image_names)
x <- array(dim = c(num_images, 224, 224, 3))
par(mar = c(0, 0, 0, 0), mfrow = c(2, 3))
for (i in 1:num_images) {
  img_path <- paste(img_dir, image_names[i], sep = "/")
  plot(load.image(img_path))
  img <- image_load(img_path, target_size = c(224, 224))
  x[i, , ,] <- image_to_array(img)
}

x <- imagenet_preprocess_input(x)

2 Load the trained network

We load the resnet50 network trained on the ImageNet database.

model = keras.applications.resnet.ResNet50(weights = 'imagenet')
model.summary()
Model: "resnet50"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_1 (InputLayer)           [(None, 224, 224, 3  0           []                               
                                )]                                                                
                                                                                                  
 conv1_pad (ZeroPadding2D)      (None, 230, 230, 3)  0           ['input_1[0][0]']                
                                                                                                  
 conv1_conv (Conv2D)            (None, 112, 112, 64  9472        ['conv1_pad[0][0]']              
                                )                                                                 
                                                                                                  
 conv1_bn (BatchNormalization)  (None, 112, 112, 64  256         ['conv1_conv[0][0]']             
                                )                                                                 
                                                                                                  
 conv1_relu (Activation)        (None, 112, 112, 64  0           ['conv1_bn[0][0]']               
                                )                                                                 
                                                                                                  
 pool1_pad (ZeroPadding2D)      (None, 114, 114, 64  0           ['conv1_relu[0][0]']             
                                )                                                                 
                                                                                                  
 pool1_pool (MaxPooling2D)      (None, 56, 56, 64)   0           ['pool1_pad[0][0]']              
                                                                                                  
 conv2_block1_1_conv (Conv2D)   (None, 56, 56, 64)   4160        ['pool1_pool[0][0]']             
                                                                                                  
 conv2_block1_1_bn (BatchNormal  (None, 56, 56, 64)  256         ['conv2_block1_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block1_1_relu (Activatio  (None, 56, 56, 64)  0           ['conv2_block1_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv2_block1_2_conv (Conv2D)   (None, 56, 56, 64)   36928       ['conv2_block1_1_relu[0][0]']    
                                                                                                  
 conv2_block1_2_bn (BatchNormal  (None, 56, 56, 64)  256         ['conv2_block1_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block1_2_relu (Activatio  (None, 56, 56, 64)  0           ['conv2_block1_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv2_block1_0_conv (Conv2D)   (None, 56, 56, 256)  16640       ['pool1_pool[0][0]']             
                                                                                                  
 conv2_block1_3_conv (Conv2D)   (None, 56, 56, 256)  16640       ['conv2_block1_2_relu[0][0]']    
                                                                                                  
 conv2_block1_0_bn (BatchNormal  (None, 56, 56, 256)  1024       ['conv2_block1_0_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block1_3_bn (BatchNormal  (None, 56, 56, 256)  1024       ['conv2_block1_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block1_add (Add)         (None, 56, 56, 256)  0           ['conv2_block1_0_bn[0][0]',      
                                                                  'conv2_block1_3_bn[0][0]']      
                                                                                                  
 conv2_block1_out (Activation)  (None, 56, 56, 256)  0           ['conv2_block1_add[0][0]']       
                                                                                                  
 conv2_block2_1_conv (Conv2D)   (None, 56, 56, 64)   16448       ['conv2_block1_out[0][0]']       
                                                                                                  
 conv2_block2_1_bn (BatchNormal  (None, 56, 56, 64)  256         ['conv2_block2_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block2_1_relu (Activatio  (None, 56, 56, 64)  0           ['conv2_block2_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv2_block2_2_conv (Conv2D)   (None, 56, 56, 64)   36928       ['conv2_block2_1_relu[0][0]']    
                                                                                                  
 conv2_block2_2_bn (BatchNormal  (None, 56, 56, 64)  256         ['conv2_block2_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block2_2_relu (Activatio  (None, 56, 56, 64)  0           ['conv2_block2_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv2_block2_3_conv (Conv2D)   (None, 56, 56, 256)  16640       ['conv2_block2_2_relu[0][0]']    
                                                                                                  
 conv2_block2_3_bn (BatchNormal  (None, 56, 56, 256)  1024       ['conv2_block2_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block2_add (Add)         (None, 56, 56, 256)  0           ['conv2_block1_out[0][0]',       
                                                                  'conv2_block2_3_bn[0][0]']      
                                                                                                  
 conv2_block2_out (Activation)  (None, 56, 56, 256)  0           ['conv2_block2_add[0][0]']       
                                                                                                  
 conv2_block3_1_conv (Conv2D)   (None, 56, 56, 64)   16448       ['conv2_block2_out[0][0]']       
                                                                                                  
 conv2_block3_1_bn (BatchNormal  (None, 56, 56, 64)  256         ['conv2_block3_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block3_1_relu (Activatio  (None, 56, 56, 64)  0           ['conv2_block3_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv2_block3_2_conv (Conv2D)   (None, 56, 56, 64)   36928       ['conv2_block3_1_relu[0][0]']    
                                                                                                  
 conv2_block3_2_bn (BatchNormal  (None, 56, 56, 64)  256         ['conv2_block3_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block3_2_relu (Activatio  (None, 56, 56, 64)  0           ['conv2_block3_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv2_block3_3_conv (Conv2D)   (None, 56, 56, 256)  16640       ['conv2_block3_2_relu[0][0]']    
                                                                                                  
 conv2_block3_3_bn (BatchNormal  (None, 56, 56, 256)  1024       ['conv2_block3_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv2_block3_add (Add)         (None, 56, 56, 256)  0           ['conv2_block2_out[0][0]',       
                                                                  'conv2_block3_3_bn[0][0]']      
                                                                                                  
 conv2_block3_out (Activation)  (None, 56, 56, 256)  0           ['conv2_block3_add[0][0]']       
                                                                                                  
 conv3_block1_1_conv (Conv2D)   (None, 28, 28, 128)  32896       ['conv2_block3_out[0][0]']       
                                                                                                  
 conv3_block1_1_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block1_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block1_1_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block1_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block1_2_conv (Conv2D)   (None, 28, 28, 128)  147584      ['conv3_block1_1_relu[0][0]']    
                                                                                                  
 conv3_block1_2_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block1_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block1_2_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block1_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block1_0_conv (Conv2D)   (None, 28, 28, 512)  131584      ['conv2_block3_out[0][0]']       
                                                                                                  
 conv3_block1_3_conv (Conv2D)   (None, 28, 28, 512)  66048       ['conv3_block1_2_relu[0][0]']    
                                                                                                  
 conv3_block1_0_bn (BatchNormal  (None, 28, 28, 512)  2048       ['conv3_block1_0_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block1_3_bn (BatchNormal  (None, 28, 28, 512)  2048       ['conv3_block1_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block1_add (Add)         (None, 28, 28, 512)  0           ['conv3_block1_0_bn[0][0]',      
                                                                  'conv3_block1_3_bn[0][0]']      
                                                                                                  
 conv3_block1_out (Activation)  (None, 28, 28, 512)  0           ['conv3_block1_add[0][0]']       
                                                                                                  
 conv3_block2_1_conv (Conv2D)   (None, 28, 28, 128)  65664       ['conv3_block1_out[0][0]']       
                                                                                                  
 conv3_block2_1_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block2_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block2_1_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block2_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block2_2_conv (Conv2D)   (None, 28, 28, 128)  147584      ['conv3_block2_1_relu[0][0]']    
                                                                                                  
 conv3_block2_2_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block2_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block2_2_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block2_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block2_3_conv (Conv2D)   (None, 28, 28, 512)  66048       ['conv3_block2_2_relu[0][0]']    
                                                                                                  
 conv3_block2_3_bn (BatchNormal  (None, 28, 28, 512)  2048       ['conv3_block2_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block2_add (Add)         (None, 28, 28, 512)  0           ['conv3_block1_out[0][0]',       
                                                                  'conv3_block2_3_bn[0][0]']      
                                                                                                  
 conv3_block2_out (Activation)  (None, 28, 28, 512)  0           ['conv3_block2_add[0][0]']       
                                                                                                  
 conv3_block3_1_conv (Conv2D)   (None, 28, 28, 128)  65664       ['conv3_block2_out[0][0]']       
                                                                                                  
 conv3_block3_1_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block3_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block3_1_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block3_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block3_2_conv (Conv2D)   (None, 28, 28, 128)  147584      ['conv3_block3_1_relu[0][0]']    
                                                                                                  
 conv3_block3_2_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block3_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block3_2_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block3_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block3_3_conv (Conv2D)   (None, 28, 28, 512)  66048       ['conv3_block3_2_relu[0][0]']    
                                                                                                  
 conv3_block3_3_bn (BatchNormal  (None, 28, 28, 512)  2048       ['conv3_block3_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block3_add (Add)         (None, 28, 28, 512)  0           ['conv3_block2_out[0][0]',       
                                                                  'conv3_block3_3_bn[0][0]']      
                                                                                                  
 conv3_block3_out (Activation)  (None, 28, 28, 512)  0           ['conv3_block3_add[0][0]']       
                                                                                                  
 conv3_block4_1_conv (Conv2D)   (None, 28, 28, 128)  65664       ['conv3_block3_out[0][0]']       
                                                                                                  
 conv3_block4_1_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block4_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block4_1_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block4_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block4_2_conv (Conv2D)   (None, 28, 28, 128)  147584      ['conv3_block4_1_relu[0][0]']    
                                                                                                  
 conv3_block4_2_bn (BatchNormal  (None, 28, 28, 128)  512        ['conv3_block4_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block4_2_relu (Activatio  (None, 28, 28, 128)  0          ['conv3_block4_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv3_block4_3_conv (Conv2D)   (None, 28, 28, 512)  66048       ['conv3_block4_2_relu[0][0]']    
                                                                                                  
 conv3_block4_3_bn (BatchNormal  (None, 28, 28, 512)  2048       ['conv3_block4_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv3_block4_add (Add)         (None, 28, 28, 512)  0           ['conv3_block3_out[0][0]',       
                                                                  'conv3_block4_3_bn[0][0]']      
                                                                                                  
 conv3_block4_out (Activation)  (None, 28, 28, 512)  0           ['conv3_block4_add[0][0]']       
                                                                                                  
 conv4_block1_1_conv (Conv2D)   (None, 14, 14, 256)  131328      ['conv3_block4_out[0][0]']       
                                                                                                  
 conv4_block1_1_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block1_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block1_1_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block1_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block1_2_conv (Conv2D)   (None, 14, 14, 256)  590080      ['conv4_block1_1_relu[0][0]']    
                                                                                                  
 conv4_block1_2_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block1_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block1_2_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block1_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block1_0_conv (Conv2D)   (None, 14, 14, 1024  525312      ['conv3_block4_out[0][0]']       
                                )                                                                 
                                                                                                  
 conv4_block1_3_conv (Conv2D)   (None, 14, 14, 1024  263168      ['conv4_block1_2_relu[0][0]']    
                                )                                                                 
                                                                                                  
 conv4_block1_0_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block1_0_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block1_3_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block1_3_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block1_add (Add)         (None, 14, 14, 1024  0           ['conv4_block1_0_bn[0][0]',      
                                )                                 'conv4_block1_3_bn[0][0]']      
                                                                                                  
 conv4_block1_out (Activation)  (None, 14, 14, 1024  0           ['conv4_block1_add[0][0]']       
                                )                                                                 
                                                                                                  
 conv4_block2_1_conv (Conv2D)   (None, 14, 14, 256)  262400      ['conv4_block1_out[0][0]']       
                                                                                                  
 conv4_block2_1_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block2_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block2_1_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block2_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block2_2_conv (Conv2D)   (None, 14, 14, 256)  590080      ['conv4_block2_1_relu[0][0]']    
                                                                                                  
 conv4_block2_2_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block2_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block2_2_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block2_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block2_3_conv (Conv2D)   (None, 14, 14, 1024  263168      ['conv4_block2_2_relu[0][0]']    
                                )                                                                 
                                                                                                  
 conv4_block2_3_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block2_3_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block2_add (Add)         (None, 14, 14, 1024  0           ['conv4_block1_out[0][0]',       
                                )                                 'conv4_block2_3_bn[0][0]']      
                                                                                                  
 conv4_block2_out (Activation)  (None, 14, 14, 1024  0           ['conv4_block2_add[0][0]']       
                                )                                                                 
                                                                                                  
 conv4_block3_1_conv (Conv2D)   (None, 14, 14, 256)  262400      ['conv4_block2_out[0][0]']       
                                                                                                  
 conv4_block3_1_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block3_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block3_1_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block3_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block3_2_conv (Conv2D)   (None, 14, 14, 256)  590080      ['conv4_block3_1_relu[0][0]']    
                                                                                                  
 conv4_block3_2_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block3_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block3_2_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block3_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block3_3_conv (Conv2D)   (None, 14, 14, 1024  263168      ['conv4_block3_2_relu[0][0]']    
                                )                                                                 
                                                                                                  
 conv4_block3_3_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block3_3_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block3_add (Add)         (None, 14, 14, 1024  0           ['conv4_block2_out[0][0]',       
                                )                                 'conv4_block3_3_bn[0][0]']      
                                                                                                  
 conv4_block3_out (Activation)  (None, 14, 14, 1024  0           ['conv4_block3_add[0][0]']       
                                )                                                                 
                                                                                                  
 conv4_block4_1_conv (Conv2D)   (None, 14, 14, 256)  262400      ['conv4_block3_out[0][0]']       
                                                                                                  
 conv4_block4_1_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block4_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block4_1_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block4_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block4_2_conv (Conv2D)   (None, 14, 14, 256)  590080      ['conv4_block4_1_relu[0][0]']    
                                                                                                  
 conv4_block4_2_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block4_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block4_2_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block4_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block4_3_conv (Conv2D)   (None, 14, 14, 1024  263168      ['conv4_block4_2_relu[0][0]']    
                                )                                                                 
                                                                                                  
 conv4_block4_3_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block4_3_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block4_add (Add)         (None, 14, 14, 1024  0           ['conv4_block3_out[0][0]',       
                                )                                 'conv4_block4_3_bn[0][0]']      
                                                                                                  
 conv4_block4_out (Activation)  (None, 14, 14, 1024  0           ['conv4_block4_add[0][0]']       
                                )                                                                 
                                                                                                  
 conv4_block5_1_conv (Conv2D)   (None, 14, 14, 256)  262400      ['conv4_block4_out[0][0]']       
                                                                                                  
 conv4_block5_1_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block5_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block5_1_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block5_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block5_2_conv (Conv2D)   (None, 14, 14, 256)  590080      ['conv4_block5_1_relu[0][0]']    
                                                                                                  
 conv4_block5_2_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block5_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block5_2_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block5_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block5_3_conv (Conv2D)   (None, 14, 14, 1024  263168      ['conv4_block5_2_relu[0][0]']    
                                )                                                                 
                                                                                                  
 conv4_block5_3_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block5_3_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block5_add (Add)         (None, 14, 14, 1024  0           ['conv4_block4_out[0][0]',       
                                )                                 'conv4_block5_3_bn[0][0]']      
                                                                                                  
 conv4_block5_out (Activation)  (None, 14, 14, 1024  0           ['conv4_block5_add[0][0]']       
                                )                                                                 
                                                                                                  
 conv4_block6_1_conv (Conv2D)   (None, 14, 14, 256)  262400      ['conv4_block5_out[0][0]']       
                                                                                                  
 conv4_block6_1_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block6_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block6_1_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block6_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block6_2_conv (Conv2D)   (None, 14, 14, 256)  590080      ['conv4_block6_1_relu[0][0]']    
                                                                                                  
 conv4_block6_2_bn (BatchNormal  (None, 14, 14, 256)  1024       ['conv4_block6_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv4_block6_2_relu (Activatio  (None, 14, 14, 256)  0          ['conv4_block6_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv4_block6_3_conv (Conv2D)   (None, 14, 14, 1024  263168      ['conv4_block6_2_relu[0][0]']    
                                )                                                                 
                                                                                                  
 conv4_block6_3_bn (BatchNormal  (None, 14, 14, 1024  4096       ['conv4_block6_3_conv[0][0]']    
 ization)                       )                                                                 
                                                                                                  
 conv4_block6_add (Add)         (None, 14, 14, 1024  0           ['conv4_block5_out[0][0]',       
                                )                                 'conv4_block6_3_bn[0][0]']      
                                                                                                  
 conv4_block6_out (Activation)  (None, 14, 14, 1024  0           ['conv4_block6_add[0][0]']       
                                )                                                                 
                                                                                                  
 conv5_block1_1_conv (Conv2D)   (None, 7, 7, 512)    524800      ['conv4_block6_out[0][0]']       
                                                                                                  
 conv5_block1_1_bn (BatchNormal  (None, 7, 7, 512)   2048        ['conv5_block1_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block1_1_relu (Activatio  (None, 7, 7, 512)   0           ['conv5_block1_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv5_block1_2_conv (Conv2D)   (None, 7, 7, 512)    2359808     ['conv5_block1_1_relu[0][0]']    
                                                                                                  
 conv5_block1_2_bn (BatchNormal  (None, 7, 7, 512)   2048        ['conv5_block1_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block1_2_relu (Activatio  (None, 7, 7, 512)   0           ['conv5_block1_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv5_block1_0_conv (Conv2D)   (None, 7, 7, 2048)   2099200     ['conv4_block6_out[0][0]']       
                                                                                                  
 conv5_block1_3_conv (Conv2D)   (None, 7, 7, 2048)   1050624     ['conv5_block1_2_relu[0][0]']    
                                                                                                  
 conv5_block1_0_bn (BatchNormal  (None, 7, 7, 2048)  8192        ['conv5_block1_0_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block1_3_bn (BatchNormal  (None, 7, 7, 2048)  8192        ['conv5_block1_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block1_add (Add)         (None, 7, 7, 2048)   0           ['conv5_block1_0_bn[0][0]',      
                                                                  'conv5_block1_3_bn[0][0]']      
                                                                                                  
 conv5_block1_out (Activation)  (None, 7, 7, 2048)   0           ['conv5_block1_add[0][0]']       
                                                                                                  
 conv5_block2_1_conv (Conv2D)   (None, 7, 7, 512)    1049088     ['conv5_block1_out[0][0]']       
                                                                                                  
 conv5_block2_1_bn (BatchNormal  (None, 7, 7, 512)   2048        ['conv5_block2_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block2_1_relu (Activatio  (None, 7, 7, 512)   0           ['conv5_block2_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv5_block2_2_conv (Conv2D)   (None, 7, 7, 512)    2359808     ['conv5_block2_1_relu[0][0]']    
                                                                                                  
 conv5_block2_2_bn (BatchNormal  (None, 7, 7, 512)   2048        ['conv5_block2_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block2_2_relu (Activatio  (None, 7, 7, 512)   0           ['conv5_block2_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv5_block2_3_conv (Conv2D)   (None, 7, 7, 2048)   1050624     ['conv5_block2_2_relu[0][0]']    
                                                                                                  
 conv5_block2_3_bn (BatchNormal  (None, 7, 7, 2048)  8192        ['conv5_block2_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block2_add (Add)         (None, 7, 7, 2048)   0           ['conv5_block1_out[0][0]',       
                                                                  'conv5_block2_3_bn[0][0]']      
                                                                                                  
 conv5_block2_out (Activation)  (None, 7, 7, 2048)   0           ['conv5_block2_add[0][0]']       
                                                                                                  
 conv5_block3_1_conv (Conv2D)   (None, 7, 7, 512)    1049088     ['conv5_block2_out[0][0]']       
                                                                                                  
 conv5_block3_1_bn (BatchNormal  (None, 7, 7, 512)   2048        ['conv5_block3_1_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block3_1_relu (Activatio  (None, 7, 7, 512)   0           ['conv5_block3_1_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv5_block3_2_conv (Conv2D)   (None, 7, 7, 512)    2359808     ['conv5_block3_1_relu[0][0]']    
                                                                                                  
 conv5_block3_2_bn (BatchNormal  (None, 7, 7, 512)   2048        ['conv5_block3_2_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block3_2_relu (Activatio  (None, 7, 7, 512)   0           ['conv5_block3_2_bn[0][0]']      
 n)                                                                                               
                                                                                                  
 conv5_block3_3_conv (Conv2D)   (None, 7, 7, 2048)   1050624     ['conv5_block3_2_relu[0][0]']    
                                                                                                  
 conv5_block3_3_bn (BatchNormal  (None, 7, 7, 2048)  8192        ['conv5_block3_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block3_add (Add)         (None, 7, 7, 2048)   0           ['conv5_block2_out[0][0]',       
                                                                  'conv5_block3_3_bn[0][0]']      
                                                                                                  
 conv5_block3_out (Activation)  (None, 7, 7, 2048)   0           ['conv5_block3_add[0][0]']       
                                                                                                  
 avg_pool (GlobalAveragePooling  (None, 2048)        0           ['conv5_block3_out[0][0]']       
 2D)                                                                                              
                                                                                                  
 predictions (Dense)            (None, 1000)         2049000     ['avg_pool[0][0]']               
                                                                                                  
==================================================================================================
Total params: 25,636,712
Trainable params: 25,583,592
Non-trainable params: 53,120
__________________________________________________________________________________________________

Plot the model:

tf.keras.utils.plot_model(
    model,
    to_file = "model.png",
    show_shapes = True,
    show_dtype = False,
    show_layer_names = True,
    rankdir = "TB",
    expand_nested = False,
    dpi = 96,
    layer_range = None,
    show_layer_activations = False,
)

model <- application_resnet50(weights = "imagenet")
summary(model)
Model: "resnet50"
________________________________________________________________________________
 Layer (type)         Output Shape   Param #  Connected to           Trainable  
================================================================================
 input_2 (InputLayer)  [(None, 224,   0       []                     Y          
                      224, 3)]                                                  
 conv1_pad (ZeroPaddi  (None, 230, 2  0       ['input_2[0][0]']      Y          
 ng2D)                30, 3)                                                    
 conv1_conv (Conv2D)  (None, 112, 1  9472     ['conv1_pad[0][0]']    Y          
                      12, 64)                                                   
 conv1_bn (BatchNorma  (None, 112, 1  256     ['conv1_conv[0][0]']   Y          
 lization)            12, 64)                                                   
 conv1_relu (Activati  (None, 112, 1  0       ['conv1_bn[0][0]']     Y          
 on)                  12, 64)                                                   
 pool1_pad (ZeroPaddi  (None, 114, 1  0       ['conv1_relu[0][0]']   Y          
 ng2D)                14, 64)                                                   
 pool1_pool (MaxPooli  (None, 56, 56  0       ['pool1_pad[0][0]']    Y          
 ng2D)                , 64)                                                     
 conv2_block1_1_conv   (None, 56, 56  4160    ['pool1_pool[0][0]']   Y          
 (Conv2D)             , 64)                                                     
 conv2_block1_1_bn (B  (None, 56, 56  256     ['conv2_block1_1_conv  Y          
 atchNormalization)   , 64)                   [0][0]']                          
 conv2_block1_1_relu   (None, 56, 56  0       ['conv2_block1_1_bn[0  Y          
 (Activation)         , 64)                   ][0]']                            
 conv2_block1_2_conv   (None, 56, 56  36928   ['conv2_block1_1_relu  Y          
 (Conv2D)             , 64)                   [0][0]']                          
 conv2_block1_2_bn (B  (None, 56, 56  256     ['conv2_block1_2_conv  Y          
 atchNormalization)   , 64)                   [0][0]']                          
 conv2_block1_2_relu   (None, 56, 56  0       ['conv2_block1_2_bn[0  Y          
 (Activation)         , 64)                   ][0]']                            
 conv2_block1_0_conv   (None, 56, 56  16640   ['pool1_pool[0][0]']   Y          
 (Conv2D)             , 256)                                                    
 conv2_block1_3_conv   (None, 56, 56  16640   ['conv2_block1_2_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv2_block1_0_bn (B  (None, 56, 56  1024    ['conv2_block1_0_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv2_block1_3_bn (B  (None, 56, 56  1024    ['conv2_block1_3_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv2_block1_add (Ad  (None, 56, 56  0       ['conv2_block1_0_bn[0  Y          
 d)                   , 256)                  ][0]',                            
                                               'conv2_block1_3_bn[0             
                                              ][0]']                            
 conv2_block1_out (Ac  (None, 56, 56  0       ['conv2_block1_add[0]  Y          
 tivation)            , 256)                  [0]']                             
 conv2_block2_1_conv   (None, 56, 56  16448   ['conv2_block1_out[0]  Y          
 (Conv2D)             , 64)                   [0]']                             
 conv2_block2_1_bn (B  (None, 56, 56  256     ['conv2_block2_1_conv  Y          
 atchNormalization)   , 64)                   [0][0]']                          
 conv2_block2_1_relu   (None, 56, 56  0       ['conv2_block2_1_bn[0  Y          
 (Activation)         , 64)                   ][0]']                            
 conv2_block2_2_conv   (None, 56, 56  36928   ['conv2_block2_1_relu  Y          
 (Conv2D)             , 64)                   [0][0]']                          
 conv2_block2_2_bn (B  (None, 56, 56  256     ['conv2_block2_2_conv  Y          
 atchNormalization)   , 64)                   [0][0]']                          
 conv2_block2_2_relu   (None, 56, 56  0       ['conv2_block2_2_bn[0  Y          
 (Activation)         , 64)                   ][0]']                            
 conv2_block2_3_conv   (None, 56, 56  16640   ['conv2_block2_2_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv2_block2_3_bn (B  (None, 56, 56  1024    ['conv2_block2_3_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv2_block2_add (Ad  (None, 56, 56  0       ['conv2_block1_out[0]  Y          
 d)                   , 256)                  [0]',                             
                                               'conv2_block2_3_bn[0             
                                              ][0]']                            
 conv2_block2_out (Ac  (None, 56, 56  0       ['conv2_block2_add[0]  Y          
 tivation)            , 256)                  [0]']                             
 conv2_block3_1_conv   (None, 56, 56  16448   ['conv2_block2_out[0]  Y          
 (Conv2D)             , 64)                   [0]']                             
 conv2_block3_1_bn (B  (None, 56, 56  256     ['conv2_block3_1_conv  Y          
 atchNormalization)   , 64)                   [0][0]']                          
 conv2_block3_1_relu   (None, 56, 56  0       ['conv2_block3_1_bn[0  Y          
 (Activation)         , 64)                   ][0]']                            
 conv2_block3_2_conv   (None, 56, 56  36928   ['conv2_block3_1_relu  Y          
 (Conv2D)             , 64)                   [0][0]']                          
 conv2_block3_2_bn (B  (None, 56, 56  256     ['conv2_block3_2_conv  Y          
 atchNormalization)   , 64)                   [0][0]']                          
 conv2_block3_2_relu   (None, 56, 56  0       ['conv2_block3_2_bn[0  Y          
 (Activation)         , 64)                   ][0]']                            
 conv2_block3_3_conv   (None, 56, 56  16640   ['conv2_block3_2_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv2_block3_3_bn (B  (None, 56, 56  1024    ['conv2_block3_3_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv2_block3_add (Ad  (None, 56, 56  0       ['conv2_block2_out[0]  Y          
 d)                   , 256)                  [0]',                             
                                               'conv2_block3_3_bn[0             
                                              ][0]']                            
 conv2_block3_out (Ac  (None, 56, 56  0       ['conv2_block3_add[0]  Y          
 tivation)            , 256)                  [0]']                             
 conv3_block1_1_conv   (None, 28, 28  32896   ['conv2_block3_out[0]  Y          
 (Conv2D)             , 128)                  [0]']                             
 conv3_block1_1_bn (B  (None, 28, 28  512     ['conv3_block1_1_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block1_1_relu   (None, 28, 28  0       ['conv3_block1_1_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block1_2_conv   (None, 28, 28  147584  ['conv3_block1_1_relu  Y          
 (Conv2D)             , 128)                  [0][0]']                          
 conv3_block1_2_bn (B  (None, 28, 28  512     ['conv3_block1_2_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block1_2_relu   (None, 28, 28  0       ['conv3_block1_2_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block1_0_conv   (None, 28, 28  131584  ['conv2_block3_out[0]  Y          
 (Conv2D)             , 512)                  [0]']                             
 conv3_block1_3_conv   (None, 28, 28  66048   ['conv3_block1_2_relu  Y          
 (Conv2D)             , 512)                  [0][0]']                          
 conv3_block1_0_bn (B  (None, 28, 28  2048    ['conv3_block1_0_conv  Y          
 atchNormalization)   , 512)                  [0][0]']                          
 conv3_block1_3_bn (B  (None, 28, 28  2048    ['conv3_block1_3_conv  Y          
 atchNormalization)   , 512)                  [0][0]']                          
 conv3_block1_add (Ad  (None, 28, 28  0       ['conv3_block1_0_bn[0  Y          
 d)                   , 512)                  ][0]',                            
                                               'conv3_block1_3_bn[0             
                                              ][0]']                            
 conv3_block1_out (Ac  (None, 28, 28  0       ['conv3_block1_add[0]  Y          
 tivation)            , 512)                  [0]']                             
 conv3_block2_1_conv   (None, 28, 28  65664   ['conv3_block1_out[0]  Y          
 (Conv2D)             , 128)                  [0]']                             
 conv3_block2_1_bn (B  (None, 28, 28  512     ['conv3_block2_1_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block2_1_relu   (None, 28, 28  0       ['conv3_block2_1_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block2_2_conv   (None, 28, 28  147584  ['conv3_block2_1_relu  Y          
 (Conv2D)             , 128)                  [0][0]']                          
 conv3_block2_2_bn (B  (None, 28, 28  512     ['conv3_block2_2_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block2_2_relu   (None, 28, 28  0       ['conv3_block2_2_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block2_3_conv   (None, 28, 28  66048   ['conv3_block2_2_relu  Y          
 (Conv2D)             , 512)                  [0][0]']                          
 conv3_block2_3_bn (B  (None, 28, 28  2048    ['conv3_block2_3_conv  Y          
 atchNormalization)   , 512)                  [0][0]']                          
 conv3_block2_add (Ad  (None, 28, 28  0       ['conv3_block1_out[0]  Y          
 d)                   , 512)                  [0]',                             
                                               'conv3_block2_3_bn[0             
                                              ][0]']                            
 conv3_block2_out (Ac  (None, 28, 28  0       ['conv3_block2_add[0]  Y          
 tivation)            , 512)                  [0]']                             
 conv3_block3_1_conv   (None, 28, 28  65664   ['conv3_block2_out[0]  Y          
 (Conv2D)             , 128)                  [0]']                             
 conv3_block3_1_bn (B  (None, 28, 28  512     ['conv3_block3_1_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block3_1_relu   (None, 28, 28  0       ['conv3_block3_1_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block3_2_conv   (None, 28, 28  147584  ['conv3_block3_1_relu  Y          
 (Conv2D)             , 128)                  [0][0]']                          
 conv3_block3_2_bn (B  (None, 28, 28  512     ['conv3_block3_2_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block3_2_relu   (None, 28, 28  0       ['conv3_block3_2_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block3_3_conv   (None, 28, 28  66048   ['conv3_block3_2_relu  Y          
 (Conv2D)             , 512)                  [0][0]']                          
 conv3_block3_3_bn (B  (None, 28, 28  2048    ['conv3_block3_3_conv  Y          
 atchNormalization)   , 512)                  [0][0]']                          
 conv3_block3_add (Ad  (None, 28, 28  0       ['conv3_block2_out[0]  Y          
 d)                   , 512)                  [0]',                             
                                               'conv3_block3_3_bn[0             
                                              ][0]']                            
 conv3_block3_out (Ac  (None, 28, 28  0       ['conv3_block3_add[0]  Y          
 tivation)            , 512)                  [0]']                             
 conv3_block4_1_conv   (None, 28, 28  65664   ['conv3_block3_out[0]  Y          
 (Conv2D)             , 128)                  [0]']                             
 conv3_block4_1_bn (B  (None, 28, 28  512     ['conv3_block4_1_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block4_1_relu   (None, 28, 28  0       ['conv3_block4_1_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block4_2_conv   (None, 28, 28  147584  ['conv3_block4_1_relu  Y          
 (Conv2D)             , 128)                  [0][0]']                          
 conv3_block4_2_bn (B  (None, 28, 28  512     ['conv3_block4_2_conv  Y          
 atchNormalization)   , 128)                  [0][0]']                          
 conv3_block4_2_relu   (None, 28, 28  0       ['conv3_block4_2_bn[0  Y          
 (Activation)         , 128)                  ][0]']                            
 conv3_block4_3_conv   (None, 28, 28  66048   ['conv3_block4_2_relu  Y          
 (Conv2D)             , 512)                  [0][0]']                          
 conv3_block4_3_bn (B  (None, 28, 28  2048    ['conv3_block4_3_conv  Y          
 atchNormalization)   , 512)                  [0][0]']                          
 conv3_block4_add (Ad  (None, 28, 28  0       ['conv3_block3_out[0]  Y          
 d)                   , 512)                  [0]',                             
                                               'conv3_block4_3_bn[0             
                                              ][0]']                            
 conv3_block4_out (Ac  (None, 28, 28  0       ['conv3_block4_add[0]  Y          
 tivation)            , 512)                  [0]']                             
 conv4_block1_1_conv   (None, 14, 14  131328  ['conv3_block4_out[0]  Y          
 (Conv2D)             , 256)                  [0]']                             
 conv4_block1_1_bn (B  (None, 14, 14  1024    ['conv4_block1_1_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block1_1_relu   (None, 14, 14  0       ['conv4_block1_1_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block1_2_conv   (None, 14, 14  590080  ['conv4_block1_1_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv4_block1_2_bn (B  (None, 14, 14  1024    ['conv4_block1_2_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block1_2_relu   (None, 14, 14  0       ['conv4_block1_2_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block1_0_conv   (None, 14, 14  525312  ['conv3_block4_out[0]  Y          
 (Conv2D)             , 1024)                 [0]']                             
 conv4_block1_3_conv   (None, 14, 14  263168  ['conv4_block1_2_relu  Y          
 (Conv2D)             , 1024)                 [0][0]']                          
 conv4_block1_0_bn (B  (None, 14, 14  4096    ['conv4_block1_0_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block1_3_bn (B  (None, 14, 14  4096    ['conv4_block1_3_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block1_add (Ad  (None, 14, 14  0       ['conv4_block1_0_bn[0  Y          
 d)                   , 1024)                 ][0]',                            
                                               'conv4_block1_3_bn[0             
                                              ][0]']                            
 conv4_block1_out (Ac  (None, 14, 14  0       ['conv4_block1_add[0]  Y          
 tivation)            , 1024)                 [0]']                             
 conv4_block2_1_conv   (None, 14, 14  262400  ['conv4_block1_out[0]  Y          
 (Conv2D)             , 256)                  [0]']                             
 conv4_block2_1_bn (B  (None, 14, 14  1024    ['conv4_block2_1_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block2_1_relu   (None, 14, 14  0       ['conv4_block2_1_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block2_2_conv   (None, 14, 14  590080  ['conv4_block2_1_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv4_block2_2_bn (B  (None, 14, 14  1024    ['conv4_block2_2_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block2_2_relu   (None, 14, 14  0       ['conv4_block2_2_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block2_3_conv   (None, 14, 14  263168  ['conv4_block2_2_relu  Y          
 (Conv2D)             , 1024)                 [0][0]']                          
 conv4_block2_3_bn (B  (None, 14, 14  4096    ['conv4_block2_3_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block2_add (Ad  (None, 14, 14  0       ['conv4_block1_out[0]  Y          
 d)                   , 1024)                 [0]',                             
                                               'conv4_block2_3_bn[0             
                                              ][0]']                            
 conv4_block2_out (Ac  (None, 14, 14  0       ['conv4_block2_add[0]  Y          
 tivation)            , 1024)                 [0]']                             
 conv4_block3_1_conv   (None, 14, 14  262400  ['conv4_block2_out[0]  Y          
 (Conv2D)             , 256)                  [0]']                             
 conv4_block3_1_bn (B  (None, 14, 14  1024    ['conv4_block3_1_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block3_1_relu   (None, 14, 14  0       ['conv4_block3_1_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block3_2_conv   (None, 14, 14  590080  ['conv4_block3_1_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv4_block3_2_bn (B  (None, 14, 14  1024    ['conv4_block3_2_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block3_2_relu   (None, 14, 14  0       ['conv4_block3_2_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block3_3_conv   (None, 14, 14  263168  ['conv4_block3_2_relu  Y          
 (Conv2D)             , 1024)                 [0][0]']                          
 conv4_block3_3_bn (B  (None, 14, 14  4096    ['conv4_block3_3_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block3_add (Ad  (None, 14, 14  0       ['conv4_block2_out[0]  Y          
 d)                   , 1024)                 [0]',                             
                                               'conv4_block3_3_bn[0             
                                              ][0]']                            
 conv4_block3_out (Ac  (None, 14, 14  0       ['conv4_block3_add[0]  Y          
 tivation)            , 1024)                 [0]']                             
 conv4_block4_1_conv   (None, 14, 14  262400  ['conv4_block3_out[0]  Y          
 (Conv2D)             , 256)                  [0]']                             
 conv4_block4_1_bn (B  (None, 14, 14  1024    ['conv4_block4_1_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block4_1_relu   (None, 14, 14  0       ['conv4_block4_1_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block4_2_conv   (None, 14, 14  590080  ['conv4_block4_1_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv4_block4_2_bn (B  (None, 14, 14  1024    ['conv4_block4_2_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block4_2_relu   (None, 14, 14  0       ['conv4_block4_2_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block4_3_conv   (None, 14, 14  263168  ['conv4_block4_2_relu  Y          
 (Conv2D)             , 1024)                 [0][0]']                          
 conv4_block4_3_bn (B  (None, 14, 14  4096    ['conv4_block4_3_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block4_add (Ad  (None, 14, 14  0       ['conv4_block3_out[0]  Y          
 d)                   , 1024)                 [0]',                             
                                               'conv4_block4_3_bn[0             
                                              ][0]']                            
 conv4_block4_out (Ac  (None, 14, 14  0       ['conv4_block4_add[0]  Y          
 tivation)            , 1024)                 [0]']                             
 conv4_block5_1_conv   (None, 14, 14  262400  ['conv4_block4_out[0]  Y          
 (Conv2D)             , 256)                  [0]']                             
 conv4_block5_1_bn (B  (None, 14, 14  1024    ['conv4_block5_1_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block5_1_relu   (None, 14, 14  0       ['conv4_block5_1_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block5_2_conv   (None, 14, 14  590080  ['conv4_block5_1_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv4_block5_2_bn (B  (None, 14, 14  1024    ['conv4_block5_2_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block5_2_relu   (None, 14, 14  0       ['conv4_block5_2_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block5_3_conv   (None, 14, 14  263168  ['conv4_block5_2_relu  Y          
 (Conv2D)             , 1024)                 [0][0]']                          
 conv4_block5_3_bn (B  (None, 14, 14  4096    ['conv4_block5_3_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block5_add (Ad  (None, 14, 14  0       ['conv4_block4_out[0]  Y          
 d)                   , 1024)                 [0]',                             
                                               'conv4_block5_3_bn[0             
                                              ][0]']                            
 conv4_block5_out (Ac  (None, 14, 14  0       ['conv4_block5_add[0]  Y          
 tivation)            , 1024)                 [0]']                             
 conv4_block6_1_conv   (None, 14, 14  262400  ['conv4_block5_out[0]  Y          
 (Conv2D)             , 256)                  [0]']                             
 conv4_block6_1_bn (B  (None, 14, 14  1024    ['conv4_block6_1_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block6_1_relu   (None, 14, 14  0       ['conv4_block6_1_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block6_2_conv   (None, 14, 14  590080  ['conv4_block6_1_relu  Y          
 (Conv2D)             , 256)                  [0][0]']                          
 conv4_block6_2_bn (B  (None, 14, 14  1024    ['conv4_block6_2_conv  Y          
 atchNormalization)   , 256)                  [0][0]']                          
 conv4_block6_2_relu   (None, 14, 14  0       ['conv4_block6_2_bn[0  Y          
 (Activation)         , 256)                  ][0]']                            
 conv4_block6_3_conv   (None, 14, 14  263168  ['conv4_block6_2_relu  Y          
 (Conv2D)             , 1024)                 [0][0]']                          
 conv4_block6_3_bn (B  (None, 14, 14  4096    ['conv4_block6_3_conv  Y          
 atchNormalization)   , 1024)                 [0][0]']                          
 conv4_block6_add (Ad  (None, 14, 14  0       ['conv4_block5_out[0]  Y          
 d)                   , 1024)                 [0]',                             
                                               'conv4_block6_3_bn[0             
                                              ][0]']                            
 conv4_block6_out (Ac  (None, 14, 14  0       ['conv4_block6_add[0]  Y          
 tivation)            , 1024)                 [0]']                             
 conv5_block1_1_conv   (None, 7, 7,   524800  ['conv4_block6_out[0]  Y          
 (Conv2D)             512)                    [0]']                             
 conv5_block1_1_bn (B  (None, 7, 7,   2048    ['conv5_block1_1_conv  Y          
 atchNormalization)   512)                    [0][0]']                          
 conv5_block1_1_relu   (None, 7, 7,   0       ['conv5_block1_1_bn[0  Y          
 (Activation)         512)                    ][0]']                            
 conv5_block1_2_conv   (None, 7, 7,   2359808  ['conv5_block1_1_relu  Y         
 (Conv2D)             512)                    [0][0]']                          
 conv5_block1_2_bn (B  (None, 7, 7,   2048    ['conv5_block1_2_conv  Y          
 atchNormalization)   512)                    [0][0]']                          
 conv5_block1_2_relu   (None, 7, 7,   0       ['conv5_block1_2_bn[0  Y          
 (Activation)         512)                    ][0]']                            
 conv5_block1_0_conv   (None, 7, 7,   2099200  ['conv4_block6_out[0]  Y         
 (Conv2D)             2048)                   [0]']                             
 conv5_block1_3_conv   (None, 7, 7,   1050624  ['conv5_block1_2_relu  Y         
 (Conv2D)             2048)                   [0][0]']                          
 conv5_block1_0_bn (B  (None, 7, 7,   8192    ['conv5_block1_0_conv  Y          
 atchNormalization)   2048)                   [0][0]']                          
 conv5_block1_3_bn (B  (None, 7, 7,   8192    ['conv5_block1_3_conv  Y          
 atchNormalization)   2048)                   [0][0]']                          
 conv5_block1_add (Ad  (None, 7, 7,   0       ['conv5_block1_0_bn[0  Y          
 d)                   2048)                   ][0]',                            
                                               'conv5_block1_3_bn[0             
                                              ][0]']                            
 conv5_block1_out (Ac  (None, 7, 7,   0       ['conv5_block1_add[0]  Y          
 tivation)            2048)                   [0]']                             
 conv5_block2_1_conv   (None, 7, 7,   1049088  ['conv5_block1_out[0]  Y         
 (Conv2D)             512)                    [0]']                             
 conv5_block2_1_bn (B  (None, 7, 7,   2048    ['conv5_block2_1_conv  Y          
 atchNormalization)   512)                    [0][0]']                          
 conv5_block2_1_relu   (None, 7, 7,   0       ['conv5_block2_1_bn[0  Y          
 (Activation)         512)                    ][0]']                            
 conv5_block2_2_conv   (None, 7, 7,   2359808  ['conv5_block2_1_relu  Y         
 (Conv2D)             512)                    [0][0]']                          
 conv5_block2_2_bn (B  (None, 7, 7,   2048    ['conv5_block2_2_conv  Y          
 atchNormalization)   512)                    [0][0]']                          
 conv5_block2_2_relu   (None, 7, 7,   0       ['conv5_block2_2_bn[0  Y          
 (Activation)         512)                    ][0]']                            
 conv5_block2_3_conv   (None, 7, 7,   1050624  ['conv5_block2_2_relu  Y         
 (Conv2D)             2048)                   [0][0]']                          
 conv5_block2_3_bn (B  (None, 7, 7,   8192    ['conv5_block2_3_conv  Y          
 atchNormalization)   2048)                   [0][0]']                          
 conv5_block2_add (Ad  (None, 7, 7,   0       ['conv5_block1_out[0]  Y          
 d)                   2048)                   [0]',                             
                                               'conv5_block2_3_bn[0             
                                              ][0]']                            
 conv5_block2_out (Ac  (None, 7, 7,   0       ['conv5_block2_add[0]  Y          
 tivation)            2048)                   [0]']                             
 conv5_block3_1_conv   (None, 7, 7,   1049088  ['conv5_block2_out[0]  Y         
 (Conv2D)             512)                    [0]']                             
 conv5_block3_1_bn (B  (None, 7, 7,   2048    ['conv5_block3_1_conv  Y          
 atchNormalization)   512)                    [0][0]']                          
 conv5_block3_1_relu   (None, 7, 7,   0       ['conv5_block3_1_bn[0  Y          
 (Activation)         512)                    ][0]']                            
 conv5_block3_2_conv   (None, 7, 7,   2359808  ['conv5_block3_1_relu  Y         
 (Conv2D)             512)                    [0][0]']                          
 conv5_block3_2_bn (B  (None, 7, 7,   2048    ['conv5_block3_2_conv  Y          
 atchNormalization)   512)                    [0][0]']                          
 conv5_block3_2_relu   (None, 7, 7,   0       ['conv5_block3_2_bn[0  Y          
 (Activation)         512)                    ][0]']                            
 conv5_block3_3_conv   (None, 7, 7,   1050624  ['conv5_block3_2_relu  Y         
 (Conv2D)             2048)                   [0][0]']                          
 conv5_block3_3_bn (B  (None, 7, 7,   8192    ['conv5_block3_3_conv  Y          
 atchNormalization)   2048)                   [0][0]']                          
 conv5_block3_add (Ad  (None, 7, 7,   0       ['conv5_block2_out[0]  Y          
 d)                   2048)                   [0]',                             
                                               'conv5_block3_3_bn[0             
                                              ][0]']                            
 conv5_block3_out (Ac  (None, 7, 7,   0       ['conv5_block3_add[0]  Y          
 tivation)            2048)                   [0]']                             
 avg_pool (GlobalAver  (None, 2048)  0        ['conv5_block3_out[0]  Y          
 agePooling2D)                                [0]']                             
 predictions (Dense)  (None, 1000)   2049000  ['avg_pool[0][0]']     Y          
================================================================================
Total params: 25,636,712
Trainable params: 25,583,592
Non-trainable params: 53,120
________________________________________________________________________________

3 Classify images

Finally, we classify our six images, and return the top three class choices in terms of predicted probability for each.

pred6 = keras.applications.imagenet_utils.decode_predictions(
  model.predict(x), 
  top = 3
  )

1/1 [==============================] - ETA: 0s
1/1 [==============================] - 1s 1s/step
for i in range(num_images):
  print(image_names[i])
  print(np.asarray(pred6[i]))
weaver.jpg
[['n01843065' 'jacamar' '0.4865981']
 ['n01818515' 'macaw' '0.23028952']
 ['n02494079' 'squirrel_monkey' '0.044571042']]
flamingo.jpg
[['n02007558' 'flamingo' '0.93010914']
 ['n02006656' 'spoonbill' '0.06801486']
 ['n02002556' 'white_stork' '0.0011723748']]
huey.jpg
[['n02097474' 'Tibetan_terrier' '0.5246206']
 ['n02098413' 'Lhasa' '0.40590215']
 ['n02094114' 'Norfolk_terrier' '0.016995324']]
hawk_cropped.jpeg
[['n01608432' 'kite' '0.7243426']
 ['n01622779' 'great_grey_owl' '0.086835146']
 ['n01532829' 'house_finch' '0.039976425']]
hawk.jpg
[['n03388043' 'fountain' '0.27721697']
 ['n03532672' 'hook' '0.17947067']
 ['n03804744' 'nail' '0.1103583']]
kitty.jpg
[['n02105641' 'Old_English_sheepdog' '0.83900243']
 ['n02086240' 'Shih-Tzu' '0.04235816']
 ['n03223299' 'doormat' '0.032265782']]
pred6 <- model %>% 
  predict(x) %>%
  imagenet_decode_predictions(top = 3)

names(pred6) <- image_names
pred6
$flamingo.jpg
  class_name class_description       score
1  n02007558          flamingo 0.930109143
2  n02006656         spoonbill 0.068014860
3  n02002556       white_stork 0.001172375

$hawk_cropped.jpeg
  class_name class_description      score
1  n01608432              kite 0.72434247
2  n01622779    great_grey_owl 0.08683521
3  n01532829       house_finch 0.03997646

$hawk.jpg
  class_name class_description     score
1  n03388043          fountain 0.2772168
2  n03532672              hook 0.1794709
3  n03804744              nail 0.1103582

$huey.jpg
  class_name class_description      score
1  n02097474   Tibetan_terrier 0.52462149
2  n02098413             Lhasa 0.40590131
3  n02094114   Norfolk_terrier 0.01699529

$kitty.jpg
  class_name    class_description      score
1  n02105641 Old_English_sheepdog 0.83900261
2  n02086240             Shih-Tzu 0.04235817
3  n03223299              doormat 0.03226573

$weaver.jpg
  class_name class_description      score
1  n01843065           jacamar 0.48659873
2  n01818515             macaw 0.23028916
3  n02494079   squirrel_monkey 0.04457097