forked from ppliuboy/SelFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
212 lines (189 loc) · 11.8 KB
/
datasets.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
import tensorflow.compat.v1 as tf
import numpy as np
import scipy.misc as misc
import cv2
import matplotlib.pyplot as plt
from flowlib import read_flo, read_pfm
from data_augmentation import *
from utils import mvn
from skimage.segmentation import slic
class BasicDataset(object):
def __init__(self, crop_h=320, crop_w=896, batch_size=4, data_list_file='path_to_your_data_list_file',
img_dir='path_to_your_image_directory', fake_flow_occ_dir='path_to_your_fake_flow_occlusion_directory', is_normalize_img=True,
superpixel_dir='path_to_superpixel'):
self.crop_h = crop_h
self.crop_w = crop_w
self.batch_size = batch_size
self.img_dir = img_dir
self.data_list = np.loadtxt(data_list_file, dtype=bytes).astype(np.str)
self.data_num = self.data_list.shape[0]
self.fake_flow_occ_dir = fake_flow_occ_dir
self.is_normalize_img = is_normalize_img
self.superpixel_dir = superpixel_dir
# KITTI's data format for storing flow and mask
# The first two channels are flow, the third channel is mask
def extract_flow_and_mask(self, flow):
optical_flow = flow[:, :, :2]
optical_flow = (optical_flow - 32768) / 64.0
mask = tf.cast(tf.greater(flow[:, :, 2], 0), tf.float32)
#mask = tf.cast(flow[:, :, 2], tf.float32)
mask = tf.expand_dims(mask, -1)
return optical_flow, mask
# The default image type is PNG.
"""
def read_and_decode(self, filename_queue):
img0_name = tf.string_join([self.img_dir, '/', filename_queue[0]])
img1_name = tf.string_join([self.img_dir, '/', filename_queue[1]])
img2_name = tf.string_join([self.img_dir, '/', filename_queue[2]])
img0 = tf.image.decode_png(tf.read_file(img0_name), channels=3)
img0 = tf.cast(img0, tf.float32)
img1 = tf.image.decode_png(tf.read_file(img1_name), channels=3)
img1 = tf.cast(img1, tf.float32)
img2 = tf.image.decode_png(tf.read_file(img2_name), channels=3)
img2 = tf.cast(img2, tf.float32)
return img0, img1, img2
"""
# For Validation or Testing
def preprocess_one_shot(self, filename_queue):
img0, img1, img2 = self.read_and_decode(filename_queue)
img0 = img0 / 255.
img1 = img1 / 255.
img2 = img2 / 255.
if self.is_normalize_img:
img0 = mvn(img0)
img1 = mvn(img1)
img2 = mvn(img2)
return img0, img1, img2
def create_one_shot_iterator(self, data_list, num_parallel_calls=4):
""" For Validation or Testing
Generate image and flow one_by_one without cropping, image and flow size may change every iteration
"""
data_list = tf.convert_to_tensor(data_list, dtype=tf.string)
dataset = tf.data.Dataset.from_tensor_slices(data_list)
dataset = dataset.map(self.preprocess_one_shot, num_parallel_calls=num_parallel_calls)
dataset = dataset.batch(1)
dataset = dataset.repeat()
iterator = dataset.make_initializable_iterator()
return iterator
def preprocess_one_shot_five_frame(self, filename_queue):
img0, img1, img2, img3, img4 = self.read_and_decode(filename_queue)
img0 = img0 / 255.
img1 = img1 / 255.
img2 = img2 / 255.
img3 = img3 / 255.
img4 = img4 / 255.
return img0, img1, img2, img3, img4
def create_one_shot_five_frame_iterator(self, data_list, num_parallel_calls=4):
""" For Validation or Testing
Generate image and flow one_by_one without cropping, image and flow size may change every iteration
"""
data_list = tf.convert_to_tensor(data_list, dtype=tf.string)
dataset = tf.data.Dataset.from_tensor_slices(data_list)
dataset = dataset.map(self.preprocess_one_shot_five_frame, num_parallel_calls=num_parallel_calls)
dataset = dataset.batch(1)
dataset = dataset.repeat()
iterator = dataset.make_initializable_iterator()
return iterator
def create_batch_iterator(self, data_list, batch_size, shuffle=True, buffer_size=5000, num_parallel_calls=4):
data_list = tf.convert_to_tensor(data_list, dtype=tf.string)
dataset = tf.data.Dataset.from_tensor_slices(data_list)
dataset = dataset.map(self.preprocess_augmentation, num_parallel_calls=num_parallel_calls)
if shuffle:
dataset = dataset.shuffle(buffer_size=buffer_size)
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
iterator = dataset.make_initializable_iterator()
return iterator
def create_batch_distillation_iterator(self, data_list, batch_size, shuffle=True, buffer_size=5000, num_parallel_calls=4):
data_list = tf.convert_to_tensor(data_list, dtype=tf.string)
dataset = tf.data.Dataset.from_tensor_slices(data_list)
dataset = dataset.map(self.preprocess_augmentation_distillation, num_parallel_calls=num_parallel_calls)
if shuffle:
dataset = dataset.shuffle(buffer_size=buffer_size)
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
iterator = dataset.make_initializable_iterator()
return iterator
def preprocess_augmentation(self, filename_queue):
img1, img2, img3, img4, img5 = self.read_and_decode(filename_queue)
img1 = img1 / 255.
img2 = img2 / 255.
img3 = img3 / 255.
img4 = img4 / 255.
img5 = img5 / 255.
img1, img2, img3, img4, img5 = self.augmentation(img1, img2, img3, img4, img5)
return img1, img2, img3, img4, img5
def read_and_decode(self, filename_queue):
img1_name = tf.string_join([self.img_dir, '/', filename_queue[0]])
img2_name = tf.string_join([self.img_dir, '/', filename_queue[1]])
img3_name = tf.string_join([self.img_dir, '/', filename_queue[2]])
img4_name = tf.string_join([self.img_dir, '/', filename_queue[3]])
img5_name = tf.string_join([self.img_dir, '/', filename_queue[4]])
img1 = tf.image.decode_png(tf.read_file(img1_name), channels=3)
img1 = tf.cast(img1, tf.float32)
img2 = tf.image.decode_png(tf.read_file(img2_name), channels=3)
img2 = tf.cast(img2, tf.float32)
img3 = tf.image.decode_png(tf.read_file(img3_name), channels=3)
img3 = tf.cast(img3, tf.float32)
img4 = tf.image.decode_png(tf.read_file(img4_name), channels=3)
img4 = tf.cast(img4, tf.float32)
img5 = tf.image.decode_png(tf.read_file(img5_name), channels=3)
img5 = tf.cast(img5, tf.float32)
return img1, img2, img3, img4, img5
def augmentation(self, img1, img2, img3, img4, img5):
img1, img2, img3, img4, img5 = random_crop([img1, img2, img3, img4, img5], self.crop_h, self.crop_w)
img1, img2, img3, img4, img5 = random_flip([img1, img2, img3, img4, img5])
img1, img2, img3, img4, img5 = random_channel_swap([img1, img2, img3, img4, img5])
return img1, img2, img3, img4, img5
def preprocess_augmentation_distillation(self, filename_queue):
img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels = self.read_and_decode_distillation(filename_queue)
img1 = img1 / 255.
img2 = img2 / 255.
img3 = img3 / 255.
img4 = img4 / 255.
img5 = img5 / 255.
img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels = self.augmentation_distillation(img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels)
return img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels
def read_and_decode_distillation(self, filename_queue):
img1_name = tf.string_join([self.img_dir, '/', filename_queue[0]])
img2_name = tf.string_join([self.img_dir, '/', filename_queue[1]])
img3_name = tf.string_join([self.img_dir, '/', filename_queue[2]])
img4_name = tf.string_join([self.img_dir, '/', filename_queue[3]])
img5_name = tf.string_join([self.img_dir, '/', filename_queue[4]])
img1 = tf.image.decode_png(tf.read_file(img1_name), channels=3)
img1 = tf.cast(img1, tf.float32)
img2 = tf.image.decode_png(tf.read_file(img2_name), channels=3)
img2 = tf.cast(img2, tf.float32)
img3 = tf.image.decode_png(tf.read_file(img3_name), channels=3)
img3 = tf.cast(img3, tf.float32)
img4 = tf.image.decode_png(tf.read_file(img4_name), channels=3)
img4 = tf.cast(img4, tf.float32)
img5 = tf.image.decode_png(tf.read_file(img5_name), channels=3)
img5 = tf.cast(img5, tf.float32)
flow_occ_fw_12_name = tf.string_join([self.fake_flow_occ_dir, '/flow_occ_12_fw_', filename_queue[5], '.png'])
flow_occ_bw_21_name = tf.string_join([self.fake_flow_occ_dir, '/flow_occ_21_bw_', filename_queue[5], '.png'])
flow_occ_fw_12 = tf.image.decode_png(tf.read_file(flow_occ_fw_12_name), dtype=tf.uint16, channels=3)
flow_occ_fw_12 = tf.cast(flow_occ_fw_12, tf.float32)
flow_occ_bw_21 = tf.image.decode_png(tf.read_file(flow_occ_bw_21_name), dtype=tf.uint16, channels=3)
flow_occ_bw_21 = tf.cast(flow_occ_bw_21, tf.float32)
flow_fw_12, occ_fw_12 = self.extract_flow_and_mask(flow_occ_fw_12)
flow_bw_21, occ_bw_21 = self.extract_flow_and_mask(flow_occ_bw_21)
flow_occ_fw_23_name = tf.string_join([self.fake_flow_occ_dir, '/flow_occ_23_fw_', filename_queue[5], '.png'])
flow_occ_bw_32_name = tf.string_join([self.fake_flow_occ_dir, '/flow_occ_32_bw_', filename_queue[5], '.png'])
flow_occ_fw_23 = tf.image.decode_png(tf.read_file(flow_occ_fw_23_name), dtype=tf.uint16, channels=3)
flow_occ_fw_23 = tf.cast(flow_occ_fw_23, tf.float32)
flow_occ_bw_32 = tf.image.decode_png(tf.read_file(flow_occ_bw_32_name), dtype=tf.uint16, channels=3)
flow_occ_bw_32 = tf.cast(flow_occ_bw_32, tf.float32)
flow_fw_23, occ_fw_23 = self.extract_flow_and_mask(flow_occ_fw_23)
flow_bw_32, occ_bw_32 = self.extract_flow_and_mask(flow_occ_bw_32)
superpix_name = tf.string_join([self.superpixel_dir, '/', filename_queue[2]])
superpixels = tf.image.decode_png(tf.read_file(superpix_name), channels=1)
superpixels = tf.cast(superpixels, tf.int32)
return img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels
def augmentation_distillation(self, img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels):
[img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels] = random_crop([img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels], self.crop_h, self.crop_w)
[img1, img2, img3, img4, img5, occ_fw_12, occ_bw_21, occ_fw_23, occ_bw_32, superpixels], [flow_fw_12, flow_bw_21, flow_fw_23, flow_bw_32] = random_flip_with_flow([img1, img2, img3, img4, img5, occ_fw_12, occ_bw_21, occ_fw_23, occ_bw_32, superpixels], [flow_fw_12, flow_bw_21, flow_fw_23, flow_bw_32])
img1, img2, img3, img4, img5 = random_channel_swap([img1, img2, img3, img4, img5])
superpixels = superpixels[:,:,0]
return img1, img2, img3, img4, img5, flow_fw_12, flow_bw_21, occ_fw_12, occ_bw_21, flow_fw_23, flow_bw_32, occ_fw_23, occ_bw_32, superpixels