-
Notifications
You must be signed in to change notification settings - Fork 2
/
mini_imagenet.py
60 lines (47 loc) · 1.72 KB
/
mini_imagenet.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
import os.path as osp
import torch
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
ROOT_PATH = './materials/'
class MiniImageNet(Dataset):
def __init__(self, setname):
csv_path = osp.join(ROOT_PATH, setname + '.csv')
lines = [x.strip() for x in open(csv_path, 'r').readlines()][1:]
data = []
label = []
lb = -1
self.wnids = []
for l in lines:
name, wnid = l.split(',')
path = osp.join(ROOT_PATH, 'images', name)
if wnid not in self.wnids:
self.wnids.append(wnid)
lb += 1
data.append(path)
label.append(lb)
self.data = data
self.label = label
self.transform_train = transforms.Compose([
transforms.Resize(90),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(84,padding=4),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
self.transform_test = transforms.Compose([
transforms.Resize(84),
transforms.CenterCrop(84),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
def __len__(self):
return len(self.data)
def __getitem__(self, i):
path, label = self.data[i], self.label[i]
image1 = self.transform_train(Image.open(path).convert('RGB'))
image2 = self.transform_test(Image.open(path).convert('RGB'))
image = torch.cat([image1, image2])
return image, label