Image Classification with PaddlePaddle-ResNet (Dynamic Layout)

ResNet

ResNet(Residual Network) is the champion of the ImageNet Image Classification, Image Object Location and Image Object Detection Competition in 2015.ResNet proposed a residual learning method to alleviate the difficulty of training the deep network in order to improve the accuracy of the network training.On the basis of the existing design ideas (BN, small convolution kernel, full convolution network), the residual module is introduced.Each residual module contains two paths, one of which is a direct connected path to the input feature, the other which convolutes the feature two or three times to get the residual of the feature, and finally adds the features on the two paths.

The Residual module is shown in Figure 1. On the left is the basic module connection, which consists of a 3x3 convolution with the same number of output channels.On the right is the Bottleneck connection, which is called the bottleneck because the 1x1 convolution above is used to reduce dimensionality (in the illustration example, 256->64), and the 1x1 convolution below is used to increase dimensionality (in the illustration example, 64->256), so the number of input and output channels for the middle 3x3 convolution is small (in the illustration example, 64->64).


Figure 1. Residual module

Figure 2 shows the network connection diagrams of layers 50, 101, and 152 using bottleneck modules.The difference between the three models is that the number of repetitions of the residual module is different in each group (see the upper right corner of the figure).ResNet training converges quickly and successfully trains hundreds or thousands of layers of convolution neural networks.


Figure 2. ResNet model based on ImageNet

ResNet Interpretation Blog https://blog.csdn.net/lanran2/article/details/79057994

In[1]
# Unzip Flower Dataset
!cd data/data2815 && unzip -q flower_photos.zip
In[2]
import codecs
import os
import random
import shutil
from PIL import Image

train_ratio = 4.0 / 5

all_file_dir = 'data/data2815'
class_list = [c for c in os.listdir(all_file_dir) if os.path.isdir(os.path.join(all_file_dir, c)) and not c.endswith('Set') and not c.startswith('.')]
class_list.sort()
print(class_list)
train_image_dir = os.path.join(all_file_dir, "trainImageSet")
if not os.path.exists(train_image_dir):
    os.makedirs(train_image_dir)
    
eval_image_dir = os.path.join(all_file_dir, "evalImageSet")
if not os.path.exists(eval_image_dir):
    os.makedirs(eval_image_dir)

train_file = codecs.open(os.path.join(all_file_dir, "train.txt"), 'w')
eval_file = codecs.open(os.path.join(all_file_dir, "eval.txt"), 'w')

with codecs.open(os.path.join(all_file_dir, "label_list.txt"), "w") as label_list:
    label_id = 0
    for class_dir in class_list:
        label_list.write("{0}\t{1}\n".format(label_id, class_dir))
        image_path_pre = os.path.join(all_file_dir, class_dir)
        for file in os.listdir(image_path_pre):
            try:
                img = Image.open(os.path.join(image_path_pre, file))
                if random.uniform(0, 1) <= train_ratio:
                    shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(train_image_dir, file))
                    train_file.write("{0}\t{1}\n".format(os.path.join(train_image_dir, file), label_id))
                else:
                    shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(eval_image_dir, file))
                    eval_file.write("{0}\t{1}\n".format(os.path.join(eval_image_dir, file), label_id))
            except Exception as e:
                pass
                # There are some files that can't be opened and need to be cleaned a little here
        label_id += 1
            
train_file.close()
eval_file.close()
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
In[4]
#model training
!python train.py
2020-03-06 16:56:59,114-INFO: model saved at epoch 49, best accuracy is 0.9454861111111111
2020-03-06 16:56:59,114 - train.py[line:166] - INFO: model saved at epoch 49, best accuracy is 0.9454861111111111
2020-03-06 16:56:59,115-INFO: Final loss: [0.24703844]
2020-03-06 16:56:59,115 - train.py[line:167] - INFO: Final loss: [0.24703844]
In[5]
#Train 50 rounds, evaluate on validation set
!python eval.py
W0306 16:57:08.802810   231 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 9.0
W0306 16:57:08.806915   231 device_context.cc:245] device: 0, cuDNN Version: 7.3.
0.82777035
In[6]
#model prediction
!python infer.py
W0306 16:59:22.282727   291 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 9.0
W0306 16:59:22.286063   291 device_context.cc:245] device: 0, cuDNN Version: 7.3.
checkpoint loaded
image data/data2815/sunflowers/3840761441_7c648abf4d_n.jpg Infer result is: sunflowers

Click on the link and use AI Studio to get started with the project: https://aistudio.baidu.com/aistudio/projectdetail/204995 

Tags: network Python

Posted on Mon, 13 Apr 2020 20:58:07 -0700 by fr34k2oo4