def _init_data(self): """ Store some metadata that needs to be known during training. In order to sample, the viable sequences must be known. Sequences are viable if a snippet of given sample length can be selected, starting with an annotated frame and containing at least one more annotated frame. """ print("-- YTVOS dataset initialization started.") framework_path = os.path.join(os.path.dirname(__file__), '..') cache_path = os.path.join(framework_path, 'cache', 'ytvos_v2_{}_100px_threshold.json'.format(self._split)) # First find visible objects in all annotated frames if os.path.exists(cache_path): with open(cache_path, 'r') as f: self._visible_objects, self._resolutions = json.load(f) self._visible_objects = {seqname: OrderedDict((int(idx), objlst) for idx, objlst in val.items()) for seqname, val in self._visible_objects.items()} assert len(self._visible_objects) == len(self._resolutions) print("Datafile {} loaded, describing {} sequences.".format(cache_path, len(self._visible_objects))) else: # Grab all sequences in dataset seqnames = os.listdir(os.path.join(self._root_path, self._split, self._impath)) # Construct meta-info self._visible_objects = {} self._resolutions = {} for seqname in tqdm.tqdm(seqnames): anno_paths = sorted(glob.glob(self._full_anno_path(seqname, "*.png"))) self._visible_objects[seqname] = OrderedDict( (self._frame_name_to_idx(os.path.basename(path)), get_anno_ids(path, dataset_utils.LabelToLongTensor(), 100)) for path in anno_paths) self._resolutions[seqname] = Image.open(anno_paths[0]).size[::-1] # Save meta-info if not os.path.exists(os.path.dirname(cache_path)): os.makedirs(os.path.dirname(cache_path)) with open(cache_path, 'w') as f: json.dump((self._visible_objects, self._resolutions), f) print("Datafile {} was not found, creating it with {} sequences.".format( cache_path, len(self._visible_objects))) # Find sequences in the requested image_set if self._split == 'train': with open(os.path.join(self._root_path, "ImageSets", self._image_set + '.txt'), 'r') as f: self._all_seqs = f.read().splitlines() print("{} sequences found in image set \"{}\"".format(len(self._all_seqs), self._image_set)) else: self._all_seqs = os.listdir(os.path.join(self._root_path, self._split, "Annotations")) print("{} sequences found in the Annotations directory.".format(len(self._all_seqs))) # Filter out sequences that are too short from first frame with object, to last annotation self._nonempty_frame_ids = {seq: [frame_idx for frame_idx, obj_ids in lst.items() if len(obj_ids) >= self._min_num_objects] for seq, lst in self._visible_objects.items()} self._viable_seqs = [seq for seq in self._all_seqs if len(self._nonempty_frame_ids[seq]) > 0 and len(self.get_image_frame_ids(seq)[min(self._nonempty_frame_ids[seq]) : max(self._visible_objects[seq].keys()) + 1]) >= self._seqlen] print("{} sequences remaining after filtering on length (from first anno obj appearance to last anno frame.".format(len(self._viable_seqs)))
def label_read(path): if os.path.exists(path): pic = Image.open(path) transform = tv.transforms.Compose( [tv.transforms.Resize(size, interpolation=Image.NEAREST), dataset_utils.LabelToLongTensor()]) label = transform(pic) else: label = torch.LongTensor(1,*size).fill_(255) # Put label that will be ignored return label
def _init_data(self): framework_path = os.path.join(os.path.dirname(__file__), '..') cache_path = os.path.join( framework_path, 'cache', 'davis17_v2_visible_objects_100px_threshold.json') if os.path.exists(cache_path): with open(cache_path, 'r') as f: self._visible_objects = json.load(f) self._visible_objects = { seqname: OrderedDict( (int(idx), objlst) for idx, objlst in val.items()) for seqname, val in self._visible_objects.items() } else: seqnames = os.listdir( os.path.join(self._root_path, 'JPEGImages', '480p')) self._visible_objects = {} for seqname in seqnames: anno_paths = sorted( glob.glob(self._full_anno_path(seqname, '*.png'))) self._visible_objects[seqname] = OrderedDict( (self._frame_name_to_idx(os.path.basename(path)), get_anno_ids(path, dataset_utils.LabelToLongTensor(), 100) ) for path in anno_paths) if not os.path.exists(os.path.dirname(cache_path)): os.makedirs(os.path.dirname(cache_path)) with open(cache_path, 'w') as f: json.dump(self._visible_objects, f) print("Datafile {} was not found, creating it with {} sequences.". format(cache_path, len(self._visible_objects))) with open( os.path.join(self._root_path, 'ImageSets', self._version, self._image_set + '.txt'), 'r') as f: self._all_seqs = f.read().splitlines() self._nonempty_frame_ids = { seq: [ frame_idx for frame_idx, obj_ids in lst.items() if len(obj_ids) >= self._min_num_objects ] for seq, lst in self._visible_objects.items() } self._viable_seqs = [ seq for seq in self._all_seqs if len(self._nonempty_frame_ids[seq]) > 0 and len( self.get_image_frame_ids(seq) [min(self._nonempty_frame_ids[seq] ):max(self._visible_objects[seq].keys()) + 1]) >= self._seqlen ]
def _init_data(self): """ Store some metadata that needs to be known during training. In order to sample, the viable sequences must be known. Sequences are viable if a snippet of given sample length can be selected, starting with an annotated frame and containing at least one more annotated frame. """ print("-- DAVIS17 dataset initialization started.") framework_path = os.path.join(os.path.dirname(__file__), "..") cache_path = os.path.join( framework_path, "cache", "davis17_v2_visible_objects_100px_threshold.json" ) # First find visible objects in all annotated frames if os.path.exists(cache_path): with open(cache_path, "r") as f: self._visible_objects = json.load(f) self._visible_objects = { seqname: OrderedDict( (int(idx), objlst) for idx, objlst in val.items() ) for seqname, val in self._visible_objects.items() } print( "Datafile {} loaded, describing {} sequences.".format( cache_path, len(self._visible_objects) ) ) else: # Grab all sequences in dataset seqnames = os.listdir(os.path.join(self._root_path, "JPEGImages", "480p")) # Construct meta-info self._visible_objects = {} for seqname in seqnames: anno_paths = sorted(glob.glob(self._full_anno_path(seqname, "*.png"))) self._visible_objects[seqname] = OrderedDict( ( self._frame_name_to_idx(os.path.basename(path)), get_anno_ids(path, dataset_utils.LabelToLongTensor(), 100), ) for path in anno_paths ) if not os.path.exists(os.path.dirname(cache_path)): os.makedirs(os.path.dirname(cache_path)) with open(cache_path, "w") as f: json.dump(self._visible_objects, f) print( "Datafile {} was not found, creating it with {} sequences.".format( cache_path, len(self._visible_objects) ) ) # Find sequences in the requested image_set with open( os.path.join( self._root_path, "ImageSets", self._version, self._image_set + ".txt" ), "r", ) as f: self._all_seqs = f.read().splitlines() print( '{} sequences found in image set "{}"'.format( len(self._all_seqs), self._image_set ) ) # Filter out sequences that are too short from first frame with object, to last annotation self._nonempty_frame_ids = { seq: [ frame_idx for frame_idx, obj_ids in lst.items() if len(obj_ids) >= self._min_num_objects ] for seq, lst in self._visible_objects.items() } self._viable_seqs = [ seq for seq in self._all_seqs if len(self._nonempty_frame_ids[seq]) > 0 and len( self.get_image_frame_ids(seq)[ min(self._nonempty_frame_ids[seq]) : max( self._visible_objects[seq].keys() ) + 1 ] ) >= self._seqlen ] print( "{} sequences remaining after filtering on length (from first anno obj appearance to last anno frame.".format( len(self._viable_seqs) ) )