Master PyTorch picture classification of simple tutorial | attached complete code

Depth study of the game, the picture is very common classification game, but also difficult to achieve particularly high ranking game, because the picture classification we have been very thorough study, some open source network is easy to get a high score. If you still can not master the use of open source network is trained, then slowly go model tuning, it is difficult to achieve better results.

1. Introduction Data

Data Download:
https://download.csdn.net/download/xiaosongshine/11128410
data in real time using a data set of traffic signs, traffic signs, a total of 62 classes. Wherein the training data set has 4572 photos (about seventy each category), the test data set has 2520 photos (about 40 in each category). Data contains two subdirectories respectively train and test:
why do we need a test data set it? The test data set is not used to training, it is used to evaluate and tune the model.
Here Insert Picture Description
train and test each folder there are 62 sub-folders, each folder in the same category file:
Here Insert Picture Description
my room which opens a file, the picture shows the inside out:
Here Insert Picture Description

Wherein each photo similar to the example below 100 100 the size of 3. 100 is a photo of a photo of length and width of 3 What is it? This is actually a picture of the number of color channels, RGB. Color photos are stored in the computer is in a three-dimensional array form. We also fed into the network of these arrays.

2. Network building

1. Import Python package, define parameters

 1import torch as t
 2import torchvision as tv
 3import os
 4import time
 5import numpy as np
 6from tqdm import tqdm
 7
 8
 9class DefaultConfigs(object):
10
11 data_dir = "./traffic-sign/"
12 data_list = ["train","test"]
13
14 lr = 0.001
15 epochs = 10
16 num_classes = 62
17 image_size = 224
18 batch_size = 40
19 channels = 3
20 gpu = "0"
21 train_len = 4572
22 test_len = 2520
23 use_gpu = t.cuda.is_available()
24
25config = DefaultConfigs()

2. Data preparation, using the read mode PyTorch provide
attention to the fact Train data are required to be cut, Test data do not make the cut

 1normalize = tv.transforms.Normalize(mean = [0.485, 0.456, 0.406],
 2 std = [0.229, 0.224, 0.225]
 3 )
 4
 5transform = {
 6 config.data_list[0]:tv.transforms.Compose(
 7 [tv.transforms.Resize([224,224]),tv.transforms.CenterCrop([224,224]),
 8 tv.transforms.ToTensor(),normalize]#tv.transforms.Resize 用于重设图片大小
 9 ) ,
10 config.data_list[1]:tv.transforms.Compose(
11 [tv.transforms.Resize([224,224]),tv.transforms.ToTensor(),normalize]
12 ) 
13}
14
15datasets = {
16 x:tv.datasets.ImageFolder(root = os.path.join(config.data_dir,x),transform=transform[x])
17 for x in config.data_list
18}
19
20dataloader = {
21 x:t.utils.data.DataLoader(dataset= datasets[x],
22 batch_size=config.batch_size,
23 shuffle=True
24 ) 
25 for x in config.data_list
26}

3. Construction of the network model (using resnet18 migration study, training parameters for the last layer fully connected t.nn.Linear (512, num_classes))

 1def get_model(num_classes):
 2
 3 model = tv.models.resnet18(pretrained=True)
 4 for parma in model.parameters():
 5 parma.requires_grad = False
 6 model.fc = t.nn.Sequential(
 7 t.nn.Dropout(p=0.3),
 8 t.nn.Linear(512,num_classes)
 9 )
10 return(model)

If the computer hardware support, you can put the following code is masked, training the entire network, the final accuracy rate will rise, the training data will slow down.

1for parma in model.parameters():
2 parma.requires_grad = False

Model output

