-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictMulti.py
executable file
·179 lines (137 loc) · 6.26 KB
/
predictMulti.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
import argparse
import logging
import os
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
from torchvision import transforms
import sys
sys.path.append('../pytorch-unet')
from unet import UNet
from data_vis import plot_img_and_mask
from dataset import BasicDataset
# python3 predictMulti.py --model checkpoints_WF_adult_large_small_with_aphids_warmRestarts/CP_epoch25.pth --input /home/rob/Pictures/LesBargefieldstation/FullSize/*.jpg --scale 1 --mask-threshold 0.65 --path /home/rob/Pictures/LesBargefieldstation/FullSize/
def get_mask(net_output, resize) :
probs = net_output # we removed the extra sigmoid
probs = probs.squeeze(0)
tf = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize(resize),
transforms.ToTensor()
]
)
probs = tf(probs.cpu())
full_mask = probs.squeeze().cpu().numpy()
return full_mask
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)
img_size = img.size()
print(img_size)
crop_number = 10
img_size = (img_size[2], img_size[3]) # height and width as tuple
if img_size[0]>2048 and img_size[1]>2048 :
full_mask = torch.zeros(img.size())
crop_x = img_size[0]//crop_number # a double // converts to an int from the double
print(img_size)
crop_y = img_size[1]//crop_number
with torch.no_grad():
for x in range(crop_number) :
for y in range(crop_number) :
#print(str(crop_x))
full_mask[:,:,x*crop_x : x*crop_x+crop_x, y*crop_y : y*crop_y+crop_y] = net(img[:,:,x*crop_x : x*crop_x+crop_x, y*crop_y : y*crop_y+crop_y] )
else :
full_mask = net(img)
full_mask= get_mask(full_mask, full_img.size[1])
print(full_mask.max())
return full_mask
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('--input', '-i', metavar='INPUT', nargs='+',
help='filenames of input images', required=True)
parser.add_argument('--path', '-p', metavar='INPUT', nargs='+',
help='Path of ouput images') # RJL put this in to specify path when outputting multiple files so not hard wired in code
parser.add_argument('--output', '-o', metavar='INPUT', nargs='+',
help='Filenames of ouput images')
parser.add_argument('--viz', '-v', action='store_true',
help="Visualize the images as they are processed",
default=False)
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)
parser.add_argument('--scale', '-s', type=float,
help="Scale factor for the input images",
default=0.5)
return parser.parse_args()
def get_output_filenames(args):
in_files = args.input
output_path = args.path[0] # RJL read the path in from args
out_files = []
if not args.output:
for f in in_files:
#pathsplit = os.path.splitext(f)
fname = os.path.basename(f)
#out_files.append("{}_OUT{}".format(pathsplit[0], pathsplit[1]))
out_files.append("{}_OUT{}".format(output_path, fname)) # outputs all files to a set folder
elif len(in_files) != len(args.output):
logging.error("Input files and output files are not of the same length")
raise SystemExit()
else:
out_files = args.output
return out_files
def mask_to_image(mask):
return Image.fromarray((mask * 255).astype(np.uint8))
if __name__ == "__main__":
args = get_args()
in_files = args.input
out_files = get_output_filenames(args)
net = UNet(n_channels=3, n_classes=3)
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 !")
for i, fn in enumerate(in_files): # can input multiple files
logging.info("\nPredicting image {} ...".format(fn))
img = Image.open(fn)
mask = predict_img(net=net,
full_img=img,
scale_factor=args.scale,
out_threshold=args.mask_threshold,
device=device)
if not args.no_save:
out_fn = out_files[i]
# get mask dimensions for rgb output image
arrayWidth = mask.shape[1]
arrayHeight = mask.shape[2]
# make output rgb to be filled with mask channels
result_rgb = Image.new("RGB", [arrayWidth, arrayHeight], 255)
result_r = mask_to_image(mask[0,:,:])
result_g = mask_to_image(mask[1,:,:])
result_b = mask_to_image(mask[2,:,:])
result_r.save(out_files[i] + '0.png')
result_g.save(out_files[i] + '1.png')
result_b.save(out_files[i] + '2.png')
# use the split and merge commands to unpack and pack rgb images
result_rgb = Image.merge('RGB', (result_r, result_g, result_b))
result_rgb.save(out_files[i] + 'ALL.png') # save out rgb mask
logging.info("Mask saved to {}".format(out_files[i]))
if args.viz:
logging.info("Visualizing results for image {}, close to continue ...".format(fn))
plot_img_and_mask(img, mask)