Andrew Ng deeplearning courese-4:Convolutional Neural Network

Github地址

Residual Networks

Welcome to the second assignment of this week! You will learn how to build very deep convolutional networks, using Residual Networks (ResNets). In theory, very deep networks can represent very complex functions; but in practice, they are hard to train. Residual Networks, introduced by He et al., allow you to train much deeper networks than were previously practically feasible.

In this assignment, you will:

  • Implement the basic building blocks of ResNets.
  • Put together these building blocks to implement and train a state-of-the-art neural network for image classification.

This assignment will be done in Keras.

Before jumping into the problem, let's run the cell below to load the required packages.

  1. import numpy as np
  2. from keras import layers
  3. from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
  4. from keras.models import Model, load_model
  5. from keras.preprocessing import image
  6. from keras.utils import layer_utils
  7. from keras.utils.data_utils import get_file
  8. from keras.applications.imagenet_utils import preprocess_input
  9. import pydot
  10. from IPython.display import SVG
  11. from keras.utils.vis_utils import model_to_dot
  12. from keras.utils import plot_model
  13. from resnets_utils import *
  14. from keras.initializers import glorot_uniform
  15. import scipy.misc
  16. from matplotlib.pyplot import imshow
  17. %matplotlib inline
  18. import keras.backend as K
  19. K.set_image_data_format('channels_last')
  20. K.set_learning_phase(1)
  1. Using TensorFlow backend.

1 - The problem of very deep neural networks

Last week, you built your first convolutional neural network. In recent years, neural networks have become deeper, with state-of-the-art networks going from just a few layers (e.g., AlexNet) to over a hundred layers.

The main benefit of a very deep network is that it can represent very complex functions. It can also learn features at many different levels of abstraction, from edges (at the lower layers) to very complex features (at the deeper layers). However, using a deeper network doesn't always help. A huge barrier to training them is vanishing gradients: very deep networks often have a gradient signal that goes to zero quickly, thus making gradient descent unbearably slow. More specifically, during gradient descent, as you backprop from the final layer back to the first layer, you are multiplying by the weight matrix on each step, and thus the gradient can decrease exponentially quickly to zero (or, in rare cases, grow exponentially quickly and "explode" to take very large values).

During training, you might therefore see the magnitude (or norm) of the gradient for the earlier layers descrease to zero very rapidly as training proceeds:

**Figure 1** : **Vanishing gradient**
The speed of learning decreases very rapidly for the early layers as the network trains

You are now going to solve this problem by building a Residual Network!

2 - Building a Residual Network

In ResNets, a "shortcut" or a "skip connection" allows the gradient to be directly backpropagated to earlier layers:

**Figure 2** : A ResNet block showing a **skip-connection**

The image on the left shows the "main path" through the network. The image on the right adds a shortcut to the main path. By stacking these ResNet blocks on top of each other, you can form a very deep network.

