-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_examples.py
More file actions
142 lines (104 loc) · 4.16 KB
/
Copy pathplot_examples.py
File metadata and controls
142 lines (104 loc) · 4.16 KB
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
import os
import shutil
import argparse
import importlib
import tqdm
import torch
import numpy as np
import matplotlib.pyplot as plt
def parse_command_line():
"""
Parse command line and get the name of the experiment and type
"""
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--exp', type=str, help='experiments name')
parser.add_argument('-imgs', '--n', type=int, help='Amount of pictures to make')
parser.add_argument('-best','--best', type=bool, default=False, help='Use best checkpoint')
parser.add_argument('-rm','--remove',type=bool,default=False, help='Remove previously generated images in folder')
args = parser.parse_args()
return args
def load(config):
opt = config['opt']
resume = os.path.join('exp', opt.exp)
if opt.best:
resume_file = os.path.join(resume, 'best_checkpoint.pt')
print("=> loading best checkpoint '{}'".format(resume))
else:
resume_file = os.path.join(resume, 'checkpoint.pt')
print("=> loading checkpoint '{}'".format(resume))
if os.path.isfile(resume_file):
checkpoint = torch.load(resume_file, map_location=config['device'])
# print(checkpoint['inference'])
# error(':)')
config['inference']['net'].load_state_dict(checkpoint['state_dict'])
config['train']['optimizer'].load_state_dict(checkpoint['optimizer'])
config['train']['epoch'] = checkpoint['epoch']
config['inference']['best_loss'] = checkpoint['best_loss']
print("=> loaded checkpoint '{}' (epoch {})".format(resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(resume))
exit(0)
def save_picture(config, output, label):
_, pred = torch.max(output[-1], dim=0)
plt.subplot(2,1,1)
plt.imshow(pred.cpu())
plt.xticks([])
plt.yticks([])
plt.title('Network Performance')
plt.subplot(2,1,2)
plt.imshow(label.squeeze().cpu())
plt.xticks([])
plt.yticks([])
plt.title('Ground Truth')
plt.savefig(os.path.join(config['opt'].pic_path, config['phase'] + "%02d"%(config['n']) + ".png"), bbox_inches='tight')
plt.close()
def make_picture_minibatch(config, phase, inputs, labels):
outputs = config['inference']['net'].model(inputs)
for i in range(outputs.shape[0]):
if config['n'] > config['opt'].n:
break
save_picture(config, outputs[i], labels[i])
config['n'] += 1
return config
def make_pictures(config, data_func, phase):
dataloader = data_func(phase)
config['n'] = 0
config['phase'] = phase
for _, (inputs, labels) in tqdm.tqdm(enumerate(dataloader), total=np.ceil(config['opt'].n/len(dataloader)).astype('int'), ascii=True):
make_picture_minibatch(config, phase, inputs, labels)
if config['n'] >= config['opt'].n:
break
def init():
opt = parse_command_line()
exp_path = os.path.join('exp', opt.exp)
pic_path = os.path.join(exp_path, 'pictures')
opt.exp_path = exp_path
opt.pic_path = pic_path
try: os.makedirs(pic_path)
except FileExistsError: pass
for filename in os.listdir(pic_path):
file_path = os.path.join(pic_path, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
config = importlib.import_module('hyperparameters').__config__
network = importlib.import_module('models.network')
config['opt'] = opt
config['data_provider'] = importlib.import_module(config['data_provider'])
config['device'] = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("=> Using", config['device'])
func = network.make_network(config)
load(config)
return config
if __name__ == "__main__":
config = init()
print("=> Loading data")
data_func = config['data_provider'].init(config)
for phase in ['train','valid','test']:
print("Start " + phase + " " + config['opt'].exp)
make_pictures(config, data_func, phase)
print("=> Completed")