forked from pranavmkoundinya/VIPCUP2023_OLIVES
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
158 lines (116 loc) · 4.35 KB
/
train.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
import torch
from utils import AverageMeter,save_model,output_state
import sys
import time
import numpy as np
from sklearn.metrics import f1_score
from config import parse_option
import os
from utils import set_loader, set_model, set_optimizer, adjust_learning_rate
def train_supervised(train_loader, model,criterion, optimizer, epoch, opt):
"""one epoch training"""
model.train()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
device = opt.device
end = time.time()
for idx, (image, bio_tensor) in enumerate(train_loader):
data_time.update(time.time() - end)
images = image.to(device)
labels = bio_tensor.float()
labels = labels.to(device)
bsz = labels.shape[0]
# compute loss
output = model(images)
loss = criterion(output, labels)
# update metric
losses.update(loss.item(), bsz)
# SGD
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# print info
if (idx + 1) % opt.print_freq == 0:
print('Train: [{0}][{1}/{2}]\t'.format(
epoch, idx + 1, len(train_loader)))
sys.stdout.flush()
## dd
# Calculate and print F1 score
# sample_evaluation(train_loader, model, opt)
## dd
return losses.avg
def submission_generate(val_loader, model, opt):
"""validation"""
model.eval()
device = opt.device
out_list = []
with torch.no_grad():
for idx, (image) in (enumerate(val_loader)):# for idx, (image,bio_tensor) in (enumerate(val_loader)):
images = image.float().to(device)
# forward
output = model(images)
output = torch.round(torch.sigmoid(output))
out_list.append(output.squeeze().detach().cpu().numpy())
out_submisison = np.array(out_list)
np.save('output',out_submisison)
def sample_evaluation(val_loader, model, opt):
"""validation"""
model.eval()
device = opt.device
out_list = []
label_list = []
with torch.no_grad():
for idx, image in (enumerate(val_loader)):
images = image.float().to(device)
labels = bio_tensor.float()
labels = labels.float()
label_list.append(labels.squeeze().detach().cpu().numpy())
# forward
output = model(images)
output = torch.round(torch.sigmoid(output))
out_list.append(output.squeeze().detach().cpu().numpy())
label_array = np.array(label_list)
out_array = np.array(out_list)
f = f1_score(label_array,out_array,average='macro')
print(f)
def main():
opt = parse_option()
# build data loader
train_loader,test_loader = set_loader(opt)
# build model and criterion
model, criterion = set_model(opt)
# build optimizer
optimizer = set_optimizer(opt, model)
# training routine
for epoch in range(1, opt.epochs + 1):
train_supervised(train_loader, model, criterion, optimizer, epoch, opt)
'''
# Print train loss after each epoch
print(f"Epoch [{epoch}/{opt.epochs}] - Train Loss: {train_loss:.4f}")
# Evaluate model's training and print F1 score
sample_evaluation(train_loader, model, opt)
'''
submission_generate(test_loader, model, opt)
#sample_evaluation(test_loader, model, opt)
save_file = os.path.join(
opt.save_folder, 'last.pth')
save_model(model, optimizer, opt, opt.epochs, save_file)
global output_csv_name
output_csv_name=output_state(opt.model,opt.batch_size,opt.epochs,opt.learning_rate,opt.momentum,opt.temp)
###edited by dd
txt_file_path = '/kaggle/working/output_csv_name.txt'
try:
with open(txt_file_path, 'w') as file:
file.write(output_csv_name)
print("csv name file created successfully.")
except Exception as e:
print("Error creating file:", e)
###
# global output_csv_name='a'#str(model)#+str(opt.epochs)+str(opt.batch_size) #this line is added to output name for csv.use this vairable only.
# return a
if __name__ == '__main__':
main()