-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
50 lines (41 loc) · 1.48 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
import os
import random
import h5py
import numpy as np
import torch
import torch.utils.data as udata
class HyperDataset(udata.Dataset):
def __init__(self, mode='train'):
self.mode = mode
if self.mode == 'train':
self.h5f = h5py.File('/data0/langzhiqiang/data/train.h5', 'r')
elif self.mode == 'test':
self.h5f = h5py.File('/data0/langzhiqiang/data/test_final.h5', 'r')
elif self.mode == 'train_rw':
self.h5f = h5py.File('/data0/langzhiqiang/data/train_realworld.h5', 'r')
elif self.mode == 'test_rw':
self.h5f = h5py.File('/data0/langzhiqiang/data/test_final_realworld.h5', 'r')
self.keys = list(self.h5f.keys())
if 'test' in self.mode:
print(self.keys)
if 'train' in self.mode:
random.shuffle(self.keys)
else:
self.keys.sort()
def __len__(self):
return len(self.keys)
def __getitem__(self, index):
key = str(self.keys[index])
data = np.array(self.h5f[key])
data = torch.Tensor(data)
return data[0:31,:,:], data[31:34,:,:], data[34:65,:,:]
def get_data_by_key(self, key):
assert self.mode == 'test'
data = np.array(self.h5f[key])
data = torch.Tensor(data)
return data[0:31,:,:], data[31:34,:,:], data[34:65,:,:]
def close(self):
self.h5f.close()
def shuffle(self):
if 'train' in self.mode:
random.shuffle(self.keys)