-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathdavis.py
203 lines (169 loc) · 8.33 KB
/
davis.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
import os
from glob import glob
from collections import defaultdict
import numpy as np
from PIL import Image
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
from os import path
def all_to_onehot(masks, labels):
if len(masks.shape) == 3:
Ms = np.zeros((len(labels), masks.shape[0], masks.shape[1], masks.shape[2]), dtype=np.uint8)
else:
Ms = np.zeros((len(labels), masks.shape[0], masks.shape[1]), dtype=np.uint8)
for k, l in enumerate(labels):
Ms[k] = (masks == l).astype(np.uint8)
return Ms
class DAVISTestDataset(Dataset):
def __init__(self, root, imset='2017/val.txt', resolution=480, single_object=False, target_name=None):
self.root = root
if resolution == 480:
res_tag = '480p'
else:
res_tag = 'Full-Resolution'
self.mask_dir = path.join(root, 'Annotations', res_tag)
self.mask480_dir = path.join(root, 'Annotations', '480p')
self.image_dir = path.join(root, 'JPEGImages', res_tag)
self.resolution = resolution
_imset_dir = path.join(root, 'ImageSets')
_imset_f = path.join(_imset_dir, imset)
self.videos = []
self.num_frames = {}
self.num_objects = {}
self.shape = {}
self.size_480p = {}
with open(path.join(_imset_f), "r") as lines:
for line in lines:
_video = line.rstrip('\n')
if target_name is not None and target_name != _video:
continue
self.videos.append(_video)
self.num_frames[_video] = len(os.listdir(path.join(self.image_dir, _video)))
_mask = np.array(Image.open(path.join(self.mask_dir, _video, '00000.png')).convert("P"))
self.num_objects[_video] = np.max(_mask)
self.shape[_video] = np.shape(_mask)
_mask480 = np.array(Image.open(path.join(self.mask480_dir, _video, '00000.png')).convert("P"))
self.size_480p[_video] = np.shape(_mask480)
self.single_object = single_object
def __len__(self):
return len(self.videos)
def __getitem__(self, index):
video = self.videos[index]
info = {}
info['name'] = video
info['frames'] = []
info['num_frames'] = self.num_frames[video]
info['size_480p'] = self.size_480p[video]
images = []
masks = []
for f in range(self.num_frames[video]):
img_file = path.join(self.image_dir, video, '{:05d}.jpg'.format(f))
img = Image.open(img_file).convert('RGB')
img = np.array(img, dtype = 'uint8')
images.append(img)
info['frames'].append('{:05d}.jpg'.format(f))
mask_file = path.join(self.mask_dir, video, '{:05d}.png'.format(f))
if path.exists(mask_file):
m = np.array(Image.open(mask_file).convert('P'), dtype=np.uint8) #(480, 910)
masks.append(m) #(480, 910), numpy
else:
masks.append(np.zeros_like(masks[0]))
images = np.stack(images, 0)
masks = np.stack(masks, 0)
if self.single_object:
labels = [1]
masks = (masks > 0.5).astype(np.uint8)
masks = all_to_onehot(masks, labels)
else:
labels = np.unique(masks[0])
labels = labels[labels!=0]
masks = all_to_onehot(masks, labels)
info['labels'] = labels
data = {
'rgb': images,
'gt': masks,
'info': info,
}
return data
class DAVIS(object):
SUBSET_OPTIONS = ['train', 'val', 'test-dev', 'test-challenge']
TASKS = ['semi-supervised', 'unsupervised']
DATASET_WEB = 'https://davischallenge.org/davis2017/code.html'
VOID_LABEL = 255
def __init__(self, root, task='unsupervised', subset='val', sequences='all', resolution='480p', codalab=False):
"""
Class to read the DAVIS dataset
:param root: Path to the DAVIS folder that contains JPEGImages, Annotations, etc. folders.
:param task: Task to load the annotations, choose between semi-supervised or unsupervised.
:param subset: Set to load the annotations
:param sequences: Sequences to consider, 'all' to use all the sequences in a set.
:param resolution: Specify the resolution to use the dataset, choose between '480' and 'Full-Resolution'
"""
if subset not in self.SUBSET_OPTIONS:
raise ValueError(f'Subset should be in {self.SUBSET_OPTIONS}')
if task not in self.TASKS:
raise ValueError(f'The only tasks that are supported are {self.TASKS}')
self.task = task
self.subset = subset
self.root = root
self.img_path = os.path.join(self.root, 'JPEGImages', resolution)
annotations_folder = 'Annotations' if task == 'semi-supervised' else 'Annotations_unsupervised'
self.mask_path = os.path.join(self.root, annotations_folder, resolution)
year = '2019' if task == 'unsupervised' and (subset == 'test-dev' or subset == 'test-challenge') else '2017'
self.imagesets_path = os.path.join(self.root, 'ImageSets', year)
self._check_directories()
if sequences == 'all':
with open(os.path.join(self.imagesets_path, f'{self.subset}.txt'), 'r') as f:
tmp = f.readlines()
sequences_names = [x.strip() for x in tmp]
else:
sequences_names = sequences if isinstance(sequences, list) else [sequences]
self.sequences = defaultdict(dict)
for seq in sequences_names:
images = np.sort(glob(os.path.join(self.img_path, seq, '*.jpg'))).tolist()
if len(images) == 0 and not codalab:
raise FileNotFoundError(f'Images for sequence {seq} not found.')
self.sequences[seq]['images'] = images
masks = np.sort(glob(os.path.join(self.mask_path, seq, '*.png'))).tolist()
masks.extend([-1] * (len(images) - len(masks)))
self.sequences[seq]['masks'] = masks
def _check_directories(self):
if not os.path.exists(self.root):
raise FileNotFoundError(f'DAVIS not found in the specified directory, download it from {self.DATASET_WEB}')
if not os.path.exists(os.path.join(self.imagesets_path, f'{self.subset}.txt')):
raise FileNotFoundError(f'Subset sequences list for {self.subset} not found, download the missing subset '
f'for the {self.task} task from {self.DATASET_WEB}')
if self.subset in ['train', 'val'] and not os.path.exists(self.mask_path):
raise FileNotFoundError(f'Annotations folder for the {self.task} task not found, download it from {self.DATASET_WEB}')
def get_frames(self, sequence):
for img, msk in zip(self.sequences[sequence]['images'], self.sequences[sequence]['masks']):
image = np.array(Image.open(img))
mask = None if msk is None else np.array(Image.open(msk))
yield image, mask
def _get_all_elements(self, sequence, obj_type):
obj = np.array(Image.open(self.sequences[sequence][obj_type][0]))
all_objs = np.zeros((len(self.sequences[sequence][obj_type]), *obj.shape))
obj_id = []
for i, obj in enumerate(self.sequences[sequence][obj_type]):
all_objs[i, ...] = np.array(Image.open(obj))
obj_id.append(''.join(obj.split('/')[-1].split('.')[:-1]))
return all_objs, obj_id
def get_all_images(self, sequence):
return self._get_all_elements(sequence, 'images')
def get_all_masks(self, sequence, separate_objects_masks=False):
masks, masks_id = self._get_all_elements(sequence, 'masks')
masks_void = np.zeros_like(masks)
# Separate void and object masks
for i in range(masks.shape[0]):
masks_void[i, ...] = masks[i, ...] == 255
masks[i, masks[i, ...] == 255] = 0
if separate_objects_masks:
num_objects = int(np.max(masks[0, ...]))
tmp = np.ones((num_objects, *masks.shape))
tmp = tmp * np.arange(1, num_objects + 1)[:, None, None, None]
masks = (tmp == masks[None, ...])
masks = masks > 0
return masks, masks_void, masks_id
def get_sequences(self):
for seq in self.sequences:
yield seq