From: marina.kolpakova Date: Thu, 24 Jan 2013 12:22:08 +0000 (+0400) Subject: ROC script refactoring X-Git-Tag: submit/tizen_ivi/20141117.190038~2^2~1187^2~19 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=faecb4f01d8f0c22f61baabdc75281b1c99a974f;p=profile%2Fivi%2Fopencv.git ROC script refactoring --- diff --git a/apps/sft/misc/roc_test.py b/apps/sft/misc/roc_test.py index 5cfcfb8..f640af3 100755 --- a/apps/sft/misc/roc_test.py +++ b/apps/sft/misc/roc_test.py @@ -74,10 +74,7 @@ if __name__ == "__main__": name = pattern % (nframes,) _, tail = os.path.split(name) - boxes = samples[tail] - boxes = sft.norm_acpect_ratio(boxes, 0.5) - boxes = [b for b in boxes if (b[3] - b[1]) > args.scale_range[0] / args.ext_ratio] - boxes = [b for b in boxes if (b[3] - b[1]) < args.scale_range[1] * args.ext_ratio] + boxes = sft.filter_for_range(samples[tail], args.scale_range, args.ext_ratio) nannotated = nannotated + len(boxes) nframes = nframes + 1 diff --git a/apps/sft/misc/scale_caltech.py b/apps/sft/misc/scale_caltech.py index 5eddc43..959a1f3 100755 --- a/apps/sft/misc/scale_caltech.py +++ b/apps/sft/misc/scale_caltech.py @@ -6,20 +6,6 @@ from optparse import OptionParser import re import numpy as np -def resize(image, d_w, d_h): - if (d_h < image.shape[0]) or (d_w < image.shape[1]): - ratio = min(d_h / float(image.shape[0]), d_w / float(image.shape[1])) - - kernel_size = int( 5 / (2 * ratio)) - sigma = 0.5 / ratio - image_to_resize = cv2.filter2D(image, cv2.CV_8UC3, cv2.getGaussianKernel(kernel_size, sigma)) - interpolation_type = cv2.INTER_AREA - else: - image_to_resize = image - interpolation_type = cv2.INTER_CUBIC - - return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type) - def extractPositive(f, path, opath, octave, min_possible): newobj = re.compile("^lbl=\'(\w+)\'\s+str=(\d+)\s+end=(\d+)\s+hide=0$") pos = re.compile("^pos\s=(\[[((\d+\.+\d*)|\s+|\;)]*\])$") @@ -107,7 +93,7 @@ def extractPositive(f, path, opath, octave, min_possible): continue cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE) - resized = resize(cropped, whole_mod_w, whole_mod_h) + resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h) flipped = cv2.flip(resized, 1) cv2.imshow("resized", resized) diff --git a/apps/sft/misc/scale_inria.py b/apps/sft/misc/scale_inria.py index 4fc6b8b..b08a4bc 100755 --- a/apps/sft/misc/scale_inria.py +++ b/apps/sft/misc/scale_inria.py @@ -30,20 +30,6 @@ def adjust(box, tb, lr): return [mix, miy, max, may] -def resize(image, d_w, d_h): - if (d_h < image.shape[0]) or (d_w < image.shape[1]): - ratio = min(d_h / float(image.shape[0]), d_w / float(image.shape[1])) - - kernel_size = int( 5 / (2 * ratio)) - sigma = 0.5 / ratio - image_to_resize = cv2.filter2D(image, cv2.CV_8UC3, cv2.getGaussianKernel(kernel_size, sigma)) - interpolation_type = cv2.INTER_AREA - else: - image_to_resize = image - interpolation_type = cv2.INTER_CUBIC - - return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type) - if __name__ == "__main__": parser = OptionParser() parser.add_option("-i", "--input", dest="input", metavar="DIRECTORY", type="string", @@ -110,7 +96,7 @@ if __name__ == "__main__": left = int(max(0, 0 - box[0])) right = int(max(0, box[2] - mat_w)) cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE) - resized = resize(cropped, whole_mod_w, whole_mod_h) + resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h) out_name = ".png" if round(math.log(scale)/math.log(2)) < each: @@ -143,7 +129,7 @@ if __name__ == "__main__": if (img.shape[0] <= min_shape[0]): resized_size = (int(min_shape[0] * ratio), int(min_shape[0])) - img = resize(img, resized_size[0], resized_size[1]) + img = sft.resize_sample(img, resized_size[0], resized_size[1]) else: out_name = "negative_sample_%i.png" % idx diff --git a/apps/sft/misc/sft.py b/apps/sft/misc/sft.py index 87004b4..147c391 100644 --- a/apps/sft/misc/sft.py +++ b/apps/sft/misc/sft.py @@ -30,7 +30,7 @@ def cascade(min_scale, max_scale, nscales, f): assert c.load(dom) return c -""" Compute prefix sum for en array""" +""" Compute prefix sum for en array.""" def cumsum(n): cum = [] y = 0 @@ -39,7 +39,7 @@ def cumsum(n): cum.append(y) return cum -""" Compute x and y arrays for ROC plot""" +""" Compute x and y arrays for ROC plot.""" def computeROC(confidenses, tp, nannotated, nframes, ignored): confidenses, tp, ignored = zip(*sorted(zip(confidenses, tp, ignored), reverse = True)) @@ -53,14 +53,14 @@ def computeROC(confidenses, tp, nannotated, nframes, ignored): return fppi, miss_rate -""" Crop rectangle by factor""" +""" Crop rectangle by factor.""" def crop_rect(rect, factor): val_x = factor * float(rect[2]) val_y = factor * float(rect[3]) x = [int(rect[0] + val_x), int(rect[1] + val_y), int(rect[2] - 2.0 * val_x), int(rect[3] - 2.0 * val_y)] return x -"""Initialize plot axises""" +""" Initialize plot axises.""" def initPlot(name = "ROC curve Bahnhof"): fig, ax = plt.subplots() @@ -73,13 +73,14 @@ def initPlot(name = "ROC curve Bahnhof"): plt.xscale('log') plt.yscale('log') -"""Show resulted plot""" +""" Show resulted plot.""" def showPlot(file_name): plt.savefig(file_name) plt.axis((pow(10, -3), pow(10, 1), 0.0, 1)) plt.yticks( [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.64, 0.8, 1], ['.05', '.10', '.20', '.30', '.40', '.50', '.64', '.80', '1'] ) plt.show() +""" Filter true positives and ignored detections for cascade detector output.""" def match(gts, dts): matches_gt = [0]*len(gts) matches_dt = [0]*len(dts) @@ -94,7 +95,7 @@ def match(gts, dts): for idx, row in enumerate(overlaps): imax = row.index(max(row)) - # try to match ground thrush + # try to match ground truth if (matches_gt[imax] == 0 and row[imax] > 0.5): matches_gt[imax] = 1 matches_dt[idx] = 1 @@ -109,17 +110,18 @@ def match(gts, dts): matches_ignore[idx] = 1 return matches_dt, matches_ignore - +""" Draw plot.""" def plotLogLog(fppi, miss_rate, c): print plt.loglog(fppi, miss_rate, color = c, linewidth = 2) - +""" Draw detections or ground truth on image.""" def draw_rects(img, rects, color, l = lambda x, y : x + y): if rects is not None: for x1, y1, x2, y2 in rects: cv2.rectangle(img, (x1, y1), (l(x1, x2), l(y1, y2)), color, 2) + def draw_dt(img, dts, color, l = lambda x, y : x + y): if dts is not None: for dt in dts: @@ -128,10 +130,6 @@ def draw_dt(img, dts, color, l = lambda x, y : x + y): cv2.rectangle(img, (x1, y1), (l(x1, x2), l(y1, y2)), color, 2) -class Annotation: - def __init__(self, bb): - self.bb = bb - class Detection: def __init__(self, bb, conf): self.bb = bb @@ -168,7 +166,7 @@ class Detection: def mark_matched(self): self.matched = True - +"""Parse INPIA annotation format""" def parse_inria(ipath, f): bbs = [] path = None @@ -185,10 +183,11 @@ def parse_inria(ipath, f): return Sample(path, bbs) + def glob_set(pattern): - return [__n for __n in glob.iglob(pattern)] #glob.iglob(pattern) + return [__n for __n in glob.iglob(pattern)] -# parse ETH idl file +""" Parse ETH idl file. """ def parse_idl(f): map = {} for l in open(f): @@ -198,11 +197,35 @@ def parse_idl(f): map.update(eval(l)) return map +""" Normalize detection box to unified aspect ration.""" def norm_box(box, ratio): middle = float(box[0] + box[2]) / 2.0 new_half_width = float(box[3] - box[1]) * ratio / 2.0 return (int(round(middle - new_half_width)), box[1], int(round(middle + new_half_width)), box[3]) - +""" Process array of boxes.""" def norm_acpect_ratio(boxes, ratio): - return [ norm_box(box, ratio) for box in boxes] \ No newline at end of file + return [ norm_box(box, ratio) for box in boxes] + +""" Filter detections out of extended range. """ +def filter_for_range(boxes, scale_range, ext_ratio): + boxes = sft.norm_acpect_ratio(boxes, 0.5) + boxes = [b for b in boxes if (b[3] - b[1]) > scale_range[0] / ext_ratio] + boxes = [b for b in boxes if (b[3] - b[1]) < scale_range[1] * ext_ratio] + return boxes + +""" Resize sample for training.""" +def resize_sample(image, d_w, d_h): + h, w, _ = image.shape + if (d_h < h) or (d_w < w): + ratio = min(d_h / float(h), d_w / float(w)) + + kernel_size = int( 5 / (2 * ratio)) + sigma = 0.5 / ratio + image_to_resize = cv2.filter2D(image, cv2.CV_8UC3, cv2.getGaussianKernel(kernel_size, sigma)) + interpolation_type = cv2.INTER_AREA + else: + image_to_resize = image + interpolation_type = cv2.INTER_CUBIC + + return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type) \ No newline at end of file