Path: blob/master/CNN/lab-11-2-mnist_deep_cnn.py
618 views
# Lab 11 MNIST and Deep learning CNN1import torch2import torchvision.datasets as dsets3import torchvision.transforms as transforms4import torch.nn.init56device = 'cuda' if torch.cuda.is_available() else 'cpu'78# for reproducibility9torch.manual_seed(777)10if device == 'cuda':11torch.cuda.manual_seed_all(777)1213# parameters14learning_rate = 0.00115training_epochs = 1516batch_size = 1001718# MNIST dataset19mnist_train = dsets.MNIST(root='MNIST_data/',20train=True,21transform=transforms.ToTensor(),22download=True)2324mnist_test = dsets.MNIST(root='MNIST_data/',25train=False,26transform=transforms.ToTensor(),27download=True)2829# dataset loader30data_loader = torch.utils.data.DataLoader(dataset=mnist_train,31batch_size=batch_size,32shuffle=True,33drop_last=True)3435# CNN Model36class CNN(torch.nn.Module):3738def __init__(self):39super(CNN, self).__init__()40self.keep_prob = 0.541# L1 ImgIn shape=(?, 28, 28, 1)42# Conv -> (?, 28, 28, 32)43# Pool -> (?, 14, 14, 32)44self.layer1 = torch.nn.Sequential(45torch.nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),46torch.nn.ReLU(),47torch.nn.MaxPool2d(kernel_size=2, stride=2))48# L2 ImgIn shape=(?, 14, 14, 32)49# Conv ->(?, 14, 14, 64)50# Pool ->(?, 7, 7, 64)51self.layer2 = torch.nn.Sequential(52torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),53torch.nn.ReLU(),54torch.nn.MaxPool2d(kernel_size=2, stride=2))55# L3 ImgIn shape=(?, 7, 7, 64)56# Conv ->(?, 7, 7, 128)57# Pool ->(?, 4, 4, 128)58self.layer3 = torch.nn.Sequential(59torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),60torch.nn.ReLU(),61torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=1))6263# L4 FC 4x4x128 inputs -> 625 outputs64self.fc1 = torch.nn.Linear(4 * 4 * 128, 625, bias=True)65torch.nn.init.xavier_uniform_(self.fc1.weight)66self.layer4 = torch.nn.Sequential(67self.fc1,68torch.nn.ReLU(),69torch.nn.Dropout(p=1 - self.keep_prob))70# L5 Final FC 625 inputs -> 10 outputs71self.fc2 = torch.nn.Linear(625, 10, bias=True)72torch.nn.init.xavier_uniform_(self.fc2.weight)7374def forward(self, x):75out = self.layer1(x)76out = self.layer2(out)77out = self.layer3(out)78out = out.view(out.size(0), -1) # Flatten them for FC79out = self.layer4(out)80out = self.fc2(out)81return out828384# instantiate CNN model85model = CNN().to(device)8687# define cost/loss & optimizer88criterion = torch.nn.CrossEntropyLoss().to(device) # Softmax is internally computed.89optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)9091# train my model92total_batch = len(data_loader)93model.train() # set the model to train mode (dropout=True)94print('Learning started. It takes sometime.')95for epoch in range(training_epochs):96avg_cost = 09798for X, Y in data_loader:99# image is already size of (28x28), no reshape100# label is not one-hot encoded101X = X.to(device)102Y = Y.to(device)103104optimizer.zero_grad()105hypothesis = model(X)106cost = criterion(hypothesis, Y)107cost.backward()108optimizer.step()109110avg_cost += cost / total_batch111112print('[Epoch: {:>4}] cost = {:>.9}'.format(epoch + 1, avg_cost))113114print('Learning Finished!')115116# Test model and check accuracy117with torch.no_grad():118model.eval() # set the model to evaluation mode (dropout=False)119120X_test = mnist_test.test_data.view(len(mnist_test), 1, 28, 28).float().to(device)121Y_test = mnist_test.test_labels.to(device)122123prediction = model(X_test)124correct_prediction = torch.argmax(prediction, 1) == Y_test125accuracy = correct_prediction.float().mean()126print('Accuracy:', accuracy.item())127128