-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain.py
132 lines (101 loc) · 3.58 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
from torch.utils.data import DataLoader
from utils import *
from network.Network import *
from utils.load_train_setting import *
'''
train
'''
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
network = Network(H, W, message_length, noise_layers, device, batch_size, lr, with_diffusion, only_decoder)
train_dataset = MBRSDataset(os.path.join(dataset_path, "train"), H, W)
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0, pin_memory=True)
val_dataset = MBRSDataset(os.path.join(dataset_path, "validation"), H, W)
val_dataloader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=0, pin_memory=True)
if train_continue:
EC_path = "results/" + train_continue_path + "/models/EC_" + str(train_continue_epoch) + ".pth"
D_path = "results/" + train_continue_path + "/models/D_" + str(train_continue_epoch) + ".pth"
network.load_model(EC_path, D_path)
print("\nStart training : \n\n")
for epoch in range(epoch_number):
epoch += train_continue_epoch if train_continue else 0
running_result = {
"error_rate": 0.0,
"psnr": 0.0,
"ssim": 0.0,
"g_loss": 0.0,
"g_loss_on_discriminator": 0.0,
"g_loss_on_encoder": 0.0,
"g_loss_on_decoder": 0.0,
"d_cover_loss": 0.0,
"d_encoded_loss": 0.0
}
start_time = time.time()
'''
train
'''
num = 0
for _, images, in enumerate(train_dataloader):
image = images.to(device)
message = torch.Tensor(np.random.choice([0, 1], (image.shape[0], message_length))).to(device)
result = network.train(image, message) if not only_decoder else network.train_only_decoder(image, message)
for key in result:
running_result[key] += float(result[key])
num += 1
'''
train results
'''
content = "Epoch " + str(epoch) + " : " + str(int(time.time() - start_time)) + "\n"
for key in running_result:
content += key + "=" + str(running_result[key] / num) + ","
content += "\n"
with open(result_folder + "/train_log.txt", "a") as file:
file.write(content)
print(content)
'''
validation
'''
val_result = {
"error_rate": 0.0,
"psnr": 0.0,
"ssim": 0.0,
"g_loss": 0.0,
"g_loss_on_discriminator": 0.0,
"g_loss_on_encoder": 0.0,
"g_loss_on_decoder": 0.0,
"d_cover_loss": 0.0,
"d_encoded_loss": 0.0
}
start_time = time.time()
saved_iterations = np.random.choice(np.arange(len(val_dataloader)), size=save_images_number, replace=False)
saved_all = None
num = 0
for i, images in enumerate(val_dataloader):
image = images.to(device)
message = torch.Tensor(np.random.choice([0, 1], (image.shape[0], message_length))).to(device)
result, (images, encoded_images, noised_images, messages, decoded_messages) = network.validation(image, message)
for key in result:
val_result[key] += float(result[key])
num += 1
if i in saved_iterations:
if saved_all is None:
saved_all = get_random_images(image, encoded_images, noised_images)
else:
saved_all = concatenate_images(saved_all, image, encoded_images, noised_images)
save_images(saved_all, epoch, result_folder + "images/", resize_to=(W, H))
'''
validation results
'''
content = "Epoch " + str(epoch) + " : " + str(int(time.time() - start_time)) + "\n"
for key in val_result:
content += key + "=" + str(val_result[key] / num) + ","
content += "\n"
with open(result_folder + "/val_log.txt", "a") as file:
file.write(content)
print(content)
'''
save model
'''
path_model = result_folder + "models/"
path_encoder_decoder = path_model + "EC_" + str(epoch) + ".pth"
path_discriminator = path_model + "D_" + str(epoch) + ".pth"
network.save_model(path_encoder_decoder, path_discriminator)