We also saw in lecture that having ResNet blocks with the shortcut also makes it very easy for one of the blocks to learn an identity function. This means that you can stack on additional ResNet blocks with little risk of harming training set performance. (There is also some evidence that the ease of learning an identity function--even more than skip connections helping with vanishing gradients--accounts for ResNets' remarkable performance.)

Two main types of blocks are used in a ResNet, depending mainly on whether the input/output dimensions are same or different. You are going to implement both of them.

2.1 - The identity block

The identity block is the standard block used in ResNets, and corresponds to the case where the input activation (say \(a^{[l]}\)) has the same dimension as the output activation (say \(a^{[l+2]}\)). To flesh out the different steps of what happens in a ResNet's identity block, here is an alternative diagram showing the individual steps:

**Figure 3** : **Identity block.** Skip connection "skips over" 2 layers.

The upper path is the "shortcut path." The lower path is the "main path." In this diagram, we have also made explicit the CONV2D and ReLU steps in each layer. To speed up training we have also added a BatchNorm step. Don't worry about this being complicated to implement--you'll see that BatchNorm is just one line of code in Keras!

In this exercise, you'll actually implement a slightly more powerful version of this identity block, in which the skip connection "skips over" 3 hidden layers rather than 2 layers. It looks like this:

**Figure 4** : **Identity block.** Skip connection "skips over" 3 layers.

Here're the individual steps.

First component of main path:

  • The first CONV2D has \(F_1\) filters of shape (1,1) and a stride of (1,1). Its padding is "valid" and its name should be conv_name_base + '2a'. Use 0 as the seed for the random initialization.
  • The first BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '2a'.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Second component of main path:

  • The second CONV2D has \(F_2\) filters of shape \((f,f)\) and a stride of (1,1). Its padding is "same" and its name should be conv_name_base + '2b'. Use 0 as the seed for the random initialization.
  • The second BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '2b'.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Third component of main path:

  • The third CONV2D has \(F_3\) filters of shape (1,1) and a stride of (1,1). Its padding is "valid" and its name should be conv_name_base + '2c'. Use 0 as the seed for the random initialization.
  • The third BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '2c'. Note that there is no ReLU activation function in this component.

Final step:

  • The shortcut and the input are added together.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Exercise: Implement the ResNet identity block. We have implemented the first component of the main path. Please read over this carefully to make sure you understand what it is doing. You should implement the rest.

  • To implement the Conv2D step: See reference
  • To implement BatchNorm: See reference (axis: Integer, the axis that should be normalized (typically the channels axis))
  • For the activation, use: Activation('relu')(X)
  • To add the value passed forward by the shortcut: See reference
  1. # GRADED FUNCTION: identity_block
  2. def identity_block(X, f, filters, stage, block):
  3. """
  4. Implementation of the identity block as defined in Figure 3
  5. Arguments:
  6. X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
  7. f -- integer, specifying the shape of the middle CONV's window for the main path
  8. filters -- python list of integers, defining the number of filters in the CONV layers of the main path
  9. stage -- integer, used to name the layers, depending on their position in the network
  10. block -- string/character, used to name the layers, depending on their position in the network
  11. Returns:
  12. X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
  13. """
  14. # defining name basis
  15. conv_name_base = 'res' + str(stage) + block + '_branch'
  16. bn_name_base = 'bn' + str(stage) + block + '_branch'
  17. # Retrieve Filters
  18. F1, F2, F3 = filters
  19. # Save the input value. You'll need this later to add back to the main path.
  20. X_shortcut = X
  21. # First component of main path
  22. X = Conv2D(filters = F1, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X)
  23. X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
  24. X = Activation('relu')(X)
  25. ### START CODE HERE ###
  26. # Second component of main path (≈3 lines)
  27. X = Conv2D(filters = F2, kernel_size = (f, f), strides = (1,1), padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform(seed=0))(X)
  28. X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X)
  29. X = Activation('relu')(X)
  30. # Third component of main path (≈2 lines)
  31. X = Conv2D(filters = F3, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2c', kernel_initializer = glorot_uniform(seed=0))(X)
  32. X = BatchNormalization(axis = 3, name = bn_name_base + '2c')(X)
  33. # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
  34. X = Add()([X_shortcut, X]) # equivalent to added = keras.layers.add([x1, x2])
  35. X = Activation('relu')(X)
  36. ### END CODE HERE ###
  37. return X
  1. tf.reset_default_graph()
  2. with tf.Session() as test:
  3. np.random.seed(1)
  4. A_prev = tf.placeholder("float", [3, 4, 4, 6])
  5. X = np.random.randn(3, 4, 4, 6)
  6. A = identity_block(A_prev, f = 2, filters = [2, 4, 6], stage = 1, block = 'a')
  7. test.run(tf.global_variables_initializer())
  8. out = test.run([A], feed_dict={A_prev: X, K.learning_phase(): 0})
  9. print("out = " + str(out[0][1][1][0]))
  1. out = [ 0.94822985 0. 1.16101444 2.747859 0. 1.36677003]

Expected Output:

**out** [ 0.94822985 0. 1.16101444 2.747859 0. 1.36677003]

2.2 - The convolutional block

You've implemented the ResNet identity block. Next, the ResNet "convolutional block" is the other type of block. You can use this type of block when the input and output dimensions don't match up. The difference with the identity block is that there is a CONV2D layer in the shortcut path:

**Figure 4** : **Convolutional block**

The CONV2D layer in the shortcut path is used to resize the input \(x\) to a different dimension, so that the dimensions match up in the final addition needed to add the shortcut value back to the main path. (This plays a similar role as the matrix \(W_s\) discussed in lecture.) For example, to reduce the activation dimensions's height and width by a factor of 2, you can use a 1x1 convolution with a stride of 2. The CONV2D layer on the shortcut path does not use any non-linear activation function. Its main role is to just apply a (learned) linear function that reduces the dimension of the input, so that the dimensions match up for the later addition step.

The details of the convolutional block are as follows.

First component of main path:

  • The first CONV2D has \(F_1\) filters of shape (1,1) and a stride of (s,s). Its padding is "valid" and its name should be conv_name_base + '2a'.
  • The first BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '2a'.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Second component of main path:

  • The second CONV2D has \(F_2\) filters of (f,f) and a stride of (1,1). Its padding is "same" and it's name should be conv_name_base + '2b'.
  • The second BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '2b'.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Third component of main path:

  • The third CONV2D has \(F_3\) filters of (1,1) and a stride of (1,1). Its padding is "valid" and it's name should be conv_name_base + '2c'.
  • The third BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '2c'. Note that there is no ReLU activation function in this component.

Shortcut path:

  • The CONV2D has \(F_3\) filters of shape (1,1) and a stride of (s,s). Its padding is "valid" and its name should be conv_name_base + '1'.
  • The BatchNorm is normalizing the channels axis. Its name should be bn_name_base + '1'.

Final step:

  • The shortcut and the main path values are added together.
  • Then apply the ReLU activation function. This has no name and no hyperparameters.

Exercise: Implement the convolutional block. We have implemented the first component of the main path; you should implement the rest. As before, always use 0 as the seed for the random initialization, to ensure consistency with our grader.

  1. # GRADED FUNCTION: convolutional_block
  2. def convolutional_block(X, f, filters, stage, block, s = 2):
  3. """
  4. Implementation of the convolutional block as defined in Figure 4
  5. Arguments:
  6. X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
  7. f -- integer, specifying the shape of the middle CONV's window for the main path
  8. filters -- python list of integers, defining the number of filters in the CONV layers of the main path
  9. stage -- integer, used to name the layers, depending on their position in the network
  10. block -- string/character, used to name the layers, depending on their position in the network
  11. s -- Integer, specifying the stride to be used
  12. Returns:
  13. X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
  14. """
  15. # defining name basis
  16. conv_name_base = 'res' + str(stage) + block + '_branch'
  17. bn_name_base = 'bn' + str(stage) + block + '_branch'
  18. # Retrieve Filters
  19. F1, F2, F3 = filters
  20. # Save the input value
  21. X_shortcut = X
  22. ##### MAIN PATH #####
  23. # First component of main path
  24. X = Conv2D(F1, kernel_size=(1, 1), strides = (s,s),padding = 'valid', name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X)
  25. X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
  26. X = Activation('relu')(X)
  27. ### START CODE HERE ###
  28. # Second component of main path (≈3 lines)
  29. X = Conv2D(F2,kernel_size= (f, f), strides = (1,1),padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform(seed=0))(X)
  30. X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X)
  31. X = Activation('relu')(X)
  32. # Third component of main path (≈2 lines)
  33. X = Conv2D(filters = F3, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2c', kernel_initializer = glorot_uniform(seed=0))(X)
  34. X = BatchNormalization(axis = 3, name = bn_name_base + '2c')(X)
  35. ##### SHORTCUT PATH #### (≈2 lines)
  36. X_shortcut = Conv2D(filters = F3, kernel_size = (1, 1), strides = (s,s), padding = 'valid', name = conv_name_base + '1', kernel_initializer = glorot_uniform(seed=0))(X_shortcut)
  37. X_shortcut = BatchNormalization(axis = 3, name = bn_name_base + '1')(X_shortcut)
  38. # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
  39. X = Add()([X_shortcut, X]) # equivalent to added = keras.layers.add([x1, x2])
  40. X = Activation('relu')(X)
  41. ### END CODE HERE ###
  42. return X
  1. tf.reset_default_graph()
  2. with tf.Session() as test:
  3. np.random.seed(1)
  4. A_prev = tf.placeholder("float", [3, 4, 4, 6])
  5. X = np.random.randn(3, 4, 4, 6)
  6. A = convolutional_block(A_prev, f = 2, filters = [2, 4, 6], stage = 1, block = 'a')
  7. test.run(tf.global_variables_initializer())
  8. out = test.run([A], feed_dict={A_prev: X, K.learning_phase(): 0})
  9. print("out = " + str(out[0][1][1][0]))
  1. out = [ 0.09018463 1.23489773 0.46822017 0.0367176 0. 0.65516603]

Expected Output:

**out** [ 0.09018463 1.23489773 0.46822017 0.0367176 0. 0.65516603]

3 - Building your first ResNet model (50 layers)

You now have the necessary blocks to build a very deep ResNet. The following figure describes in detail the architecture of this neural network. "ID BLOCK" in the diagram stands for "Identity block," and "ID BLOCK x3" means you should stack 3 identity blocks together.

**Figure 5** : **ResNet-50 model**

The details of this ResNet-50 model are:

  • Zero-padding pads the input with a pad of (3,3)
  • Stage 1:
    • The 2D Convolution has 64 filters of shape (7,7) and uses a stride of (2,2). Its name is "conv1".
    • BatchNorm is applied to the channels axis of the input.
    • MaxPooling uses a (3,3) window and a (2,2) stride.
  • Stage 2:
    • The convolutional block uses three set of filters of size [64,64,256], "f" is 3, "s" is 1 and the block is "a".
    • The 2 identity blocks use three set of filters of size [64,64,256], "f" is 3 and the blocks are "b" and "c".
  • Stage 3:
    • The convolutional block uses three set of filters of size [128,128,512], "f" is 3, "s" is 2 and the block is "a".
    • The 3 identity blocks use three set of filters of size [128,128,512], "f" is 3 and the blocks are "b", "c" and "d".
  • Stage 4:
    • The convolutional block uses three set of filters of size [256, 256, 1024], "f" is 3, "s" is 2 and the block is "a".
    • The 5 identity blocks use three set of filters of size [256, 256, 1024], "f" is 3 and the blocks are "b", "c", "d", "e" and "f".
  • Stage 5:
    • The convolutional block uses three set of filters of size [512, 512, 2048], "f" is 3, "s" is 2 and the block is "a".
    • The 2 identity blocks use three set of filters of size [512, 512, 2048], "f" is 3 and the blocks are "b" and "c".
  • The 2D Average Pooling uses a window of shape (2,2) and its name is "avg_pool".
  • The flatten doesn't have any hyperparameters or name.
  • The Fully Connected (Dense) layer reduces its input to the number of classes using a softmax activation. Its name should be 'fc' + str(classes).

Exercise: Implement the ResNet with 50 layers described in the figure above. We have implemented Stages 1 and 2. Please implement the rest. (The syntax for implementing Stages 3-5 should be quite similar to that of Stage 2.) Make sure you follow the naming convention in the text above.

You'll need to use this function:

Here're some other functions we used in the code below:

  1. # GRADED FUNCTION: ResNet50
  2. def ResNet50(input_shape = (64, 64, 3), classes = 6):
  3. """
  4. Implementation of the popular ResNet50 the following architecture:
  5. CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
  6. -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER
  7. Arguments:
  8. input_shape -- shape of the images of the dataset
  9. classes -- integer, number of classes
  10. Returns:
  11. model -- a Model() instance in Keras
  12. """
  13. # Define the input as a tensor with shape input_shape
  14. X_input = Input(input_shape)
  15. # Zero-Padding
  16. X = ZeroPadding2D((3, 3))(X_input)
  17. # Stage 1
  18. X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X)
  19. X = BatchNormalization(axis = 3, name = 'bn_conv1')(X)
  20. X = Activation('relu')(X)
  21. X = MaxPooling2D((3, 3), strides=(2, 2))(X)
  22. # Stage 2
  23. X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 1)
  24. X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
  25. X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')
  26. ### START CODE HERE ###
  27. # Stage 3 (≈4 lines)
  28. X = convolutional_block(X, f = 3, filters = [128,128,512], stage = 3, block='a', s = 2)
  29. X = identity_block(X, 3, [128,128,512], stage=3, block='b')
  30. X = identity_block(X, 3, [128,128,512], stage=3, block='c')
  31. X = identity_block(X, 3, [128,128,512], stage=3, block='d')
  32. # Stage 4 (≈6 lines)
  33. X = convolutional_block(X, f = 3, filters = [256, 256, 1024], stage = 4, block='a', s = 2)
  34. X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b')
  35. X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c')
  36. X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d')
  37. X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e')
  38. X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f')
  39. # Stage 5 (≈3 lines)
  40. X = convolutional_block(X, f = 3, filters = [512, 512, 2048], stage = 5, block='a', s = 2)
  41. X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')
  42. X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')
  43. # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
  44. X = AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid',name='avg_pool' )(X)
  45. ### END CODE HERE ###
  46. # output layer
  47. X = Flatten()(X)
  48. X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X)
  49. # Create model
  50. model = Model(inputs = X_input, outputs = X, name='ResNet50')
  51. return model