1ResNet(
 2 (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
 3 (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
 4 (relu): ReLU(inplace)
 5 (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
 6 (layer1): Sequential(
 7 (0): BasicBlock(
 8 (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
 9 (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
10 (relu): ReLU(inplace)
11 (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
12 (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
13 )
14 (1): BasicBlock(
15 (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
16 (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
17 (relu): ReLU(inplace)
18 (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
19 (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
20 )
21 )
22 (layer2): Sequential(
23 (0): BasicBlock(
24 (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
25 (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
26 (relu): ReLU(inplace)
27 (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
28 (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
29 (downsample): Sequential(
30 (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
31 (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
32 )
33 )
34 (1): BasicBlock(
35 (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
36 (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
37 (relu): ReLU(inplace)
38 (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
39 (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
40 )
41 )
42 (layer3): Sequential(
43 (0): BasicBlock(
44 (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
45 (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
46 (relu): ReLU(inplace)
47 (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
48 (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
49 (downsample): Sequential(
50 (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
51 (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
52 )
53 )
54 (1): BasicBlock(
55 (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
56 (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
57 (relu): ReLU(inplace)
58 (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
59 (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
60 )
61 )
62 (layer4): Sequential(
63 (0): BasicBlock(
64 (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
65 (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
66 (relu): ReLU(inplace)
67 (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
68 (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
69 (downsample): Sequential(
70 (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
71 (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
72 )
73 )
74 (1): BasicBlock(
75 (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
76 (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
77 (relu): ReLU(inplace)
78 (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
79 (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
80 )
81 )
82 (avgpool): AvgPool2d(kernel_size=7, stride=1, padding=0)
83 (fc): Sequential(
84 (0): Dropout(p=0.3)
85 (1): Linear(in_features=512, out_features=62, bias=True)
86 )
87)

4. training model (automatic support GPU acceleration)

 1def train(epochs):
 2
 3 model = get_model(config.num_classes)
 4 print(model)
 5 loss_f = t.nn.CrossEntropyLoss()
 6 if(config.use_gpu):
 7 model = model.cuda()
 8 loss_f = loss_f.cuda()
 9
10 opt = t.optim.Adam(model.fc.parameters(),lr = config.lr)
11 time_start = time.time()
12
13 for epoch in range(epochs):
14 train_loss = []
15 train_acc = []
16 test_loss = []
17 test_acc = []
18 model.train(True)
19 print("Epoch {}/{}".format(epoch+1,epochs))
20 for batch, datas in tqdm(enumerate(iter(dataloader["train"]))):
21 x,y = datas
22 if (config.use_gpu):
23 x,y = x.cuda(),y.cuda()
24 y_ = model(x)
25 #print(x.shape,y.shape,y_.shape)
26 _, pre_y_ = t.max(y_,1)
27 pre_y = y
28 #print(y_.shape)
29 loss = loss_f(y_,pre_y)
30 #print(y_.shape)
31 acc = t.sum(pre_y_ == pre_y)
32
33 loss.backward()
34 opt.step()
35 opt.zero_grad()
36 if(config.use_gpu):
37 loss = loss.cpu()
38 acc = acc.cpu()
39 train_loss.append(loss.data)
40 train_acc.append(acc)
41 #if((batch+1)%5 ==0):
42 time_end = time.time()
43 print("Batch {}, Train loss:{:.4f}, Train acc:{:.4f}, Time: {}"\
44 .format(batch+1,np.mean(train_loss)/config.batch_size,np.mean(train_acc)/config.batch_size,(time_end-time_start)))
45 time_start = time.time()
46
47 model.train(False)
48 for batch, datas in tqdm(enumerate(iter(dataloader["test"]))):
49 x,y = datas
50 if (config.use_gpu):
51 x,y = x.cuda(),y.cuda()
52 y_ = model(x)
53 #print(x.shape,y.shape,y_.shape)
54 _, pre_y_ = t.max(y_,1)
55 pre_y = y
56 #print(y_.shape)
57 loss = loss_f(y_,pre_y)
58 acc = t.sum(pre_y_ == pre_y)
59
60 if(config.use_gpu):
61 loss = loss.cpu()
62 acc = acc.cpu()
63
64 test_loss.append(loss.data)
65 test_acc.append(acc)
66 print("Batch {}, Test loss:{:.4f}, Test acc:{:.4f}".format(batch+1,np.mean(test_loss)/config.batch_size,np.mean(test_acc)/config.batch_size))
67
68 t.save(model,str(epoch+1)+"ttmodel.pkl")
69
70
71
72if __name__ == "__main__":
73 train(config.epochs)

Training results are as follows:

1Epoch 1/10
 2115it [00:48, 2.63it/s]
 3Batch 115, Train loss:0.0590, Train acc:0.4635, Time: 48.985504150390625
 463it [00:24, 2.62it/s]
 5Batch 63, Test loss:0.0374, Test acc:0.6790, Time :24.648272275924683
 6Epoch 2/10
 7115it [00:45, 3.22it/s]
 8Batch 115, Train loss:0.0271, Train acc:0.7576, Time: 45.68823838233948
 963it [00:23, 2.62it/s]
10Batch 63, Test loss:0.0255, Test acc:0.7524, Time :23.271782875061035
11Epoch 3/10
12115it [00:45, 3.19it/s]
13Batch 115, Train loss:0.0181, Train acc:0.8300, Time: 45.92648506164551
1463it [00:23, 2.60it/s]
15Batch 63, Test loss:0.0212, Test acc:0.7861, Time :23.80789279937744
16Epoch 4/10
17115it [00:45, 3.28it/s]
18Batch 115, Train loss:0.0138, Train acc:0.8767, Time: 45.27525019645691
1963it [00:23, 2.57it/s]
20Batch 63, Test loss:0.0173, Test acc:0.8385, Time :23.736321449279785
21Epoch 5/10
22115it [00:44, 3.22it/s]
23Batch 115, Train loss:0.0112, Train acc:0.8950, Time: 44.983638286590576
2463it [00:22, 2.69it/s]
25Batch 63, Test loss:0.0156, Test acc:0.8520, Time :22.790074348449707
26Epoch 6/10
27115it [00:44, 3.19it/s]
28Batch 115, Train loss:0.0095, Train acc:0.9159, Time: 45.10426950454712
2963it [00:22, 2.77it/s]
30Batch 63, Test loss:0.0158, Test acc:0.8214, Time :22.80412459373474
31Epoch 7/10
32115it [00:45, 2.95it/s]
33Batch 115, Train loss:0.0081, Train acc:0.9280, Time: 45.30439043045044
3463it [00:23, 2.66it/s]
35Batch 63, Test loss:0.0139, Test acc:0.8528, Time :23.122379541397095
36Epoch 8/10
37115it [00:44, 3.23it/s]
38Batch 115, Train loss:0.0073, Train acc:0.9300, Time: 44.304762840270996
3963it [00:22, 2.74it/s]
40Batch 63, Test loss:0.0142, Test acc:0.8496, Time :22.801835536956787
41Epoch 9/10
42115it [00:43, 3.19it/s]
43Batch 115, Train loss:0.0068, Train acc:0.9361, Time: 44.08414030075073
4463it [00:23, 2.44it/s]
45Batch 63, Test loss:0.0142, Test acc:0.8437, Time :23.604419231414795
46Epoch 10/10
47115it [00:46, 3.12it/s]
48Batch 115, Train loss:0.0063, Train acc:0.9337, Time: 46.76597046852112
4963it [00:24, 2.65it/s]
50Batch 63, Test loss:0.0130, Test acc:0.8591, Time :24.64351773262024

Training 10 Epoch, test set accuracy can reach 0.86, have reached good results. By modifying the parameters, increase training, you can achieve higher accuracy.

Want more information on artificial intelligence
can be added to V ,, letter: hcgx0904 (Remarks "artificial intelligence")
click on the "Deep Learning & computer vision succinctly"
start learning! ! !

Guess you like

Origin blog.51cto.com/14352303/2414419