-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
79 lines (64 loc) · 2.18 KB
/
test.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
import torch.nn as nn
from utils import *
from models.deeplabv3 import DeepLabv3
import sys
import os
import time
from tqdm import tqdm
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
def predict(FLAGS):
# Check if the pretrained model is available
if not FLAGS.m.endswith('.pth'):
raise RuntimeError('Unknown file passed. Must end with .pth')
if FLAGS.image_path is None or not os.path.exists(FLAGS.image_path):
raise RuntimeError('An image file path must be passed')
h = FLAGS.resize_height
w = FLAGS.resize_width
print ('[INFO]Loading Checkpoint...')
checkpoint = torch.load(FLAGS.m, map_location='cpu')
print ('[INFO]Checkpoint Loaded')
# Assuming the dataset is camvid
deeplabv3 = DeepLabv3(FLAGS.num_classes)
deeplabv3.load_state_dict(checkpoint['model_state_dict'])
print ('[INFO]Initiated model with pretraiend weights.')
tmg_ = np.array(Image.open(FLAGS.image_path))
tmg_ = cv2.resize(tmg_, (w, h), cv2.INTER_NEAREST)
tmg = torch.tensor(tmg_).unsqueeze(0).float()
tmg = tmg.transpose(2, 3).transpose(1, 2)
print ('[INFO]Starting inference...')
deeplabv3.eval()
s = time.time()
out1 = deeplabv3(tmg.float()).squeeze(0)
o = time.time()
deeplabv3.train()
print ('[INFO]Inference complete!')
print ('[INFO]Time taken: ', o - s)
out2 = out1.squeeze(0).cpu().detach().numpy()
b_ = out1.data.max(0)[1].cpu().detach().numpy()
b = decode_segmap_cscapes(b_)
print ('[INFO]Got segmented results!')
plt.title('Input Image')
plt.axis('off')
plt.imshow(tmg_)
plt.show()
plt.title('Output Image')
plt.axis('off')
plt.imshow(b)
plt.show()
plt.figure(figsize=(10, 10))
gs = gridspec.GridSpec(9, 4)
gs.update(wspace=0.025, hspace=0.005)
label = 0
for ii in range(34):
plt.subplot(gs[ii])
plt.axis('off')
plt.imshow(out2[label, :, :])
label += 1
plt.show()
if FLAGS.save:
cv2.imwrite('seg.png', b)
print ('[INFO]Segmented image saved successfully!')
print ('[INFO] Prediction complete successfully!')