-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransferLearning.py
73 lines (54 loc) · 1.97 KB
/
TransferLearning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import torch
import torchvision
import torch.nn.functional as F
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch import optim
from torch import nn
from torch.utils.data import DataLoader
from tqdm import tqdm
num_classes = 10
learning_rate = 1e-3
batch_size = 1024
num_epochs = 5
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Identity(nn.Module):
def __init__(self):
super(Identity,self).__init__()
def forward(self,x):
return x
model = torchvision.models.vgg16(pretrained=True)
for params in model.parameters():
params.requires_grad=False
model.avgpool=Identity()
model.classifier = nn.Sequential(nn.Linear(512,100),nn.ReLU(),nn.Linear(100,10))
model.to(device)
train_dataset = datasets.CIFAR10(root="data/", train=True, transform=transforms.ToTensor(), download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for batch_idx, (data, targets) in enumerate(tqdm(train_loader)):
data = data.to(device=device)
targets = targets.to(device=device)
scores = model(data)
loss = criterion(scores, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
def check_accuracy(loader, model):
num_correct = 0
num_samples = 0
model.eval()
with torch.no_grad():
for x, y in loader:
x = x.to(device=device)
y = y.to(device=device)
scores = model(x)
_, predictions = scores.max(1)
num_correct += (predictions == y).sum()
num_samples += predictions.size(0)
model.train()
return num_correct/num_samples
print(f"Accuracy on training set: {check_accuracy(train_loader, model)*100:.2f}")
print(f"Accuracy on test set: {check_accuracy(test_loader, model)*100:.2f}")