-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_localize.py
executable file
·129 lines (100 loc) · 3.5 KB
/
main_localize.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
import os
from torchvision import transforms
import torch.optim as optim
import torch.nn as nn
import torch
from torchvision import transforms as T
from dataset import HandDataset
from utils import shuffle_loader
from models import HandSegmentationModel
class Args:
def __init__(self):
self.random_seed = 1
self.device = "cpu"
self.IMG_SIZE = 256
normalize = transforms.Normalize(
mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
)
self.transforms = T.Compose([T.ToTensor(), normalize])
self.optimizer = optim.Adam
self.loss_fn = nn.BCELoss()
self.epochs = 10
def load_data(transform):
train_images = []
train_masks = []
for i in os.listdir("dataset/training/color/"):
train_images.append("dataset/training/color/" + i)
train_masks.append("dataset/training/mask/" + i)
eval_images = []
eval_masks = []
for i in os.listdir("dataset/evaluation/color/"):
eval_images.append("dataset/evaluation/color/" + i)
eval_masks.append("dataset/evaluation/mask/" + i)
train_dataset = HandDataset(
train_images, train_masks, transform=transform, augment=False
)
train_loader = shuffle_loader(train_dataset)
eval_dataset = HandDataset(eval_images, eval_masks, transform=transform)
eval_loader = shuffle_loader(eval_dataset)
return train_loader, eval_loader
def evaluate(model, test_loader, args):
model = model.eval()
running_loss = 0.0
for data, labels in test_loader:
outputs = model(data)
# print(labels.shape)
# print(outputs.shape)
loss = args.criterion(
outputs, labels.view([-1, args.IMG_SIZE, args.IMG_SIZE]).float()
)
running_loss += loss.item()
# print(loss)
return running_loss / len(test_loader)
def train(args):
# Define an optimizer and criterion
model = HandSegmentationModel().to(args.device).float()
criterion = nn.BCELoss()
optimizer = args.optimizer(model.parameters(), lr=0.00001)
train_loader, eval_loader = load_data(args.transform)
best_accuracy = 1000000
for epoch in range(args.epochs):
index = 0
running_loss = 0.0
for data, target in train_loader:
model.train()
inputs = data.to(args.device)
labels = target.to(args.device)
# ============ Forward ============
outputs = model(inputs)
# print(outputs.shape)
# print((labels==1).sum())
# print(outputs.shape)
loss = criterion(
outputs, labels.view([-1, 1, args.IMG_SIZE, args.IMG_SIZE]).float()
)
# ============ Backward ============
optimizer.zero_grad()
loss.backward()
optimizer.step()
index += 1
running_loss += loss.item()
print(
"Epoch: ",
epoch,
" ",
loss.item(),
"Index (",
index,
"/",
len(train_loader),
")",
" ",
(running_loss / index),
)
print("\n")
test_loss = evaluate(model, eval_loader, args)
print("Epoch: ", epoch, " ", (running_loss / index), " Test Loss: ", test_loss)
print("\n")
if test_loss <= best_accuracy:
torch.save(model.state_dict(), "model_256_resnet.pth")
best_accuracy = test_loss