-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
155 lines (118 loc) · 4.6 KB
/
inference.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
import argparse
import logging
import os
import glob
import time
import numpy as np
import torch
import torch.nn.functional as F
#from PIL import Image
import imageio
from torchvision import transforms
from unet import UNet
from utils.dataset import BasicDataset
def predict_img(net,
full_img,
device,
scale_factor=1,
out_threshold=0.5):
net.eval()
img = torch.from_numpy(BasicDataset.preprocess(full_img, scale_factor))
img = img.unsqueeze(0)
img = img.to(device=device, dtype=torch.float32)
with torch.no_grad():
output = net(img)
probs = F.softmax(output, dim=1)
probs = probs.squeeze(0)
tf = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize(full_img.shape[1]),
transforms.ToTensor()
]
)
probs = tf(probs.cpu())
full_mask = probs.squeeze().cpu().numpy()
return full_mask > out_threshold
def get_args():
parser = argparse.ArgumentParser(description='Predict masks from input images',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--model', '-m', default='MODEL.pth',
metavar='FILE',
help="Specify the file in which the model is stored")
parser.add_argument('--no-save', '-n', action='store_true',
help="Do not save the output masks",
default=False)
parser.add_argument('--mask-threshold', '-t', type=float,
help="Minimum probability value to consider a mask pixel white",
default=0.5)
return parser.parse_args()
def get_output_filenames(in_files):
out_files = []
for f in in_files:
pathsplit = os.path.splitext(f)
out_files.append("{}_OUT{}".format(pathsplit[0].replace("/images/or/","/detections/"), pathsplit[1]))
return out_files
def mask_to_image(mask):
return np.argmax(mask * 255, axis=0).astype(np.uint8)
def toRGB(predicted_classes, width, height):
predicted_rgb = np.zeros((width, height, 3))
for ii in range(width):
for jj in range(height):
if predicted_classes[ii,jj] == 0:
predicted_rgb[ii,jj,0] = 240
predicted_rgb[ii,jj,1] = 228
predicted_rgb[ii,jj,2] = 66
elif predicted_classes[ii,jj] == 1:
predicted_rgb[ii,jj,0] = 86
predicted_rgb[ii,jj,1] = 180
predicted_rgb[ii,jj,2] = 233
elif predicted_classes[ii,jj] == 2:
predicted_rgb[ii,jj,0] = 0
predicted_rgb[ii,jj,1] = 158
predicted_rgb[ii,jj,2] = 115
elif predicted_classes[ii,jj] == 3:
predicted_rgb[ii,jj,0] = 0
predicted_rgb[ii,jj,1] = 0
predicted_rgb[ii,jj,2] = 0
else:
predicted_rgb[ii,jj,0] = 0
predicted_rgb[ii,jj,1] = 255
predicted_rgb[ii,jj,2] = 0
predicted_rgb = predicted_rgb.astype(np.uint8)
return predicted_rgb
if __name__ == "__main__":
args = get_args()
in_files = glob.glob( './images/or/*.tif')
out_files = get_output_filenames(in_files)
net = UNet(n_channels=10, n_classes=4, bilinear=False)
logging.info("Loading model {}".format(args.model))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logging.info(f'Using device {device}')
net.to(device=device)
net.load_state_dict(torch.load(args.model, map_location=device))
logging.info("Model loaded !")
start = time.time()
for i, fn in enumerate(in_files):
print(fn)
logging.info("\nPredicting image {} ...".format(fn))
# Open image
img = imageio.imread(fn)
# Predict image
startpred= time.time()
mask = predict_img(net=net,
full_img=img,
scale_factor=1.0,
out_threshold=args.mask_threshold,
device=device)
endpred=time.time()
print("\n\t time:",str(endpred-startpred))
# Save grayscale and rgb images
out_fn = out_files[i].replace('.tif','.png')
result = mask_to_image(mask)
print(out_fn, result.shape)
imageio.imwrite(out_fn,result)
imageio.imwrite(out_fn,toRGB(result, 256,256))
logging.info("Mask saved to {}".format(out_fn))
end = time.time()
print("Tiempo predicciones: ",str(end-start))