-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_sequence_data_module.py
431 lines (385 loc) · 15.2 KB
/
image_sequence_data_module.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
from typing import List
import pandas as pd
import pytorch_lightning as pl
from pytorch_lightning.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
from torch.utils.data import DataLoader
from datasets import (
ImageSequenceDataset,
ImageSequenceDuvDataset,
ImageSequenceMotionLabelDataset,
)
from utils.processing import unique_video_train_test
from utils.transforms import dict_list_to_augment
class ImageSequenceDataModule(pl.LightningDataModule):
def __init__(
self,
df: pd.DataFrame,
prefix: str,
train_split: float,
batch_size: int,
num_workers: int = 2,
random_state: int = 42,
tolerance: float = 0.05,
seq_len: int = 10,
frame_increment: int = 1,
frames_between_clips: int = 1,
random_frame_offset: bool = False,
train_photometric_transforms: List[dict] = None,
train_geometric_transforms: List[dict] = None,
val_photometric_transforms: List[dict] = None,
val_geometric_transforms: List[dict] = None,
test_photometric_transforms: List[dict] = None,
test_geometric_transforms: List[dict] = None,
load_images: bool = True,
):
super().__init__()
# train/test
self._train_df = df[df.train == True]
self._test_df = df[df.train == False]
# further split train into train and validation set
self._train_df = unique_video_train_test(
self._train_df, train_split, tolerance=tolerance, random_state=random_state
)
self._val_df = self._train_df[self._train_df.train == False].reset_index()
self._train_df = self._train_df[self._train_df.train == True].reset_index()
self._prefix = prefix
self._batch_size = batch_size
self._num_workers = num_workers
self._seq_len = seq_len
self._frame_increment = frame_increment
self._frames_between_clips = frames_between_clips
self._random_frame_offset = random_frame_offset
self._train_spectral_tranforms = dict_list_to_augment(
train_photometric_transforms
)
self._train_geometric_transforms = dict_list_to_augment(
train_geometric_transforms
)
self._val_photometric_transforms = dict_list_to_augment(
val_photometric_transforms
)
self._val_geometric_transforms = dict_list_to_augment(val_geometric_transforms)
self._test_photometric_transforms = dict_list_to_augment(
test_photometric_transforms
)
self._test_geometric_transforms = dict_list_to_augment(
test_geometric_transforms
)
self._load_images = load_images
def setup(self, stage: str = None) -> None:
if stage == "fit" or stage is None:
self._train_set = ImageSequenceDataset(
df=self._train_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=self._random_frame_offset,
photometric_transforms=self._train_spectral_tranforms,
geometric_transforms=self._train_geometric_transforms,
load_images=self._load_images,
seeds=False,
)
self._val_set = ImageSequenceDataset(
df=self._val_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=False,
photometric_transforms=self._val_photometric_transforms,
geometric_transforms=self._val_geometric_transforms,
load_images=self._load_images,
seeds=True,
)
if stage == "test":
self._test_set = ImageSequenceDataset(
df=self._test_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=False,
photometric_transforms=self._test_photometric_transforms,
geometric_transforms=self._test_geometric_transforms,
load_images=self._load_images,
seeds=True,
)
# def transfer_batch_to_device(self, batch, device, dataloader_idx):
# pass
def train_dataloader(self) -> TRAIN_DATALOADERS:
return DataLoader(
self._train_set,
batch_size=self._batch_size,
shuffle=True,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
def val_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(
self._val_set,
batch_size=self._batch_size,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
def test_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(
self._test_set,
batch_size=self._batch_size,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
class ImageSequenceDuvDataModule(pl.LightningDataModule):
def __init__(
self,
df: pd.DataFrame,
prefix: str,
train_split: float,
batch_size: int,
num_workers: int = 2,
random_state: int = 42,
tolerance: float = 0.05,
seq_len: int = 10,
frame_increment: int = 1,
frames_between_clips: int = 1,
random_frame_offset: bool = False,
train_transforms: List[dict] = None,
val_transforms: List[dict] = None,
test_transforms: List[dict] = None,
load_images: bool = True,
):
super().__init__()
# train/test
self._train_df = df[df.train == True]
self._test_df = df[df.train == False]
# further split train into train and validation set
self._train_df = unique_video_train_test(
self._train_df, train_split, tolerance=tolerance, random_state=random_state
)
self._val_df = self._train_df[self._train_df.train == False].reset_index()
self._train_df = self._train_df[self._train_df.train == True].reset_index()
self._prefix = prefix
self._batch_size = batch_size
self._num_workers = num_workers
self._seq_len = seq_len
self._frame_increment = frame_increment
self._frames_between_clips = frames_between_clips
self._random_frame_offset = random_frame_offset
self._train_tranforms = dict_list_to_augment(train_transforms)
self._val_transforms = dict_list_to_augment(val_transforms)
self._test_transforms = dict_list_to_augment(test_transforms)
self._load_images = load_images
def setup(self, stage: str = None) -> None:
if stage == "fit":
self._train_set = ImageSequenceDuvDataset(
df=self._train_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=self._random_frame_offset,
transforms=self._train_tranforms,
load_images=self._load_images,
seeds=False,
)
self._val_set = ImageSequenceDuvDataset(
df=self._val_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=False,
transforms=self._val_transforms,
load_images=True,
seeds=True,
)
if stage == "test":
self._test_set = ImageSequenceDuvDataset(
df=self._test_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=False,
transforms=self._test_transforms,
load_images=True,
seeds=True,
)
def train_dataloader(self) -> TRAIN_DATALOADERS:
return DataLoader(
self._train_set,
batch_size=self._batch_size,
shuffle=True,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
def val_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(
self._val_set,
batch_size=self._batch_size,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
def test_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(
self._test_set,
batch_size=self._batch_size,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
class ImageSequenceMotionLabelDataModule(pl.LightningDataModule):
def __init__(
self,
df: pd.DataFrame,
prefix: str,
train_split: float,
batch_size: int,
num_workers: int = 2,
random_state: int = 42,
tolerance: float = 0.05,
seq_len: int = 10,
frame_increment: int = 1,
frames_between_clips: int = 1,
random_frame_offset: bool = False,
train_photometric_transforms: List[dict] = None,
train_geometric_transforms: List[dict] = None,
val_photometric_transforms: List[dict] = None,
val_geometric_transforms: List[dict] = None,
test_photometric_transforms: List[dict] = None,
test_geometric_transforms: List[dict] = None,
load_images: bool = True,
):
super().__init__()
# train/test
self._train_df = df[df.train == True]
self._test_df = df[df.train == False]
# further split train into train and validation set
self._train_df = unique_video_train_test(
self._train_df, train_split, tolerance=tolerance, random_state=random_state
)
self._val_df = self._train_df[self._train_df.train == False].reset_index()
self._train_df = self._train_df[self._train_df.train == True].reset_index()
self._prefix = prefix
self._batch_size = batch_size
self._num_workers = num_workers
self._seq_len = seq_len
self._frame_increment = frame_increment
self._frames_between_clips = frames_between_clips
self._random_frame_offset = random_frame_offset
self._train_spectral_tranforms = dict_list_to_augment(
train_photometric_transforms
)
self._train_geometric_transforms = dict_list_to_augment(
train_geometric_transforms
)
self._val_photometric_transforms = dict_list_to_augment(
val_photometric_transforms
)
self._val_geometric_transforms = dict_list_to_augment(val_geometric_transforms)
self._test_photometric_transforms = dict_list_to_augment(
test_photometric_transforms
)
self._test_geometric_transforms = dict_list_to_augment(
test_geometric_transforms
)
self._load_images = load_images
def setup(self, stage: str = None) -> None:
if stage == "fit" or stage is None:
self._train_set = ImageSequenceMotionLabelDataset(
df=self._train_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=self._random_frame_offset,
photometric_transforms=self._train_spectral_tranforms,
geometric_transforms=self._train_geometric_transforms,
load_images=self._load_images,
seeds=False,
)
self._val_set = ImageSequenceMotionLabelDataset(
df=self._val_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=False,
photometric_transforms=self._val_photometric_transforms,
geometric_transforms=self._val_geometric_transforms,
load_images=self._load_images,
seeds=True,
)
if stage == "test":
self._test_set = ImageSequenceMotionLabelDataset(
df=self._test_df,
prefix=self._prefix,
seq_len=self._seq_len,
frame_increment=self._frame_increment,
frames_between_clips=self._frames_between_clips,
random_frame_offset=False,
photometric_transforms=self._test_photometric_transforms,
geometric_transforms=self._test_geometric_transforms,
load_images=self._load_images,
seeds=True,
)
# def transfer_batch_to_device(self, batch, device, dataloader_idx):
# pass
def train_dataloader(self) -> TRAIN_DATALOADERS:
return DataLoader(
self._train_set,
batch_size=self._batch_size,
shuffle=True,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
def val_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(
self._val_set,
batch_size=self._batch_size,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
def test_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(
self._test_set,
batch_size=self._batch_size,
num_workers=self._num_workers,
drop_last=False,
pin_memory=True,
persistent_workers=True,
)
if __name__ == "__main__":
import cv2
from kornia import tensor_to_image
from utils.processing import unique_video_train_test
df = pd.read_pickle(
"/media/martin/Samsung_T5/data/endoscopic_data/cholec80_frames/log.pkl"
)
df = unique_video_train_test(df)
prefix = "/media/martin/Samsung_T5/data/endoscopic_data/cholec80_frames"
dm = ImageSequenceDataModule(
df, prefix, train_split=0.8, batch_size=10, tolerance=0.2, seq_len=10
)
dm.setup()
for frames, frames_transformed, idcs, vid_idx in dm.train_dataloader():
print(idcs)
for frame in frames[0]:
frame = tensor_to_image(frame, False)
cv2.imshow("frame", frame)
cv2.waitKey()