| import json |
| |
|
|
| import numpy as np |
| import contextlib |
| import joblib |
| from tqdm import tqdm |
|
|
|
|
| API = 'http://ec2-52-11-11-89.us-west-2.compute.amazonaws.com/challenge16/api.py' |
|
|
| @contextlib.contextmanager |
| def tqdm_joblib(tqdm_object): |
| """Context manager to patch joblib to report into tqdm progress bar given as argument""" |
| class TqdmBatchCompletionCallback(joblib.parallel.BatchCompletionCallBack): |
| def __call__(self, *args, **kwargs): |
| tqdm_object.update(n=self.batch_size) |
| return super().__call__(*args, **kwargs) |
|
|
| old_batch_callback = joblib.parallel.BatchCompletionCallBack |
| joblib.parallel.BatchCompletionCallBack = TqdmBatchCompletionCallback |
| try: |
| yield tqdm_object |
| finally: |
| joblib.parallel.BatchCompletionCallBack = old_batch_callback |
| tqdm_object.close() |
|
|
| def get_blocked_videos(api=API): |
| api_url = '{}?action=get_blocked'.format(api) |
| req = urllib2.Request(api_url) |
| response = urllib2.urlopen(req) |
| return json.loads(response.read()) |
|
|
| def interpolated_prec_rec(prec, rec): |
| """Interpolated AP - VOCdevkit from VOC 2011. |
| """ |
| mprec = np.hstack([[0], prec, [0]]) |
| mrec = np.hstack([[0], rec, [1]]) |
| for i in range(len(mprec) - 1)[::-1]: |
| mprec[i] = max(mprec[i], mprec[i + 1]) |
| idx = np.where(mrec[1::] != mrec[0:-1])[0] + 1 |
| ap = np.sum((mrec[idx] - mrec[idx - 1]) * mprec[idx]) |
| return ap |
|
|
| def segment_iou(target_segment, candidate_segments): |
| """Compute the temporal intersection over union between a |
| target segment and all the test segments. |
| |
| Parameters |
| ---------- |
| target_segment : 1d array |
| Temporal target segment containing [starting, ending] times. |
| candidate_segments : 2d array |
| Temporal candidate segments containing N x [starting, ending] times. |
| |
| Outputs |
| ------- |
| tiou : 1d array |
| Temporal intersection over union score of the N's candidate segments. |
| """ |
| tt1 = np.maximum(target_segment[0], candidate_segments[:, 0]) |
| tt2 = np.minimum(target_segment[1], candidate_segments[:, 1]) |
| |
| segments_intersection = (tt2 - tt1).clip(0) |
| |
| segments_union = (candidate_segments[:, 1] - candidate_segments[:, 0]) \ |
| + (target_segment[1] - target_segment[0]) - segments_intersection |
| |
| |
| tIoU = segments_intersection.astype(float) / segments_union |
| return tIoU |
|
|
| def wrapper_segment_iou(target_segments, candidate_segments): |
| """Compute intersection over union btw segments |
| Parameters |
| ---------- |
| target_segments : ndarray |
| 2-dim array in format [m x 2:=[init, end]] |
| candidate_segments : ndarray |
| 2-dim array in format [n x 2:=[init, end]] |
| Outputs |
| ------- |
| tiou : ndarray |
| 2-dim array [n x m] with IOU ratio. |
| Note: It assumes that candidate-segments are more scarce that target-segments |
| """ |
| if candidate_segments.ndim != 2 or target_segments.ndim != 2: |
| raise ValueError('Dimension of arguments is incorrect') |
|
|
| n, m = candidate_segments.shape[0], target_segments.shape[0] |
| tiou = np.empty((n, m)) |
| for i in xrange(m): |
| tiou[:, i] = segment_iou(target_segments[i,:], candidate_segments) |
|
|
| return tiou |
|
|