Spaces:
Running
Running
Update inference_utils.py
Browse files- inference_utils.py +25 -114
inference_utils.py
CHANGED
|
@@ -1,133 +1,44 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
import cv2
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
from cv2_utils import getContours
|
| 10 |
-
import torchvision.transforms as transforms
|
| 11 |
-
from models.deepcrack_model import DeepCrackModel # 너가 만든 deepcrack_model.py 참고
|
| 12 |
-
|
| 13 |
-
def tensor2im(input_image, imtype=np.uint8):
|
| 14 |
-
""""Converts a Tensor array into a numpy image array.
|
| 15 |
-
|
| 16 |
-
Parameters:
|
| 17 |
-
input_image (tensor) -- the input image tensor array
|
| 18 |
-
imtype (type) -- the desired type of the converted numpy array
|
| 19 |
-
"""
|
| 20 |
-
if not isinstance(input_image, np.ndarray):
|
| 21 |
-
if isinstance(input_image, torch.Tensor): # get the data from a variable
|
| 22 |
-
image_tensor = input_image.data
|
| 23 |
-
else:
|
| 24 |
-
return input_image
|
| 25 |
-
image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array
|
| 26 |
-
if image_numpy.shape[0] == 1: # grayscale to RGB
|
| 27 |
-
image_numpy = np.tile(image_numpy, (3, 1, 1))
|
| 28 |
-
image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling
|
| 29 |
-
else: # if it is a numpy array, do nothing
|
| 30 |
-
image_numpy = input_image
|
| 31 |
-
return image_numpy.astype(imtype)
|
| 32 |
-
|
| 33 |
-
def bytes_to_array(b: bytes) -> np.ndarray:
|
| 34 |
-
np_bytes = BytesIO(b)
|
| 35 |
-
return np.load(np_bytes, allow_pickle=True)
|
| 36 |
-
|
| 37 |
-
def read_image(bytesImg, dim=(256, 256)): #Decode Bytes to array
|
| 38 |
-
img_transforms = transforms.Compose([transforms.ToTensor(),
|
| 39 |
-
transforms.Normalize((0.5, 0.5, 0.5),
|
| 40 |
-
(0.5, 0.5, 0.5))])
|
| 41 |
-
img = np.fromstring(bytesImg, np.uint8)
|
| 42 |
-
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
|
| 43 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 44 |
-
|
| 45 |
-
# adjust the image size
|
| 46 |
-
w, h = dim
|
| 47 |
-
if w > 0 or h > 0:
|
| 48 |
-
img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)
|
| 49 |
-
|
| 50 |
-
# apply the transform to both A and B
|
| 51 |
-
img = img_transforms(Image.fromarray(img.copy()))
|
| 52 |
-
return img
|
| 53 |
|
| 54 |
def create_model(opt, cp_path='pretrained_net_G.pth'):
|
| 55 |
-
model = DeepCrackModel(opt)
|
| 56 |
-
checkpoint = torch.load(cp_path)
|
| 57 |
-
|
|
|
|
| 58 |
model.netG.module.load_state_dict(checkpoint, strict=False)
|
| 59 |
else:
|
| 60 |
model.netG.load_state_dict(checkpoint, strict=False)
|
|
|
|
| 61 |
model.eval()
|
| 62 |
return model
|
| 63 |
|
| 64 |
-
def overlay(
|
| 65 |
-
image: np.ndarray,
|
| 66 |
-
mask: np.ndarray,
|
| 67 |
-
color = (255, 0, 0),
|
| 68 |
-
alpha: float = 0.5,
|
| 69 |
-
resize = (256, 256)
|
| 70 |
-
) -> np.ndarray:
|
| 71 |
-
"""Combines image and its segmentation mask into a single image.
|
| 72 |
-
|
| 73 |
-
Params:
|
| 74 |
-
image: Training image.
|
| 75 |
-
mask: Segmentation mask.
|
| 76 |
-
color: Color for segmentation mask rendering.
|
| 77 |
-
alpha: Segmentation mask's transparency.
|
| 78 |
-
resize: If provided, both image and its mask are resized before blending them together.
|
| 79 |
-
|
| 80 |
-
Returns:
|
| 81 |
-
image_combined: The combined image.
|
| 82 |
-
|
| 83 |
-
"""
|
| 84 |
-
color = np.asarray(color).reshape(1, 1, 3)
|
| 85 |
-
colored_mask = np.expand_dims(mask, 0).repeat(3, axis=2)
|
| 86 |
-
masked = np.ma.MaskedArray(image, mask=colored_mask, fill_value=color)
|
| 87 |
-
image_overlay = masked.filled()
|
| 88 |
-
|
| 89 |
-
if resize is not None:
|
| 90 |
-
image = cv2.resize(image, resize)
|
| 91 |
-
image_overlay = cv2.resize(image_overlay, resize)
|
| 92 |
-
|
| 93 |
-
image_combined = cv2.addWeighted(image, 1 - alpha, image_overlay, alpha, 0)
|
| 94 |
-
|
| 95 |
-
return image_combined
|
| 96 |
|
| 97 |
-
def
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
model.test() # run inference
|
| 106 |
-
visuals = model.get_current_visuals() # get image results
|
| 107 |
-
confidence = visuals['fused'].max()
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
visuals[key] = tensor2im(visuals[key])
|
| 112 |
-
|
| 113 |
-
h, w, _ = visuals['fused'].shape
|
| 114 |
-
fused = Image.fromarray(visuals['fused'])
|
| 115 |
-
fused = np.array(fused, dtype='uint8')
|
| 116 |
-
realHeight=dim[1]
|
| 117 |
-
realWidth=dim[0]
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
mask[mask >= 90] = 255
|
| 122 |
-
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL,
|
| 123 |
-
cv2.CHAIN_APPROX_SIMPLE)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
contour_img = getContours(fused, overlay_img, realHeight, realWidth, unit, confidence)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from models.deepcrack_model import DeepCrackModel
|
| 7 |
from cv2_utils import getContours
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def create_model(opt, cp_path='pretrained_net_G.pth'):
|
| 10 |
+
model = DeepCrackModel(opt)
|
| 11 |
+
checkpoint = torch.load(cp_path, map_location="cpu")
|
| 12 |
+
|
| 13 |
+
if hasattr(model.netG, "module"):
|
| 14 |
model.netG.module.load_state_dict(checkpoint, strict=False)
|
| 15 |
else:
|
| 16 |
model.netG.load_state_dict(checkpoint, strict=False)
|
| 17 |
+
|
| 18 |
model.eval()
|
| 19 |
return model
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def preprocess(img: Image.Image):
|
| 23 |
+
transform = transforms.Compose([
|
| 24 |
+
transforms.Resize((256, 256)),
|
| 25 |
+
transforms.ToTensor(),
|
| 26 |
+
transforms.Normalize((0.5,), (0.5,))
|
| 27 |
+
])
|
| 28 |
+
return transform(img).unsqueeze(0)
|
| 29 |
+
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def inference(model, img: Image.Image):
|
| 32 |
+
tensor = preprocess(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
model.set_input({"image": tensor, "label": torch.zeros_like(tensor), "A_paths": ""})
|
| 35 |
+
model.test()
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
visuals = model.get_current_visuals()
|
| 38 |
+
fused = visuals["fused"].detach().cpu().numpy()[0]
|
| 39 |
+
confidence = float(fused.max())
|
|
|
|
| 40 |
|
| 41 |
+
fused_img = (fused * 255).astype("uint8")
|
| 42 |
+
contour_img = getContours(fused_img, np.array(img), img.size[1], img.size[0], "px", confidence)
|
| 43 |
|
| 44 |
+
return contour_img, confidence
|