-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadData.py
More file actions
35 lines (29 loc) · 1.38 KB
/
LoadData.py
File metadata and controls
35 lines (29 loc) · 1.38 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
from PIL import Image
import os
from torch.utils.data import Dataset
class SceneDataset(Dataset):
"""Custom dataset for scene classification"""
def __init__(self, root_dir, transform=None):
self.root_dir = root_dir
self.transform = transform
self.classes = ['Office', 'Kitchen', 'LivingRoom', 'Bedroom', 'Store',
'Industrial', 'TallBuilding', 'InsideCity', 'Street',
'Highway', 'Coast', 'OpenCountry', 'Mountain', 'Forest', 'Suburb']
self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.classes)}
self.samples = []
# Load all image paths and labels
for class_name in self.classes:
class_dir = os.path.join(root_dir, class_name)
if os.path.exists(class_dir):
for img_name in os.listdir(class_dir):
if img_name.lower().endswith(('.png', '.jpg', '.jpeg')):
img_path = os.path.join(class_dir, img_name)
self.samples.append((img_path, self.class_to_idx[class_name]))
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
img_path, label = self.samples[idx]
image = Image.open(img_path).convert("RGB")
if self.transform:
image = self.transform(image)
return image, label