-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpre_extracted_data_loader.py
139 lines (100 loc) · 3.64 KB
/
pre_extracted_data_loader.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# coding: utf-8
import torch
from torch.utils.data import Dataset
import torchvision
import numpy as np
import os
import sys
class pre_extracted_dataset(Dataset):
train_list = [
'places365/train.npz',
'imagenet/train.npz',
]
test_list = [
'places365/validate.npz',
'imagenet/validate.npz',
]
def __init__(self, root, train=True, transform=None, target_transform=None):
self.root = os.path.expanduser(root)
self.transform = transform
self.target_transform = target_transform
self.train = train # training set or test set
if self.train:
f = self.train_list[0]
file_path = os.path.join(self.root, f)
fo = np.load(file_path)
self.train_data_places = fo['data']
self.train_labels = fo['label']
fo.close()
f = self.train_list[1]
file_path = os.path.join(self.root, f)
fo = np.load(file_path)
self.train_data_imagenet = fo['data']
fo.close()
else:
f = self.test_list[0]
file_path = os.path.join(self.root, f)
fo = np.load(file_path)
self.test_data_places = fo['data']
self.test_labels = fo['label']
fo.close()
f = self.test_list[1]
file_path = os.path.join(self.root, f)
fo = np.load(file_path)
self.test_data_imagenet = fo['data']
fo.close()
def __getitem__(self, index):
if self.train:
data_places = self.train_data_places[index]
data_imagenet = self.train_data_imagenet[index]
target = self.train_labels[index]
else:
data_places = self.test_data_places[index]
data_imagenet = self.test_data_imagenet[index]
target = self.test_labels[index]
if self.transform is not None:
data_places = self.transform(data_places)
data_places = self.transform(data_imagenet)
if self.target_transform is not None:
target = self.target_transform(target)
return data_places, data_imagenet, target
def __len__(self):
if self.train:
return len(self.train_labels)
else:
return len(self.test_labels)
class pre_extracted_places_feature(Dataset):
def __init__(self, root, train=True, transform=None, target_transform=None):
self.root = os.path.expanduser(root)
self.transform = transform
self.target_transform = target_transform
self.train = train # training set or test set
if self.train:
file_path = self.root
fo = np.load(file_path)
self.train_data_places = fo['data']
self.train_labels = fo['label']
fo.close()
else:
file_path = self.root
fo = np.load(file_path)
self.test_data_places = fo['data']
self.test_labels = fo['label']
fo.close()
def __getitem__(self, index):
if self.train:
data_places = self.train_data_places[index]
target = self.train_labels[index]
else:
data_places = self.test_data_places[index]
target = self.test_labels[index]
if self.transform is not None:
data_places = self.transform(data_places)
if self.target_transform is not None:
target = self.target_transform(target)
return data_places, target
def __len__(self):
if self.train:
return len(self.train_labels)
else:
return len(self.test_labels)