-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathrun_demo.py
169 lines (125 loc) · 5.76 KB
/
run_demo.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
import argparse
import os
import torch.backends.cudnn as cudnn
import models
import torchvision.transforms as transforms
import flow_transforms
from scipy.ndimage import imread
from scipy.misc import imsave
from loss import *
import time
import random
from glob import glob
import matplotlib.pyplot as plt
# import sys
# sys.path.append('../cython')
# from connectivity import enforce_connectivity
'''
Infer from custom dataset:
author:Fengting Yang
last modification: Mar.5th 2020
usage:
1. set the ckpt path (--pretrained) and output
2. comment the output if do not need
results will be saved at the args.output
'''
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__"))
parser = argparse.ArgumentParser(description='PyTorch SPixelNet inference on a folder of imgs',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--data_dir', metavar='DIR', default='./demo/inputs', help='path to images folder')
parser.add_argument('--data_suffix', default='jpg', help='suffix of the testing image')
parser.add_argument('--pretrained', metavar='PTH', help='path to pre-trained model',
default= './pretrain_ckpt/SpixelNet_bsd_ckpt.tar')
parser.add_argument('--output', metavar='DIR', default= './demo' , help='path to output folder')
parser.add_argument('--downsize', default=16, type=float,help='superpixel grid cell, must be same as training setting')
parser.add_argument('-nw', '--num_threads', default=1, type=int, help='num_threads')
parser.add_argument('-b', '--batch-size', default=1, type=int, metavar='N', help='mini-batch size')
args = parser.parse_args()
random.seed(100)
@torch.no_grad()
def test(args, model, img_paths, save_path, idx):
# Data loading code
input_transform = transforms.Compose([
flow_transforms.ArrayToTensor(),
transforms.Normalize(mean=[0,0,0], std=[255,255,255]),
transforms.Normalize(mean=[0.411,0.432,0.45], std=[1,1,1])
])
img_file = img_paths[idx]
load_path = img_file
imgId = os.path.basename(img_file)[:-4]
# may get 4 channel (alpha channel) for some format
img_ = imread(load_path)[:, :, :3]
H, W, _ = img_.shape
H_, W_ = int(np.ceil(H/16.)*16), int(np.ceil(W/16.)*16)
# get spixel id
n_spixl_h = int(np.floor(H_ / args.downsize))
n_spixl_w = int(np.floor(W_ / args.downsize))
spix_values = np.int32(np.arange(0, n_spixl_w * n_spixl_h).reshape((n_spixl_h, n_spixl_w)))
spix_idx_tensor_ = shift9pos(spix_values)
spix_idx_tensor = np.repeat(
np.repeat(spix_idx_tensor_, args.downsize, axis=1), args.downsize, axis=2)
spixeIds = torch.from_numpy(np.tile(spix_idx_tensor, (1, 1, 1, 1))).type(torch.float).cuda()
n_spixel = int(n_spixl_h * n_spixl_w)
img = cv2.resize(img_, (W_, H_), interpolation=cv2.INTER_CUBIC)
img1 = input_transform(img)
ori_img = input_transform(img_)
# compute output
tic = time.time()
output = model(img1.cuda().unsqueeze(0))
toc = time.time() - tic
# assign the spixel map
curr_spixl_map = update_spixl_map(spixeIds, output)
ori_sz_spixel_map = F.interpolate(curr_spixl_map.type(torch.float), size=( H_,W_), mode='nearest').type(torch.int)
mean_values = torch.tensor([0.411, 0.432, 0.45], dtype=img1.cuda().unsqueeze(0).dtype).view(3, 1, 1)
spixel_viz, spixel_label_map = get_spixel_image((ori_img + mean_values).clamp(0, 1), ori_sz_spixel_map.squeeze(), n_spixels= n_spixel, b_enforce_connect=True)
# ************************ Save all result********************************************
# save img, uncomment it if needed
# if not os.path.isdir(os.path.join(save_path, 'img')):
# os.makedirs(os.path.join(save_path, 'img'))
# spixl_save_name = os.path.join(save_path, 'img', imgId + '.jpg')
# img_save = (ori_img + mean_values).clamp(0, 1)
# imsave(spixl_save_name, img_save.detach().cpu().numpy().transpose(1, 2, 0))
# save spixel viz
if not os.path.isdir(os.path.join(save_path, 'spixel_viz')):
os.makedirs(os.path.join(save_path, 'spixel_viz'))
spixl_save_name = os.path.join(save_path, 'spixel_viz', imgId + '_sPixel.png')
imsave(spixl_save_name, spixel_viz.transpose(1, 2, 0))
# save the unique maps as csv, uncomment it if needed
# if not os.path.isdir(os.path.join(save_path, 'map_csv')):
# os.makedirs(os.path.join(save_path, 'map_csv'))
# output_path = os.path.join(save_path, 'map_csv', imgId + '.csv')
# # plus 1 to make it consistent with the toolkit format
# np.savetxt(output_path, (spixel_label_map + 1).astype(int), fmt='%i',delimiter=",")
if idx % 10 == 0:
print("processing %d"%idx)
return toc
def main():
global args, save_path
data_dir = args.data_dir
print("=> fetching img pairs in '{}'".format(data_dir))
save_path = args.output
print('=> will save everything to {}'.format(save_path))
if not os.path.isdir(save_path):
os.makedirs(save_path)
tst_lst = glob(args.data_dir + '/*.' + args.data_suffix)
tst_lst.sort()
if len(tst_lst) == 0:
print('Wrong data dir or suffix!')
exit(1)
print('{} samples found'.format(len(tst_lst)))
# create model
network_data = torch.load(args.pretrained)
print("=> using pre-trained model '{}'".format(network_data['arch']))
model = models.__dict__[network_data['arch']]( data = network_data).cuda()
model.eval()
args.arch = network_data['arch']
cudnn.benchmark = True
mean_time = 0
for n in range(len(tst_lst)):
time = test(args, model, tst_lst, save_path, n)
mean_time += time
print("avg_time per img: %.3f"%(mean_time/len(tst_lst)))
if __name__ == '__main__':
main()