-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
83 lines (59 loc) · 2.37 KB
/
dataset.py
File metadata and controls
83 lines (59 loc) · 2.37 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# encoding: utf-8
import numpy as np
import glob
import time
import cv2
import tensorflow as tf
# from cvtransforms import *
def load_file(filename):
cap = np.load(filename)
arrays = np.stack([cv2.cvtColor(cap[_], cv2.COLOR_RGB2GRAY) for _ in range(29)], axis=0)
arrays = arrays / 255.
return arrays
class LRW():
def __init__(self, folds, path):
self.folds = folds # ['train', 'val', 'test']
self.path = path
self.istrain = (folds == 'train')
with open('label_sorted.txt') as myfile:
self.data_dir = myfile.read().splitlines()
print(self.path+'*/'+self.folds+'/*.npy')
self.data_files = glob.glob(self.path+'*/'+self.folds+'/*.npy')
self.list = {}
for i, x in enumerate(self.data_files):
target = x.split('/')[-3]
for j, elem in enumerate(self.data_dir):
if elem == target:
self.list[i] = [x]
self.list[i].append(j)
print('Load {} part'.format(self.folds))
def __getitem__(self, idx):
print("__getitem__")
inputs = load_file(self.list[idx][0])
labels = self.list[idx][1]
print("idx: ", idx)
print(inputs)
print()
print(labels)
return inputs, labels
def __len__(self):
return len(self.data_files)
def prepare_train_batch(batch, device=None, non_blocking=False):
inputs, targets = batch
batch_img = RandomCrop(inputs.numpy(), (88, 88))
batch_img = ColorNormalize(batch_img)
batch_img = HorizontalFlip(batch_img)
batch_img = np.reshape(batch_img, (batch_img.shape[0], batch_img.shape[1], batch_img.shape[2], batch_img.shape[3], 1))
inputs = tf.convert_to_tensor(batch_img)
inputs = inputs.float().permute(0, 4, 1, 2, 3)
inputs, targets = inputs.to(device), targets.to(device)
return inputs, targets
def prepare_val_batch(batch, device=None, non_blocking=False):
inputs, targets = batch
batch_img = CenterCrop(inputs.numpy(), (88, 88))
batch_img = ColorNormalize(batch_img)
batch_img = np.reshape(batch_img, (batch_img.shape[0], batch_img.shape[1], batch_img.shape[2], batch_img.shape[3], 1))
inputs = tf.convert_to_tensor(batch_img)
inputs = inputs.float().permute(0, 4, 1, 2, 3)
inputs, targets = inputs.to(device), targets.to(device)
return inputs, targets