ResNet was originally developed as a method to solve the vanishing gradient problem. This is a problem where backpropagating gradients become extremely small as they multiply over and over again, limiting the size of the neural network. The ResNet architecture tries to solve this problem by skipping connections, that is, adding shortcuts that allow data to skip past layers.
The model consists of a series of convolutional layers + skipped connections, then a middle pool, then an output fully connected (dense) layer. For transfer learning, we want the convolutional layers to contain only the features we are interested in, so we would like to skip them when importing the model. Finally, since we are deleting the output layers, we need to replace them with our own series of layers.
Statement of the problem
To illustrate the process of teaching transmission, I will use Caltech-101 dataset, image dataset with 101 categories and approximately 40-800 images per category.
First download and extract the dataset here . Be sure to delete the "BACKGROUND_Google" folder after checkout.
Code: For proper evaluation, we need to separate the data into training and test cases. Here we need to split within each category to ensure proper presentation in the test suite.
This above code creates the file structure: 101_ObjectCategories / - accordion - airplanes - anchor - ... caltech_test / - accordion - airplanes - anchor - ...
The first folder contains images of trains, the second contains test images. Each subfolder contains images belonging to this category. We will use the Keras ImageDataGenerator class to enter data. ImageDataGenerator allows for easy processing of image data and also has options for enlargement.
The above code takes the path to the image directory file and creates an object for generating data.
Building the model
Code: add a basic pretrained model.
| tr> |
This dataset is relatively small — about 5628 images after splitting, with most categories only having 50 images, so fine-tuning the convolutional layers can lead to overfitting. Our new dataset is very similar to the ImageNet dataset, so we can be sure that many of the pretrained weights have the correct characteristics as well. This way we can freeze these trained convolutional layers so that they don't change when we train the rest of the classifier. If you have a smaller dataset that is significantly different from the original, fine-tuning can still lead to overfitting, but later layers will not contain the correct functionality. This way, you can freeze the convolutional layers again, but only use the results of the earlier layers as they contain more general functionality. With a large dataset, you don't have to worry about overfitting, so you can often fine tune the entire network.
Now we can add the rest of the classifier. This takes the output from the pretrained convolutional layers and injects it into a separate classifier that trains on the new dataset.
Code: train the model
Epoch 1/5 176/176 [==============================] - 27s 156ms / step - loss: 1.6601 - acc : 0.6338 - val_loss: 0.3799 - val_acc: 0.8922 Epoch 2/5 176/176 [=============================] - 19s 107ms / step - loss: 0.4637 - acc: 0.8696 - val_loss: 0.2841 - val_acc: 0.9225 Epoch 3/5 176/176 [====================== ========] - 19s 107ms / step - loss: 0.2777 - acc: 0.9211 - val_loss: 0.2714 - val_acc: 0.9225 Epoch 4/5 176/176 [============ ==================] - 19s 107ms / step - loss: 0.2223 - acc: 0.9327 - val_loss: 0.2419 - val_acc: 0.9284 Epoch 5/5 176/176 [== ===========================] - 19s 106ms / step - loss: 0.1784 - acc: 0.9461 - val_loss: 0.2499 - val_acc: 0.9239
Code: to evaluate test case
53/53 [==================== =========] - 5s 95ms / step The model achieved a loss of 0.23 and accuracy of 92.80%.
For class 101 dataset, we achieved 92.8% accuracy in just 5 epochs. For perspective, the original ResNet was trained on a dataset of ~ 1 million images, over 120 epochs.
There are a couple things that can be improved. On the one hand, looking at the mismatch between loss of validation and loss of training in the past era, you can see that the model is starting to retool. One way to solve this problem — add image enlargement. Simple image enlargement can be easily implemented with using the ImageDataGenerator class . You can also play around with adding / removing layers or changing hyper parameters such as dropout or dense layer size.
By executing this code here with Google Colab's free GPU computing resources.
The genesis of this book began in 2012. Hadoop was being explored in mainstream organizations, and we believed that information architecture was about to be transformed. For many years, business intel...
Python: - The Bible - 3 Manuscripts in 1 book:
Learn how data literacy is changing the world and giving you a better understanding of life's biggest problems in this "Important and Comprehensive" Guide to Statistical Thinking (New York). The bi...
Why this Book? Hadoop has been the base for most of the emerging technologies in today’s big data world. It changed the face of distributed processing by using commodity hardware for large data set...