Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6d73a6e

Browse filesBrowse files
adam9500370meetps
authored andcommitted
Make scripts clean
1 parent 54aa674 commit 6d73a6e
Copy full SHA for 6d73a6e

File tree

Expand file treeCollapse file tree

3 files changed

+24
-40
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+24
-40
lines changed

‎test.py

Copy file name to clipboardExpand all lines: test.py
+6-8Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test(args):
3939
resized_img = misc.imresize(img, (loader.img_size[0], loader.img_size[1]), interp='bicubic')
4040

4141
orig_size = img.shape[:-1]
42-
if model_name == 'pspnet':
42+
if model_name in ['pspnet', 'icnet', 'icnetBN']:
4343
img = misc.imresize(img, (orig_size[0]//2*2+1, orig_size[1]//2*2+1)) # uint8 with RGB mode, resize width and height which are odd numbers
4444
else:
4545
img = misc.imresize(img, (loader.img_size[0], loader.img_size[1]))
@@ -65,10 +65,7 @@ def test(args):
6565
else:
6666
images = Variable(img, volatile=True)
6767

68-
if model_name == 'pspnet':
69-
outputs = model(images)[-1]
70-
else:
71-
outputs = model(images)
68+
outputs = model(images)
7269
#outputs = F.softmax(outputs, dim=1)
7370

7471
if args.dcrf:
@@ -93,9 +90,10 @@ def test(args):
9390
misc.imsave(dcrf_path, decoded_crf)
9491
print("Dense CRF Processed Mask Saved at: {}".format(dcrf_path))
9592

96-
pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=0).astype(np.uint8)
97-
if model_name == 'pspnet':
98-
pred = misc.imresize(pred, orig_size, 'nearest') # uint8 with RGB mode
93+
pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=0)
94+
if model_name in ['pspnet', 'icnet', 'icnetBN']:
95+
pred = pred.astype(np.float32)
96+
pred = misc.imresize(pred, orig_size, 'nearest', mode='F') # float32 with F mode, resize back to orig_size
9997
decoded = loader.decode_segmap(pred)
10098
print('Classes found: ', np.unique(pred))
10199
misc.imsave(args.out_path, decoded)

‎train.py

Copy file name to clipboardExpand all lines: train.py
+1-10Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,7 @@ def train(args):
8686
optimizer.zero_grad()
8787
outputs = model(images)
8888

89-
if args.arch == 'pspnet':
90-
aux_cls, final_cls = outputs
91-
92-
aux_loss = loss_fn(input=aux_cls, target=labels)
93-
final_loss = loss_fn(input=final_cls, target=labels)
94-
95-
LAMBDA1, LAMBDA2 = 0.4, 1.0
96-
loss = LAMBDA1 * aux_loss + LAMBDA2 * final_loss
97-
else:
98-
loss = loss_fn(input=outputs, target=labels)
89+
loss = loss_fn(input=outputs, target=labels)
9990

10091
loss.backward()
10192
optimizer.step()

‎validate.py

Copy file name to clipboardExpand all lines: validate.py
+17-22Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,22 @@ def validate(args):
4949
images = Variable(images.cuda(), volatile=True)
5050
#labels = Variable(labels.cuda(), volatile=True)
5151

52-
if model_name == 'pspnet':
53-
outputs = model(images)[-1]
54-
if args.include_flip_mode:
55-
outputs = outputs.data.cpu().numpy()
56-
flipped_images = np.copy(images.data.cpu().numpy()[:, :, :, ::-1])
57-
flipped_images = Variable(torch.from_numpy( flipped_images ).float().cuda(), volatile=True)
58-
outputs_flipped = model( flipped_images )[-1].data.cpu().numpy()
59-
outputs = (outputs + outputs_flipped[:, :, :, ::-1]) / 2.0
60-
else:
52+
if args.eval_flip:
6153
outputs = model(images)
62-
if args.include_flip_mode:
63-
outputs = outputs.data.cpu().numpy()
64-
flipped_images = np.copy(images.data.cpu().numpy()[:, :, :, ::-1])
65-
flipped_images = Variable(torch.from_numpy( flipped_images ).float().cuda(), volatile=True)
66-
outputs_flipped = model( flipped_images ).data.cpu().numpy()
67-
outputs = (outputs + outputs_flipped[:, :, :, ::-1]) / 2.0
68-
69-
if args.include_flip_mode:
70-
pred = np.argmax(outputs, axis=1).astype(np.uint8)
54+
55+
# Flip images in numpy (not support in tensor)
56+
outputs = outputs.data.cpu().numpy()
57+
flipped_images = np.copy(images.data.cpu().numpy()[:, :, :, ::-1])
58+
flipped_images = Variable(torch.from_numpy( flipped_images ).float().cuda(), volatile=True)
59+
outputs_flipped = model( flipped_images )
60+
outputs_flipped = outputs_flipped.data.cpu().numpy()
61+
outputs = (outputs + outputs_flipped[:, :, :, ::-1]) / 2.0
62+
63+
pred = np.argmax(outputs, axis=1)
7164
else:
72-
pred = outputs.data.max(1)[1].cpu().numpy().astype(np.uint8)
65+
outputs = model(images)
66+
pred = outputs.data.max(1)[1].cpu().numpy()
67+
7368
#gt = labels.data.cpu().numpy()
7469
gt = labels.numpy()
7570

@@ -103,11 +98,11 @@ def validate(args):
10398
help='Disable input image scales normalization [0, 1] | True by default')
10499
parser.set_defaults(img_norm=True)
105100

106-
parser.add_argument('--include_flip_mode', dest='include_flip_mode', action='store_true',
101+
parser.add_argument('--eval_flip', dest='eval_flip', action='store_true',
107102
help='Enable evaluation with flipped image | True by default')
108-
parser.add_argument('--no-include_flip_mode', dest='include_flip_mode', action='store_false',
103+
parser.add_argument('--no-eval_flip', dest='eval_flip', action='store_false',
109104
help='Disable evaluation with flipped image | True by default')
110-
parser.set_defaults(include_flip_mode=True)
105+
parser.set_defaults(eval_flip=True)
111106

112107
parser.add_argument('--batch_size', nargs='?', type=int, default=1,
113108
help='Batch Size')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.