-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_train.py
73 lines (55 loc) · 1.81 KB
/
run_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
import torch
import torch.nn as nn
import torch.optim as optim
import tqdm
import time
from torchvision.models.vgg import vgg16
from vgg_game_model import VggGameModel
from preprocessing import Preprocessing
from sklearn.metrics import precision_recall_fscore_support, confusion_matrix
train_path = './dataset/train/'
test_path = './dataset/test/'
pre = Preprocessing(train_path, test_path, 12)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = VggGameModel(12).forward()
model.to(device)
model.train()
optimizer = optim
criterion = nn.CrossEntropyLoss()
epochs = 50
loss_list = []
for epoch in range(epochs):
start_time = time.time()
epoch_loss = 0.0
num_batchs = 0
for data, target in tqdm.tqdm(pre.run_train_preprocess()):
optimizer.zero_grad()
pred = model(data.to(device))
loss = criterion(pred, target.to(device))
epoch_loss += loss.item()
num_batchs += 1
loss.backward()
optimizer.step()
avg_loss = epoch_loss / num_batchs
loss_list.append(avg_loss)
model.eval()
correct = 0
total = 0
all_preds = []
all_labels = []
with torch.no_grad():
for img, label in pre.test_dataloader_load():
img, label = img.to(device), label.to(device)
pred = model(img)
result = pred.max(1)[1]
all_preds.extend(result.cpu().numpy())
all_labels.extend(label.cpu().numpy())
correct += result.eq(label).sum().item()
total += label.size(0)
accuracy = correct / total
precision, recall, f1, _ = precision_recall_fscore_support(all_labels, all_preds, average='weighted')
print(f'Accuracy : {accuracy}')
print(f'Precision : {precision}')
print(f'Recall : {recall}')
print(f'F1 Score : {f1}')
torch.save(model.state_dict(), 'game_shot_model.pth')