-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_CNN.py
205 lines (163 loc) · 6.09 KB
/
train_CNN.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
import torch
import torch.nn as nn
from torch import autograd
from torchvision import transforms
from torch.utils.data import DataLoader
from PIL import Image
from cnn_dataset import *
#from model.CNN import *
from utils import *
import yaml
import argparse
print(f"Torch version: {torch.__version__}")
print(f"Cuda available: {torch.cuda.is_available()}")
parser = argparse.ArgumentParser()
parser.add_argument("--config_file", type=str, default="config/confCNN.yaml", help="Path to the config file.")
args = parser.parse_args()
config_file = args.config_file
with open(config_file, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
## Basic Training Parameters ##
step_size = config["step_size"]
num_epoch = config["num_epoch"]
batch_size = config["batch_size"]
lr = config["lr"]
weight_decay = config["weight_decay"]
model_name = config["model_name"]
device = config["device"]
## Neural Network Parameters ##
loss_function = config["loss_function"]
out_features = config["out_features"]
color_space = config["color_space"]
input_color_space = config["input_color_space"]
is_classification = config["is_classification"]
input_size = config["input_size"]
normalize_rgb = config["normalize_rgb"]
normalize_cielab = config["normalize_cielab"]
if out_features == 1:
out_type = "Lightness"
else:
out_type = "Color"
print("Evaluating for the model: ", model_name, "\n",
"Loss function: ", loss_function, "\n",
"Output Color Space: ", color_space, "\n",
"Color or Lightness?: ", out_type, "\n",
"Device: ", device, "\n")
transform = transforms.Compose([
transforms.Resize((input_size, input_size)),
#transforms.Normalize((0.5,), (0.5,))
])
model = model_switch_CNN(model_name, out_features).to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=weight_decay)
if loss_function == "MSE":
criterion = nn.MSELoss()
elif loss_function == "Cross-Entropy":
criterion = nn.CrossEntropyLoss()
elif loss_function == "MAE":
criterion = nn.L1Loss()
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=0.1)
train_dataset = PreviewDataset(transform=transform,
color_space=color_space,
input_color_space=input_color_space,
normalize_rgb=normalize_rgb,
normalize_cielab=normalize_cielab)
test_dataset = PreviewDataset(transform=transform,
test=True,
color_space=color_space,
input_color_space=input_color_space,
normalize_rgb=normalize_rgb,
normalize_cielab=normalize_cielab)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=True)
save_path = "../CNN_models/" + model_name + "/weights"
loss_path = "../CNN_models/" + model_name + "/losses"
log_path = "../CNN_models/"+model_name
if not os.path.exists(save_path):
os.makedirs(save_path)
if not os.path.exists(loss_path):
os.makedirs(loss_path)
# Copy yaml file to the model folder
with open("../CNN_models/"+model_name+"/conf.yaml", "w+") as wf:
yaml.dump(config, wf)
def train(data, color):
model.train()
optimizer.zero_grad()
out = model(data)
if out_features == 1:
loss = criterion(out[:, 0], color[:, 0])
else:
if loss_function != "CIELab":
loss = criterion(out, color)
else:
loss = 0
for i in range(batch_size):
loss += colormath_CIE2000(out[i], color[i])
loss /= out.shape[0]
loss.backward()
optimizer.step()
return loss, out
def test(data, color):
model.eval()
out = model(data)
if out_features == 1:
loss = criterion(out[:, 0], color[:, 0])
else:
if loss_function != "CIELab":
loss = criterion(out, color)
else:
loss = colormath_CIE2000(out, color)
return loss, out
def classification_train(data, color):
model.train()
optimizer.zero_grad()
r, g, b = model(data)
loss_r = criterion(r, color[0].to(device))
loss_g = criterion(g, color[1].to(device))
loss_b = criterion(b, color[2].to(device))
loss = loss_r + loss_g + loss_b
loss.backward()
optimizer.step()
return loss
def classification_test(data, color):
model.eval()
r, g, b = model(data)
loss_r = criterion(r, color[0].to(device))
loss_g = criterion(g, color[1].to(device))
loss_b = criterion(b, color[2].to(device))
loss = loss_r + loss_g + loss_b
return loss
train_losses = []
val_losses = []
best_loss = 100000
for epoch in range(num_epoch):
total_train_loss = 0
total_val_loss = 0
count = 0
num_batches = len(train_loader)
for input_data, target_color in train_loader:
if is_classification:
loss = classification_train(input_data.to(device), target_color)
else:
loss, out = train(input_data.to(device), target_color.to(device))
total_train_loss += loss.item()
for input_data, target_color in test_loader:
if is_classification:
loss = classification_test(input_data.to(device), target_color)
else:
loss, out = test(input_data.to(device), target_color.to(device))
total_val_loss += loss.item()
val_loss = total_val_loss/len(test_dataset)
train_loss = total_train_loss/num_batches
train_losses.append(train_loss)
val_losses.append(val_loss)
print("------- Epoch ", epoch, " -------")
print("Train loss: ", train_loss)
print("Test loss: ", val_loss)
save_plot(train_losses=train_losses, val_losses=val_losses, loss_type=loss_function, loss_path=loss_path)
if val_loss < best_loss:
best_loss = val_loss
model = model.cpu()
best_model = model.state_dict()
model = model.to(device)
save_model(state_dict=best_model, train_losses=train_losses, val_losses=val_losses, epoch=epoch, save_path=save_path)
lr_scheduler.step()