-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
33 lines (27 loc) · 1.05 KB
/
model.py
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
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image
import os
class CustomDataset(Dataset):
def __init__(self, root_dir, transform=None):
self.root_dir = root_dir
self.transform = transform
self.classes = sorted(os.listdir(root_dir))
self.class_to_idx = {cls: i for i, cls in enumerate(self.classes)}
self.images = self.load_images()
def load_images(self):
images = []
for cls in self.classes:
class_path = os.path.join(self.root_dir, cls)
for img_name in os.listdir(class_path):
img_path = os.path.join(class_path, img_name)
images.append((img_path, self.class_to_idx[cls]))
return images
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
img_path, label = self.images[idx]
image = Image.open(img_path).convert("L") # グレースケールに変換
if self.transform:
image = self.transform(image)
return image, label