-
Notifications
You must be signed in to change notification settings - Fork 5
/
finetune.py
79 lines (63 loc) · 2.69 KB
/
finetune.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
import torch.optim as optim
from options.train_options import TrainOptions
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from PIL import Image
import os
from models import networks
from options.test_options import TestOptions
from util import util
import time
def test():
opt = TrainOptions().parse() # get testing options
model = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.netG, opt.norm,
not opt.no_dropout, opt.init_type, opt.init_gain, opt.gpu_ids)
# load the pre-trained weights
# print('Loading……………………………………')
# state_dict = torch.load('./finetune/0_net_finetune.pth')
# model.load_state_dict(state_dict)
# model.cuda()
print('Loading……………………………………')
# state_dict = torch.load('./latest_net_G_A.pth')
# model.module.load_state_dict(state_dict)
# model.cuda()
state_dict = torch.load('./finetune_5e-6_100/900_net_finetune.pth')
model.load_state_dict(state_dict)
model.cuda()
# set up the image transformation pipeline
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# set up the input and output folders
input_folder = '../datasets/chinamm/testA'
output_folder = './finetune_results'
# create the output folder if it does not exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# loop through all the images in the input folder
for file_name in os.listdir(input_folder):
print(file_name)
# load the input image
input_image = Image.open(os.path.join(input_folder, file_name))
# apply the transformation pipeline
input_tensor = transform(input_image)
input_tensor = input_tensor.unsqueeze(0).cuda()
# run the model and get the output
with torch.no_grad():
# torch.cuda.synchronize()
# start = time.time()
output_tensor = model(input_tensor)
# torch.cuda.synchronize()
# end = time.time()
# p_time = end - start
# print("================================= time for %f============================" % (p_time))
# convert the output tensor to an image
im = util.tensor2im(output_tensor)
output_image = transforms.ToPILImage()(output_tensor.squeeze().cpu())
# save the output image
output_file_name = os.path.splitext(file_name)[0] + '.png'
output_path = os.path.join(output_folder, output_file_name)
util.save_image(im, output_path)
if __name__ == '__main__':
# train()
test()