-
Notifications
You must be signed in to change notification settings - Fork 212
/
generate_masks.py
144 lines (114 loc) · 5.31 KB
/
generate_masks.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
"""
Script generates predictions, splitting original images into tiles, and assembling prediction back together
"""
import argparse
from prepare_train_val import get_split
from dataset import RoboticsDataset
import cv2
from models import UNet16, LinkNet34, UNet11, UNet, AlbuNet
import torch
from pathlib import Path
from tqdm import tqdm
import numpy as np
import utils
import prepare_data
from torch.utils.data import DataLoader
from torch.nn import functional as F
from prepare_data import (original_height,
original_width,
h_start, w_start
)
from albumentations import Compose, Normalize
def img_transform(p=1):
return Compose([
Normalize(p=1)
], p=p)
def get_model(model_path, model_type='UNet11', problem_type='binary'):
"""
:param model_path:
:param model_type: 'UNet', 'UNet16', 'UNet11', 'LinkNet34', 'AlbuNet'
:param problem_type: 'binary', 'parts', 'instruments'
:return:
"""
if problem_type == 'binary':
num_classes = 1
elif problem_type == 'parts':
num_classes = 4
elif problem_type == 'instruments':
num_classes = 8
if model_type == 'UNet16':
model = UNet16(num_classes=num_classes)
elif model_type == 'UNet11':
model = UNet11(num_classes=num_classes)
elif model_type == 'LinkNet34':
model = LinkNet34(num_classes=num_classes)
elif model_type == 'AlbuNet':
model = AlbuNet(num_classes=num_classes)
elif model_type == 'UNet':
model = UNet(num_classes=num_classes)
state = torch.load(str(model_path))
state = {key.replace('module.', ''): value for key, value in state['model'].items()}
model.load_state_dict(state)
if torch.cuda.is_available():
return model.cuda()
model.eval()
return model
def predict(model, from_file_names, batch_size, to_path, problem_type, img_transform):
loader = DataLoader(
dataset=RoboticsDataset(from_file_names, transform=img_transform, mode='predict', problem_type=problem_type),
shuffle=False,
batch_size=batch_size,
num_workers=args.workers,
pin_memory=torch.cuda.is_available()
)
with torch.no_grad():
for batch_num, (inputs, paths) in enumerate(tqdm(loader, desc='Predict')):
inputs = utils.cuda(inputs)
outputs = model(inputs)
for i, image_name in enumerate(paths):
if problem_type == 'binary':
factor = prepare_data.binary_factor
t_mask = (F.sigmoid(outputs[i, 0]).data.cpu().numpy() * factor).astype(np.uint8)
elif problem_type == 'parts':
factor = prepare_data.parts_factor
t_mask = (outputs[i].data.cpu().numpy().argmax(axis=0) * factor).astype(np.uint8)
elif problem_type == 'instruments':
factor = prepare_data.instrument_factor
t_mask = (outputs[i].data.cpu().numpy().argmax(axis=0) * factor).astype(np.uint8)
h, w = t_mask.shape
full_mask = np.zeros((original_height, original_width))
full_mask[h_start:h_start + h, w_start:w_start + w] = t_mask
instrument_folder = Path(paths[i]).parent.parent.name
(to_path / instrument_folder).mkdir(exist_ok=True, parents=True)
cv2.imwrite(str(to_path / instrument_folder / (Path(paths[i]).stem + '.png')), full_mask)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
arg = parser.add_argument
arg('--model_path', type=str, default='data/models/UNet', help='path to model folder')
arg('--model_type', type=str, default='UNet', help='network architecture',
choices=['UNet', 'UNet11', 'UNet16', 'LinkNet34', 'AlbuNet'])
arg('--output_path', type=str, help='path to save images', default='1')
arg('--batch-size', type=int, default=4)
arg('--fold', type=int, default=-1, choices=[0, 1, 2, 3, -1], help='-1: all folds')
arg('--problem_type', type=str, default='binary', choices=['binary', 'parts', 'instruments'])
arg('--workers', type=int, default=12)
args = parser.parse_args()
if args.fold == -1:
for fold in [0, 1, 2, 3]:
_, file_names = get_split(fold)
model = get_model(str(Path(args.model_path).joinpath('model_{fold}.pt'.format(fold=fold))),
model_type=args.model_type, problem_type=args.problem_type)
print('num file_names = {}'.format(len(file_names)))
output_path = Path(args.output_path)
output_path.mkdir(exist_ok=True, parents=True)
predict(model, file_names, args.batch_size, output_path, problem_type=args.problem_type,
img_transform=img_transform(p=1))
else:
_, file_names = get_split(args.fold)
model = get_model(str(Path(args.model_path).joinpath('model_{fold}.pt'.format(fold=args.fold))),
model_type=args.model_type, problem_type=args.problem_type)
print('num file_names = {}'.format(len(file_names)))
output_path = Path(args.output_path)
output_path.mkdir(exist_ok=True, parents=True)
predict(model, file_names, args.batch_size, output_path, problem_type=args.problem_type,
img_transform=img_transform(p=1))