-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
1210 lines (1058 loc) · 53.3 KB
/
data_loader.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
A collection of image utilities using the Python Imaging Library (PIL).
This is a local version of utility functions from scipy that are wrapping PIL
functionality. These functions are deprecated in scipy 1.0.0 and will be
removed in scipy 1.2.0. Therefore, the functionality used in sklearn is copied
here. This file is taken from scipy/misc/pilutil.py in scipy
1.0.0. Modifications include: making this module importable if pillow is not
installed, removal of DeprecationWarning, removal of functions scikit-learn
does not need.
Copyright (c) 2001, 2002 Enthought, Inc.
All rights reserved.
Copyright (c) 2003-2017 SciPy Developers.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
a. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
b. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
c. Neither the name of Enthought nor the names of the SciPy Developers
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
"""
import glob
from platform import platform
from posixpath import dirname
from PIL import Image
# import pytorch_lightning as pl
import torch
from torch.utils.data import DataLoader
from enum import Enum
import os
import cv2
import numpy as np
import random
import torchvision.transforms as trn
from torch.utils.data.distributed import DistributedSampler
from my_utils import visualize_sampled_videos, numericalSort, localize_objects, convert_list_to_video, make_seg_maps
import json
import matplotlib.pyplot as plt
import torchvision.transforms.functional as f
from collections import OrderedDict
from os.path import join as pjoin
import collections
import scipy.misc as m
import scipy.io as io
import glob
from tqdm import tqdm
from torch.utils import data
from torchvision import transforms
from numpy import array, indices
import video_transformations
import numpy as np
from PIL import Image
import shutil
platform = None
if "node" in os.uname()[1]:
platform = "das6"
elif "ivi-cn" in os.uname()[1]:
platform = "ivi"
elif "quva01" in os.uname()[1]:
platform = "quva01"
else:
platform = "dta2"
_errstr = "Mode is unknown or incompatible with input array shape."
dataset_path = {"das6": {"davis": "/var/scratch/ssalehid/datasets/", "ytvos" : "/var/scratch/ssalehid/ytvos/", "pascal": "/var/scratch/ssalehid/datasets/"},
"ivi": {"davis": "../../dataset", "pascal": "../../dataset", "ytvos": "../../dataset", "kinetics": "/ssdstore/ssalehi", "mose": "/ssdstore/ssalehi/MOSE", "epic-kitchen": "/ssdstore/ssalehi/EpicKitchens", "visor": "/ssdstore/ssalehi/VISOR/out_data/VISOR_2022"},
"dta2": {"davis": "../../data/datasets/", "ytvos" : "../../data/", "pascal": "../../data/datasets/", "kinetics": "../../data/"},
"quva01": {"davis": "../../../datasets/", "ytvos" : "../../../datasets/ytvos/data/", "pascal": "../../../datasets/"}}
def fromimage(im, flatten=False, mode=None):
"""
Return a copy of a PIL image as a numpy array.
This function is only available if Python Imaging Library (PIL) is installed.
Parameters
----------
im : PIL image
Input image.
flatten : bool
If true, convert the output to grey-scale.
mode : str, optional
Mode to convert image to, e.g. ``'RGB'``. See the Notes of the
`imread` docstring for more details.
Returns
-------
fromimage : ndarray
The different colour bands/channels are stored in the
third dimension, such that a grey-image is MxN, an
RGB-image MxNx3 and an RGBA-image MxNx4.
"""
if not Image.isImageType(im):
raise TypeError("Input is not a PIL image.")
if mode is not None:
if mode != im.mode:
im = im.convert(mode)
elif im.mode == 'P':
# Mode 'P' means there is an indexed "palette". If we leave the mode
# as 'P', then when we do `a = array(im)` below, `a` will be a 2-D
# containing the indices into the palette, and not a 3-D array
# containing the RGB or RGBA values.
if 'transparency' in im.info:
im = im.convert('RGBA')
else:
im = im.convert('RGB')
if flatten:
im = im.convert('F')
elif im.mode == '1':
# Workaround for crash in PIL. When im is 1-bit, the call array(im)
# can cause a seg. fault, or generate garbage. See
# https://github.com/scipy/scipy/issues/2138 and
# https://github.com/python-pillow/Pillow/issues/350.
#
# This converts im from a 1-bit image to an 8-bit image.
im = im.convert('L')
a = array(im)
return a
def imread(name, flatten=False, mode=None):
"""
Read an image from a file as an array.
This function is only available if Python Imaging Library (PIL) is installed.
Parameters
----------
name : str or file object
The file name or file object to be read.
flatten : bool, optional
If True, flattens the color layers into a single gray-scale layer.
mode : str, optional
Mode to convert image to, e.g. ``'RGB'``. See the Notes for more
details.
Returns
-------
imread : ndarray
The array obtained by reading the image.
Notes
-----
`imread` uses the Python Imaging Library (PIL) to read an image.
The following notes are from the PIL documentation.
`mode` can be one of the following strings:
* 'L' (8-bit pixels, black and white)
* 'P' (8-bit pixels, mapped to any other mode using a color palette)
* 'RGB' (3x8-bit pixels, true color)
* 'RGBA' (4x8-bit pixels, true color with transparency mask)
* 'CMYK' (4x8-bit pixels, color separation)
* 'YCbCr' (3x8-bit pixels, color video format)
* 'I' (32-bit signed integer pixels)
* 'F' (32-bit floating point pixels)
PIL also provides limited support for a few special modes, including
'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
(true color with premultiplied alpha).
When translating a color image to black and white (mode 'L', 'I' or
'F'), the library uses the ITU-R 601-2 luma transform::
L = R * 299/1000 + G * 587/1000 + B * 114/1000
When `flatten` is True, the image is converted using mode 'F'.
When `mode` is not None and `flatten` is True, the image is first
converted according to `mode`, and the result is then flattened using
mode 'F'.
"""
im = Image.open(name)
return fromimage(im, flatten=flatten, mode=mode)
def bytescale(data, cmin=None, cmax=None, high=255, low=0):
"""
Byte scales an array (image).
Byte scaling means converting the input image to uint8 dtype and scaling
the range to ``(low, high)`` (default 0-255).
If the input image already has dtype uint8, no scaling is done.
This function is only available if Python Imaging Library (PIL) is installed.
Parameters
----------
data : ndarray
PIL image data array.
cmin : scalar, optional
Bias scaling of small values. Default is ``data.min()``.
cmax : scalar, optional
Bias scaling of large values. Default is ``data.max()``.
high : scalar, optional
Scale max value to `high`. Default is 255.
low : scalar, optional
Scale min value to `low`. Default is 0.
Returns
-------
img_array : uint8 ndarray
The byte-scaled array.
Examples
--------
>>> from scipy.misc import bytescale
>>> img = np.array([[ 91.06794177, 3.39058326, 84.4221549 ],
... [ 73.88003259, 80.91433048, 4.88878881],
... [ 51.53875334, 34.45808177, 27.5873488 ]])
>>> bytescale(img)
array([[255, 0, 236],
[205, 225, 4],
[140, 90, 70]], dtype=uint8)
>>> bytescale(img, high=200, low=100)
array([[200, 100, 192],
[180, 188, 102],
[155, 135, 128]], dtype=uint8)
>>> bytescale(img, cmin=0, cmax=255)
array([[91, 3, 84],
[74, 81, 5],
[52, 34, 28]], dtype=uint8)
"""
if data.dtype == np.uint8:
return data
if high > 255:
raise ValueError("`high` should be less than or equal to 255.")
if low < 0:
raise ValueError("`low` should be greater than or equal to 0.")
if high < low:
raise ValueError("`high` should be greater than or equal to `low`.")
if cmin is None:
cmin = data.min()
if cmax is None:
cmax = data.max()
cscale = cmax - cmin
if cscale < 0:
raise ValueError("`cmax` should be larger than `cmin`.")
elif cscale == 0:
cscale = 1
scale = float(high - low) / cscale
bytedata = (data - cmin) * scale + low
return (bytedata.clip(low, high) + 0.5).astype(np.uint8)
def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
mode=None, channel_axis=None):
"""Takes a numpy array and returns a PIL image.
This function is only available if Python Imaging Library (PIL) is installed.
The mode of the PIL image depends on the array shape and the `pal` and
`mode` keywords.
For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values
(from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode
is given as 'F' or 'I' in which case a float and/or integer array is made.
.. warning::
This function uses `bytescale` under the hood to rescale images to use
the full (0, 255) range if ``mode`` is one of ``None, 'L', 'P', 'l'``.
It will also cast data for 2-D images to ``uint32`` for ``mode=None``
(which is the default).
Notes
-----
For 3-D arrays, the `channel_axis` argument tells which dimension of the
array holds the channel data.
For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
by default or 'YCbCr' if selected.
The numpy array must be either 2 dimensional or 3 dimensional.
"""
data = np.asarray(arr)
if np.iscomplexobj(data):
raise ValueError("Cannot convert a complex-valued array.")
shape = list(data.shape)
valid = len(shape) == 2 or ((len(shape) == 3) and
((3 in shape) or (4 in shape)))
if not valid:
raise ValueError("'arr' does not have a suitable array shape for "
"any mode.")
if len(shape) == 2:
shape = (shape[1], shape[0]) # columns show up first
if mode == 'F':
data32 = data.astype(np.float32)
image = Image.frombytes(mode, shape, data32.tostring())
return image
if mode in [None, 'L', 'P']:
bytedata = bytescale(data, high=high, low=low,
cmin=cmin, cmax=cmax)
image = Image.frombytes('L', shape, bytedata.tostring())
if pal is not None:
image.putpalette(np.asarray(pal, dtype=np.uint8).tostring())
# Becomes a mode='P' automagically.
elif mode == 'P': # default gray-scale
pal = (np.arange(0, 256, 1, dtype=np.uint8)[:, np.newaxis] *
np.ones((3,), dtype=np.uint8)[np.newaxis, :])
image.putpalette(np.asarray(pal, dtype=np.uint8).tostring())
return image
if mode == '1': # high input gives threshold for 1
bytedata = (data > high)
image = Image.frombytes('1', shape, bytedata.tostring())
return image
if cmin is None:
cmin = np.amin(np.ravel(data))
if cmax is None:
cmax = np.amax(np.ravel(data))
data = (data*1.0 - cmin)*(high - low)/(cmax - cmin) + low
if mode == 'I':
data32 = data.astype(np.uint32)
image = Image.frombytes(mode, shape, data32.tostring())
else:
raise ValueError(_errstr)
return image
# if here then 3-d array with a 3 or a 4 in the shape length.
# Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
if channel_axis is None:
if (3 in shape):
ca = np.flatnonzero(np.asarray(shape) == 3)[0]
else:
ca = np.flatnonzero(np.asarray(shape) == 4)
if len(ca):
ca = ca[0]
else:
raise ValueError("Could not find channel dimension.")
else:
ca = channel_axis
numch = shape[ca]
if numch not in [3, 4]:
raise ValueError("Channel axis dimension is not valid.")
bytedata = bytescale(data, high=high, low=low, cmin=cmin, cmax=cmax)
if ca == 2:
strdata = bytedata.tostring()
shape = (shape[1], shape[0])
elif ca == 1:
strdata = np.transpose(bytedata, (0, 2, 1)).tostring()
shape = (shape[2], shape[0])
elif ca == 0:
strdata = np.transpose(bytedata, (1, 2, 0)).tostring()
shape = (shape[2], shape[1])
if mode is None:
if numch == 3:
mode = 'RGB'
else:
mode = 'RGBA'
if mode not in ['RGB', 'RGBA', 'YCbCr', 'CMYK']:
raise ValueError(_errstr)
if mode in ['RGB', 'YCbCr']:
if numch != 3:
raise ValueError("Invalid array shape for mode.")
if mode in ['RGBA', 'CMYK']:
if numch != 4:
raise ValueError("Invalid array shape for mode.")
# Here we know data and mode is correct
image = Image.frombytes(mode, shape, strdata)
return image
def imsave(name, arr, format=None):
"""
Save an array as an image.
This function is only available if Python Imaging Library (PIL) is installed.
.. warning::
This function uses `bytescale` under the hood to rescale images to use
the full (0, 255) range if ``mode`` is one of ``None, 'L', 'P', 'l'``.
It will also cast data for 2-D images to ``uint32`` for ``mode=None``
(which is the default).
Parameters
----------
name : str or file object
Output file name or file object.
arr : ndarray, MxN or MxNx3 or MxNx4
Array containing image values. If the shape is ``MxN``, the array
represents a grey-level image. Shape ``MxNx3`` stores the red, green
and blue bands along the last dimension. An alpha layer may be
included, specified as the last colour band of an ``MxNx4`` array.
format : str
Image format. If omitted, the format to use is determined from the
file name extension. If a file object was used instead of a file name,
this parameter should always be used.
Examples
--------
Construct an array of gradient intensity values and save to file:
>>> from scipy.misc import imsave
>>> x = np.zeros((255, 255))
>>> x = np.zeros((255, 255), dtype=np.uint8)
>>> x[:] = np.arange(255)
>>> imsave('gradient.png', x)
Construct an array with three colour bands (R, G, B) and store to file:
>>> rgb = np.zeros((255, 255, 3), dtype=np.uint8)
>>> rgb[..., 0] = np.arange(255)
>>> rgb[..., 1] = 55
>>> rgb[..., 2] = 1 - np.arange(255)
>>> imsave('rgb_gradient.png', rgb)
"""
im = toimage(arr, channel_axis=2)
if format is None:
im.save(name)
else:
im.save(name, format)
return
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
random.seed(1)
np.random.seed(1)
torch.manual_seed(1)
torch.cuda.manual_seed(1)
class SamplingMode(Enum):
UNIFORM = 0
DENSE = 1
Full = 2
Regular = 3
def diff_annotation_data_directories(annotation_directory_path, data_directory_path):
annotation_subdir = sorted(os.listdir(annotation_directory_path))
data_subdir = sorted(os.listdir(data_directory_path))
if (annotation_subdir == data_subdir) and (len(annotation_subdir) == len(data_subdir)):
print("The lists are equal.")
else:
print("The lists are not equal.")
print("=======================")
print(annotation_subdir[:10])
print(data_subdir[:10])
def make_categories_dict(meta_dict, name):
category_list = []
if name == "ytvos":
video_name_list = meta_dict["videos"].keys()
for name in video_name_list:
obj_list = meta_dict["videos"][name]["objects"].keys()
for obj in obj_list:
if meta_dict["videos"][name]["objects"][obj]["category"] not in category_list:
category_list.append(meta_dict["videos"][name]["objects"][obj]["category"])
category_list = sorted(list(OrderedDict.fromkeys(category_list)))
category_ditct = {k: v+1 for v, k in enumerate(category_list)} ## zero is always for the background
return category_ditct
## Be careful it is not considering each annotation sample independently!!!!
## We need to convert masks to the indexed_RGB format to adapt it to the further stages of evaluation.
def convert_to_indexed_RGB(inp):
bs, fs, channel, h, w = inp.shape
# plt.imshow(inp[0, 0].permute(1, 2, 0))
# plt.show()
inp = inp.permute(0, 1, 3, 4, 2)
inp = inp.contiguous().view(-1, channel)
unique_colors = np.unique(inp.numpy(), axis=0)
indexed_inp = torch.zeros((inp.shape[0], 1))
# print(unique_colors)
# print(unique_colors)
for i, color in enumerate(unique_colors):
mask = torch.all(inp == color, dim=1)
indexed_inp[mask] = i
indexed_inp = indexed_inp.view(bs, fs, h, w)
# plt.imshow(20 * indexed_inp[0, 0].unsqueeze(2).repeat(1, 1, 3))
# plt.show()
return indexed_inp
## supposes the input is between 0 and 1
def map_instances(data, meta, category_dict):
bs, fs, h, w = data.shape
for i, datum in enumerate(data):
for j, frame in enumerate(data):
objects = torch.unique(frame)
for k, obj in enumerate(objects):
if int(obj.item()) == 0:
continue
frame[frame == obj] = category_dict[meta[str(int(obj.item()))]["category"]]
return data
def build_dataset_tree(initial_directory, class_trajectory, dataset_dict, num_labels=1):
dirs = os.listdir(initial_directory)
class_num = 0
for path in dirs:
if os.path.isfile(initial_directory + path):
if len(class_trajectory) == 0:
continue
dir_name = initial_directory + path.split('.')[0]
dataset_dict[dir_name] = np.array(class_trajectory)
if not os.path.isdir(dir_name):
os.mkdir(dir_name)
else:
# print(f"The directory {dir_name} exists!")
continue
reader = cv2.VideoCapture(initial_directory + path)
frame_num = 0
while(True):
ret, frame = reader.read()
if not ret:
break
cv2.imwrite(dir_name + "/" + f'{frame_num:05d}' + ".jpg", frame)
frame_num += 1
cv2.destroyAllWindows()
reader.release()
os.remove(initial_directory + path)
else:
if len(class_trajectory) == num_labels:
dataset_dict[initial_directory + path] = np.array(class_trajectory)
else:
build_dataset_tree(initial_directory + path + "/", class_trajectory + [class_num], dataset_dict, num_labels)
class_num += 1
return dataset_dict
class VideoDataset(torch.utils.data.Dataset):
## The data loader gets training sample and annotations direcotories, sampling mode, number of clips that is being sampled of each training video, number of frames in each clip
## and number of labels for each training clip.
## Note that the number of annotations should be exactly similar to the number of frames existing in the training path.
## Frame_transform is a function that transforms the frames of the video. It is applied to each frame of the video.
## Target_transform is a function that transforms the annotations of the video. It is applied to each annotation of the video.
## Video_transform is a function that transforms the whole video. It is applied to both frames and annotations of the video.
## The same set of transformations is applied to the clips of the video.
def __init__(self, classes_directory, annotations_directory, sampling_mode, num_clips, num_frames, num_labels, frame_transform=None, target_transform=None, video_transform=None, meta_file_directory=None, regular_step=1) -> None:
super().__init__()
self.train_dict = build_dataset_tree(classes_directory, [], {}, num_labels)
self.train_dict_lenghts = {}
self.find_directory_length()
if (annotations_directory != "") and (os.path.exists(annotations_directory)):
self.train_annotations_dict = build_dataset_tree(annotations_directory, [], {}, num_labels)
self.use_annotations = True
else:
self.use_annotations = False
print("Because there is no annotation directory, only training samples have been loaded.")
if (meta_file_directory is not None):
if (os.path.exists(meta_file_directory)):
print("Meta file has been read.")
file = open(meta_file_directory)
self.meta_dict = json.load(file)
else:
self.meta_dict = None
print("There is no meta file.")
else:
print("Meta option is off.")
self.meta_dict = None
self.keys = sorted(list(self.train_dict.keys()))
if self.use_annotations:
self.annotation_keys = sorted(list(self.train_annotations_dict.keys()))
self.sampling_mode = sampling_mode
self.num_clips = num_clips
self.num_frames = num_frames
self.frame_transform = frame_transform
self.target_transform = target_transform
self.video_transform = video_transform
self.regular_step = regular_step
def __len__(self):
return len(self.keys)
def find_directory_length(self):
for key in self.train_dict:
self.train_dict_lenghts[key] = len(os.listdir(key))
def read_clips(self, path, clip_indices):
clips = []
files = sorted(glob.glob(path + "/" + "*.jpg"))
if len(files) == 0:
files = sorted(glob.glob(path + "/" + "*.png"))
for i in range(len(clip_indices)):
images = []
for j in clip_indices[i]:
# frame_path = path + "/" + f'{j:05d}' + ".jpg"
frame_path = files[j]
if not os.path.exists(frame_path):
frame_path = path + "/" + f'{j:05d}' + ".png"
if not os.path.exists(frame_path): ## This is for kinetics dataset
frame_path = path + "/" + f'img_{(j + 1):05d}' + ".jpg"
if not os.path.exists(frame_path): ## This is for kinetics dataset
frame_path = path + "/" + f'frame_{(j + 1):010d}' + ".jpg"
images.append(Image.open(frame_path))
clips.append(images)
return clips
def generate_indices(self, size, sampling_num):
indices = []
for i in range(self.num_clips):
if self.sampling_mode == SamplingMode.UNIFORM:
if size < sampling_num:
## sample repeatly
idx = random.choices(range(0, size), k=sampling_num)
else:
idx = random.sample(range(0, size), sampling_num)
idx.sort()
indices.append(idx)
elif self.sampling_mode == SamplingMode.DENSE:
base = random.randint(0, size - sampling_num)
idx = range(base, base + sampling_num)
indices.append(idx)
elif self.sampling_mode == SamplingMode.Full:
indices.append(range(0, size))
elif self.sampling_mode == SamplingMode.Regular:
if size < sampling_num * self.regular_step:
step = size // sampling_num
else:
step = self.regular_step
base = random.randint(0, size - (sampling_num * step))
idx = range(base, base + (sampling_num * step), step)
indices.append(idx)
return indices
def read_batch(self, path, annotation_path=None, frame_transformation=None, target_transformation=None, video_transformation=None):
size = self.train_dict_lenghts[path]
# sampling_num = size if self.num_frames > size else self.num_frames
clip_indices = self.generate_indices(self.train_dict_lenghts[path], self.num_frames)
sampled_clips = self.read_clips(path, clip_indices)
annotations = []
sampled_clip_annotations = []
if annotation_path is not None:
sampled_clip_annotations = self.read_clips(annotation_path, clip_indices)
if target_transformation is not None:
for i in range(len(sampled_clip_annotations)):
sampled_clip_annotations[i] = target_transformation(sampled_clip_annotations[i])
if frame_transformation is not None:
for i in range(len(sampled_clips)):
try:
sampled_clips[i] = frame_transformation(sampled_clips[i])
except:
print("Error in frame transformation")
if video_transformation is not None:
for i in range(len(sampled_clips)):
if len(sampled_clip_annotations) != 0:
sampled_clips[i], sampled_clip_annotations[i] = video_transformation(sampled_clips[i], sampled_clip_annotations[i])
else:
sampled_clips[i] = video_transformation(sampled_clips[i])
sampled_data = torch.stack(sampled_clips)
if len(sampled_clip_annotations) != 0:
sampled_annotations = torch.stack(sampled_clip_annotations)
if sampled_annotations.size(0) != 0:
sampled_annotations = (255 * sampled_annotations).type(torch.uint8)
if sampled_annotations.shape[2] == 1:
sampled_annotations = sampled_annotations.squeeze(2)
else:
sampled_annotations = torch.empty(0)
## squeezing the annotations to be in the shape of (num_sample, num_clips, num_frames, height, width)
return sampled_data, sampled_annotations
## Similar function to read_batch except the numbers are in the range 0-255 and uint8
# def read_annotations(self, path, frame_transformation=None, video_transformation=None, indices=None):
# files = sorted(glob.glob(path + "/" + "*.jpg"), key=numericalSort)
# if len(files) == 0:
# files = sorted(glob.glob(path + "/" + "*.png"), key=numericalSort)
# if (frame_transformation is not None):
# images = []
# for file in files:
# frame = Image.open(file).squeeze()
# images.append(frame)
# images = frame_transformation(images)
# images = torch.stack(images)
# else:
# images = torch.stack([torch.Tensor(Image.open(file)) for file in files])
# size = images.shape[0]
# sampling_num = size if self.num_frames > size else self.num_frames
# if self.sampling_mode == SamplingMode.UNIFORM:
# data = []
# if video_transformation is not None:
# for i in range(self.num_clips):
# if indices is not None:
# idx = indices
# else:
# idx = random.sample(range(0, size), sampling_num)
# idx.sort()
# data += [images[idx]]
# else:
# for i in range(self.num_clips):
# if indices is not None:
# idx = indices
# else:
# idx = random.sample(range(0, size), sampling_num)
# idx.sort()
# data += [images[idx]]
# data = torch.stack(data)
# elif self.sampling_mode == SamplingMode.DENSE:
# data = []
# for i in range(self.num_clips):
# base = random.randint(0, size - sampling_num)
# if indices is not None:
# idx = indices
# else:
# idx = range(base, base + sampling_num)
# if video_transformation is not None:
# data += [video_transformation(images[idx])]
# else:
# data += [images[idx]]
# data = torch.stack(data)
# elif self.sampling_mode == SamplingMode.Full:
# data = []
# for i in range(self.num_clips):
# if video_transformation is not None:
# data += [video_transformation(images)]
# else:
# data += [images]
# data = torch.stack(data)
# data = (255 * data).type(torch.uint8)
# return data
def __getitem__(self, idx): ### (B, num_clips, num_frames, C, H, W) returns None if the annotation flag is off. Be careful when loading the data.
# idx = 0 ## This is a hack to make the code work with the dataloader.
# idx = random.randint(0, 5)
video_path = self.keys[idx]
dir_name = video_path.split("/")[-1]
video_label = np.tile(self.train_dict[video_path], (self.num_clips, ))
annotations = None
annotations_path = None
if (self.use_annotations):
annotations_path = self.annotation_keys[idx]
# annotations = self.read_annotations(annotations_path, self.target_transform, indices=indices)
data, annotations = self.read_batch(video_path, annotations_path, self.frame_transform, self.target_transform ,self.video_transform)
if self.meta_dict is not None:
category_dict = make_categories_dict(self.meta_dict, "davis")
meta_dict = self.meta_dict["videos"][dir_name]["objects"]
annotations = map_instances(annotations, meta_dict, category_dict)
# else:
# annotations = convert_to_indexed_RGB(annotations)
# print(data.shape)
# print(annotations.shape)
return data, annotations, torch.Tensor(video_label)
class YVOSDataset(VideoDataset):
def __init__(self, classes_directory, annotations_directory, sampling_mode, num_clips, num_frames, num_labels, frame_transform=None, target_transform=None, video_transform=None, meta_file_directory=None, regular_step=1) -> None:
super().__init__(classes_directory, annotations_directory, sampling_mode, num_clips, num_frames, num_labels, frame_transform, target_transform, video_transform, meta_file_directory, regular_step=regular_step)
self.category_dict = make_categories_dict(self.meta_dict, "ytvos")
def __getitem__(self, idx): ### (B, num_clips, num_frames, C, H, W) returns None if the annotation flag is off. Be careful when loading the data.
video_path = self.keys[idx]
dir_name = video_path.split("/")[-1]
video_label = np.tile(self.train_dict[video_path], (self.num_clips, ))
annotations = None
annotations_path = None
if (self.use_annotations):
annotations_path = self.annotation_keys[idx]
# annotations = self.read_annotations(annotations_path, self.target_transform, indices=indices)
data, annotations = self.read_batch(video_path, annotations_path, self.frame_transform, self.target_transform ,self.video_transform)
if self.meta_dict is not None:
annotations = map_instances(annotations, self.meta_dict["videos"][dir_name]["objects"], self.category_dict)
# else:
# annotations = convert_to_indexed_RGB(annotations)
return data, annotations, torch.Tensor(video_label)
class Kinetics(VideoDataset):
def __init__(self, classes_directory, sampling_mode, num_clips, num_frames, num_labels, frame_transform=None, target_transform=None, video_transform=None, meta_file_directory=None, regular_step=1) -> None:
super().__init__(classes_directory, "", sampling_mode, num_clips, num_frames, num_labels, frame_transform, target_transform, video_transform, meta_file_directory, regular_step=regular_step)
def __getitem__(self, idx): ### (B, num_clips, num_frames, C, H, W) returns None if the annotation flag is off. Be careful when loading the data.
video_path = self.keys[idx]
dir_name = video_path.split("/")[-1]
video_label = np.tile(self.train_dict[video_path], (self.num_clips, ))
annotations = None
annotations_path = None
data, annotations = self.read_batch(video_path, None, self.frame_transform, self.target_transform ,self.video_transform)
if self.meta_dict is not None:
annotations = map_instances(annotations, self.meta_dict["videos"][dir_name]["objects"], self.category_dict)
# else:
# annotations = convert_to_indexed_RGB(annotations)
return data, annotations, torch.Tensor(video_label)
class pascalVOCLoader(data.Dataset):
"""Data loader for the Pascal VOC semantic segmentation dataset.
Annotations from both the original VOC data (which consist of RGB images
in which colours map to specific classes) and the SBD (Berkely) dataset
(where annotations are stored as .mat files) are converted into a common
`label_mask` format. Under this format, each mask is an (M,N) array of
integer values from 0 to 21, where 0 represents the background class.
The label masks are stored in a new folder, called `pre_encoded`, which
is added as a subdirectory of the `SegmentationClass` folder in the
original Pascal VOC data layout.
A total of five data splits are provided for working with the VOC data:
train: The original VOC 2012 training data - 1464 images
val: The original VOC 2012 validation data - 1449 images
trainval: The combination of `train` and `val` - 2913 images
train_aug: The unique images present in both the train split and
training images from SBD: - 8829 images (the unique members
of the result of combining lists of length 1464 and 8498)
train_aug_val: The original VOC 2012 validation data minus the images
present in `train_aug` (This is done with the same logic as
the validation set used in FCN PAMI paper, but with VOC 2012
rather than VOC 2011) - 904 images
"""
def __init__(
self,
root,
sbd_path=None,
split="train_aug",
is_transform=False,
img_size=512,
augmentations=None,
img_norm=True,
test_mode=False,
val=False,
):
self.root = root
self.sbd_path = sbd_path
self.split = split
self.is_transform = is_transform
self.augmentations = augmentations
self.img_norm = img_norm
self.test_mode = test_mode
self.n_classes = 21
self.mean = np.array([104.00699, 116.66877, 122.67892])
self.files = collections.defaultdict(list)
self.img_size = img_size if isinstance(img_size, tuple) else (img_size, img_size)
if not self.test_mode:
for split in ["train", "val", "trainval"]:
path = pjoin(self.root, "ImageSets/Segmentation", split + ".txt")
file_list = tuple(open(path, "r"))
file_list = [id_.rstrip() for id_ in file_list]
self.files[split] = file_list
self.setup_annotations()
if val:
self.tf = transforms.Compose(
[
transforms.ToTensor(),
# transforms.RandomHorizontalFlip(),
# transforms.RandomResizedCrop(size=self.img_size),
transforms.Normalize([0.485, 0.456, 0.406], [0.228, 0.224, 0.225])
]
)
else:
self.tf = transforms.Compose(
[
transforms.ToTensor(),
transforms.RandomResizedCrop(size=self.img_size, scale=(0.8, 1.)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.Normalize([0.485, 0.456, 0.406], [0.228, 0.224, 0.225])
]
)
def __len__(self):
return len(self.files[self.split])
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
im_name = self.files[self.split][index]
im_path = pjoin(self.root, "JPEGImages", im_name + ".jpg")
lbl_path = pjoin(self.root, "SegmentationClass/pre_encoded", im_name + ".png")
im = Image.open(im_path)
lbl = Image.open(lbl_path)
if self.augmentations is not None:
im, lbl = self.augmentations(im, lbl)
if self.is_transform:
im, lbl = self.transform(im, lbl)
return im, lbl
def transform(self, img, lbl):
if self.img_size == ("same", "same"):
pass
else:
img = img.resize((self.img_size[0], self.img_size[1])) # uint8 with RGB mode
lbl = lbl.resize((self.img_size[0], self.img_size[1]), Image.NEAREST)
img = self.tf(img)
lbl = torch.from_numpy(np.array(lbl)).long()
lbl[lbl == 255] = 0
return img, lbl
def get_pascal_labels(self):
"""Load the mapping that associates pascal classes with label colors
Returns:
np.ndarray with dimensions (21, 3)
"""
return np.asarray(
[
[0, 0, 0],
[128, 0, 0],
[0, 128, 0],
[128, 128, 0],
[0, 0, 128],
[128, 0, 128],
[0, 128, 128],
[128, 128, 128],
[64, 0, 0],
[192, 0, 0],
[64, 128, 0],
[192, 128, 0],
[64, 0, 128],
[192, 0, 128],
[64, 128, 128],
[192, 128, 128],
[0, 64, 0],
[128, 64, 0],
[0, 192, 0],
[128, 192, 0],
[0, 64, 128],
]
)
def encode_segmap(self, mask):
"""Encode segmentation label images as pascal classes
Args:
mask (np.ndarray): raw segmentation label image of dimension
(M, N, 3), in which the Pascal classes are encoded as colours.
Returns:
(np.ndarray): class map with dimensions (M,N), where the value at
a given location is the integer denoting the class index.
"""
mask = mask.astype(int)
label_mask = np.zeros((mask.shape[0], mask.shape[1]), dtype=np.int16)
for ii, label in enumerate(self.get_pascal_labels()):
label_mask[np.where(np.all(mask == label, axis=-1))[:2]] = ii
label_mask = label_mask.astype(int)
return label_mask
def decode_segmap(self, label_mask, plot=False):
"""Decode segmentation class labels into a color image
Args:
label_mask (np.ndarray): an (M,N) array of integer values denoting
the class label at each spatial location.
plot (bool, optional): whether to show the resulting color image
in a figure.
Returns:
(np.ndarray, optional): the resulting decoded color image.
"""
label_colours = self.get_pascal_labels()
r = label_mask.copy()
g = label_mask.copy()
b = label_mask.copy()
for ll in range(0, self.n_classes):
r[label_mask == ll] = label_colours[ll, 0]
g[label_mask == ll] = label_colours[ll, 1]
b[label_mask == ll] = label_colours[ll, 2]
rgb = np.zeros((label_mask.shape[0], label_mask.shape[1], 3))
rgb[:, :, 0] = r / 255.0
rgb[:, :, 1] = g / 255.0
rgb[:, :, 2] = b / 255.0
if plot:
plt.imshow(rgb)
plt.show()
else:
return rgb