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.
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
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)
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)
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()
When you run it, a progress bar will appear to tell you how much you are learning.
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
~~ 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