changed yolo

This commit is contained in:
2026-05-21 11:14:41 +02:00
parent db4547dc05
commit e412738d3e
6 changed files with 174 additions and 241 deletions
-65
View File
@@ -57,69 +57,4 @@ class CocoDetectionDataset(Dataset):
if self.transforms:
image = self.transforms(image)
return image, target
class CocoDetectionDatasetResized(Dataset):
def __init__(self, image_dir, annotation_path, img_size=64, transforms=None):
self.image_dir = image_dir
self.coco = COCO(annotation_path)
self.image_ids = list(self.coco.imgs.keys())
self.transforms = transforms
self.img_size = img_size
def __len__(self):
return len(self.image_ids)
def __getitem__(self, idx):
image_id = self.image_ids[idx]
image_info = self.coco.loadImgs(image_id)[0]
image_path = os.path.join(self.image_dir, image_info['file_name'])
image = Image.open(image_path).convert("RGB")
orig_w, orig_h = image.size
scale_w = self.img_size / orig_w
scale_h = self.img_size / orig_h
# Load annotations
annotation_ids = self.coco.getAnnIds(imgIds=image_id)
annotations = self.coco.loadAnns(annotation_ids)
boxes = []
labels = []
for obj in annotations:
xmin, ymin, width, height = obj['bbox']
xmin, ymin, width, height = float(xmin), float(ymin), float(width), float(height)
xmin = xmin * scale_w
ymin = ymin * scale_h
xmax = (xmin + width * scale_w)
ymax = (ymin + height * scale_h)
boxes.append([xmin, ymin, xmax, ymax])
labels.append(obj['category_id'])
boxes = torch.tensor(boxes, dtype=torch.float32)
labels = torch.tensor(labels, dtype=torch.long)
area = torch.tensor([obj['area'] * scale_w * scale_h for obj in annotations], dtype=torch.float32)
iscrowd = torch.tensor([obj.get('iscrowd', 0) for obj in annotations], dtype=torch.long)
target = {
"boxes": boxes,
"labels": labels,
"image_id": torch.tensor([image_id]),
"area": area,
"iscrowd": iscrowd
}
# resize image
image = image.resize((self.img_size, self.img_size))
if self.transforms:
image = self.transforms(image)
return image, target