2018-04-09 03:15:24 +00:00
|
|
|
import argparse
|
2019-10-24 19:37:21 +00:00
|
|
|
import logging
|
2018-06-08 17:27:32 +00:00
|
|
|
import os
|
2018-04-09 03:15:24 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
import numpy as np
|
2017-08-21 16:00:07 +00:00
|
|
|
import torch
|
2018-06-08 17:27:32 +00:00
|
|
|
from PIL import Image
|
2019-10-24 19:37:21 +00:00
|
|
|
from torchvision import transforms
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2017-11-30 05:45:19 +00:00
|
|
|
from unet import UNet
|
2018-06-08 17:27:32 +00:00
|
|
|
from utils import plot_img_and_mask
|
2019-10-24 19:37:21 +00:00
|
|
|
from utils import resize_and_crop, normalize, hwc_to_chw, dense_crf
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2018-06-17 06:31:42 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
def predict_img(net,
|
|
|
|
full_img,
|
2019-10-24 19:37:21 +00:00
|
|
|
device,
|
|
|
|
scale_factor=1,
|
2018-06-08 17:27:32 +00:00
|
|
|
out_threshold=0.5,
|
2019-10-24 19:37:21 +00:00
|
|
|
use_dense_crf=False):
|
2018-09-26 06:59:49 +00:00
|
|
|
net.eval()
|
2018-06-08 17:27:32 +00:00
|
|
|
img_height = full_img.size[1]
|
|
|
|
img_width = full_img.size[0]
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
img = resize_and_crop(full_img, scale=scale_factor)
|
|
|
|
img = normalize(img)
|
2019-10-24 19:37:21 +00:00
|
|
|
img = hwc_to_chw(img)
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
X = torch.from_numpy(img).unsqueeze(0)
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
X = X.to(device=device)
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
with torch.no_grad():
|
2019-10-24 19:37:21 +00:00
|
|
|
output = net(X)
|
|
|
|
probs = output.squeeze(0)
|
2018-06-17 06:31:42 +00:00
|
|
|
|
|
|
|
tf = transforms.Compose(
|
|
|
|
[
|
|
|
|
transforms.ToPILImage(),
|
|
|
|
transforms.Resize(img_height),
|
|
|
|
transforms.ToTensor()
|
|
|
|
]
|
|
|
|
)
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
probs = tf(probs.cpu())
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
full_mask = probs.squeeze().cpu().numpy()
|
2018-06-08 17:27:32 +00:00
|
|
|
|
|
|
|
if use_dense_crf:
|
|
|
|
full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)
|
|
|
|
|
|
|
|
return full_mask > out_threshold
|
|
|
|
|
|
|
|
|
|
|
|
def get_args():
|
2019-10-24 19:37:21 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Predict masks from input images',
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
2017-11-30 05:45:19 +00:00
|
|
|
parser.add_argument('--model', '-m', default='MODEL.pth',
|
|
|
|
metavar='FILE',
|
2019-10-24 19:37:21 +00:00
|
|
|
help="Specify the file in which the model is stored")
|
2018-06-23 17:57:53 +00:00
|
|
|
parser.add_argument('--input', '-i', metavar='INPUT', nargs='+',
|
|
|
|
help='filenames of input images', required=True)
|
2018-06-17 06:31:42 +00:00
|
|
|
|
2017-11-30 05:45:19 +00:00
|
|
|
parser.add_argument('--output', '-o', metavar='INPUT', nargs='+',
|
2019-10-24 19:37:21 +00:00
|
|
|
help='Filenames of ouput images')
|
2017-11-30 05:45:19 +00:00
|
|
|
parser.add_argument('--viz', '-v', action='store_true',
|
|
|
|
help="Visualize the images as they are processed",
|
|
|
|
default=False)
|
2018-07-10 02:19:03 +00:00
|
|
|
parser.add_argument('--no-save', '-n', action='store_true',
|
2017-11-30 05:45:19 +00:00
|
|
|
help="Do not save the output masks",
|
|
|
|
default=False)
|
2018-06-08 17:27:32 +00:00
|
|
|
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)
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
return parser.parse_args()
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
def get_output_filenames(args):
|
2017-11-30 05:45:19 +00:00
|
|
|
in_files = args.input
|
|
|
|
out_files = []
|
2018-06-08 17:27:32 +00:00
|
|
|
|
2017-11-30 05:45:19 +00:00
|
|
|
if not args.output:
|
|
|
|
for f in in_files:
|
|
|
|
pathsplit = os.path.splitext(f)
|
|
|
|
out_files.append("{}_OUT{}".format(pathsplit[0], pathsplit[1]))
|
|
|
|
elif len(in_files) != len(args.output):
|
2019-10-24 19:37:21 +00:00
|
|
|
logging.error("Input files and output files are not of the same length")
|
2017-11-30 05:45:19 +00:00
|
|
|
raise SystemExit()
|
|
|
|
else:
|
|
|
|
out_files = args.output
|
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
return out_files
|
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
def mask_to_image(mask):
|
|
|
|
return Image.fromarray((mask * 255).astype(np.uint8))
|
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
args = get_args()
|
|
|
|
in_files = args.input
|
|
|
|
out_files = get_output_filenames(args)
|
|
|
|
|
|
|
|
net = UNet(n_channels=3, n_classes=1)
|
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
logging.info("Loading model {}".format(args.model))
|
2018-06-08 17:27:32 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
logging.info(f'Using device {device}')
|
|
|
|
net.to(deviec=device)
|
|
|
|
net.load_state_dict(torch.load(args.model, map_location=device))
|
2018-06-08 17:27:32 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
logging.info("Model loaded !")
|
2017-11-30 05:45:19 +00:00
|
|
|
|
|
|
|
for i, fn in enumerate(in_files):
|
2019-10-24 19:37:21 +00:00
|
|
|
logging.info("\nPredicting image {} ...".format(fn))
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
img = Image.open(fn)
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
mask = predict_img(net=net,
|
|
|
|
full_img=img,
|
|
|
|
scale_factor=args.scale,
|
|
|
|
out_threshold=args.mask_threshold,
|
2019-10-24 19:37:21 +00:00
|
|
|
use_dense_crf=False,
|
|
|
|
device=device)
|
2017-11-30 05:45:19 +00:00
|
|
|
|
|
|
|
if not args.no_save:
|
|
|
|
out_fn = out_files[i]
|
2018-06-08 17:27:32 +00:00
|
|
|
result = mask_to_image(mask)
|
2017-11-30 05:45:19 +00:00
|
|
|
result.save(out_files[i])
|
2018-06-08 17:27:32 +00:00
|
|
|
|
2019-10-24 19:37:21 +00:00
|
|
|
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)
|