-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict.py
195 lines (155 loc) · 7.36 KB
/
predict.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from torch.utils.data import dataset
from tqdm import tqdm
import network
import utils
import os
import random
import argparse
import numpy as np
from torch.utils import data
from datasets import VOCSegmentation, Cityscapes, cityscapes, ADE20KSegmentation
from torchvision import transforms as T
from metrics import StreamSegMetrics
import torch
import torch.nn as nn
import cv2
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
from glob import glob
def get_argparser():
parser = argparse.ArgumentParser()
# Datset Options
parser.add_argument("--input", type=str, required=True,
help="path to a single image or image directory")
parser.add_argument("--dataset", type=str, default='voc',
choices=['voc', 'cityscapes', 'ade20k'], help='Name of training set')
# Deeplab Options
available_models = sorted(name for name in network.modeling.__dict__ if name.islower() and \
not (name.startswith("__") or name.startswith('_')) and callable(
network.modeling.__dict__[name])
)
parser.add_argument("--model", type=str, default='deeplabv3plus_mobilenet',
choices=available_models, help='model name')
parser.add_argument("--separable_conv", action='store_true', default=False,
help="apply separable conv to decoder and aspp")
parser.add_argument("--output_stride", type=int, default=16, choices=[8, 16])
parser.add_argument("--dram_class", type=bool, default=False,
help="ade20k class num 150 -> 7")
# Train Options
parser.add_argument("--save_val_results_to", default=None,
help="save segmentation results to the specified dir")
parser.add_argument("--crop_val", action='store_true', default=False,
help='crop validation (default: False)')
parser.add_argument("--val_batch_size", type=int, default=4,
help='batch size for validation (default: 4)')
parser.add_argument("--crop_size", type=int, default=513)
parser.add_argument("--ckpt", default=None, type=str,
help="resume from checkpoint")
parser.add_argument("--gpu_id", type=str, default='0',
help="GPU ID")
parser.add_argument("--input_type", type=str, default='image',
help="input_type")
return parser
def main():
opts = get_argparser().parse_args()
if opts.dataset.lower() == 'voc':
opts.num_classes = 21
decode_fn = VOCSegmentation.decode_target
elif opts.dataset.lower() == 'cityscapes':
opts.num_classes = 19
decode_fn = Cityscapes.decode_target
elif opts.dataset.lower() == 'ade20k':
if opts.dram_class == True:
opts.num_classes = 6
decode_fn = ADE20KSegmentation.decode_target
else:
opts.num_classes = 151
decode_fn = ADE20KSegmentation.decode_target
os.environ['CUDA_VISIBLE_DEVICES'] = opts.gpu_id
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Device: %s" % device)
# Setup dataloader
image_files = []
video_files = []
if opts.input_type == 'image':
if os.path.isdir(opts.input):
for ext in ['png', 'jpeg', 'jpg', 'JPEG']:
files = glob(os.path.join(opts.input, '**/*.%s'%(ext)), recursive=True)
if len(files)>0:
image_files.extend(files)
elif os.path.isfile(opts.input):
image_files.append(opts.input)
else:
if os.path.isfile(opts.input):
video_files.append(opts.input)
print(opts.input)
# Set up model (all models are 'constructed at network.modeling)
model = network.modeling.__dict__[opts.model](num_classes=opts.num_classes, output_stride=opts.output_stride)
if opts.separable_conv and 'plus' in opts.model:
network.convert_to_separable_conv(model.classifier)
utils.set_bn_momentum(model.backbone, momentum=0.01)
if opts.ckpt is not None and os.path.isfile(opts.ckpt):
# https://github.com/VainF/DeepLabV3Plus-Pytorch/issues/8#issuecomment-605601402, @PytaichukBohdan
checkpoint = torch.load(opts.ckpt, map_location=torch.device('cpu'))
model.load_state_dict(checkpoint["model_state"])
model = nn.DataParallel(model)
model.to(device)
print("Resume model from %s" % opts.ckpt)
del checkpoint
else:
print("[!] Retrain")
model = nn.DataParallel(model)
model.to(device)
#denorm = utils.Denormalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # denormalization for ori images
if opts.crop_val:
transform = T.Compose([
T.Resize(opts.crop_size),
T.CenterCrop(opts.crop_size),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
else:
transform = T.Compose([
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
if opts.save_val_results_to is not None:
os.makedirs(opts.save_val_results_to, exist_ok=True)
with torch.no_grad():
model = model.eval()
if opts.input_type == 'image':
for img_path in tqdm(image_files):
ext = os.path.basename(img_path).split('.')[-1]
img_name = os.path.basename(img_path)[:-len(ext)-1]
img = Image.open(img_path).convert('RGB')
img = transform(img).unsqueeze(0) # To tensor of NCHW
img = img.to(device)
pred = model(img).max(1)[1].cpu().numpy()[0] # HW
colorized_preds = decode_fn(pred).astype('uint8')
colorized_preds = Image.fromarray(colorized_preds)
if opts.save_val_results_to:
colorized_preds.save(os.path.join(opts.save_val_results_to, img_name+'.png'))
else:
for video_path in tqdm(video_files):
vidcap = cv2.VideoCapture(video_path)
ext = os.path.basename(video_path).split('.')[-1]
vid_name = os.path.basename(video_path)[:-len(ext)-1]
count = 0
while(vidcap.isOpened()):
ret, cv2_image = vidcap.read()
converted = cv2.cvtColor(cv2_image,cv2.COLOR_BGR2RGB)
img90 = cv2.rotate(converted, cv2.ROTATE_90_CLOCKWISE) # �떆怨꾨갑�뼢�쑝濡� 90�룄 �쉶�쟾
img = Image.fromarray(img90)
img = transform(img).unsqueeze(0) # To tensor of NCHW
img = img.to(device)
pred = model(img).max(1)[1].cpu().numpy()[0] # HW
colorized_preds = decode_fn(pred).astype('uint8')
colorized_preds = Image.fromarray(colorized_preds)
if opts.save_val_results_to:
colorized_preds.save(os.path.join(opts.save_val_results_to, vid_name + "_" + str(count) +'.png'))
count = count + 1
if __name__ == '__main__':
main()