[PYTHON] Run Chainer's imagenet [reliably]

Introduction

About this article

I managed to get imagenet to work while looking at some sites, so I wrote it to keep the contents. There are many sites that have run the chainer sample, but it is possible that it will get stuck due to differences in versions and codes, so I will summarize the environment in which it was actually run and the literature (articles) used at that time.

Target

The person who did the mnist sample. Beginner (because I am also a beginner)

Referenced site

Memorandum for starting image classification with chainer (v1.14.0)

Chainer's NIN allows you to deep learn and recognize your own image set

Environment construction using pyenv (Mac OS)

CPickle.UnpicklingError in Chainer

procedure

Environment

All the content this time was done on MacOS. I wanted to do it with python3, but it seems that the above site used 2.x, so I used Anaconda 2.1.0 (python2.7.13). I installed anaconda 2.1.0 with pyenv and built the environment. The version of chainer used was chainer == 1.20.0.1.

First of all

Borrow tools from git published in the first article.

git clone https://github.com/shi3z/chainer_imagenet_tools.git

Rewrite the code as in the second article. It's easier to do these two things first.

Image preparation, teacher data and test data

Images can use any dataset you like. Anything should be usable as long as it is [directory name / classification class name / actual file]. The first thing to do is to create classification teacher data & test data from the dataset. Git clone the program provided by the second article and use it.

wget http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz


To prepare the dataset. Using the code provided by the second article

python make_train_data.py 101_ObjectCategories

By doing so, the images directory that collects the image data and the three files train.txt, test.txt, and label.txt are output. Looking at the contents, train and test describe the path to the image and the number of the classification class to which it belongs.

Image resizing

Since the images are disjointed, it is necessary to reduce it to the input dimension of this neural network. For this purpose, crop.py is prepared in the article. The generated images are saved in crop images. Here, the path to the image is described in train.txt and test.txt mentioned above, but since the image used this time is cropimages, replace it with images-> cropimages using an editor or the like.

Learning phase

Here, use compute_mean.py that exists in [examples / imagenet /] of chainer to generate a mean file.

python compute_mean.py train.txt

Generate a mean file with. Also, rewrite the program by referring to the latter article. I'm rewriting it here, but roughly speaking, I change the use of ice ax to save the model to serializer. You should study this area by looking at the chainer documentation. The actual neural network class is described in nin.py. By customizing this, you can learn using the neural network you have defined.

Even if you reach this point, some people will get an error related to cPickel as it is. Therefore, refer to cPickle.UnpicklingError in Chainer and read the mean file around lines 68-70.

mean_image = np.load(args.mean) #Read mean file

Rewrite to. You should finally be able to do it.

python train_imagenet.py -E 10 train.txt test.txt 2>&1 | tee log

This execution outputs a model file. (Dump of neural network after learning)

Perform image classification

In the article posted at the beginning, the output model is used to experiment with data other than test data to see if classification works. The procedure is to resize and classify the images to be tested. As for the image, the image of the motorcycle in the article is used as it is

wget http://www.trago.co.uk/ekmps/shops/tragoshop/images/wk-650i-motorbike-35kw-restricted-version-for-a2-license-available-white-blue-35677-p.jpg

mv wk-650i-motorbike-35kw-restricted-version-for-a2-license-available-white-blue-35677-p.jpg bike.jpg 

mkdir resized

All you have to do is run

python resize.py bike.jpg
python inspection.py resized/bike.jpg

You should see the result on the screen.

Impressions

There are many materials on the market, so you can easily enjoy machine learning by moving the samples. I'm sorry I wanted to run it on python3 instead of python2.x. In future articles, I would like to cut out faces from images and classify people using them.

The sample code of chainer seems to have many files, but only train_imagenet.py and nin.py were used for learning, and since it is the code to create and test teacher data, if you read mainly these codes I think it's easy to understand.

Recommended Posts

Run Chainer's imagenet [reliably]
Loading and testing Chainer's trained imagenet model