-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
233 lines (195 loc) · 7.13 KB
/
run.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Compute depth maps for images in the input folder.
"""
import os
import glob
import torch
import utils
import cv2
import argparse
import numpy as np
from torchvision.transforms import Compose
from midas.dpt_depth import DPTDepthModel
from midas.midas_net import MidasNet
from midas.midas_net_custom import MidasNet_small
from midas.transforms import Resize, ResizeTrain, NormalizeImage, PrepareForNet, RandomCrop, MirrorSquarePad, ColorAug, RandomHorizontalFlip
from utils import parse_dataset_txt
def run(input_path, output_path, dataset_txt, model_path, model_type="large", save_full=False, mask_path="", cls2mask=[], mean=False, it=5, output_list=False):
"""Run MonoDepthNN to compute depth maps.
Args:
input_path (str): path to input folder
output_path (str): path to output folder
model_path (str): path to saved model
"""
print("initialize")
# select device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("device: %s" % device)
# load network
if model_type == "dpt_large": # DPT-Large
model = DPTDepthModel(
path=None,
backbone="vitl16_384",
non_negative=True,
)
net_w, net_h = 384, 384
normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
transform = Compose(
[
Resize(
net_w,
net_h,
resize_target=True,
keep_aspect_ratio=True,
ensure_multiple_of=32,
resize_method="lower_bound",
image_interpolation_method=cv2.INTER_CUBIC,
),
normalization,
PrepareForNet(),
]
)
elif model_type == "midas_v21":
model = MidasNet(None, non_negative=True)
net_w, net_h = 384, 384
normalization = NormalizeImage(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
# Mirror Square Pad and Resize
transform = Compose(
[
Resize(
net_w,
net_h,
resize_target=True,
keep_aspect_ratio=True,
ensure_multiple_of=32,
resize_method="upper_bound",
image_interpolation_method=cv2.INTER_CUBIC,
),
normalization,
PrepareForNet(),
]
)
else:
print(f"model_type '{model_type}' not implemented, use: --model_type large")
assert False
checkpoint = torch.load(model_path)
if 'model_state_dict' in checkpoint.keys():
model.load_state_dict(checkpoint['model_state_dict'])
else:
model.load_state_dict(checkpoint)
model.eval()
model.to(device)
# get input
dataset_dict = parse_dataset_txt(dataset_txt)
num_images = len(dataset_dict["basenames"])
# create output folder
os.makedirs(output_path, exist_ok=True)
if output_list:
fout = open(output_list, "w")
print("start processing")
np.random.seed(0)
for ind, basename in enumerate(dataset_dict["basenames"]):
img_name = os.path.join(input_path, basename)
print(" processing {} ({}/{})".format(img_name, ind + 1, num_images))
# input
img = utils.read_image(img_name)
if mask_path:
mask_name = img_name.replace(input_path, mask_path).replace(".jpg",".png")
mask = cv2.imread(mask_name, 0)
preds = []
for _ in range(args.it):
if mask_path:
if args.it == 1:
color = np.array([0.5, 0.5, 0.5])
else:
color = np.random.random([3])
for cls in cls2mask:
img[mask == cls] = color
img_input = transform({"image": img})["image"]
# compute
with torch.no_grad():
sample = torch.from_numpy(img_input).to(device).unsqueeze(0)
prediction = model.forward(sample)
if save_full:
prediction = (
torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size=img.shape[:2],
mode="bicubic",
align_corners=False,
)
.squeeze()
.cpu()
.numpy()
)
else:
prediction = prediction.squeeze().cpu().numpy()
preds.append(prediction)
prediction = np.median(np.stack(preds,axis=0), axis=0)
output_dir = os.path.join(output_path, os.path.dirname(basename))
os.makedirs(output_dir, exist_ok=True)
filename = os.path.join(output_dir, os.path.splitext(os.path.basename(img_name))[0])
np.save(filename, prediction.astype(np.float32))
if output_list:
fout.write(img_name + " " + filename + ".npy\n")
utils.write_depth(filename, prediction, bytes=2)
print("finished")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_path',
default='input',
help='folder with images'
)
parser.add_argument('--dataset_txt',
default='dataset.txt',
help='dataset txt file',
)
parser.add_argument('--mask_path',
default='',
help='folder with mask images'
)
parser.add_argument('--cls2mask',
default=[1],
type=int,
nargs='+',
help='classes to mask'
)
parser.add_argument('--it',
default=1,
type=int,
help="number of iteration to run midas"
)
parser.add_argument('-o', '--output_path',
default='output',
help='folder for output images'
)
parser.add_argument('--output_list',
default='',
help='output list of generated depths as txt file'
)
parser.add_argument('--save_full_res',
action='store_true',
help='save original resolution'
)
parser.add_argument('-m', '--model_weights',
default=None,
help='path to the trained weights of model'
)
parser.add_argument('-t', '--model_type',
default='dpt_large',
help='model type: dpt_large, midas_v21'
)
args = parser.parse_args()
default_models = {
"midas_v21": "weights/Base/midas_v21-base.pt",
"dpt_large": "weights/Base/dpt_large-base.pt",
}
if args.model_weights is None:
args.model_weights = default_models[args.model_type]
# set torch options
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
print(args)
# compute depth maps
run(args.input_path, args.output_path, args.dataset_txt, args.model_weights, args.model_type, save_full=args.save_full_res, mask_path=args.mask_path, cls2mask=args.cls2mask, it=args.it, output_list=args.output_list)