莫烦pytorch学习笔记,八——卷积神经网络

莫烦视频网址

这个代码实现了预测和可视化

  1 import os
  2 
  3 # third-party library
  4 import torch
  5 import torch.nn as nn
  6 import torch.utils.data as Data
  7 import torchvision
  8 import matplotlib.pyplot as plt
  9 
 10 # torch.manual_seed(1)    # reproducible
 11 
 12 # Hyper Parameters
 13 EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
 14 BATCH_SIZE = 50
 15 LR = 0.001              # learning rate
 16 DOWNLOAD_MNIST = False
 17 
 18 
 19 # Mnist digits dataset
 20 if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
 21     # not mnist dir or mnist is empyt dir
 22     DOWNLOAD_MNIST = True
 23 
 24 train_data = torchvision.datasets.MNIST(
 25     root='./mnist/',
 26     train=True,                                     # this is training data
 27     transform=torchvision.transforms.ToTensor(),    # 把数据压缩到0到1之间的numpy数据
 28                                                     # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
 29     download=DOWNLOAD_MNIST,
 30 )
 31 
 32 # plot one example
 33 print(train_data.train_data.size())                 # (60000, 28, 28)
 34 print(train_data.train_labels.size())               # (60000)
 35 plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
 36 plt.title('%i' % train_data.train_labels[0])
 37 plt.show()
 38 
 39 # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
 40 train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
 41 
 42 # pick 2000 samples to speed up testing
 43 test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
 44 test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
 45 test_y = test_data.test_labels[:2000]
 46 
 47 
 48 class CNN(nn.Module):
 49     def __init__(self):
 50         super(CNN, self).__init__()
 51         self.conv1 = nn.Sequential(         # input shape (1, 28, 28)
 52             nn.Conv2d(
 53                 in_channels=1,              # input height
 54                 out_channels=16,            # n_filters
 55                 kernel_size=5,              # filter size
 56                 stride=1,                   # filter movement/step
 57                 padding=2,                  # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1
 58             ),                              # output shape (16, 28, 28)
 59             nn.ReLU(),                      # activation
 60             nn.MaxPool2d(kernel_size=2),    # choose max value in 2x2 area, output shape (16, 14, 14)
 61         )
 62         self.conv2 = nn.Sequential(         # input shape (16, 14, 14)
 63             nn.Conv2d(16, 32, 5, 1, 2),     # output shape (32, 14, 14)
 64             nn.ReLU(),                      # activation
 65             nn.MaxPool2d(2),                # output shape (32, 7, 7)
 66         )
 67         self.out = nn.Linear(32 * 7 * 7, 10)   # fully connected layer, output 10 classes
 68 
 69     def forward(self, x):
 70         x = self.conv1(x)
 71         x = self.conv2(x)
 72         x = x.view(x.size(0), -1)           # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
 73         output = self.out(x)
 74         return output, x    # return x for visualization
 75 
 76 
 77 cnn = CNN()
 78 print(cnn)  # net architecture
 79 
 80 optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters
 81 loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
 82 
 83 # following function (plot_with_labels) is for visualization, can be ignored if not interested
 84 from matplotlib import cm
 85 try: from sklearn.manifold import TSNE; HAS_SK = True
 86 except: HAS_SK = False; print('Please install sklearn for layer visualization')
 87 def plot_with_labels(lowDWeights, labels):
 88     plt.cla()
 89     X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
 90     for x, y, s in zip(X, Y, labels):
 91         c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
 92     plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
 93 
 94 plt.ion()
 95 # training and testing
 96 for epoch in range(EPOCH):
 97     for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
 98 
 99         output = cnn(b_x)[0]               # cnn output
100         loss = loss_func(output, b_y)   # cross entropy loss
101         optimizer.zero_grad()           # clear gradients for this training step
102         loss.backward()                 # backpropagation, compute gradients
103         optimizer.step()                # apply gradients
104 
105         if step % 50 == 0:
106             test_output, last_layer = cnn(test_x)
107             pred_y = torch.max(test_output, 1)[1].data.numpy()
108             accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
109             print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
110             if HAS_SK:
111                 # Visualization of trained flatten layer (T-SNE)
112                 tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
113                 plot_only = 500
114                 low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
115                 labels = test_y.numpy()[:plot_only]
116                 plot_with_labels(low_dim_embs, labels)
117 plt.ioff()
118 
119 # print 10 predictions from test data
120 test_output, _ = cnn(test_x[:10])
121 pred_y = torch.max(test_output, 1)[1].data.numpy()
122 print(pred_y, 'prediction number')
123 print(test_y[:10].numpy(), 'real number')

