fish-Monger commited on
Commit
d986e4c
·
verified ·
1 Parent(s): 6a35cb2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +145 -0
README.md CHANGED
@@ -1,3 +1,148 @@
1
  ---
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
  ---
4
+ Libraries needed:
5
+ ```
6
+ import torch
7
+ import torchvision
8
+ import torchvision.transforms as transforms
9
+ from tqdm import tqdm
10
+ from torch import nn
11
+ import matplotlib.pyplot as plt
12
+ ```
13
+
14
+
15
+
16
+
17
+ to define a data loader
18
+ ```
19
+ transformRes = transforms.Compose([
20
+ transforms.ToTensor(),
21
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
22
+ # transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
23
+ ])
24
+
25
+ trainsetRes = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transformRes)
26
+ trainloaderRes64 = torch.utils.data.DataLoader(trainsetRes, batch_size=64, shuffle=True, num_workers=10)
27
+
28
+ testsetRes = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transformRes)
29
+ testloaderRes64 = torch.utils.data.DataLoader(testsetRes, batch_size=64, shuffle=False, num_workers=10)
30
+ ```
31
+
32
+
33
+ The model itself and training
34
+ ```
35
+ import torchvision.models as models
36
+
37
+ # Load the pretrained model from pytorch
38
+ resnet50v2 = models.resnet50(pretrained=True)
39
+
40
+ # Freeze the parameters of the model
41
+ for param in resnet50v2.parameters():
42
+ param.requires_grad = True
43
+
44
+
45
+ # Change the final layer to match the number of classes in the CIFAR-10 dataset
46
+ num_ftrs = resnet50v2.fc.in_features
47
+ resnet50v2.fc = nn.Sequential(
48
+ nn.Linear(num_ftrs, 500),
49
+ nn.ReLU(),
50
+ nn.Linear(500, 200),
51
+ nn.Dropout(0.5),
52
+ nn.Linear(200,40),
53
+ nn.ReLU(),
54
+ nn.Dropout(0.3),
55
+ nn.Linear(40,10),
56
+ nn.ReLU()
57
+
58
+ )
59
+ print("Model Info:")
60
+ print("ResNet50,Pretrained,weight adj. LR=0.01,Mom=0.3,WD=0.0001")
61
+ print("Schedule step=1,gamma=0.7, 20 epoches")
62
+
63
+ # Move the model to the GPU
64
+ resnet50v2 = resnet50v2.to(device, dtype=torch.float32)
65
+
66
+
67
+ optimizer = torch.optim.SGD(resnet50v2.parameters(), lr=0.01,momentum=0.3,weight_decay=0.0001)
68
+ criterion = nn.CrossEntropyLoss()
69
+ scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
70
+ train_losses = []
71
+ test_losses = []
72
+ accuracies = []
73
+ train_acc = []
74
+
75
+ for epoch in range(20): # loop over the dataset multiple times
76
+ running_loss = 0.0
77
+ correctTrain = 0
78
+ totalTrain = 0
79
+ pbar = tqdm(enumerate(trainloaderRes16, 0), total=len(trainloaderRes16), desc="Epoch {}".format(epoch+1))
80
+ for i, data in pbar:
81
+ # get the inputs; data is a list of [inputs, labels]
82
+ inputs, labels = data[0].to(device,dtype=torch.float32), data[1].to(device)
83
+
84
+ # zero the parameter gradients
85
+ optimizer.zero_grad()
86
+
87
+ # forward + backward + optimize
88
+ outputs = resnet50v2(inputs)
89
+ loss = criterion(outputs, labels)
90
+ loss.backward()
91
+ optimizer.step()
92
+
93
+ # print statistics
94
+ running_loss += loss.item()
95
+ _, predicted_train = torch.max(outputs.data, 1)
96
+ totalTrain += labels.size(0)
97
+ correctTrain += (predicted_train == labels).sum().item()
98
+ pbar.set_postfix({'loss': running_loss/(i+1)})
99
+ train_accuracy = 100 * correctTrain / totalTrain
100
+ train_acc.append(train_accuracy)
101
+ print(f'Epoch {epoch + 1} loss: {running_loss / len(trainloaderRes16):.3f}')
102
+
103
+ # Start of testing phase
104
+ resnet50v2.eval() # Set the model to evaluation mode
105
+ test_loss = 0.0
106
+ correct = 0
107
+ total = 0
108
+ with torch.no_grad():
109
+ for data in testloaderRes16:
110
+ images, labels = data[0].to(device,dtype=torch.float32), data[1].to(device)
111
+ outputs = resnet50v2(images)
112
+ loss = criterion(outputs, labels)
113
+ test_loss += loss.item()
114
+ _, predicted = torch.max(outputs.data, 1)
115
+ total += labels.size(0)
116
+ correct += (predicted == labels).sum().item()
117
+
118
+ print(f'Epoch {epoch + 1} Test loss: {test_loss / len(testloaderRes16):.3f}, Accuracy: {100 * correct / total:.2f}%')
119
+ #print the learning rate
120
+ print(f'Epoch {epoch + 1} Learning rate: {optimizer.param_groups[0]["lr"]}')
121
+ train_losses.append(running_loss / len(trainloaderRes16))
122
+ test_losses.append(test_loss / len(testloaderRes16))
123
+ accuracies.append(100 * correct / total)
124
+
125
+ resnet50v2.train() # Set the model back to training model
126
+ scheduler.step()
127
+
128
+
129
+
130
+ print('Finished Training')
131
+
132
+ plt.figure(figsize=(10, 5))
133
+ plt.plot(train_losses, label='Training Loss')
134
+ plt.plot(test_losses, label='Test Loss')
135
+ plt.xlabel('Epochs')
136
+ plt.ylabel('Loss')
137
+ plt.legend()
138
+ plt.show()
139
+
140
+ plt.figure(figsize=(10, 5))
141
+ plt.plot(accuracies, label='Accuracy')
142
+ plt.plot(train_acc, label='Training Accuracy')
143
+ plt.xlabel('Epochs')
144
+ plt.ylabel('Accuracy (%)')
145
+ plt.legend()
146
+ plt.show()
147
+ ```
148
+