Run the following code to build the model's graph. If your implementation is not correct you will know it by checking your accuracy when running model.fit(...) below.

  1. model = ResNet50(input_shape = (64, 64, 3), classes = 6)

As seen in the Keras Tutorial Notebook, prior training a model, you need to configure the learning process by compiling the model.

  1. model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

The model is now ready to be trained. The only thing you need is a dataset.

Let's load the SIGNS Dataset.

**Figure 6** : **SIGNS dataset**
  1. X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
  2. # Normalize image vectors
  3. X_train = X_train_orig/255.
  4. X_test = X_test_orig/255.
  5. # Convert training and test labels to one hot matrices
  6. Y_train = convert_to_one_hot(Y_train_orig, 6).T
  7. Y_test = convert_to_one_hot(Y_test_orig, 6).T
  8. print ("number of training examples = " + str(X_train.shape[0]))
  9. print ("number of test examples = " + str(X_test.shape[0]))
  10. print ("X_train shape: " + str(X_train.shape))
  11. print ("Y_train shape: " + str(Y_train.shape))
  12. print ("X_test shape: " + str(X_test.shape))
  13. print ("Y_test shape: " + str(Y_test.shape))
  1. number of training examples = 1080
  2. number of test examples = 120
  3. X_train shape: (1080, 64, 64, 3)
  4. Y_train shape: (1080, 6)
  5. X_test shape: (120, 64, 64, 3)
  6. Y_test shape: (120, 6)

Run the following cell to train your model on 2 epochs with a batch size of 32. On a CPU it should take you around 5min per epoch.

  1. model.fit(X_train, Y_train, epochs = 2, batch_size = 32)
  1. Epoch 1/2
  2. 1080/1080 [==============================] - 276s - loss: 3.1881 - acc: 0.2528
  3. Epoch 2/2
  4. 1080/1080 [==============================] - 266s - loss: 2.7411 - acc: 0.2806
  5. <keras.callbacks.History at 0x7f95e117c5c0>

Expected Output:

** Epoch 1/2** loss: between 1 and 5, acc: between 0.2 and 0.5, although your results can be different from ours.
** Epoch 2/2** loss: between 1 and 5, acc: between 0.2 and 0.5, you should see your loss decreasing and the accuracy increasing.

Let's see how this model (trained on only two epochs) performs on the test set.

  1. preds = model.evaluate(X_test, Y_test)
  2. print ("Loss = " + str(preds[0]))
  3. print ("Test Accuracy = " + str(preds[1]))
  1. 120/120 [==============================] - 10s
  2. Loss = 2.16483095487
  3. Test Accuracy = 0.166666668653

Expected Output:

**Test Accuracy** between 0.16 and 0.25

For the purpose of this assignment, we've asked you to train the model only for two epochs. You can see that it achieves poor performances. Please go ahead and submit your assignment; to check correctness, the online grader will run your code only for a small number of epochs as well.

After you have finished this official (graded) part of this assignment, you can also optionally train the ResNet for more iterations, if you want. We get a lot better performance when we train for ~20 epochs, but this will take more than an hour when training on a CPU.

