add model and example
Browse files- .gitattributes +1 -0
- dino_hatching.pt +3 -0
- example/inference.py +98 -0
- example/legend_correct.png +0 -0
- example/legend_wrong.png +0 -0
- example/wall.png +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
dino_hatching.pt filter=lfs diff=lfs merge=lfs -text
|
dino_hatching.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:55359bcfd93e4952185eaefd51afbd03ff62a8666b21b88ccf8473258ac95b1f
|
| 3 |
+
size 351351779
|
example/inference.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import transforms
|
| 7 |
+
|
| 8 |
+
input_data = {
|
| 9 |
+
"legends": ["legend_correct.png", "legend_wrong.png"],
|
| 10 |
+
"plan_image": "wall.png",
|
| 11 |
+
"plan_obb": [
|
| 12 |
+
20.672607421875, # x1
|
| 13 |
+
20.71624755859375, # y1
|
| 14 |
+
42.37445068359375, # x2
|
| 15 |
+
20.71624755859375, # y2
|
| 16 |
+
42.37445068359375, # ...
|
| 17 |
+
111.15782165527344,
|
| 18 |
+
20.672607421875,
|
| 19 |
+
111.15782165527344
|
| 20 |
+
] # mask bbox for wall image
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
IMAGE_SIZE = 518
|
| 25 |
+
|
| 26 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
+
|
| 28 |
+
image_tf = transforms.Compose([
|
| 29 |
+
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
|
| 30 |
+
transforms.ToTensor(),
|
| 31 |
+
transforms.Normalize(
|
| 32 |
+
mean=[0.485, 0.456, 0.406],
|
| 33 |
+
std=[0.229, 0.224, 0.225],
|
| 34 |
+
),
|
| 35 |
+
])
|
| 36 |
+
|
| 37 |
+
mask_tf = transforms.Compose([
|
| 38 |
+
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
|
| 39 |
+
transforms.ToTensor(),
|
| 40 |
+
])
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def create_full_mask(size):
|
| 44 |
+
return Image.new("L", size, 255)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def create_obb_mask(size, obb):
|
| 48 |
+
w, h = size
|
| 49 |
+
|
| 50 |
+
points = np.array(obb, dtype=np.int32).reshape(4, 2)
|
| 51 |
+
|
| 52 |
+
mask = np.zeros((h, w), dtype=np.uint8)
|
| 53 |
+
cv2.fillPoly(mask, [points], 255)
|
| 54 |
+
|
| 55 |
+
return Image.fromarray(mask)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def prepare(image_path, obb=None):
|
| 59 |
+
image = Image.open(image_path).convert("RGB")
|
| 60 |
+
|
| 61 |
+
if obb is None:
|
| 62 |
+
mask = create_full_mask(image.size)
|
| 63 |
+
else:
|
| 64 |
+
mask = create_obb_mask(image.size, obb)
|
| 65 |
+
|
| 66 |
+
image = image_tf(image).unsqueeze(0).to(device)
|
| 67 |
+
mask = mask_tf(mask).unsqueeze(0).to(device)
|
| 68 |
+
|
| 69 |
+
return image, mask
|
| 70 |
+
|
| 71 |
+
for legend in input_data["legends"]:
|
| 72 |
+
legend_image, legend_mask = prepare(legend)
|
| 73 |
+
|
| 74 |
+
plan_image, plan_mask = prepare(
|
| 75 |
+
input_data["plan_image"],
|
| 76 |
+
input_data["plan_obb"],
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
model = torch.jit.load(
|
| 80 |
+
"dino_hatching.pt",
|
| 81 |
+
map_location=device,
|
| 82 |
+
)
|
| 83 |
+
model.eval()
|
| 84 |
+
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
logit = model(
|
| 87 |
+
legend_image,
|
| 88 |
+
legend_mask,
|
| 89 |
+
plan_image,
|
| 90 |
+
plan_mask,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
score = torch.sigmoid(logit).item()
|
| 94 |
+
|
| 95 |
+
print(f"{legend}: {score}")
|
| 96 |
+
|
| 97 |
+
# legend_correct.png: 0.9882104396820068
|
| 98 |
+
# legend_wrong.png: 0.0016661642584949732
|
example/legend_correct.png
ADDED
|
example/legend_wrong.png
ADDED
|
example/wall.png
ADDED
|