PyTorchã§CIFAR10ãæ¢åã®CIFAR10ã®Dataset Classã使ããã«åé¡ãã
çããããã«ã¡ã¯ã
ãå
æ°ã§ãããé¨å¤©ãå¢ãã¦ãã¦ãåºãããã®ãåå«ã«ãªã£ã¦ãã¾ãã
PyTorchã使ã£ã¦ç»åèªèãã¼ã¿ã»ããCIFAR10ãåé¡ãã¾ããã
Kaggleã§PyTorchã¦ã¼ã¶ãå¢ãã¦ãããã¨ããããåå¼·ãã¾ããã
æè¿ããã®æã®ãã¥ã¼ããªã¢ã«ãExamplesã«è¯ããªã«ã§ãã
ãã¼ã¿å¦çå°ç¨ã¯ã©ã¹ãäºãä½ã£ã¦ãããã¨ãããã¾ãã
ãã®ç¶æ
ã¯æ°ãããã¼ã¿ã»ããã試ãããå ´åã«ä¸ä¾¿ãªã®ã§ã
ãã®ãããªå
容ãå«ã¾ããªãCIFAR10ã®ã³ã¼ããè¨è¿°ãã¾ããã
PyTorch
PyTorchã¨ã¯
Deep Learningã®ãã¬ã¼ã ã¯ã¼ã¯ã§ãã
æè»ãªãããã¯ã¼ã¯æ§ç¯ãGPUãå©ç¨ããé«éãªè¡åæ¼ç®ãå¾æã¨ãã¦ãã¾ãã
Chainerãforkãã¦ä½ãããã®ã§ãå®è£
æ¹æ³ãé常ã«ä¼¼ã¦ãã¾ãã
åã§ã¯ãããã¾ãããChainerããé«éã ã£ããããã§ãªãã£ããã
ã¤ã³ã¹ãã¼ã«
ä¸è¨ã®å
¬å¼ãµã¤ããåèã«ãã¦ãã ããã
OSãPythonã®versionãCUDAã®æç¡ããã¼ã¸ã§ã³ã«ãã£ã¦ã³ãã³ãå¤åãã¾ãã
å
¬å¼ã«ããã°ãããã®é¸æãã§ãããã®ç°å¢ã«å¿ããã³ãã³ããæ示ãã¾ãã
ãã®ã³ãã³ããå
¥åããã°ãç°å¢ã®æ§ç¯ãå®äºãã¾ãã
ä¾ãã°ãOSX, Python 2.7, pip, CUDAãªãã®å ´åã次ã®ã³ãã³ããå®è¡ãã¾ãã
pip install http://download.pytorch.org/whl/torch-0.1.12.post2-cp27-none-macosx_10_7_x86_64.whl pip install torchvision
CIFAR10ãåé¡ããã³ã¼ããä½ã£ã¦ã¿ãã
ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã§è¨å®ããé
ç®ã¯
ä»ã®ãã¬ã¼ã ã¯ã¼ã¯ï¼TensorFlow, Chianerãªã©ï¼ã¨åãã§ãã
Chainerã¨æ¯è¼çä¼¼ã¦ããå®è£
ã«ãªã£ã¦ãã¾ãã
ã½ã¼ã¹ã³ã¼ãå
¨ä½ã¯æ¬¡ã®gistã«è¨è¼ãã¦ãã¾ãã
Import
ã¯ããã«importæã§ãã
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.utils.data.dataset import Dataset from torch.utils.data import DataLoader from torchvision import transforms import cPickle import numpy as np import os
CIFAR10ã®èªã¿è¾¼ã¿
CIFAR10ãèªã¿è¾¼ãã³ã¼ãã§ãã
åããã£ã¬ã¯ããªã«CIFAR10ã®Python versionããã¦ã³ãã¼ãããå±éããã°å®äºã§ãã
ã½ã¼ã¹ã³ã¼ãã¯æ¬¡ãåèã«ããã¦ããã ãã¾ããã
ä»åç¨ã«å°ã
ä¿®æ£ãå
¥ãã¦ãã¾ãã
def unpickle(file): fo = open(file, 'rb') dict = cPickle.load(fo) fo.close() return dict def conv_data2image(data): return np.rollaxis(data.reshape((3, 32, 32)), 0, 3) def load_cirar10(folder): """ load cifar10 :return: train_X, train_y, test_X, test_y """ for i in range(1, 6): fname = os.path.join(folder, "%s%d" % ("data_batch_", i)) data_dict = unpickle(fname) if i == 1: train_X = data_dict['data'] train_y = data_dict['labels'] else: train_X = np.vstack((train_X, data_dict['data'])) train_y = np.hstack((train_y, data_dict['labels'])) data_dict = unpickle(os.path.join(folder, 'test_batch')) test_X = data_dict['data'] test_y = np.array(data_dict['labels']) train_X = [conv_data2image(x) for x in train_X] test_X = [conv_data2image(x) for x in test_X] return train_X, train_y, test_X, test_y
å ¥åãã¼ã¿ã®æ±ã
DataLoaderã使ã£ãæ¹æ³ã¯Datasetã¯ã©ã¹ãç¶æ¿ããå¿
è¦ãªã¡ã½ããã
ä½æããã¯ã©ã¹ã«ç¶æ¿ãããå¿
è¦ãããã¾ãã
å¿
è¦ãªã¡ã½ããã¯__getitem__ã¨__len__ã§ãã
__getitem__ã«indexãä¸ããããæã®ãã¼ã¿ã®å¦çã
__len__ã«ã¯ãã¼ã¿ã®æ°ãè¨ç®ããå¦çãè¨è¼ãã¾ãã
class DataSet(Dataset): def __init__(self, x, y, transform=None): self.x = x self.y = y self.transform = transform def __getitem__(self, index): x = self.x[index] y = self.y[index] if self.transform is not None: x = self.transform(x) return x, y def __len__(self): return len(self.x)
ããã§ã¯ãDataLoaderãä½ãã¾ãã第ä¸å¼æ°ã«å
ç¨ä½æããDatasetã
第äºå¼æ°ã«batch_size, 第ä¸å¼æ°ã«ãã¼ã¿ã®é çªãshuffleãããã©ãããä¸ãã¾ãã
DataLoaderã®å©ç¨æ¹æ³ã¯å¾è¿°ãã¾ãã
PyTorchã«ã¯ãtransformsããã±ã¼ã¸ãããã
ãã®transformsãå©ç¨ããã¨0-1ã¸ã®ã¹ã±ã¼ãªã³ã°ã
æ£è¦åãã©ã³ãã ã§åãæããè¡ããªã©ã®å¦çãè¨è¼ã§ãã¾ãã
train_d_loader = DataLoader( DataSet( x=train_X, y=train_y, transform=transforms.Compose( [ transforms.ToTensor() ] ) ), batch_size=64, shuffle=True ) test_d_loader = DataLoader( DataSet( x=test_X, y=test_y, transform=transforms.Compose( [ transforms.ToTensor() ] ) ), batch_size=64, shuffle=False )
Neural Networkã®ä½ãæ¹
Convolutional Neural Networkãä½ãã¾ãããä¸è¨ã®ã³ã¼ããåèã«ãã¦ãã ããã
Chainerããã¼ã¹ã«ãã¦ããã®ã§ãChainerã«é¡ä¼¼ãã¦ããå®è£
ã«ãªãã¾ãã
Chainerã§ã¯ã__call__ã«å®£è¨ãã¦ãã¾ããããPyTorchã¯forwardã«ãªãã¾ãã
class ConvolutionalNeuralNetwork(nn.Module): def __init__(self): super(ConvolutionalNeuralNetwork, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(64, 64, kernel_size=3, padding=1) self.conv4 = nn.Conv2d(64, 64, kernel_size=3, padding=1) self.conv5 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.conv6 = nn.Conv2d(128, 128, kernel_size=3, padding=1) self.fc1 = nn.Linear(512 * 4, 256) self.fc2 = nn.Linear(256, 10) def forward(self, x): h = F.relu(self.conv1(x)) h = F.relu(self.conv2(h)) h = F.max_pool2d(h, 2) h = F.relu(self.conv3(h)) h = F.relu(self.conv4(h)) h = F.max_pool2d(h, 2) h = F.relu(self.conv5(h)) h = F.relu(self.conv6(h)) h = F.max_pool2d(h, 2) h = h.view(-1, 512 * 4) h = F.relu(self.fc1(h)) h = F.dropout(h, training=self.training) h = F.log_softmax(self.fc2(h)) return h
ãã®ä»å¦ç¿ã®ããã®Optimizerã®æºå
Optimizerã®å®ç¾©ãå¿ è¦ã§ããç¹çãã¹ãã¨ããã¯ããã¾ããã
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum)
å¦ç¿
Chainerã«é常ã«ä¼¼ã¦ããã³ã¼ãã§ãã
Variableãç¨ãã¦ããã®ããChainerãã£ããã§ãã
å°ã
åã§å®£è¨ããDataLoaderã使ã£ã¦ãã¾ããforã§ä½¿ããã¨ã§ã
æå®ãããããã®æ°ãã¤ããã¼ã¿ãåãåºãã¾ãã
å¦ç¿ã³ã¼ã
for epoch in range(epochs): model.train() train_loss = 0.0 for index, (batch_train_x, batch_train_y) in enumerate(train_d_loader): train_variable = torch.autograd.Variable(batch_train_x) output = model(train_variable) loss = F.nll_loss(output, torch.autograd.Variable(batch_train_y)) optimizer.zero_grad() loss.backward() optimizer.step() train_loss += loss.data[0] print ("training epoch: {} loss: {}".format(epoch, train_loss / len(train_d_loader)))
è©ä¾¡ã³ã¼ã
å¦ç¿é¨åã¨ããã¾ã§å¤ãããªãè©ä¾¡ã³ã¼ãã§ãã
Variableã使ãã誤差ã¨æ£ççãè¨ç®ããã³ã¼ãã§ãã
ã¢ãã«ã®å¦ç¿ç¨ã¨è©ä¾¡ç¨ã®æåã¯.train(), .eval()ã§åãæ¿ãããã¾ãã
model.eval() test_loss = 0.0 correct = 0 for index, (batch_test_x, batch_test_y) in enumerate(test_d_loader): output = model(torch.autograd.Variable(batch_test_x)) test_loss += F.nll_loss(output, torch.autograd.Variable(batch_test_y)).data[0] pred = output.data.max(1)[1] correct += pred.eq(batch_test_y).cpu().sum() print ("testing epoch: {} loss: {} accuracy: {}".format(epoch, test_loss / len(test_d_loader), float(correct) / float(len(test_d_loader.dataset))))
æ¨æºåºå
éä¸ã¾ã§åºåãã¾ãããå¾ã¯çç¥ãã¦ããã¾ãã
training epoch: 0 loss: 2.19472245665 testing epoch: 0 loss: 1.85379125782 accuracy: 0.3254 training epoch: 1 loss: 1.68773823439 testing epoch: 1 loss: 1.42734462385 accuracy: 0.4772 training epoch: 2 loss: 1.36909840554 testing epoch: 2 loss: 1.21328892304 accuracy: 0.5669 training epoch: 3 loss: 1.1273400925 testing epoch: 3 loss: 0.970653118036 accuracy: 0.6634 training epoch: 4 loss: 0.941422916153 testing epoch: 4 loss: 0.885185660931 accuracy: 0.6911 training epoch: 5 loss: 0.811293978132 testing epoch: 5 loss: 0.770014607583 accuracy: 0.7351 training epoch: 6 loss: 0.713843686805 testing epoch: 6 loss: 0.762587684126 accuracy: 0.7432 training epoch: 7 loss: 0.640705172528 testing epoch: 7 loss: 0.716414518535 accuracy: 0.7554 training epoch: 8 loss: 0.59412489799 testing epoch: 8 loss: 0.745007351374 accuracy: 0.7571 training epoch: 9 loss: 0.559192490898 testing epoch: 9 loss: 0.786619110325 accuracy: 0.7396 training epoch: 10 loss: 0.522159374087 testing epoch: 10 loss: 0.766681446102 accuracy: 0.7504 training epoch: 11 loss: 0.500029139583 testing epoch: 11 loss: 0.739471812218 accuracy: 0.7623 training epoch: 12 loss: 0.489205267016 testing epoch: 12 loss: 0.755763524399 accuracy: 0.7664 training epoch: 13 loss: 0.479558758423 testing epoch: 13 loss: 0.801054440939 accuracy: 0.7527 training epoch: 14 loss: 0.4715372192 testing epoch: 14 loss: 0.862275337164 accuracy: 0.731 training epoch: 15 loss: 0.476616473731 testing epoch: 15 loss: 0.895243975301 accuracy: 0.7489 training epoch: 16 loss: 0.489420878252 testing epoch: 16 loss: 0.818035978717 accuracy: 0.7625 training epoch: 17 loss: 0.476847741119 testing epoch: 17 loss: 0.85643605626 accuracy: 0.7505 training epoch: 18 loss: 0.490220740435 testing epoch: 18 loss: 0.871974576729 accuracy: 0.7412 training epoch: 19 loss: 0.482988453422 testing epoch: 19 loss: 0.795684698529 accuracy: 0.7606 training epoch: 20 loss: 0.500881570677 testing epoch: 20 loss: 0.783078642747 accuracy: 0.7542 training epoch: 21 loss: 0.522443722843 testing epoch: 21 loss: 0.903285436023 accuracy: 0.7377 training epoch: 22 loss: 0.532127012151 testing epoch: 22 loss: 0.875951717123 accuracy: 0.739 training epoch: 23 loss: 0.544996414693 testing epoch: 23 loss: 0.914666230258 accuracy: 0.7358 training epoch: 24 loss: 0.555458197228 testing epoch: 24 loss: 0.860163988682 accuracy: 0.7438 training epoch: 25 loss: 0.577152646749 testing epoch: 25 loss: 0.969825881072 accuracy: 0.7164 training epoch: 26 loss: 0.574859146698 testing epoch: 26 loss: 0.866168110325 accuracy: 0.7408 training epoch: 27 loss: 0.574894333672 testing epoch: 27 loss: 0.920369815046 accuracy: 0.7318 training epoch: 28 loss: 0.603822341083 testing epoch: 28 loss: 0.969683260583 accuracy: 0.7406 training epoch: 29 loss: 0.630050289644 testing epoch: 29 loss: 0.859408840299 accuracy: 0.7507 training epoch: 30 loss: 0.642022043729 testing epoch: 30 loss: 0.890207239328 accuracy: 0.7272
çµããã«
Chainerã¨é¡ä¼¼ãã¦ããã®ã§é常ã«å¦ã³ãããã
æ£ç´å¦ç¿ã³ã¹ãã¯æ®ã©ãªãã£ãããªã¨æãã¾ãã
ä»å¾ãã©ã£ã¡ã使ããã¯ã±ã¼ã¹ãã¤ã±ã¼ã¹ã§ãããæã 使ã£ã¦ãããã¨æãã¾ãã