def __getitem__(self, item): if self.preloaded_images is not None: im = self.preloaded_images[item] else: im = imread(self.images[item]) lb = [] f = self.frame_name(item) obj_ids = self.start_frames.get(f, []) if len(obj_ids) > 0: lb = imread(self.anno_path / (f + ".png")) if self.merge_objects: lb = (lb != 0).byte() obj_ids = [1] else: # Suppress labels of objects not in their start frame (primarily for YouTubeVOS) suppressed_obj_ids = list( set(lb.unique().tolist()) - set([0] + obj_ids)) for obj_id in suppressed_obj_ids: lb[lb == obj_id] = 0 return im, lb, obj_ids
def __init__(self, path, year: str, split: str, restart: str = None, sequences: (list, tuple) = None, all_annotations=False): self.dset_path = Path(path).expanduser().resolve() if not self.dset_path.exists(): print("Dataset directory '%s' not found." % path) quit(1) self.jpeg_path = self.dset_path / "JPEGImages" / "480p" self.anno_path = self.dset_path / "Annotations" / "480p" imset = self.dset_path / "ImageSets" / year / (split + ".txt") self.sequences = [s.strip() for s in sorted(open(imset).readlines())] self.name = "dv%s%s" % (year, split) self.year = year self.all_annotations = all_annotations if sequences is not None: assert set(sequences).issubset(self.sequences) self.sequences = list( sorted(set(self.sequences).intersection(sequences))) if restart is not None: assert restart in self.sequences self.sequences = self.sequences[self.sequences.index(restart):] self.start_frames = dict() for seq in self.sequences: f0 = "00000" # In DAVIS, all objects appear in the first frame obj_ids = torch.unique(imread(self.anno_path / seq / (f0 + ".png"))).tolist() self.start_frames[seq] = { obj_id: f0 for obj_id in sorted(obj_ids) if obj_id != 0 }
from lib.model import FaceDetectionRegressor from lib.image import imread, draw_rectangle, imwrite from lib.utils import merge # FaceDetectionRegressor model = FaceDetectionRegressor() # load weights model.load_weights('./models') from os import listdir, path test_dir = './test/' result_dir = './results/' for file in listdir(test_dir): image = imread(path.join(test_dir, file)) predictions = model.predict(image, threshold=.2) predictions = merge(predictions) print(file, 'detect', len(predictions), 'faces') for prediction in predictions: draw_rectangle(image, (int(prediction[0]), int(prediction[1])), (int(prediction[2]), int(prediction[3]))) imwrite(path.join(result_dir, file), image)
def evaluate_dataset(dset, results_path, measure='J', to_file=True): results = odict() dset_scores = [] dset_decay = [] dset_recall = [] if to_file: f = open(results_path / ("evaluation-%s.txt" % measure), "w") def _print(msg): print(msg) if to_file: print(msg, file=f) f.flush() for j, sequence in enumerate(dset): # Load all frames annotations = odict() segmentations = odict() for file in sequence.annos: lb = imread(file) annotations[file.stem] = ( lb != 0).byte() if sequence.merge_objects else lb segmentations[file.stem] = imread(results_path / sequence.name / file.name) # Find object ids and starting frames object_info = dict() for obj_id in sequence.obj_ids: for frame, obj_ids in sequence.start_frames.items(): if obj_id in obj_ids: assert obj_id not in object_info # Only one start frame per object object_info[obj_id] = frame assert 0 not in object_info # Because a background object would be weird # Evaluate n_seqs = len(dset) n_objs = len(object_info) seq_name = sequence.name _print("%d/%d: %s: %d object%s" % (j + 1, n_seqs, seq_name, n_objs, "s" if n_objs > 1 else "")) r = utils.evaluate_sequence(segmentations, annotations, object_info, measure=measure) results[seq_name] = r # Print scores, per frame and object, ignoring NaNs per_obj_score = [] # Per-object accuracies, averaged over the sequence per_frame_score = [] # Per-frame accuracies, averaged over the objects for obj_id, score in r['raw'].items(): per_frame_score.append(score) s = utils.mean(score) # Sequence average for one object per_obj_score.append(s) if n_objs > 1: _print("joint {obj}: acc {score:.3f} ┊{apf}┊".format( obj=obj_id, score=s, apf=text_bargraph(score))) # Print mean object score per frame and final score dset_decay.extend(r['decay']) dset_recall.extend(r['recall']) dset_scores.extend(per_obj_score) seq_score = utils.mean(per_obj_score) # Final score seq_mean_score = utils.nanmean(np.array(per_frame_score), axis=0) # Mean object score per frame # Print sequence results _print("final : acc {seq:.3f} ({dset:.3f}) ┊{apf}┊".format( seq=seq_score, dset=np.mean(dset_scores), apf=text_bargraph(seq_mean_score))) _print("%s: %.3f, recall: %.3f, decay: %.3f" % (measure, utils.mean(dset_scores), utils.mean(dset_recall), utils.mean(dset_decay))) if to_file: f.close()
def preload(self, device): """ Preload all images and upload them to the GPU. """ self.preloaded_images = [imread(f).to(device) for f in self.images]