Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ECE50024_Project/dataloader.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (32 sloc)
1.11 KB
This file contains 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
import torch.utils.data as data | |
from PIL import Image | |
import os | |
IMG_EXTENSIONS = [ | |
'.jpg', '.JPG', '.jpeg', '.JPEG', | |
'.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', | |
'.tif', '.TIF', '.tiff', '.TIFF', | |
] | |
def is_image_file(filename): | |
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) | |
def make_dataset(dir, max_dataset_size=float("inf")): | |
images = [] | |
assert os.path.isdir(dir), '%s is not a valid directory' % dir | |
for root, _, fnames in sorted(os.walk(dir)): | |
for fname in fnames: | |
if is_image_file(fname): | |
path = os.path.join(root, fname) | |
images.append(path) | |
return images[:min(max_dataset_size, len(images))] | |
class ImageFolder(data.Dataset): | |
def __init__(self, root, transform=None): | |
imgs = make_dataset(root) | |
self.root = root | |
self.imgs = imgs | |
self.transform = transform | |
def __getitem__(self, index): | |
path = self.imgs[index] | |
img = Image.open(path).convert('RGB') | |
img = self.transform(img) | |
return img | |
def __len__(self): | |
return len(self.imgs) |