去掉可视化进行代码简化

 1 import os
 2 # third-party library
 3 import torch
 4 import torch.nn as nn
 5 import torch.utils.data as Data
 6 import torchvision
 7 import matplotlib.pyplot as plt
 8 
 9 EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
10 BATCH_SIZE = 50
11 LR = 0.001              # learning rate
12 
13 train_data = torchvision.datasets.MNIST(
14     root='./mnist/',                                #下载后的存放目录
15     train=True,                                     # this is training data
16     transform=torchvision.transforms.ToTensor(),    # 把数据压缩到0到1之间的numpy数据,如果原始数据是rgb数据(0-255)则变为黑白数据,并使numpy数据变为tensor数据                                                 # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
17     download=True#不存在该数据就设置为True进行下载,存在则改为False
18 )
19 
20 # plot one example
21 print(train_data.train_data.size())                 # (60000, 28, 28),六万图片
22 print(train_data.train_labels.size())               # (60000),六万标签
23 plt.imshow(train_data.train_data[0].numpy(), cmap='gray')#展现第一个训练数据图片
24 plt.title('%i' % train_data.train_labels[0])
25 plt.show()
26 
27 # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
28 train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
29 
30 # pick 2000 samples to speed up testing
31 test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
32 test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
33 test_y = test_data.test_labels[:2000]
34 
35 class CNN(nn.Module):
36     def __init__(self):
37         super(CNN, self).__init__()
38         self.conv1 = nn.Sequential(         # input shape (1, 28, 28),考虑batch是(batch,1,28,28)
39             nn.Conv2d(
40                 in_channels=1,              # input height
41                 out_channels=16,            # n_filters
42                 kernel_size=5,              # filter size
43                 stride=1,                   # filter movement/step
44                 padding=2,                  # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1
45             ),                              # output shape (16, 28, 28)
46             nn.ReLU(),                      # activation
47             nn.MaxPool2d(kernel_size=2),    # choose max value in 2x2 area, output shape (16, 14, 14)
48         )
49         self.conv2 = nn.Sequential(         # input shape (16, 14, 14)
50             nn.Conv2d(16, 32, 5, 1, 2),     # output shape (32, 14, 14)
51             nn.ReLU(),                      # activation
52             nn.MaxPool2d(2),                # output shape (32, 7, 7)
53         )
54         self.out = nn.Linear(32 * 7 * 7, 10)   # fully connected layer, output 10 classes
55 
56     def forward(self, x):
57         x = self.conv1(x)
58         x = self.conv2(x)
59         x = x.view(x.size(0), -1)           # flatten the output of conv2 to (batch_size, 32 * 7 * 7),只有tensor对象才可以使用x.size(0)
60         output = self.out(x)
61         return output, x    # return x for visualization
62 
63 
64 cnn = CNN()
65 #print(cnn)  # net architecture
66 
67 optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters
68 loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
69 
70 # training and testing
71 for epoch in range(EPOCH):
72     for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
73 
74         output = cnn(b_x)[0]            # cnn output
75         loss = loss_func(output, b_y)   # cross entropy loss
76         optimizer.zero_grad()           # clear gradients for this training step
77         loss.backward()                 # backpropagation, compute gradients
78         optimizer.step()                # apply gradients
79 
80         if step % 50 == 0:
81             test_output = cnn(test_x)[0]
82             print("----------------")
83             #print(test_output.shape)   #2000*10
84             #print(torch.max(test_output, 1))     #返回的每一行中最大值和其下标
85             pred_y = torch.max(test_output, 1)[1].data.numpy() #返回的是每个样本对应0-9数字可能性最大的概率对应的下标
86             accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
87             print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
88 
89 # print 10 predictions from test data
90 test_output, _ = cnn(test_x[:10])
91 pred_y = torch.max(test_output, 1)[1].data.numpy()
92 print(pred_y, 'prediction number')
93 print(test_y[:10].numpy(), 'real number')