Using a GPU, we've trained our own ResNet50 model's weights on the SIGNS dataset. You can load and run our trained model on the test set in the cells below. It may take ≈1min to load the model.

  1. model = load_model('ResNet50.h5')
  1. preds = model.evaluate(X_test, Y_test)
  2. print ("Loss = " + str(preds[0]))
  3. print ("Test Accuracy = " + str(preds[1]))
  1. 120/120 [==============================] - 10s
  2. Loss = 0.530178320408
  3. Test Accuracy = 0.866666662693

ResNet50 is a powerful model for image classification when it is trained for an adequate number of iterations. We hope you can use what you've learnt and apply it to your own classification problem to perform state-of-the-art accuracy.

Congratulations on finishing this assignment! You've now implemented a state-of-the-art image classification system!

4 - Test on your own image (Optional/Ungraded)

If you wish, you can also take a picture of your own hand and see the output of the model. To do this:

1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.

2. Add your image to this Jupyter Notebook's directory, in the "images" folder

3. Write your image's name in the following code

4. Run the code and check if the algorithm is right!

  1. img_path = 'images/my_image.jpg'
  2. img = image.load_img(img_path, target_size=(64, 64))
  3. x = image.img_to_array(img)
  4. x = np.expand_dims(x, axis=0)
  5. x = preprocess_input(x)
  6. print('Input image shape:', x.shape)
  7. my_image = scipy.misc.imread(img_path)
  8. imshow(my_image)
  9. print("class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] = ")
  10. print(model.predict(x))
  1. Input image shape: (1, 64, 64, 3)
  2. class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] =
  3. [[ 1. 0. 0. 0. 0. 0.]]

