-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdataloader.py
More file actions
69 lines (67 loc) · 2.76 KB
/
dataloader.py
File metadata and controls
69 lines (67 loc) · 2.76 KB
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
# *
# @file Different utility functions
# Copyright (c) Yaohui Cai, Zhewei Yao, Zhen Dong, Amir Gholami
# All rights reserved.
# This file is part of ZeroQ repository.
#
# ZeroQ is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ZeroQ is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZeroQ repository. If not, see <http://www.gnu.org/licenses/>.
# *
import torch
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import Dataset, DataLoader
import os
import numpy as np
def getTestData(dataset='imagenet',
batch_size=1024,
path='data/imagenet',
for_inception=False):
"""
Get dataloader of testset
dataset: name of the dataset
batch_size: the batch size of random data
path: the path to the data
for_inception: whether the data is for Inception because inception has input size 299 rather than 224
"""
if dataset == 'imagenet':
input_size = 299 if for_inception else 224
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
test_dataset = datasets.ImageFolder(
path + '/validation',
transforms.Compose([
transforms.Resize(int(input_size / 0.875)),
transforms.CenterCrop(input_size),
transforms.ToTensor(),
normalize,
]))
test_loader = DataLoader(test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=32)
return test_loader
elif dataset == 'cifar10':
data_dir = ' '
normalize = transforms.Normalize(mean=(0.4914, 0.4822, 0.4465),
std=(0.2023, 0.1994, 0.2010))
transform_test = transforms.Compose([transforms.ToTensor(), normalize])
test_dataset = datasets.CIFAR10(root=data_dir,
train=False,
transform=transform_test)
test_loader = DataLoader(test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=32)
return test_loader