-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
38 lines (31 loc) · 1.29 KB
/
dataset.py
File metadata and controls
38 lines (31 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
from PIL import Image
from torch.utils.data import Dataset
class CustomImageDataset(Dataset):
def __init__(self, image_paths, labels, transform=None):
self.image_paths = image_paths
self.labels = labels
self.transform = transform
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
image_path = self.image_paths[idx]
label = self.labels[idx]
image = Image.open(image_path).convert('RGB')
if self.transform:
image = self.transform(image)
return image, label
def prepare_data(data_dir, class_names):
image_paths, labels = [], []
class_to_idx = {cls_name: idx for idx, cls_name in enumerate(class_names)}
for class_name in class_names:
class_dir = os.path.join(data_dir, class_name)
if not os.path.isdir(class_dir):
raise FileNotFoundError(
f"Expected folder not found: {class_dir}"
)
for img_file in os.listdir(class_dir):
if img_file.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
image_paths.append(os.path.join(class_dir, img_file))
labels.append(class_to_idx[class_name])
return image_paths, labels, class_to_idx