-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_code.py
103 lines (64 loc) · 2.72 KB
/
main_code.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#main
#importing created python files
from display_image import *
from dataset_hotdog import *
from image_classifier_model import *
#importing libraries
from torchvision import transforms
from torch.utils.data import random_split
from torch import manual_seed
from torchvision import models
from torch.nn import Linear
from torch.nn import CrossEntropyLoss
from torch.optim import Adam
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
composed = transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(degrees=5),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
dataset_full = HotDogDataset("hotdognothotdogfull", transform = composed)
manual_seed(0)
training_size = int(len(dataset_full) * 0.7)
validation_size = int(len(dataset_full) * 0.15)
test_size = len(dataset_full) - training_size - validation_size
training_set, validation_set, test_set = random_split(dataset=dataset_full, lengths=(training_size, validation_size, test_size))
imshow(training_set[0][0])
# Batch size: train set
batch_size = 50
# Learning rate
lr = 5e-3
# Number epochs
n_epochs = 25
model = models.resnet18(pretrained = True)
for param in model.parameters():
param.requires_grad = False
# Next, we set `n_classes` to the number of classes we have.
# Recall that we have two classes: "hotdog" and "not hotdog".
n_classes = dataset_full.n_classes
# Now that we have those parameters set, we can replace the output layer,
# `model.fc` of the neural network, with a `nn.Linear` object to classify
# `n_classes`'s different classes. For the first parameter, known as **in_features**,
# we input 512 because the second last hidden layer of the neural network
# has 512 neurons.
model.fc = Linear(512, n_classes)
criterion = CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr = lr)
trainer = ClassificationModelTrainer(model,
training_set,
validation_set,
batch_size,
criterion,
optimizer)
#training the model
trainer.load_from("twenty-five-iters.pt")
trainer.train_model(n_epochs = 2)
#plot the training result
trainer.plot_training_stat()
#testing the model
accuracy = trainer.test(test_set)
#final result
print(f"The model reached an accuracy rate of {accuracy:.2f}% on images it has never seen before.")