-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainTorch.py
227 lines (183 loc) · 8.61 KB
/
trainTorch.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import torch
import torchvision.transforms as transforms
import torchvision.models as tvModels
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import PIL
from matplotlib import pyplot as plt
import numpy as np
import cv2
import pandas as pd
import os
# Function to train the neural network
def train_model(model, train_loader_generator, test_loader, criterion, optimizer,
scheduler, num_epochs, device, validateAtStep=10, modelName = "model_temp", saveAtStep=10):
model.train()
# model.to(device)
for epoch in range(num_epochs):
running_loss = 0.0
train_loader = train_loader_generator()
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
# inputs = inputs.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
scheduler.step()
if os.path.exists(f"{modelName}.log"):
with open(f"{modelName}.log", "r") as file:
for row in file:
pass
row = row.split(",")
ep = int(row[0])
prevLoss = float(row[1])
else:
with open(f"{modelName}.log", "w") as file:
file.write("epoch,loss,validation loss\n")
ep = 0
prevLoss = -1
trainLoss = running_loss/len(train_loader)#
if (epoch+1) % saveAtStep == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {trainLoss:.4f}, prev: {prevLoss}, lr: {scheduler.get_last_lr()}")
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict(),
'loss': loss
},f"{modelName}.tar")
if (epoch+1) % validateAtStep == 0:
valLoss = evaluate_model(model, test_loader, device)
else:
valLoss = -1
with open(f"{modelName}.log","a") as file:
file.writelines(f"{ep+1}, {trainLoss:.4f}")
if valLoss > 0:
file.writelines(f", {valLoss:.4f}")
file.writelines("\n")
# Function to evaluate the neural network
def evaluate_model(model, test_loader, device):
model.eval()
# model.to(device)
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
valLoss = float(correct / total)
print(f'Accuracy of the model on the test images: {100 * correct / total:.2f}%')
model.train()
return valLoss
class BirdDataset(torch.utils.data.Dataset): # inheritin from Dataset class
def __init__(self, pdDataset, root_dir="", transform=None):
self.annotation_df = pdDataset
self.root_dir = root_dir # root directory of images, leave "" if using the image path column in the __getitem__ method
self.transform = transform
def __len__(self):
return len(self.annotation_df) # return length (numer of rows) of the dataframe
def __getitem__(self, idx):
# print(os.path.join(self.root_dir, self.annotation_df.iloc[idx, 1]))
image_path = os.path.join(self.root_dir, self.annotation_df.iloc[idx, 1]) #use image path column (index = 1) in csv file
image = cv2.imread(image_path) # read image by cv2
#print("Imshape")
#print(f"Before transform: {image.shape}")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert from BGR to RGB for matplotlib
class_name = self.annotation_df.iloc[idx, 2] # use class name column (index = 2) in csv file
class_index = self.annotation_df.iloc[idx, 3] # use class index column (index = 3) in csv file
if self.transform:
image = self.transform(image)
#print(f"After transform: {image.shape}")
return image, class_index
def SetupReducedDataset(dataset, nData) -> BirdDataset:
trainDSReduced = pd.DataFrame(columns=dataset.columns)
for i in indices:
curr_ = dataset[dataset["class_index"] == i]
if len(curr_.index) > nData:
rndPrm = np.random.permutation(len(curr_.index))[0:nData]
trainDSReduced = pd.concat( [trainDSReduced, pd.DataFrame(curr_.loc[curr_.index[rndPrm],:],columns=fullData.columns)] , ignore_index=True, sort=False)
else:
trainDSReduced = pd.concat( [trainDSReduced, curr_] , ignore_index=True, sort=False)
return trainDSReduced
if __name__ == "__main__":
IMGSIZE = 512
NDataPerClass = 150
validateAtStep = 50
saveAtStep = 5
mName = "model_temp_RN101"
transformResNet = transforms.Compose([
transforms.ToTensor(),
transforms.Resize(256),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(degrees=45),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
transforms.CenterCrop(224),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
fullData = pd.read_csv('data.csv')
trainDS = fullData.sample(frac=0.8,random_state=159)
testDS = fullData.drop(trainDS.index)
numData = dict()
indices = fullData["class_index"].unique()
numClasses = len(indices)
print(f"Number of classes: {numClasses}")
for k in indices:
nData = sum(fullData["class_index"]==k)
nTrain = sum(trainDS["class_index"]==k)
nTest = sum(testDS["class_index"]==k)
numData[k] = [nData,nTrain,nTest]
trainDatasetTransformed = lambda: BirdDataset(SetupReducedDataset(trainDS,NDataPerClass), root_dir="", transform=transformResNet)
testDatasetTransformed = BirdDataset(testDS, root_dir="", transform=transformResNet)
batch_size = 128
input_size = IMGSIZE # Example input image size
# rndSampler = torch.utils.data.RandomSampler(trainDatasetTransformed)
trainloader = lambda: torch.utils.data.DataLoader(trainDatasetTransformed(), batch_size=batch_size,
shuffle=True, num_workers=8)
testloader = torch.utils.data.DataLoader(testDatasetTransformed, batch_size=batch_size,
shuffle=False, num_workers=4)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = tvModels.resnet101()
model.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=model.fc.in_features,
out_features=numClasses
),
torch.nn.Sigmoid()
)
model.to(device)
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=.9)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.9)
# optimizer.to(device)
# scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5)
# scheduler.to(device)
# model = torch.load(f"{mName}.mod")
loaded = 0
try:
state = torch.load(f"{mName}.tar",map_location=device,weights_only=False)
loaded = 1
except Exception as e:
print("Couldn't load! Continuing....")
if loaded == 1:
model.load_state_dict(state['model_state_dict'])
optimizer.load_state_dict(state['optimizer_state_dict'])
scheduler.load_state_dict(state['scheduler_state_dict'])
# model = SimpleCNN(input_size=input_size, num_classes=numClasses, layers=layers)
# torch.save(model,f"{mName}.mod")
criterion = nn.CrossEntropyLoss()
# for g in optimizer.param_groups:
# g['lr'] = 0.0001
# device = "cpu"
# print(f"Training on device {device}")
train_model(model, trainloader, testloader,
criterion, optimizer, scheduler, num_epochs=1000,
device = device, validateAtStep=validateAtStep, modelName=mName,
saveAtStep=saveAtStep)
# torch.save(model,"model.mod")