-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
71 lines (57 loc) · 2.21 KB
/
dataset.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
import os
import torch
import random
import numpy as np
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from PIL import Image
class Dataset():
def __init__(self, train_dir, basic_types = None, shuffle = True):
self.train_dir = train_dir
self.basic_types = basic_types
self.shuffle = shuffle
def get_loader(self, sz, bs, get_size = False, data_transform = None, num_workers = 1, audio_sample_num = None):
if(self.basic_types is None):
if(data_transform == None):
data_transform = transforms.Compose([
transforms.Resize(sz),
transforms.CenterCrop(sz),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
train_dataset = datasets.ImageFolder(self.train_dir, data_transform)
train_loader = DataLoader(train_dataset, batch_size = bs, shuffle = self.shuffle, num_workers = num_workers)
train_dataset_size = len(train_dataset)
size = train_dataset_size
returns = (train_loader)
if(get_size):
returns = returns + (size,)
elif(self.basic_types == 'MNIST'):
data_transform = transforms.Compose([
transforms.Resize(sz),
transforms.CenterCrop(sz),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
train_dataset = datasets.MNIST(self.train_dir, train = True, download = True, transform = data_transform)
train_loader = DataLoader(train_dataset, batch_size = bs, shuffle = self.shuffle, num_workers = num_workers)
train_dataset_size = len(train_dataset)
size = train_dataset_size
returns = (train_loader)
if(get_size):
returns = returns + (size,)
elif(self.basic_types == 'CIFAR10'):
data_transform = transforms.Compose([
transforms.Resize(sz),
transforms.CenterCrop(sz),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
train_dataset = datasets.CIFAR10(self.train_dir, train = True, download = True, transform = data_transform)
train_loader = DataLoader(train_dataset, batch_size = bs, shuffle = self.shuffle, num_workers = num_workers)
train_dataset_size = len(train_dataset)
size = train_dataset_size
returns = (train_loader)
if(get_size):
returns = returns + (size,)
return returns