-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_mnist.py
141 lines (112 loc) · 4.7 KB
/
data_mnist.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
139
140
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import imageio as imio
import numpy as np
import os
def read_gif(path, channels=1):
im = np.array(imio.mimread(path))
if channels == 3:
if im.ndim == 3:
im = np.stack([im, im, im], axis=3)
else:
im = im[..., :-1]
elif channels == 1:
im = im[..., np.newaxis]
return im
def read_gif_volume(img_path, depth=30, crop_size=(84, 84), channels=1):
crop_x, crop_y = crop_size
img = read_gif(img_path, channels=channels)
if img.shape[0] > depth:
rand_idx = np.random.randint(img.shape[0] - depth)
res_img = img[rand_idx:rand_idx+depth, ...]
elif img.shape[0] < depth:
print(img.shape)
return None
else:
res_img = img
start_x, start_y = (0, 0)
if res_img.shape[1] > crop_size[0] or res_img.shape[2] > crop_size[1]:
start_x = np.random.randint(0, (res_img.shape[1] - crop_size[0]) - 1)
start_y = np.random.randint(0, (res_img.shape[2] - crop_size[1]) - 1)
res_img = res_img[:, start_x:start_x + crop_size[0], start_y: start_y + crop_size[1], ...]
res_img = np.reshape(res_img, (1, depth, crop_x, crop_y, channels))
return res_img
def read_gif_volume_linsp(img_path, depth=30, crop_size=(84, 84), channels=1):
crop_x, crop_y = crop_size
img = read_gif(img_path, channels=channels)
if img.shape[0] > depth:
mask = np.linspace(start=0, stop=img.shape[0]-1, num=depth)
#rand_idx = 4 #np.random.randint(img.shape[0] - depth)
res_img = img[mask]
elif img.shape[0] < depth:
print(img.shape)
return None
else:
res_img = img
start_x, start_y = (0, 0)
if res_img.shape[1] > crop_size[0] or res_img.shape[2] > crop_size[1]:
start_x = np.random.randint(0, (res_img.shape[1] - crop_size[0]) - 1)
start_y = np.random.randint(0, (res_img.shape[2] - crop_size[1]) - 1)
res_img = res_img[:, start_x:start_x + crop_size[0], start_y: start_y + crop_size[1], ...]
res_img = np.reshape(res_img, (1, depth, crop_x, crop_y, channels))
return res_img
class ImageData:
def __init__(self, session, image_paths, out_depth, crop_size=256, channels=1, random=True,
return_bname=False, linsp=False):
self.sess = session
self.image_paths = image_paths
self.depth = out_depth
self.counter = 0
self.crop_size = crop_size
self.channels = channels
self.return_bname = return_bname
if random:
self.shuffle()
self.img_num = len(image_paths)
self.img_batch, _ = self.batch()
self.linsp = linsp
self.batch_method = read_gif_volume_linsp
def __len__(self):
return self.img_num
def batch_ops(self):
return self.img_batch
def batch(self):
self.counter += 1
if self.counter == len(self.image_paths):
self.shuffle()
self.counter = 0
#print(len(self.image_paths), self.counter)
image = read_gif_volume(self.image_paths[self.counter], depth=self.depth,
crop_size=self.crop_size, channels=self.channels)
if image is None:
self.image_paths.pop(self.counter)
image = read_gif_volume(self.image_paths[self.counter], depth=self.depth,
crop_size=self.crop_size, channels=self.channels)
self.img_num -= 1
image -= image.min()
image_norm = 2.0*(image/image.max()) - 1.0
if self.return_bname:
bname = os.path.basename(self.image_paths[self.counter]).split('.')[0]
return image_norm, bname
return image_norm, len(self.image_paths)
def batch_full(self, depth):
self.counter += 1
if self.counter == len(self.image_paths):
#self.shuffle()
self.counter = 0
#print(len(self.image_paths), self.counter)
image = read_gif_volume_linsp(self.image_paths[self.counter], depth=depth,
crop_size=self.crop_size, channels=self.channels)
if image is None:
self.image_paths.pop(self.counter)
image = read_gif_volume_linsp(self.image_paths[self.counter], depth=depth,
crop_size=self.crop_size, channels=self.channels)
self.img_num -= 1
image -= image.min()
image_norm = 2.0*(image/image.max()) - 1.0
if self.return_bname:
bname = os.path.basename(self.image_paths[self.counter]).split('.')[0]
return image_norm, bname
return image_norm
def shuffle(self):
np.random.shuffle(self.image_paths)