Last active
September 19, 2023 20:51
-
-
Save norabelrose/3f7c1cacbe817c60a186fa945616bd0f to your computer and use it in GitHub Desktop.
PyTorch dataset for loading imagenet images from Kaggle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from torch.utils.data import Dataset | |
import json | |
import os | |
class ImageNetKaggle(Dataset): | |
def __init__(self, root, split, transform=None): | |
self.samples = [] | |
self.targets = [] | |
self.transform = transform | |
self.syn_to_class = {} | |
with open(os.path.join(root, "imagenet_class_index.json"), "rb") as f: | |
json_file = json.load(f) | |
for class_id, v in json_file.items(): | |
self.syn_to_class[v[0]] = int(class_id) | |
with open(os.path.join(root, "ILSVRC2012_val_labels.json"), "rb") as f: | |
self.val_to_syn = json.load(f) | |
samples_dir = os.path.join(root, "ILSVRC/Data/CLS-LOC", split) | |
for entry in os.listdir(samples_dir): | |
if split == "train": | |
syn_id = entry | |
target = self.syn_to_class[syn_id] | |
syn_folder = os.path.join(samples_dir, syn_id) | |
for sample in os.listdir(syn_folder): | |
sample_path = os.path.join(syn_folder, sample) | |
self.samples.append(sample_path) | |
self.targets.append(target) | |
elif split == "val": | |
syn_id = self.val_to_syn[entry] | |
target = self.syn_to_class[syn_id] | |
sample_path = os.path.join(samples_dir, entry) | |
self.samples.append(sample_path) | |
self.targets.append(target) | |
def __len__(self): | |
return len(self.samples) | |
def __getitem__(self, idx): | |
x = Image.open(self.samples[idx]).convert("RGB").resize((256, 256)) | |
if self.transform: | |
x = self.transform(x) | |
return x, self.targets[idx] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment