-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
80 lines (62 loc) · 2.54 KB
/
dataloader.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
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 11:03:34 2018
@author: prodi
"""
from __future__ import print_function, division
import os
import torch
import pandas as pd
#from skimage import io, transform
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
# Ignore warnings
import warnings
warnings.filterwarnings("ignore")
class HandBagsDataset(Dataset):
"""Handbags dataset."""
def __init__(self, root_dir, transform=None):
"""
Args:
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.root_dir = root_dir
self.transform = transform
self.files = os.listdir(root_dir)
self.files = [os.path.join(root_dir, f) for f in self.files]
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
img_name = self.files[idx]
image = Image.open(img_name)
if self.transform:
image = self.transform(image)
return image
def fetch_data(data_dir, batch_size, normalize=False):
"""
Fetches the DataLoader object for each type in types from data_dir.
Args:
types: (list) has one or more of 'train', 'val', 'test' depending on which data is required
data_dir: (string) directory containing the dataset
params: (Params) hyperparameters
Returns:
data: (dict) contains the DataLoader object for each type in types
"""
if normalize==True:
transformer = transforms.Compose([
transforms.Resize((64,64)), # resize the image to 64x64 (remove if images are already 64x64)
transforms.RandomHorizontalFlip(), # randomly flip image horizontally
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) # transform it into a torch tensor
else:
transformer = transforms.Compose([
transforms.Resize((64,64)), # resize the image to 64x64 (remove if images are already 64x64)
transforms.RandomHorizontalFlip(), # randomly flip image horizontally
transforms.ToTensor()])
dataloader = DataLoader(HandBagsDataset(data_dir, transformer), batch_size=batch_size, shuffle=True)
return dataloader