You can also print a summary of your model by running the following code.

  1. model.summary()
  1. ____________________________________________________________________________________________________
  2. Layer (type) Output Shape Param # Connected to
  3. ====================================================================================================
  4. input_1 (InputLayer) (None, 64, 64, 3) 0
  5. ____________________________________________________________________________________________________
  6. zero_padding2d_1 (ZeroPadding2D) (None, 70, 70, 3) 0 input_1[0][0]
  7. ____________________________________________________________________________________________________
  8. conv1 (Conv2D) (None, 32, 32, 64) 9472 zero_padding2d_1[0][0]
  9. ____________________________________________________________________________________________________
  10. bn_conv1 (BatchNormalization) (None, 32, 32, 64) 256 conv1[0][0]
  11. ____________________________________________________________________________________________________
  12. activation_4 (Activation) (None, 32, 32, 64) 0 bn_conv1[0][0]
  13. ____________________________________________________________________________________________________
  14. max_pooling2d_1 (MaxPooling2D) (None, 15, 15, 64) 0 activation_4[0][0]
  15. ____________________________________________________________________________________________________
  16. res2a_branch2a (Conv2D) (None, 15, 15, 64) 4160 max_pooling2d_1[0][0]
  17. ____________________________________________________________________________________________________
  18. bn2a_branch2a (BatchNormalizatio (None, 15, 15, 64) 256 res2a_branch2a[0][0]
  19. ____________________________________________________________________________________________________
  20. activation_5 (Activation) (None, 15, 15, 64) 0 bn2a_branch2a[0][0]
  21. ____________________________________________________________________________________________________
  22. res2a_branch2b (Conv2D) (None, 15, 15, 64) 36928 activation_5[0][0]
  23. ____________________________________________________________________________________________________
  24. bn2a_branch2b (BatchNormalizatio (None, 15, 15, 64) 256 res2a_branch2b[0][0]
  25. ____________________________________________________________________________________________________
  26. activation_6 (Activation) (None, 15, 15, 64) 0 bn2a_branch2b[0][0]
  27. ____________________________________________________________________________________________________
  28. res2a_branch2c (Conv2D) (None, 15, 15, 256) 16640 activation_6[0][0]
  29. ____________________________________________________________________________________________________
  30. res2a_branch1 (Conv2D) (None, 15, 15, 256) 16640 max_pooling2d_1[0][0]
  31. ____________________________________________________________________________________________________
  32. bn2a_branch2c (BatchNormalizatio (None, 15, 15, 256) 1024 res2a_branch2c[0][0]
  33. ____________________________________________________________________________________________________
  34. bn2a_branch1 (BatchNormalization (None, 15, 15, 256) 1024 res2a_branch1[0][0]
  35. ____________________________________________________________________________________________________
  36. add_2 (Add) (None, 15, 15, 256) 0 bn2a_branch2c[0][0]
  37. bn2a_branch1[0][0]
  38. ____________________________________________________________________________________________________
  39. activation_7 (Activation) (None, 15, 15, 256) 0 add_2[0][0]
  40. ____________________________________________________________________________________________________
  41. res2b_branch2a (Conv2D) (None, 15, 15, 64) 16448 activation_7[0][0]
  42. ____________________________________________________________________________________________________
  43. bn2b_branch2a (BatchNormalizatio (None, 15, 15, 64) 256 res2b_branch2a[0][0]
  44. ____________________________________________________________________________________________________
  45. activation_8 (Activation) (None, 15, 15, 64) 0 bn2b_branch2a[0][0]
  46. ____________________________________________________________________________________________________
  47. res2b_branch2b (Conv2D) (None, 15, 15, 64) 36928 activation_8[0][0]
  48. ____________________________________________________________________________________________________
  49. bn2b_branch2b (BatchNormalizatio (None, 15, 15, 64) 256 res2b_branch2b[0][0]
  50. ____________________________________________________________________________________________________
  51. activation_9 (Activation) (None, 15, 15, 64) 0 bn2b_branch2b[0][0]
  52. ____________________________________________________________________________________________________
  53. res2b_branch2c (Conv2D) (None, 15, 15, 256) 16640 activation_9[0][0]
  54. ____________________________________________________________________________________________________
  55. bn2b_branch2c (BatchNormalizatio (None, 15, 15, 256) 1024 res2b_branch2c[0][0]
  56. ____________________________________________________________________________________________________
  57. add_3 (Add) (None, 15, 15, 256) 0 bn2b_branch2c[0][0]
  58. activation_7[0][0]
  59. ____________________________________________________________________________________________________
  60. activation_10 (Activation) (None, 15, 15, 256) 0 add_3[0][0]
  61. ____________________________________________________________________________________________________
  62. res2c_branch2a (Conv2D) (None, 15, 15, 64) 16448 activation_10[0][0]
  63. ____________________________________________________________________________________________________
  64. bn2c_branch2a (BatchNormalizatio (None, 15, 15, 64) 256 res2c_branch2a[0][0]
  65. ____________________________________________________________________________________________________
  66. activation_11 (Activation) (None, 15, 15, 64) 0 bn2c_branch2a[0][0]
  67. ____________________________________________________________________________________________________
  68. res2c_branch2b (Conv2D) (None, 15, 15, 64) 36928 activation_11[0][0]
  69. ____________________________________________________________________________________________________
  70. bn2c_branch2b (BatchNormalizatio (None, 15, 15, 64) 256 res2c_branch2b[0][0]
  71. ____________________________________________________________________________________________________
  72. activation_12 (Activation) (None, 15, 15, 64) 0 bn2c_branch2b[0][0]
  73. ____________________________________________________________________________________________________
  74. res2c_branch2c (Conv2D) (None, 15, 15, 256) 16640 activation_12[0][0]
  75. ____________________________________________________________________________________________________
  76. bn2c_branch2c (BatchNormalizatio (None, 15, 15, 256) 1024 res2c_branch2c[0][0]
  77. ____________________________________________________________________________________________________
  78. add_4 (Add) (None, 15, 15, 256) 0 bn2c_branch2c[0][0]
  79. activation_10[0][0]
  80. ____________________________________________________________________________________________________
  81. activation_13 (Activation) (None, 15, 15, 256) 0 add_4[0][0]
  82. ____________________________________________________________________________________________________
  83. res3a_branch2a (Conv2D) (None, 8, 8, 128) 32896 activation_13[0][0]
  84. ____________________________________________________________________________________________________
  85. bn3a_branch2a (BatchNormalizatio (None, 8, 8, 128) 512 res3a_branch2a[0][0]
  86. ____________________________________________________________________________________________________
  87. activation_14 (Activation) (None, 8, 8, 128) 0 bn3a_branch2a[0][0]
  88. ____________________________________________________________________________________________________
  89. res3a_branch2b (Conv2D) (None, 8, 8, 128) 147584 activation_14[0][0]
  90. ____________________________________________________________________________________________________
  91. bn3a_branch2b (BatchNormalizatio (None, 8, 8, 128) 512 res3a_branch2b[0][0]
  92. ____________________________________________________________________________________________________
  93. activation_15 (Activation) (None, 8, 8, 128) 0 bn3a_branch2b[0][0]
  94. ____________________________________________________________________________________________________
  95. res3a_branch2c (Conv2D) (None, 8, 8, 512) 66048 activation_15[0][0]
  96. ____________________________________________________________________________________________________
  97. res3a_branch1 (Conv2D) (None, 8, 8, 512) 131584 activation_13[0][0]
  98. ____________________________________________________________________________________________________
  99. bn3a_branch2c (BatchNormalizatio (None, 8, 8, 512) 2048 res3a_branch2c[0][0]
  100. ____________________________________________________________________________________________________
  101. bn3a_branch1 (BatchNormalization (None, 8, 8, 512) 2048 res3a_branch1[0][0]
  102. ____________________________________________________________________________________________________
  103. add_5 (Add) (None, 8, 8, 512) 0 bn3a_branch2c[0][0]
  104. bn3a_branch1[0][0]
  105. ____________________________________________________________________________________________________
  106. activation_16 (Activation) (None, 8, 8, 512) 0 add_5[0][0]
  107. ____________________________________________________________________________________________________
  108. res3b_branch2a (Conv2D) (None, 8, 8, 128) 65664 activation_16[0][0]
  109. ____________________________________________________________________________________________________
  110. bn3b_branch2a (BatchNormalizatio (None, 8, 8, 128) 512 res3b_branch2a[0][0]
  111. ____________________________________________________________________________________________________
  112. activation_17 (Activation) (None, 8, 8, 128) 0 bn3b_branch2a[0][0]
  113. ____________________________________________________________________________________________________
  114. res3b_branch2b (Conv2D) (None, 8, 8, 128) 147584 activation_17[0][0]
  115. ____________________________________________________________________________________________________
  116. bn3b_branch2b (BatchNormalizatio (None, 8, 8, 128) 512 res3b_branch2b[0][0]
  117. ____________________________________________________________________________________________________
  118. activation_18 (Activation) (None, 8, 8, 128) 0 bn3b_branch2b[0][0]
  119. ____________________________________________________________________________________________________
  120. res3b_branch2c (Conv2D) (None, 8, 8, 512) 66048 activation_18[0][0]
  121. ____________________________________________________________________________________________________
  122. bn3b_branch2c (BatchNormalizatio (None, 8, 8, 512) 2048 res3b_branch2c[0][0]
  123. ____________________________________________________________________________________________________
  124. add_6 (Add) (None, 8, 8, 512) 0 bn3b_branch2c[0][0]
  125. activation_16[0][0]
  126. ____________________________________________________________________________________________________
  127. activation_19 (Activation) (None, 8, 8, 512) 0 add_6[0][0]
  128. ____________________________________________________________________________________________________
  129. res3c_branch2a (Conv2D) (None, 8, 8, 128) 65664 activation_19[0][0]
  130. ____________________________________________________________________________________________________
  131. bn3c_branch2a (BatchNormalizatio (None, 8, 8, 128) 512 res3c_branch2a[0][0]
  132. ____________________________________________________________________________________________________
  133. activation_20 (Activation) (None, 8, 8, 128) 0 bn3c_branch2a[0][0]
  134. ____________________________________________________________________________________________________
  135. res3c_branch2b (Conv2D) (None, 8, 8, 128) 147584 activation_20[0][0]
  136. ____________________________________________________________________________________________________
  137. bn3c_branch2b (BatchNormalizatio (None, 8, 8, 128) 512 res3c_branch2b[0][0]
  138. ____________________________________________________________________________________________________
  139. activation_21 (Activation) (None, 8, 8, 128) 0 bn3c_branch2b[0][0]
  140. ____________________________________________________________________________________________________
  141. res3c_branch2c (Conv2D) (None, 8, 8, 512) 66048 activation_21[0][0]
  142. ____________________________________________________________________________________________________
  143. bn3c_branch2c (BatchNormalizatio (None, 8, 8, 512) 2048 res3c_branch2c[0][0]
  144. ____________________________________________________________________________________________________
  145. add_7 (Add) (None, 8, 8, 512) 0 bn3c_branch2c[0][0]
  146. activation_19[0][0]
  147. ____________________________________________________________________________________________________
  148. activation_22 (Activation) (None, 8, 8, 512) 0 add_7[0][0]
  149. ____________________________________________________________________________________________________
  150. res3d_branch2a (Conv2D) (None, 8, 8, 128) 65664 activation_22[0][0]
  151. ____________________________________________________________________________________________________
  152. bn3d_branch2a (BatchNormalizatio (None, 8, 8, 128) 512 res3d_branch2a[0][0]
  153. ____________________________________________________________________________________________________
  154. activation_23 (Activation) (None, 8, 8, 128) 0 bn3d_branch2a[0][0]
  155. ____________________________________________________________________________________________________
  156. res3d_branch2b (Conv2D) (None, 8, 8, 128) 147584 activation_23[0][0]
  157. ____________________________________________________________________________________________________
  158. bn3d_branch2b (BatchNormalizatio (None, 8, 8, 128) 512 res3d_branch2b[0][0]
  159. ____________________________________________________________________________________________________
  160. activation_24 (Activation) (None, 8, 8, 128) 0 bn3d_branch2b[0][0]
  161. ____________________________________________________________________________________________________
  162. res3d_branch2c (Conv2D) (None, 8, 8, 512) 66048 activation_24[0][0]
  163. ____________________________________________________________________________________________________
  164. bn3d_branch2c (BatchNormalizatio (None, 8, 8, 512) 2048 res3d_branch2c[0][0]
  165. ____________________________________________________________________________________________________
  166. add_8 (Add) (None, 8, 8, 512) 0 bn3d_branch2c[0][0]
  167. activation_22[0][0]
  168. ____________________________________________________________________________________________________
  169. activation_25 (Activation) (None, 8, 8, 512) 0 add_8[0][0]
  170. ____________________________________________________________________________________________________
  171. res4a_branch2a (Conv2D) (None, 4, 4, 256) 131328 activation_25[0][0]
  172. ____________________________________________________________________________________________________
  173. bn4a_branch2a (BatchNormalizatio (None, 4, 4, 256) 1024 res4a_branch2a[0][0]
  174. ____________________________________________________________________________________________________
  175. activation_26 (Activation) (None, 4, 4, 256) 0 bn4a_branch2a[0][0]
  176. ____________________________________________________________________________________________________
  177. res4a_branch2b (Conv2D) (None, 4, 4, 256) 590080 activation_26[0][0]
  178. ____________________________________________________________________________________________________
  179. bn4a_branch2b (BatchNormalizatio (None, 4, 4, 256) 1024 res4a_branch2b[0][0]
  180. ____________________________________________________________________________________________________
  181. activation_27 (Activation) (None, 4, 4, 256) 0 bn4a_branch2b[0][0]
  182. ____________________________________________________________________________________________________
  183. res4a_branch2c (Conv2D) (None, 4, 4, 1024) 263168 activation_27[0][0]
  184. ____________________________________________________________________________________________________
  185. res4a_branch1 (Conv2D) (None, 4, 4, 1024) 525312 activation_25[0][0]
  186. ____________________________________________________________________________________________________
  187. bn4a_branch2c (BatchNormalizatio (None, 4, 4, 1024) 4096 res4a_branch2c[0][0]
  188. ____________________________________________________________________________________________________
  189. bn4a_branch1 (BatchNormalization (None, 4, 4, 1024) 4096 res4a_branch1[0][0]
  190. ____________________________________________________________________________________________________
  191. add_9 (Add) (None, 4, 4, 1024) 0 bn4a_branch2c[0][0]
  192. bn4a_branch1[0][0]
  193. ____________________________________________________________________________________________________
  194. activation_28 (Activation) (None, 4, 4, 1024) 0 add_9[0][0]
  195. ____________________________________________________________________________________________________
  196. res4b_branch2a (Conv2D) (None, 4, 4, 256) 262400 activation_28[0][0]
  197. ____________________________________________________________________________________________________
  198. bn4b_branch2a (BatchNormalizatio (None, 4, 4, 256) 1024 res4b_branch2a[0][0]
  199. ____________________________________________________________________________________________________
  200. activation_29 (Activation) (None, 4, 4, 256) 0 bn4b_branch2a[0][0]
  201. ____________________________________________________________________________________________________
  202. res4b_branch2b (Conv2D) (None, 4, 4, 256) 590080 activation_29[0][0]
  203. ____________________________________________________________________________________________________
  204. bn4b_branch2b (BatchNormalizatio (None, 4, 4, 256) 1024 res4b_branch2b[0][0]
  205. ____________________________________________________________________________________________________
  206. activation_30 (Activation) (None, 4, 4, 256) 0 bn4b_branch2b[0][0]
  207. ____________________________________________________________________________________________________
  208. res4b_branch2c (Conv2D) (None, 4, 4, 1024) 263168 activation_30[0][0]
  209. ____________________________________________________________________________________________________
  210. bn4b_branch2c (BatchNormalizatio (None, 4, 4, 1024) 4096 res4b_branch2c[0][0]
  211. ____________________________________________________________________________________________________
  212. add_10 (Add) (None, 4, 4, 1024) 0 bn4b_branch2c[0][0]
  213. activation_28[0][0]
  214. ____________________________________________________________________________________________________
  215. activation_31 (Activation) (None, 4, 4, 1024) 0 add_10[0][0]
  216. ____________________________________________________________________________________________________
  217. res4c_branch2a (Conv2D) (None, 4, 4, 256) 262400 activation_31[0][0]
  218. ____________________________________________________________________________________________________
  219. bn4c_branch2a (BatchNormalizatio (None, 4, 4, 256) 1024 res4c_branch2a[0][0]
  220. ____________________________________________________________________________________________________
  221. activation_32 (Activation) (None, 4, 4, 256) 0 bn4c_branch2a[0][0]
  222. ____________________________________________________________________________________________________
  223. res4c_branch2b (Conv2D) (None, 4, 4, 256) 590080 activation_32[0][0]
  224. ____________________________________________________________________________________________________
  225. bn4c_branch2b (BatchNormalizatio (None, 4, 4, 256) 1024 res4c_branch2b[0][0]
  226. ____________________________________________________________________________________________________
  227. activation_33 (Activation) (None, 4, 4, 256) 0 bn4c_branch2b[0][0]
  228. ____________________________________________________________________________________________________
  229. res4c_branch2c (Conv2D) (None, 4, 4, 1024) 263168 activation_33[0][0]
  230. ____________________________________________________________________________________________________
  231. bn4c_branch2c (BatchNormalizatio (None, 4, 4, 1024) 4096 res4c_branch2c[0][0]
  232. ____________________________________________________________________________________________________
  233. add_11 (Add) (None, 4, 4, 1024) 0 bn4c_branch2c[0][0]
  234. activation_31[0][0]
  235. ____________________________________________________________________________________________________
  236. activation_34 (Activation) (None, 4, 4, 1024) 0 add_11[0][0]
  237. ____________________________________________________________________________________________________
  238. res4d_branch2a (Conv2D) (None, 4, 4, 256) 262400 activation_34[0][0]
  239. ____________________________________________________________________________________________________
  240. bn4d_branch2a (BatchNormalizatio (None, 4, 4, 256) 1024 res4d_branch2a[0][0]
  241. ____________________________________________________________________________________________________
  242. activation_35 (Activation) (None, 4, 4, 256) 0 bn4d_branch2a[0][0]
  243. ____________________________________________________________________________________________________
  244. res4d_branch2b (Conv2D) (None, 4, 4, 256) 590080 activation_35[0][0]
  245. ____________________________________________________________________________________________________
  246. bn4d_branch2b (BatchNormalizatio (None, 4, 4, 256) 1024 res4d_branch2b[0][0]
  247. ____________________________________________________________________________________________________
  248. activation_36 (Activation) (None, 4, 4, 256) 0 bn4d_branch2b[0][0]
  249. ____________________________________________________________________________________________________
  250. res4d_branch2c (Conv2D) (None, 4, 4, 1024) 263168 activation_36[0][0]
  251. ____________________________________________________________________________________________________
  252. bn4d_branch2c (BatchNormalizatio (None, 4, 4, 1024) 4096 res4d_branch2c[0][0]
  253. ____________________________________________________________________________________________________
  254. add_12 (Add) (None, 4, 4, 1024) 0 bn4d_branch2c[0][0]
  255. activation_34[0][0]
  256. ____________________________________________________________________________________________________
  257. activation_37 (Activation) (None, 4, 4, 1024) 0 add_12[0][0]
  258. ____________________________________________________________________________________________________
  259. res4e_branch2a (Conv2D) (None, 4, 4, 256) 262400 activation_37[0][0]
  260. ____________________________________________________________________________________________________
  261. bn4e_branch2a (BatchNormalizatio (None, 4, 4, 256) 1024 res4e_branch2a[0][0]
  262. ____________________________________________________________________________________________________
  263. activation_38 (Activation) (None, 4, 4, 256) 0 bn4e_branch2a[0][0]
  264. ____________________________________________________________________________________________________
  265. res4e_branch2b (Conv2D) (None, 4, 4, 256) 590080 activation_38[0][0]
  266. ____________________________________________________________________________________________________
  267. bn4e_branch2b (BatchNormalizatio (None, 4, 4, 256) 1024 res4e_branch2b[0][0]
  268. ____________________________________________________________________________________________________
  269. activation_39 (Activation) (None, 4, 4, 256) 0 bn4e_branch2b[0][0]
  270. ____________________________________________________________________________________________________
  271. res4e_branch2c (Conv2D) (None, 4, 4, 1024) 263168 activation_39[0][0]
  272. ____________________________________________________________________________________________________
  273. bn4e_branch2c (BatchNormalizatio (None, 4, 4, 1024) 4096 res4e_branch2c[0][0]
  274. ____________________________________________________________________________________________________
  275. add_13 (Add) (None, 4, 4, 1024) 0 bn4e_branch2c[0][0]
  276. activation_37[0][0]
  277. ____________________________________________________________________________________________________
  278. activation_40 (Activation) (None, 4, 4, 1024) 0 add_13[0][0]
  279. ____________________________________________________________________________________________________
  280. res4f_branch2a (Conv2D) (None, 4, 4, 256) 262400 activation_40[0][0]
  281. ____________________________________________________________________________________________________
  282. bn4f_branch2a (BatchNormalizatio (None, 4, 4, 256) 1024 res4f_branch2a[0][0]
  283. ____________________________________________________________________________________________________
  284. activation_41 (Activation) (None, 4, 4, 256) 0 bn4f_branch2a[0][0]
  285. ____________________________________________________________________________________________________
  286. res4f_branch2b (Conv2D) (None, 4, 4, 256) 590080 activation_41[0][0]
  287. ____________________________________________________________________________________________________
  288. bn4f_branch2b (BatchNormalizatio (None, 4, 4, 256) 1024 res4f_branch2b[0][0]
  289. ____________________________________________________________________________________________________
  290. activation_42 (Activation) (None, 4, 4, 256) 0 bn4f_branch2b[0][0]
  291. ____________________________________________________________________________________________________
  292. res4f_branch2c (Conv2D) (None, 4, 4, 1024) 263168 activation_42[0][0]
  293. ____________________________________________________________________________________________________
  294. bn4f_branch2c (BatchNormalizatio (None, 4, 4, 1024) 4096 res4f_branch2c[0][0]
  295. ____________________________________________________________________________________________________
  296. add_14 (Add) (None, 4, 4, 1024) 0 bn4f_branch2c[0][0]
  297. activation_40[0][0]
  298. ____________________________________________________________________________________________________
  299. activation_43 (Activation) (None, 4, 4, 1024) 0 add_14[0][0]
  300. ____________________________________________________________________________________________________
  301. res5a_branch2a (Conv2D) (None, 2, 2, 512) 524800 activation_43[0][0]
  302. ____________________________________________________________________________________________________
  303. bn5a_branch2a (BatchNormalizatio (None, 2, 2, 512) 2048 res5a_branch2a[0][0]
  304. ____________________________________________________________________________________________________
  305. activation_44 (Activation) (None, 2, 2, 512) 0 bn5a_branch2a[0][0]
  306. ____________________________________________________________________________________________________
  307. res5a_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_44[0][0]
  308. ____________________________________________________________________________________________________
  309. bn5a_branch2b (BatchNormalizatio (None, 2, 2, 512) 2048 res5a_branch2b[0][0]
  310. ____________________________________________________________________________________________________
  311. activation_45 (Activation) (None, 2, 2, 512) 0 bn5a_branch2b[0][0]
  312. ____________________________________________________________________________________________________
  313. res5a_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_45[0][0]
  314. ____________________________________________________________________________________________________
  315. res5a_branch1 (Conv2D) (None, 2, 2, 2048) 2099200 activation_43[0][0]
  316. ____________________________________________________________________________________________________
  317. bn5a_branch2c (BatchNormalizatio (None, 2, 2, 2048) 8192 res5a_branch2c[0][0]
  318. ____________________________________________________________________________________________________
  319. bn5a_branch1 (BatchNormalization (None, 2, 2, 2048) 8192 res5a_branch1[0][0]
  320. ____________________________________________________________________________________________________
  321. add_15 (Add) (None, 2, 2, 2048) 0 bn5a_branch2c[0][0]
  322. bn5a_branch1[0][0]
  323. ____________________________________________________________________________________________________
  324. activation_46 (Activation) (None, 2, 2, 2048) 0 add_15[0][0]
  325. ____________________________________________________________________________________________________
  326. res5b_branch2a (Conv2D) (None, 2, 2, 512) 1049088 activation_46[0][0]
  327. ____________________________________________________________________________________________________
  328. bn5b_branch2a (BatchNormalizatio (None, 2, 2, 512) 2048 res5b_branch2a[0][0]
  329. ____________________________________________________________________________________________________
  330. activation_47 (Activation) (None, 2, 2, 512) 0 bn5b_branch2a[0][0]
  331. ____________________________________________________________________________________________________
  332. res5b_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_47[0][0]
  333. ____________________________________________________________________________________________________
  334. bn5b_branch2b (BatchNormalizatio (None, 2, 2, 512) 2048 res5b_branch2b[0][0]
  335. ____________________________________________________________________________________________________
  336. activation_48 (Activation) (None, 2, 2, 512) 0 bn5b_branch2b[0][0]
  337. ____________________________________________________________________________________________________
  338. res5b_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_48[0][0]
  339. ____________________________________________________________________________________________________
  340. bn5b_branch2c (BatchNormalizatio (None, 2, 2, 2048) 8192 res5b_branch2c[0][0]
  341. ____________________________________________________________________________________________________
  342. add_16 (Add) (None, 2, 2, 2048) 0 bn5b_branch2c[0][0]
  343. activation_46[0][0]
  344. ____________________________________________________________________________________________________
  345. activation_49 (Activation) (None, 2, 2, 2048) 0 add_16[0][0]
  346. ____________________________________________________________________________________________________
  347. res5c_branch2a (Conv2D) (None, 2, 2, 512) 1049088 activation_49[0][0]
  348. ____________________________________________________________________________________________________
  349. bn5c_branch2a (BatchNormalizatio (None, 2, 2, 512) 2048 res5c_branch2a[0][0]
  350. ____________________________________________________________________________________________________
  351. activation_50 (Activation) (None, 2, 2, 512) 0 bn5c_branch2a[0][0]
  352. ____________________________________________________________________________________________________
  353. res5c_branch2b (Conv2D) (None, 2, 2, 512) 2359808 activation_50[0][0]
  354. ____________________________________________________________________________________________________
  355. bn5c_branch2b (BatchNormalizatio (None, 2, 2, 512) 2048 res5c_branch2b[0][0]
  356. ____________________________________________________________________________________________________
  357. activation_51 (Activation) (None, 2, 2, 512) 0 bn5c_branch2b[0][0]
  358. ____________________________________________________________________________________________________
  359. res5c_branch2c (Conv2D) (None, 2, 2, 2048) 1050624 activation_51[0][0]
  360. ____________________________________________________________________________________________________
  361. bn5c_branch2c (BatchNormalizatio (None, 2, 2, 2048) 8192 res5c_branch2c[0][0]
  362. ____________________________________________________________________________________________________
  363. add_17 (Add) (None, 2, 2, 2048) 0 bn5c_branch2c[0][0]
  364. activation_49[0][0]
  365. ____________________________________________________________________________________________________
  366. activation_52 (Activation) (None, 2, 2, 2048) 0 add_17[0][0]
  367. ____________________________________________________________________________________________________
  368. avg_pool (AveragePooling2D) (None, 1, 1, 2048) 0 activation_52[0][0]
  369. ____________________________________________________________________________________________________
  370. flatten_1 (Flatten) (None, 2048) 0 avg_pool[0][0]
  371. ____________________________________________________________________________________________________
  372. fc6 (Dense) (None, 6) 12294 flatten_1[0][0]
  373. ====================================================================================================
  374. Total params: 23,600,006
  375. Trainable params: 23,546,886
  376. Non-trainable params: 53,120
  377. ____________________________________________________________________________________________________

Finally, run the code below to visualize your ResNet50. You can also download a .png picture of your model by going to "File -> Open...-> model.png".

  1. plot_model(model, to_file='model.png')
  2. SVG(model_to_dot(model).create(prog='dot', format='svg'))

**What you should remember:**
- Very deep "plain" networks don't work in practice because they are hard to train due to vanishing gradients.
- The skip-connections help to address the Vanishing Gradient problem. They also make it easy for a ResNet block to learn an identity function.
- There are two main type of blocks: The identity block and the convolutional block.
- Very deep Residual Networks are built by stacking these blocks together.

References

This notebook presents the ResNet algorithm due to He et al. (2015). The implementation here also took significant inspiration and follows the structure given in the github repository of Francois Chollet:

Residual Networks的更多相关文章

  1. Residual Networks <2015 ICCV, ImageNet 图像分类Top1>

    本文介绍一下2015 ImageNet中分类任务的冠军——MSRA何凯明团队的Residual Networks.实际上,MSRA是今年Imagenet的大赢家,不单在分类任务,MSRA还用resid ...

  2. Residual Networks &lt;2015 ICCV, ImageNet 图像分类Top1&gt;

    本文介绍一下2015 ImageNet中分类任务的冠军--MSRA何凯明团队的Residual Networks.实际上.MSRA是今年Imagenet的大赢家.不单在分类任务,MSRA还用resid ...

  3. 课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) ——3.Programming assignments : Residual Networks

    Residual Networks Welcome to the second assignment of this week! You will learn how to build very de ...

  4. Re-thinking Deep Residual Networks

    本文是对ImageNet 2015的冠军ResNet(Deep Residual Networks)以及目前围绕ResNet这个工作研究者后续所发论文的总结,主要涉及到下面5篇论文. 1. Link: ...

  5. 残差网络(Residual Networks, ResNets)

    1. 什么是残差(residual)? “残差在数理统计中是指实际观察值与估计值(拟合值)之间的差.”“如果回归模型正确的话, 我们可以将残差看作误差的观测值.” 更准确地,假设我们想要找一个 $x$ ...

  6. 深度残差网(deep residual networks)的训练过程

    这里介绍一种深度残差网(deep residual networks)的训练过程: 1.通过下面的地址下载基于python的训练代码: https://github.com/dnlcrl/deep-r ...

  7. 深度学习论文笔记:Deep Residual Networks with Dynamically Weighted Wavelet Coefficients for Fault Diagnosis of Planetary Gearboxes

    这篇文章将深度学习算法应用于机械故障诊断,采用了“小波包分解+深度残差网络(ResNet)”的思路,将机械振动信号按照故障类型进行分类. 文章的核心创新点:复杂旋转机械系统的振动信号包含着很多不同频率 ...

  8. 解析Wide Residual Networks

    Wide Residual Networks (WRNs)是2016年被提出的基于扩展通道数学习机制的卷积神经网络.对深度卷积神经网络有了解的应该知道随着网络越深性能越好,但是训练深度卷积神经网络存在 ...

  9. Convolutional Neural Network-week2编程题2(Residual Networks)

    1. Residual Networks(残差网络) 残差网络 就是为了解决深网络的难以训练的问题的. In this assignment, you will: Implement the basi ...

随机推荐

  1. css3图片旋转

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  2. wap页面

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. jquery----数据增删改

    简单版本 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  4. python接口自动化测试十二:对返回的json的简单操作

    # 1.requests里面自带解析器转字典 print(r.json()) print(type(r.json())) # 取出json中的'result_sk_temp'字段 # {"r ...

  5. plsql developer连接Oracle报错ORA-12154: TNS:could not resolve the connect identifier specified

    今日更改Oracle网络配置文件后使用plsql developer 尝试连接到Oracle出现报错 ORA-12154: TNS:could not resolve the connect iden ...

  6. Java获取当前时间的年月日方法

    package com.ob; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...

  7. visual studio 2015百度云下载

    visual studio 2015百度云下载 https://pan.baidu.com/s/1b198Zo3mX5_zA2VX3xRRfw 提取码: 关注公众号[GitHubCN]回复2015获取

  8. window 下忘记了mysql 密码的解决方法

    1.以管理员身份打开cmd,关闭MySQL. net stop mysql 2.跳过权限检查启动,进入安装目录bin下. mysqld --skip-grant-tables或者mysqld-nt - ...

  9. PHP浮点数的精确计算BCMath

    Php: BCMath bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(string $left_ope ...

  10. Codeforces Round #310 (Div. 2)

    Problem A: 题目大意:给你一个由0,1组成的字符串,如果有相邻的0和1要消去,问你最后还剩几个字符. 写的时候不想看题意直接看样例,结果我以为是1在前0在后才行,交上去错了..后来仔细 看了 ...