|
|
import torch |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
class CrosswalkDataset: |
|
|
def __init__(self, annotation_path, image_path, transform=None): |
|
|
self.annotations = pd.read_csv(annotation_path) |
|
|
self.image_dir = image_path |
|
|
|
|
|
self.transform = transform |
|
|
|
|
|
|
|
|
self.unique_labels = sorted(self.annotations['class'].unique()) |
|
|
|
|
|
self.type_mapping = {type_value: 1 + idx for idx, type_value in enumerate(self.unique_labels)} |
|
|
|
|
|
|
|
|
|
|
|
self.image_data = [] |
|
|
self.labels = [] |
|
|
|
|
|
self.process_annotations() |
|
|
|
|
|
def __len__(self): |
|
|
return len(self.image_data) |
|
|
|
|
|
def __getitem__(self, index): |
|
|
image = self.image_data[index] |
|
|
class_label = self.labels[index] |
|
|
|
|
|
tensor_image = torch.tensor(image, dtype=torch.float32).permute(2, 0, 1) |
|
|
|
|
|
tensor_label = torch.tensor(class_label, dtype=torch.float32) |
|
|
|
|
|
if self.transform: |
|
|
tensor_image = self.transform(tensor_image) |
|
|
|
|
|
return tensor_image, tensor_label |
|
|
|
|
|
def process_annotations(self): |
|
|
for filename, group in self.annotations.groupby('filename'): |
|
|
completed_image_path = f"{self.image_dir}/{filename}" |
|
|
image = Image.open(completed_image_path) |
|
|
image_array = np.array(image) |
|
|
entity_annotations = [1, 0] |
|
|
|
|
|
for _, row in group.iterrows(): |
|
|
|
|
|
if row['class'] == "ZebraStyle": |
|
|
|
|
|
entity_annotations = [0, 1] |
|
|
|
|
|
|
|
|
else: |
|
|
pass |
|
|
|
|
|
|
|
|
self.image_data.append(image_array) |
|
|
self.labels.append(entity_annotations) |
|
|
|
|
|
|
|
|
crosswalk_dataset = CrosswalkDataset("Crosswalk.v7-crosswalk-t3.tensorflow/train/_annotations.csv", |
|
|
"Crosswalk.v7-crosswalk-t3.tensorflow/train") |
|
|
|
|
|
|