[PYTHON] Loading and testing Chainer's trained imagenet model

I want to read the imagenet model trained with Chainer and evaluate it with test data. The environment is OS X (10.9.5 CPU), Chainer1.6.0, Python2.7.10. There may be a similar article, but I searched for about 5 seconds and could not find it, so I will write it, please forgive me.

** Model training **

You can't test without first training the model. For the training method, please refer to http://hi-king.hatenablog.com/entry/2015/06/11/021144. However, if you try the code attached to the link above with your own image taken from Caltech101, the size of the image The change itself was done correctly, but the resulting image was distorted as shown below. The top is the image before the change, and the bottom is the image after the change (the same object is duplicated).

image_0001.jpg

image_0001_conc_.jpg

There may be no problem as it is, but I was curious about it, so I preprocessed it a little. If fine adjustment is troublesome, I feel that you can use .resize ((256,256)) of PIL.Image.

** Preparing for the test **

After training, prepare for the test. The following four items are required for the test. It is almost the same as the pretreatment during training.

--Test image with a size of 256 x 256. Place it so that it matches the path written in test.txt. 2. Average image file (use the same one as during training). Place it in the execution directory. 3. test.txt file (prepare in the same format as during training). Place it in the execution directory. 4. Trained imagenet model (should have been saved as model by training). Place it in the execution directory.

** Evaluation of model **

I wrote it by looking at the official Chainer documentation and other examples code. Since I just rewrote chainer / examples / imagenet / train_imagenet.py for testing, I think there is a lot of useless processing, but it worked for the time being in my environment. After making the above preparations, put the code of ↓ (* I skipped the details, so ignore the command line argument test_batchsize. If this is set to anything other than the default value (= 1), it cannot be evaluated correctly) https://github.com/fullflu/ML/blob/master/evaluate_chainer_imagenet.py

python evaluate_chainer_imagenet.py -g -1 test.txt

If you type, an error (how much you missed the prediction on average) will be displayed with the following feeling. The contents of the process are just to read the model with serializers.load_npz (path_to_model, model) after importing the model and write model (x, t) for prediction and evaluation. Convenient.

Load model from model


{"iteration": 19, "loss": 0.7662104054501182, "type": "test", "error": 0.4736842105263158}

Since about half is removed, you can see that you are not learning seriously.

Recommended Posts

Loading and testing Chainer's trained imagenet model
Memorandum of saving and loading model
Model Complexity and Robustness
Debugging and testing reports
Run Chainer's imagenet [reliably]