CPickle.UnpicklingError in Chainer

I referred to some sites to use Chainer.

-Image classification with chainer, a deep learning framework from PFN (Neural network 1 with chainer)

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

However, when I ran train_imagenet.py to learn my own image, the following error occurred.

error


cPickle.UnpicklingError: invalid load key, 

The relevant part is the non-Pickle processing by the function called pickle.load in the code below.

train_imagenet.py


# Prepare dataset
train_list = load_image_list(args.train, args.root)
val_list = load_image_list(args.val, args.root)
mean_image = pickle.load(open(args.mean, 'rb'))

The value of the argument args.mean is a file called mean.npy, so if you look for the source of this file ...

compute_mean.py


#!/usr/bin/env python
import argparse
import os
import sys

import numpy
from PIL import Image
import six.moves.cPickle as pickle


parser = argparse.ArgumentParser(description='Compute images mean array')
parser.add_argument('dataset', help='Path to training image-label list file')
parser.add_argument('--root', '-r', default='.',
                    help='Root directory path of image files')
parser.add_argument('--output', '-o', default='mean.npy',
                    help='path to output mean array')
args = parser.parse_args()

sum_image = None
count = 0
for line in open(args.dataset):
    filepath = os.path.join(args.root, line.strip().split()[0])
    image = numpy.asarray(Image.open(filepath)).transpose(2, 0, 1)
    if sum_image is None:
        sum_image = numpy.ndarray(image.shape, dtype=numpy.float32)
        sum_image[:] = image
    else:
        sum_image += image
    count += 1
    sys.stderr.write('\r{}'.format(count))
    sys.stderr.flush()

sys.stderr.write('\n')

mean = sum_image / count
pickle.dump(mean, open(args.output, 'wb'), -1)

It seems that the object created by the numpy.ndarray function is output to a file called mean.npy by the pickle.dump function. In other words, the entity of mean.npy is like a byte stream of a NumPy array.

So, instead of reading mean.npy as non-Pickle in train_imagenet.py, I modified it to read it as a NumPy array.

train_imagenet.py


# Prepare dataset
train_list = load_image_list(args.train, args.root)
val_list = load_image_list(args.val, args.root)
# mean_image = pickle.load(open(args.mean, 'rb'))← cPickle when read as non-Pickle.UnpicklingError
mean_image = np.load(args.mean) #Read as a NumPy array

Then I managed to read it.

Recommended Posts

CPickle.UnpicklingError in Chainer
DQN's background + Deep Q-Network was written in Chainer
How to create data to put in CNN (Chainer)