-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_loader.py
103 lines (86 loc) · 3.01 KB
/
dataset_loader.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Updated ImageNet100Dataset
class ImageNet100Dataset(torch.utils.data.Dataset):
def __init__(self, root_dirs, labels_file, transform=None, augment=None):
self.transform = transform
self.augment = augment
self.images = []
self.labels = []
self.label_to_idx = {}
with open(labels_file, 'r') as f:
label_dict = json.load(f)
unique_labels = sorted(label_dict.keys())
self.label_to_idx = {label: idx for idx, label in enumerate(unique_labels)}
for root_dir in root_dirs:
for label in os.listdir(root_dir):
label_path = os.path.join(root_dir, label)
if os.path.isdir(label_path):
for img_name in os.listdir(label_path):
img_path = os.path.join(label_path, img_name)
self.images.append(img_path)
self.labels.append(self.label_to_idx[label])
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
img_path = self.images[idx]
image = Image.open(img_path).convert('RGB')
label = self.labels[idx]
if self.transform:
image = self.transform(image)
if self.augment:
image = self.augment(image)
label = torch.tensor(label)
return image, label
# Define transformations
train_transform = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.05, 1.0)),
transforms.RandomHorizontalFlip(),
RandAugment(n=9, m=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
RandomErasing(probability=0.25)
])
val_transform = transforms.Compose([
transforms.Resize((224,224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Create the datasets
train_dirs = [
'/kaggle/input/imagenet100/train.X1',
'/kaggle/input/imagenet100/train.X2',
'/kaggle/input/imagenet100/train.X3',
'/kaggle/input/imagenet100/train.X4'
]
val_dir = ['/kaggle/input/imagenet100/val.X']
labels_file = '/kaggle/input/imagenet100/Labels.json'
train_dataset = ImageNet100Dataset(
root_dirs=train_dirs,
labels_file=labels_file,
transform=train_transform
)
val_dataset = ImageNet100Dataset(
root_dirs=val_dir,
labels_file=labels_file,
transform=val_transform
)
# Custom collate function for Mixup and CutMix
def collate_fn(batch):
images, labels = torch.utils.data.default_collate(batch)
if random.random() < 0.5:
return Mixup(alpha=0.8)((images, labels))
else:
return CutMix(alpha=1.0)((images, labels))
# Create data loaders
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
collate_fn=collate_fn,
drop_last=True
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=batch_size,
shuffle=False,
drop_last=True
)