-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_model.py
170 lines (124 loc) · 4.25 KB
/
train_model.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import torch
import numpy as np
import torch.nn as nn
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
from inception_v3 import InceptionV3
import time
import tqdm
import random
# hyper parameters
batch_size = 32
LR = 0.001
num_epochs = 25
WIDTH = 160
HEIGHT = 120
n_classes = 5
# path and file names
dir_path = './Data/'
img_file_name = 'train_images.npy'
labels_file_name = 'train_labels.npy'
print("Loading our dataset...")
# looading our model data
X = np.load(dir_path + img_file_name, allow_pickle=True)
y = np.load(dir_path + labels_file_name, allow_pickle=True)
X = X.astype(float)/255 # normalization for training
# custom dataset definition
print("Loading DataLoader...")
class DatasetGTA5(Dataset):
def __init__(self, numpy_data, labels):
self.data = numpy_data
self.labels = labels
def __len__(self):
return len(self.data)
def __getitem__(self, index):
data = self.data[index]
label = self.labels[index]
data = torch.from_numpy(data)
label = torch.from_numpy(label)
return data, label
# device setup
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# custom dataset transformations
# transform = transforms.Compose([
# transforms.Resize((WIDTH, HEIGHT)),
# transforms.ToTensor(),
# ])
# initialize pytorch custom dataset
dataset = DatasetGTA5(X, y)
# splitting dataset into train and test dataset
train_size = int(0.8 * len(dataset))
test_size = len(dataset) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(dataset, [train_size, test_size])
# dataloaders for training and testing
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
print("Initializing our Inception3 model...")
# our inception 3 model
model = InceptionV3(num_classes=n_classes)
model = model.to(device)
# loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LR)
# training loop
print("\nTraining the model:-")
total_steps = len(train_loader)
start = time.time()
for epoch in range(num_epochs):
running_loss = 0.
correct = 0
total = 0
print("\nProgress:-")
for i, (images, labels) in enumerate(tqdm.tqdm(train_loader)):
# move tensors to devices
images = images.to(device)
labels = labels.to(device).to(torch.float32)
# zero the gradients
optimizer.zero_grad()
# forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# backward pass and optimization
loss.backward()
optimizer.step()
# calculate running loss
running_loss += loss.item()
# calculating running accuracy
_, pred_classes = torch.max(outputs, dim=1)
_, true_classes = torch.max(labels, dim=1)
correct += (pred_classes == true_classes).sum().item()
total += labels.size(0)
# Calculate epoch loss and accuracy
epoch_loss = running_loss / len(train_loader)
epoch_acc = correct / total
# Print epoch statistics
print(f"Epoch [{epoch+1}/{num_epochs}] - Loss:\
{epoch_loss:.4f} - Accuracy: {epoch_acc:.4f}")
last = time.time()
print("\nTraining time elapsed: %.2fs"%(last-start))
# evaluate the model
# Set the model to evaluation mode
model.eval()
# Initialize variables for accuracy calculation
correct = 0
total = 0
# Disable gradient calculation
with torch.no_grad():
for i, (images, labels) in enumerate(test_loader):
images = images.to(device)
labels = labels.to(device).to(torch.float32)
# Forward pass
outputs = model(images)
# Calculate accuracy
_, predicted_classes = torch.max(outputs, dim=1)
_, true_classes = torch.max(labels, dim=1)
correct += (predicted_classes == true_classes).sum().item()
total += labels.size(0)
# Calculate accuracy
accuracy = correct / total
print(f"Test Accuracy: {accuracy:.4f}")
# Saving model state
print("\nSaving the model weights...")
torch.save(model.state_dict(), './ModelSaves/inceptv3_model.pth')
print("Model saved in ModelSaves folder.")