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
|
2022-07-05 10:17:32 +00:00
|
|
|
import onnx
|
|
|
|
import onnxruntime
|
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
|
|
|
|
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-07-05 10:06:12 +00:00
|
|
|
def sigmoid(x):
|
|
|
|
return 1 / (1 + np.exp(-x))
|
|
|
|
|
|
|
|
|
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-07-05 10:17:32 +00:00
|
|
|
onnx_model = onnx.load(args.model)
|
|
|
|
onnx.checker.check_model(onnx_model)
|
2022-06-29 12:15:04 +00:00
|
|
|
|
2022-07-05 10:17:32 +00:00
|
|
|
ort_session = onnxruntime.InferenceSession(args.model)
|
2022-07-05 10:06:12 +00:00
|
|
|
|
2022-07-05 10:17:32 +00:00
|
|
|
def to_numpy(tensor):
|
|
|
|
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
|
|
|
|
|
|
|
|
img = Image.open(args.input).convert("RGB")
|
|
|
|
|
|
|
|
logging.info(f"Preprocessing image {args.input}")
|
|
|
|
tf = A.Compose(
|
|
|
|
[
|
|
|
|
A.ToFloat(max_value=255),
|
|
|
|
ToTensorV2(),
|
|
|
|
],
|
2022-06-29 14:12:00 +00:00
|
|
|
)
|
2022-07-05 10:17:32 +00:00
|
|
|
aug = tf(image=np.asarray(img))
|
|
|
|
img = aug["image"]
|
|
|
|
|
|
|
|
logging.info(f"Predicting image {args.input}")
|
|
|
|
img = img.unsqueeze(0)
|
|
|
|
|
|
|
|
# compute ONNX Runtime output prediction
|
|
|
|
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(img)}
|
|
|
|
ort_outs = ort_session.run(None, ort_inputs)
|
|
|
|
|
|
|
|
img_out_y = ort_outs[0]
|
2022-06-30 14:47:28 +00:00
|
|
|
|
2022-07-05 10:17:32 +00:00
|
|
|
img_out_y = Image.fromarray(np.uint8((img_out_y[0] * 255.0).clip(0, 255)[0]), mode="L")
|
2019-10-24 19:37:21 +00:00
|
|
|
|
2022-07-05 10:17:32 +00:00
|
|
|
img_out_y.save(args.output)
|