small impovements
BIN
mmp/a4/.output/2244905_transformed.png
Normal file
|
After Width: | Height: | Size: 184 KiB |
BIN
mmp/a4/.output/2246530_transformed.png
Normal file
|
After Width: | Height: | Size: 85 KiB |
BIN
mmp/a4/.output/2248597_transformed.png
Normal file
|
After Width: | Height: | Size: 138 KiB |
BIN
mmp/a4/.output/2249347_transformed.png
Normal file
|
After Width: | Height: | Size: 197 KiB |
BIN
mmp/a4/.output/2253259_transformed.png
Normal file
|
After Width: | Height: | Size: 224 KiB |
BIN
mmp/a4/.output/2257498_transformed.png
Normal file
|
After Width: | Height: | Size: 233 KiB |
BIN
mmp/a4/.output/2257580_transformed.png
Normal file
|
After Width: | Height: | Size: 150 KiB |
BIN
mmp/a4/.output/2260011_transformed.png
Normal file
|
After Width: | Height: | Size: 238 KiB |
BIN
mmp/a4/.output/2260743_transformed.png
Normal file
|
After Width: | Height: | Size: 223 KiB |
BIN
mmp/a4/.output/2262101_transformed.png
Normal file
|
After Width: | Height: | Size: 190 KiB |
BIN
mmp/a4/.output/2263691_transformed.png
Normal file
|
After Width: | Height: | Size: 84 KiB |
BIN
mmp/a4/.output/2264479_transformed.png
Normal file
|
After Width: | Height: | Size: 131 KiB |
|
Before Width: | Height: | Size: 285 KiB |
|
Before Width: | Height: | Size: 71 KiB |
|
Before Width: | Height: | Size: 429 KiB |
|
Before Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 252 KiB |
|
Before Width: | Height: | Size: 37 KiB |
|
Before Width: | Height: | Size: 192 KiB |
|
Before Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 150 KiB |
|
Before Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 243 KiB |
|
Before Width: | Height: | Size: 56 KiB |
|
Before Width: | Height: | Size: 432 KiB |
|
Before Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 234 KiB |
|
Before Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 274 KiB |
|
Before Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 349 KiB |
|
Before Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 257 KiB |
|
Before Width: | Height: | Size: 51 KiB |
@@ -5,7 +5,7 @@ import numpy as np
|
|||||||
import torch
|
import torch
|
||||||
from torch.utils.data import DataLoader
|
from torch.utils.data import DataLoader
|
||||||
from ..a3.annotation import read_groundtruth_file, AnnotationRect
|
from ..a3.annotation import read_groundtruth_file, AnnotationRect
|
||||||
from .label_grid import get_label_grid, draw_annotation_rects
|
from .label_grid import get_label_grid, iou
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import matplotlib.patches as patches
|
import matplotlib.patches as patches
|
||||||
from .anchor_grid import get_anchor_grid
|
from .anchor_grid import get_anchor_grid
|
||||||
@@ -33,6 +33,7 @@ class MMP_Dataset(torch.utils.data.Dataset):
|
|||||||
self.anchor_grid = anchor_grid
|
self.anchor_grid = anchor_grid
|
||||||
self.min_iou = min_iou
|
self.min_iou = min_iou
|
||||||
self.is_test = is_test
|
self.is_test = is_test
|
||||||
|
self.path_to_data = path_to_data
|
||||||
img_pattern = re.compile(r"^(\d+)\.jpg$")
|
img_pattern = re.compile(r"^(\d+)\.jpg$")
|
||||||
files = set(os.listdir(path_to_data))
|
files = set(os.listdir(path_to_data))
|
||||||
|
|
||||||
@@ -126,15 +127,38 @@ def calculate_max_coverage(loader: DataLoader, min_iou: float) -> float:
|
|||||||
@param min_iou: Minimum IoU overlap that is required to count a ground truth box as covered.
|
@param min_iou: Minimum IoU overlap that is required to count a ground truth box as covered.
|
||||||
@return: Ratio of how mamy ground truth boxes are covered by a label grid box. Must be a value between 0 and 1.
|
@return: Ratio of how mamy ground truth boxes are covered by a label grid box. Must be a value between 0 and 1.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
total_boxes = 0
|
||||||
|
covered_boxes = 0
|
||||||
|
dataset: MMP_Dataset = loader.dataset
|
||||||
|
anchor_grid = dataset.anchor_grid
|
||||||
|
|
||||||
|
for img, _, img_id in islice(loader, 4):
|
||||||
|
for batch_index in range(len(img)):
|
||||||
|
gts_file = os.path.join(
|
||||||
|
dataset.path_to_data,
|
||||||
|
f"{str(img_id[batch_index].item()).zfill(8)}.gt_data.txt",
|
||||||
|
)
|
||||||
|
|
||||||
|
gts = read_groundtruth_file(gts_file)
|
||||||
|
total_boxes += len(gts)
|
||||||
|
for annotation in gts:
|
||||||
|
for box_idx in np.ndindex(anchor_grid.shape[:-1]):
|
||||||
|
box_annotation = AnnotationRect.fromarray(anchor_grid[box_idx])
|
||||||
|
calculated_iou = iou(annotation, box_annotation)
|
||||||
|
if calculated_iou >= min_iou:
|
||||||
|
covered_boxes += 1
|
||||||
|
break
|
||||||
|
return covered_boxes / total_boxes
|
||||||
|
|
||||||
|
|
||||||
def print_img_tensor_with_annotations(
|
def draw_image_tensor_with_annotations(
|
||||||
img: torch.Tensor, annotations: Sequence["AnnotationRect"], output_file: str
|
img: torch.Tensor,
|
||||||
|
annotations: Sequence["AnnotationRect"] | None,
|
||||||
|
output_file: str,
|
||||||
):
|
):
|
||||||
# Convert tensor to numpy, permute dimensions
|
# Convert tensor to numpy, permute dimensions
|
||||||
img_np = img.permute(1, 2, 0).cpu().numpy()
|
img_np = img.permute(1, 2, 0).numpy()
|
||||||
img_np = img_np.astype(np.uint8)
|
img_np = np.clip(img_np, 0, 1)
|
||||||
|
|
||||||
fig, ax = plt.subplots(1)
|
fig, ax = plt.subplots(1)
|
||||||
ax.imshow(img_np)
|
ax.imshow(img_np)
|
||||||
@@ -152,27 +176,30 @@ def print_img_tensor_with_annotations(
|
|||||||
plt.close(fig)
|
plt.close(fig)
|
||||||
|
|
||||||
|
|
||||||
def print_positive_boxes(
|
def denormalize_image_tensor(
|
||||||
|
img: torch.Tensor,
|
||||||
|
mean=torch.tensor([0.485, 0.456, 0.406]).view(-1, 1, 1),
|
||||||
|
std=torch.tensor([0.229, 0.224, 0.225]).view(-1, 1, 1),
|
||||||
|
) -> torch.Tensor:
|
||||||
|
img_denormalized = img * std + mean
|
||||||
|
return img_denormalized
|
||||||
|
|
||||||
|
|
||||||
|
def draw_positive_boxes(
|
||||||
img_tensor: torch.Tensor,
|
img_tensor: torch.Tensor,
|
||||||
label_grid: np.ndarray,
|
label_grid: np.ndarray,
|
||||||
img_id: torch.Tensor,
|
img_id: torch.Tensor,
|
||||||
anchor_grid: np.ndarray,
|
anchor_grid: np.ndarray,
|
||||||
path_to_data: str,
|
|
||||||
):
|
):
|
||||||
annotations = [
|
annotations = [
|
||||||
AnnotationRect.fromarray(anchor_grid[idx])
|
AnnotationRect.fromarray(anchor_grid[idx])
|
||||||
for idx in np.ndindex(anchor_grid.shape[:-1])
|
for idx in np.ndindex(anchor_grid.shape[:-1])
|
||||||
if label_grid[idx]
|
if label_grid[idx]
|
||||||
]
|
]
|
||||||
print_img_tensor_with_annotations(
|
draw_image_tensor_with_annotations(
|
||||||
img_tensor,
|
img_tensor,
|
||||||
annotations=annotations,
|
annotations=annotations,
|
||||||
output_file=f"mmp/a4/{img_id}_transformed.png",
|
output_file=f"mmp/a4/.output/{img_id}_transformed.png",
|
||||||
)
|
|
||||||
draw_annotation_rects(
|
|
||||||
annotations=annotations,
|
|
||||||
image=f"{os.path.join(path_to_data, f'{str(img_id.item()).zfill(8)}.jpg')}",
|
|
||||||
output_path=f"mmp/a4/{img_id}_original.png",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -195,12 +222,11 @@ def main():
|
|||||||
)
|
)
|
||||||
|
|
||||||
for img, label, img_id in islice(dataloader, 12):
|
for img, label, img_id in islice(dataloader, 12):
|
||||||
print_positive_boxes(
|
draw_positive_boxes(
|
||||||
img_tensor=img[5],
|
img_tensor=denormalize_image_tensor(img=img[5]),
|
||||||
label_grid=label[5],
|
label_grid=label[5],
|
||||||
img_id=img_id[5],
|
img_id=img_id[5],
|
||||||
anchor_grid=anchor_grid,
|
anchor_grid=anchor_grid,
|
||||||
path_to_data=".data/mmp-public-3.2/train",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||