2018-04-09 03:15:24 +00:00
|
|
|
import argparse
|
2019-10-24 19:37:21 +00:00
|
|
|
import logging
|
2018-04-09 03:15:24 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
import albumentations as A
|
2018-06-08 17:27:32 +00:00
|
|
|
import numpy as np
|
2017-08-21 16:00:07 +00:00
|
|
|
import torch
|
2022-06-29 14:12:00 +00:00
|
|
|
from albumentations.pytorch import ToTensorV2
|
2018-06-08 17:27:32 +00:00
|
|
|
from PIL import Image
|
2017-08-21 16:00:07 +00:00
|
|
|
|
2017-11-30 05:45:19 +00:00
|
|
|
from unet import UNet
|
2018-06-17 06:31:42 +00:00
|
|
|
|
2022-06-27 13:39:44 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
def get_args():
|
2022-06-27 13:39:44 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Predict masks from input images",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--model",
|
|
|
|
"-m",
|
2022-06-29 14:12:00 +00:00
|
|
|
default="model.pth",
|
2022-06-27 13:39:44 +00:00
|
|
|
metavar="FILE",
|
|
|
|
help="Specify the file in which the model is stored",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--input",
|
|
|
|
"-i",
|
|
|
|
metavar="INPUT",
|
|
|
|
help="Filenames of input images",
|
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--output",
|
|
|
|
"-o",
|
|
|
|
metavar="OUTPUT",
|
|
|
|
help="Filenames of output images",
|
|
|
|
)
|
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
|
|
|
|
2022-06-27 13:39:44 +00:00
|
|
|
if __name__ == "__main__":
|
2018-06-08 17:27:32 +00:00
|
|
|
args = get_args()
|
|
|
|
|
2022-06-30 12:36:48 +00:00
|
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
|
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
net = UNet(n_channels=3, n_classes=1)
|
2018-06-08 17:27:32 +00:00
|
|
|
|
2022-06-27 13:39:44 +00:00
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
logging.info(f"Using device {device}")
|
2021-08-16 00:53:00 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
logging.info("Transfering model to device")
|
2019-10-30 11:27:03 +00:00
|
|
|
net.to(device=device)
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
logging.info(f"Loading model {args.model}")
|
|
|
|
net.load_state_dict(torch.load(args.model, map_location=device))
|
2022-06-29 12:15:04 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
logging.info(f"Loading image {args.input}")
|
|
|
|
img = Image.open(args.input).convert("RGB")
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
logging.info(f"Preprocessing image {args.input}")
|
|
|
|
tf = A.Compose(
|
|
|
|
[
|
|
|
|
A.ToFloat(max_value=255),
|
|
|
|
ToTensorV2(),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
aug = tf(image=np.asarray(img))
|
|
|
|
img = aug["image"]
|
2017-11-30 05:45:19 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
logging.info(f"Predicting image {args.input}")
|
2022-06-30 14:47:28 +00:00
|
|
|
img = img.unsqueeze(0).to(device=device, dtype=torch.float32)
|
|
|
|
|
|
|
|
net.eval()
|
|
|
|
with torch.inference_mode():
|
|
|
|
mask = net(img)
|
|
|
|
mask = torch.sigmoid(mask)[0]
|
|
|
|
mask = mask.cpu()
|
|
|
|
mask = mask.squeeze()
|
|
|
|
mask = mask > 0.5
|
|
|
|
mask = np.asarray(mask)
|
2019-10-24 19:37:21 +00:00
|
|
|
|
2022-06-29 14:12:00 +00:00
|
|
|
logging.info(f"Saving prediction to {args.output}")
|
2022-06-30 14:47:28 +00:00
|
|
|
mask = Image.fromarray(mask)
|
2022-06-30 12:36:48 +00:00
|
|
|
mask.save(args.output)
|