[PYTHON] I wanted to challenge the classification of CIFAR-10 using Chainer's trainer

Introduction

The other day here I learned that Chainer can be written very concisely, so I challenged the image classification of CIFAR-10 that I wanted to try from before. I tried I wanted to write ..., but I have only a poor CPU environment, so I have not been able to confirm execution. I moved it all day and advanced about 2 epoch, so it's probably correct ... ^^; Regarding the implementation, I referred to the blog of here.

Implementation

Load the image of CIFAR-10

Download and load the CIFAR-10 data from here. Since it is like pickle, it is read by the following function.

def unpickle(file):
    fp = open(file, 'rb')
    if sys.version_info.major == 2:
        data = pickle.load(fp)
    elif sys.version_info.major == 3:
        data = pickle.load(fp, encoding='latin-1')                                                                                    
    fp.close()

    return data

neural network

I referred to the blog I introduced earlier. I'm still not sure how to design this area ...

class Cifar10Model(chainer.Chain):

    def __init__(self):
        super(Cifar10Model,self).__init__(
                conv1 = F.Convolution2D(3, 32, 3, pad=1),
                conv2 = F.Convolution2D(32, 32, 3, pad=1),
                conv3 = F.Convolution2D(32, 32, 3, pad=1),
                conv4 = F.Convolution2D(32, 32, 3, pad=1),
                conv5 = F.Convolution2D(32, 32, 3, pad=1),
                conv6 = F.Convolution2D(32, 32, 3, pad=1),
                l1 = L.Linear(512, 512),
                l2 = L.Linear(512,10))

    def __call__(self, x, train=True):
        h = F.relu(self.conv1(x))
        h = F.max_pooling_2d(F.relu(self.conv2(h)), 2)
        h = F.relu(self.conv3(h))
        h = F.max_pooling_2d(F.relu(self.conv4(h)), 2)
        h = F.relu(self.conv5(h))
        h = F.max_pooling_2d(F.relu(self.conv6(h)), 2)
        h = F.dropout(F.relu(self.l1(h)), train=train)
        return self.l2(h)

Data read

I got a little clogged here. When using Chainer's new function trainer, I pass the data I want to learn to the iterator, but in tutorials etc.

train_iter = chainer.iterators.SerialIterator(train, 100)
test_iter = chainer.iterators.SerialIterator(test, 100,repeat=False, shuffle=False)

I didn't know how to pass the label etc. After a lot of research, I found that Tuple_dataset should be used.

train = chainer.tuple_dataset.TupleDataset(train_data, train_label)

It seems to be good to do like this.

Below is the entire code for the read part.

x_train = None
y_train = []
for i in range(1,6):
    data_dic = unpickle("cifar-10-batches-py/data_batch_{}".format(i))
    if i == 1:
        x_train = data_dic['data']
    else:
        x_train = np.vstack((x_train, data_dic['data']))
    y_train += data_dic['labels']

test_data_dic = unpickle("cifar-10-batches-py/test_batch")
x_test = test_data_dic['data']
x_test = x_test.reshape(len(x_test),3,32,32)
y_test = np.array(test_data_dic['labels'])
x_train = x_train.reshape((len(x_train),3, 32, 32))
y_train = np.array(y_train)
x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)
x_train /= 255
x_test/=255                                                                                                                     
y_train = y_train.astype(np.int32)
y_test = y_test.astype(np.int32)

train = tuple_dataset.TupleDataset(x_train, y_train)
test = tuple_dataset.TupleDataset(x_test, y_test)

Learning part

I am learning with the neural network defined earlier. The code is just a little tweak to the tutorial MNIST. I am surprised to be able to write it insanely concisely.


model = L.Classifier(Cifar10Model())
optimizer = chainer.optimizers.Adam()
optimizer.setup(model)

train_iter = chainer.iterators.SerialIterator(train, 100)
test_iter = chainer.iterators.SerialIterator(test, 100,repeat=False, shuffle=False)

updater = training.StandardUpdater(train_iter, optimizer, device=-1)
trainer = training.Trainer(updater, (40, 'epoch'), out="logs")
trainer.extend(extensions.Evaluator(test_iter, model, device=-1))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy']))
trainer.extend(extensions.ProgressBar())                                                                                          
trainer.run()

result

When you run it, a progress bar will appear to tell you how much you are learning.

Screenshot from 2016-08-06 18:04:00.png

Estimated time to finish: 6 days

~~ I gave up ~~ (Fixed on 2016.08.15) I did my best

I read the output log into dictionary and graphed it with matplotlib

figure_1.png

figure_2.png

in conclusion

~~ I couldn't confirm that the result was correct, but I learned how to use ~~ trainer. After all GPU is indispensable to study Deep Learning

Recommended Posts

I wanted to challenge the classification of CIFAR-10 using Chainer's trainer
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to get the batting results of Hachinai using image processing
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I wanted to be careful about the behavior of Python's default arguments
I tried to extract and illustrate the stage of the story using COTOHA
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I wanted to play with the Bezier curve
I tried to touch the API of ebay
I tried to correct the keystone of the image
I want to customize the appearance of zabbix
I tried using the image filter of OpenCV
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to predict the deterioration of the lithium ion battery using the Qore SDK
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
[Python] I tried to judge the member image of the idol group using Keras
I made a script to record the active window using win32gui of Python
Continue to challenge Cyma's challenges using the OCR service of Google Cloud Platform
I tried to predict the victory or defeat of the Premier League using the Qore SDK
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I tried to summarize the basic form of GPLVM
Python practice 100 knocks I tried to visualize the decision tree of Chapter 5 using graphviz
I wanted to use the Python library from MATLAB
I want to fully understand the basics of Bokeh
I tried to extract the text in the image file using Tesseract of the OCR engine
I want to take a screenshot of the site on Docker using any font
I tried to approximate the sin function using chainer
I examined the argument class_weight of Chainer's softmax_cross_entropy function.
I tried using the API of the salmon data project
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I just wanted to extract the data of the desired date and time with Django
[Failure] I wanted to generate sentences using Flair's TextRegressor
I want to automate ssh using the expect command!
The story of using circleci to build manylinux wheels
I tried the simplest method of multi-label document classification
I tried to identify the language using CNN + Melspectogram
I tried to complement the knowledge graph using OpenKE
I tried to classify the voices of voice actors
I want to increase the security of ssh connections
I tried to compress the image using machine learning
I tried to summarize the string operations of Python
[Pokemon Sword Shield] I tried to visualize the judgment basis of deep learning using the three family classification as an example.
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I tried to compare the accuracy of machine learning models using kaggle as a theme.
I tried to verify the yin and yang classification of Hololive members by machine learning
I tried to predict the infection of new pneumonia using the SIR model: ☓ Wuhan edition ○ Hubei edition
Implementation of recommendation system ~ I tried to find the similarity from the outline of the movie using TF-IDF ~
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
I dare to fill out the form without using selenium
I tried to find the entropy of the image with python
[Horse Racing] I tried to quantify the strength of racehorses
I tried to get the location information of Odakyu Bus
I wanted to solve the Panasonic Programming Contest 2020 with Python
I tried to find the average of the sequence with TensorFlow
I want to automate ssh using the expect command! part2
I tried refactoring the CNN model of TensorFlow using TF-Slim
I tried to simulate ad optimization using the bandit algorithm.
I tried face recognition of the laughter problem using Keras.