Skip to content

Instantly share code, notes, and snippets.

@Grabber
Created January 26, 2017 02:49
Show Gist options
  • Save Grabber/de8a2b7de587cf8e58f4e3f6bb55cf23 to your computer and use it in GitHub Desktop.
Save Grabber/de8a2b7de587cf8e58f4e3f6bb55cf23 to your computer and use it in GitHub Desktop.
calculating priori anchors with k-means
# -*- coding: utf-8 -*-
import time
import numpy as np
from random import randint
iou_vals = []
iou_count = 0
class Test:
@staticmethod
def overlap():
a = [0,0,5,5]
b = [0,0,10,10]
overlap_x = overlap(a[0], a[2], b[0], b[2])
overlap_y = overlap(a[1], a[3], b[1], b[3])
return [overlap_x, overlap_y]
@staticmethod
def intersection():
a = [0,0,5,5]
b = [0,0,10,10]
return intersection(a, b)
@staticmethod
def area():
a = [0,0,10,10]
return area(a)
@staticmethod
def union():
a = [0,0,5,5]
b = [0,0,10,10]
return union(a, b)
@staticmethod
def iou():
a = [0,0,5,5]
b = [0,0,10,10]
return iou(a, b)
@staticmethod
def uoi():
a = [0,0,5,5]
b = [0,0,10,10]
return uoi(a,b)
@staticmethod
def kmeans():
X = np.array([
#[ x, y, w, h],
[191,158,226,188],
[191,158,226,188],
[126,213,163,231],
[132,204,170,230],
[133,194,171,224],
[139,181,179,214],
[140,168,180,202],
[135,156,182,189],
[140,140,186,174],
[146,130,186,164],
[139,114,186,148],
[144,99,186,140],
[139,90,185,127],
[136,79,185,118],
[136,64,179,105],
[134,53,173,96],
[127,48,170,86],
[116,40,159,70],
[108,32,154,62],
[106,32,141,54],
[132,210,158,232],
[133,202,179,228],
[144,195,187,225],
[145,194,191,224],
[156,180,204,216],
[160,167,205,204],
[171,162,212,195],
[176,149,216,182],
[176,143,219,172],
[166,130,212,164],
[173,122,221,156],
[173,116,217,145],
[172,116,211,144],
[164,83,208,115],
[159,75,210,106],
[159,67,207,96],
[157,53,208,88],
[156,49,205,80],
[154,46,207,77],
[157,35,205,66],
[148,32,203,53],
[155,32,204,53],
[120,31,157,53],
[124,32,163,64],
[132,39,172,79],
[137,51,175,94],
[142,62,179,108],
[142,69,181,119],
[148,83,186,132],
[154,100,187,144],
[156,108,191,154],
[158,114,194,166],
[160,130,191,180],
[161,145,199,197],
[158,156,198,200],
[164,171,201,214],
[160,180,196,221],
[154,193,191,227],
[151,204,184,230],
[133,30,163,53],
[134,35,168,63],
[139,46,171,79],
[142,59,170,87],
[140,63,172,99],
[141,76,172,110],
[144,87,178,125],
[153,100,183,131],
[147,104,185,138],
[153,119,188,154],
[154,130,189,169],
[158,148,188,187],
[151,164,180,198],
[152,179,184,213],
[143,195,178,228],
[137,205,170,231],
[110,32,141,49],
[114,33,149,54],
[119,32,155,66],
[119,37,161,78],
[123,50,162,91],
[126,61,166,96],
[127,67,168,103],
[132,76,170,111],
[131,83,169,116],
[136,93,171,129],
[139,102,174,141],
[141,109,178,148],
[149,123,182,157],
[149,130,185,171],
[152,142,188,180],
[151,150,184,185],
[148,156,182,195],
[148,163,182,203],
[147,178,180,214],
[143,182,180,221],
[137,191,171,228],
[136,197,171,235],
[133,203,169,236],
[130,211,168,234],
[130,217,166,232],
[130,222,160,235],
[131,203,164,230],
[133,193,165,228],
[136,187,169,226],
[136,179,170,221],
[138,170,168,212],
[136,165,166,207],
[132,157,168,199],
[132,152,166,195],
[130,144,164,186],
[131,136,168,179],
[127,130,165,172],
[129,124,165,162],
[126,111,165,153],
[126,106,168,143],
[122,99,163,136],
[126,94,165,129],
[125,84,165,124],
[124,77,168,113],
[131,71,169,106],
[133,60,168,97],
[128,49,167,88],
[131,47,169,83],
[128,39,169,77],
[126,33,169,70],
[122,33,165,68],
[121,31,161,62],
[121,32,158,57],
[119,32,150,52],
[117,30,145,49],
[103,30,135,51],
[103,32,140,54],
[107,30,146,58],
[111,31,148,56],
[112,31,152,60],
[113,32,157,63],
[117,37,164,67],
[120,43,167,76],
[118,42,163,78],
[124,50,167,81],
[123,53,169,89],
[123,61,171,94],
[126,63,172,95],
[127,66,175,97],
[132,73,175,104],
[131,71,177,105],
[136,74,177,106],
[139,78,180,110],
[148,82,192,116],
[153,89,193,118],
[156,90,197,122],
[162,94,204,127],
[163,97,209,130],
[167,100,213,135],
[174,104,215,137],
[180,109,215,137],
[184,111,217,138],
[187,109,219,137],
[182,107,218,139],
[188,105,218,138],
[178,101,219,135],
[181,97,223,126],
[184,90,220,124],
[188,84,223,116],
[193,79,225,112],
[196,69,227,104],
[196,56,229,92],
[199,45,231,79],
[202,37,236,68],
[207,29,240,61],
[204,33,240,60],
[205,32,242,52],
[205,32,246,57],
[208,32,256,63],
[216,32,249,63],
[215,41,254,72],
[215,49,246,76],
[208,57,247,87],
[206,64,240,95],
[199,72,232,103],
[202,76,234,104],
[191,83,229,114],
[189,85,224,116],
[182,86,223,121],
[183,89,216,124],
[180,91,215,129],
[181,93,217,131],
[186,99,217,134],
[187,102,218,136],
[182,103,220,140],
[192,112,228,145],
[196,122,232,160],
[205,132,234,160],
[209,135,239,167],
[209,146,239,170],
[208,151,239,181],
[208,156,246,192],
[215,169,248,200],
[219,176,250,206],
[212,180,251,215],
[222,193,254,222],
[106,31,138,48],
[112,32,144,58],
[123,33,153,69],
[126,45,160,85],
[135,60,169,97],
[142,72,174,110],
[150,89,180,123],
[148,101,181,139],
[155,110,187,152],
[156,123,186,158],
[151,138,184,177],
[147,154,182,191],
[146,165,184,201],
[143,176,179,210],
[143,185,177,223],
[139,196,172,233],
[137,207,172,233],
[136,214,167,235],
[170,210,198,227],
[172,202,203,227],
[175,194,209,224],
[171,184,207,217],
[169,176,205,209],
[173,164,205,197],
[176,159,206,188],
[169,149,203,176],
[169,138,204,165],
[174,126,207,154],
[174,115,207,143],
[175,103,206,132],
[171,86,210,122],
[173,78,210,108],
[176,64,210,96],
[175,50,211,86],
[171,37,212,72],
[176,33,211,63],
[174,32,212,53],
[104,33,132,50],
[110,33,144,57],
[119,40,152,69],
[125,54,161,84],
[129,68,160,101],
[124,77,160,112],
[132,91,168,126],
[135,105,170,137],
[134,113,170,151],
[128,128,166,165],
[135,140,172,180],
[134,152,171,191],
[134,161,169,203],
[130,181,165,218],
[128,196,160,229],
[121,202,157,232],
[117,205,154,232],
[144,204,173,229],
[148,191,178,223],
[149,184,182,216],
[152,176,185,210],
[153,162,187,197],
[157,150,190,185],
[159,138,194,173],
[157,122,198,156],
[154,107,193,146],
[158,89,193,128],
[150,79,190,119],
[142,68,184,107],
[136,58,176,94],
[131,45,174,87],
[117,35,167,79],
[108,34,154,66],
[101,32,143,54],
[101,31,130,45],
[132,32,160,55],
[136,36,167,67],
[135,41,169,80],
[141,57,173,93],
[145,75,177,108],
[149,89,183,127],
[152,107,187,145],
[157,123,187,160],
[154,134,187,176],
[151,150,184,188],
[144,164,181,203],
[140,175,179,218],
[135,188,172,231],
[130,200,166,234],
[129,212,160,235],
[155,206,185,229],
[155,188,190,226],
[168,183,199,219],
[171,170,206,206],
[175,155,206,191],
[174,143,206,178],
[171,131,207,169],
[170,118,206,158],
[166,105,205,147],
[163,92,201,134],
[157,80,193,122],
[143,67,183,113],
[136,54,179,100],
[126,43,170,88],
[116,35,164,80],
[109,30,159,69],
[103,31,154,57],
[113,30,151,49],
[224,32,262,54],
[216,34,255,66],
[212,43,247,77],
[208,58,241,92],
[209,69,238,102],
[205,81,237,116],
[200,92,228,129],
[194,107,228,141],
[193,120,228,156],
[198,134,228,165],
[193,150,221,178],
[186,162,218,189],
[180,171,216,210],
[178,179,211,209],
[171,188,205,220],
[165,197,197,229],
[162,207,192,231],
[154,213,188,233],
[206,209,236,225],
[210,203,241,225],
[208,197,245,224],
[210,191,248,223],
[210,189,246,220],
[209,187,245,217],
[207,183,242,216],
[204,184,238,215],
[203,181,240,213],
[202,180,240,216],
[201,184,236,217],
[198,186,233,221],
[199,187,234,223],
[198,192,232,224],
[196,194,228,223],
[191,195,227,225],
[191,203,225,224],
[189,205,219,224],
[186,210,214,225],
[143,210,175,231],
[141,204,183,232],
[151,200,184,231],
[155,192,188,229],
[160,188,195,220],
[165,177,204,208],
[171,167,206,200],
[174,158,206,190],
[182,147,212,180],
[181,138,214,173],
[185,131,220,166],
[186,122,217,156],
[187,111,222,145],
[189,96,223,131],
[185,85,219,117],
[182,71,213,106],
[176,56,211,93],
[175,42,208,81],
[173,33,205,67],
[166,33,203,55],
[170,32,199,48],
[94,32,125,51],
[97,31,135,62],
[101,33,140,74],
[106,39,148,89],
[113,53,153,99],
[121,63,160,104],
[121,68,165,114],
[131,81,166,121],
[127,82,168,122],
[132,88,166,125],
[130,89,164,123],
[129,84,163,120],
[127,81,164,115],
[128,74,161,106],
[126,65,160,98],
[125,52,160,89],
[126,42,159,73],
[127,32,161,61],
[125,32,159,53],
[124,31,158,45],
[136,211,167,232],
[143,204,175,231],
[142,197,176,230],
[152,188,181,216],
[159,174,185,201],
[163,165,195,193],
[163,150,193,181],
[161,141,193,173],
[164,133,193,159],
[157,117,193,146],
[163,109,194,136],
[160,101,188,125],
[153,91,185,115],
[146,80,181,109],
[144,75,175,103],
[137,65,167,90],
[129,55,163,86],
[124,48,155,73],
[115,37,149,65],
[108,32,139,57],
[102,32,132,48],
[180,205,219,225],
[185,201,223,224],
[189,195,223,223],
[193,193,222,224],
[187,189,220,221],
[190,185,217,221],
[186,178,220,216],
[185,171,221,214],
[188,162,216,212],
[177,160,212,204],
[177,153,209,198],
[180,147,215,193],
[175,140,208,184],
[175,135,211,182],
[180,127,214,177],
[178,126,213,170],
[177,118,214,164],
[176,111,215,156],
[178,105,216,151],
[180,97,216,146],
[180,99,218,141],
[175,92,215,133],
[173,90,217,129],
[168,86,213,125],
[172,84,214,126],
[166,78,208,121],
[160,78,206,118],
[154,74,199,112],
[155,71,197,114],
[148,67,191,108],
[144,66,186,107],
[142,62,185,103],
[137,58,181,100],
[133,50,180,98],
[134,49,180,93],
[136,43,179,86],
[133,37,176,81],
[137,34,175,73],
[143,32,175,65],
[143,31,174,62],
[141,30,176,60],
[141,32,174,53],
[135,34,175,49],
[132,32,179,42],
[131,32,162,50],
[128,32,161,54],
[126,30,162,56],
[128,32,162,61],
[124,33,162,64],
[123,35,164,69],
[130,39,165,73],
[131,39,165,76],
[140,40,171,85],
[138,46,172,90],
[144,51,180,95],
[151,60,186,101],
[147,64,186,111],
[159,70,188,114],
[154,74,189,119],
[161,84,192,127],
[159,92,194,133],
[152,99,191,139],
[157,110,191,143],
[149,118,186,152],
[144,124,189,159],
[146,129,189,168],
[143,138,189,175],
[143,144,184,184],
[145,150,184,189],
[143,154,189,195],
[149,163,185,203],
[150,170,188,210],
[153,175,189,212],
[153,179,188,216],
[158,189,191,228],
[158,195,191,230],
[157,199,192,233],
[161,200,191,229],
[157,209,192,232],
[127,214,154,237],
[134,201,178,233],
[145,199,183,234],
[150,192,191,230],
[146,174,192,219],
[150,165,190,208],
[144,154,194,197],
[157,150,190,185],
[149,143,192,175],
[155,136,191,169],
[152,120,190,150],
[154,108,192,145],
[159,95,195,134],
[158,83,199,118],
[155,71,197,108],
[155,62,193,99],
[149,54,186,87],
[142,44,183,76],
[135,37,173,66],
[126,34,167,56],
[123,33,160,47],
[166,32,193,48],
[170,34,198,63],
[166,34,202,75],
[161,39,199,84],
[163,52,199,94],
[160,62,197,103],
[163,74,196,111],
[164,80,199,123],
[169,101,199,137],
[166,105,200,145],
[172,117,198,154],
[170,127,200,163],
[172,136,203,173],
[177,147,202,183],
[173,156,201,192],
[170,164,198,200],
[165,171,194,212],
[166,180,198,218],
[161,188,198,226],
[153,197,187,226],
[148,203,184,231],
[171,32,216,52],
[169,33,217,48],
[166,32,216,52],
[165,33,208,54],
[165,33,207,61],
[168,33,204,67],
[168,37,207,74],
[176,43,210,81],
[176,50,210,88],
[175,63,216,91],
[179,67,216,96],
[182,73,219,103],
[180,84,228,111],
[185,88,226,122],
[188,91,224,130],
[188,96,217,135],
[180,97,220,141],
[180,107,213,148],
[178,115,209,156],
[175,124,205,164],
[174,124,204,166],
[174,134,205,176],
[164,156,202,186],
[165,161,205,193],
[165,163,205,198],
[164,170,207,204],
[168,174,215,211],
[175,180,211,213],
[179,191,219,221],
[180,189,218,223],
[175,196,215,224],
[180,199,216,225],
[112,32,146,57],
[115,35,153,65],
[124,41,160,73],
[136,54,166,84],
[133,60,167,95],
[144,74,172,107],
[149,80,179,116],
[161,95,189,130],
[168,103,198,143],
[169,115,200,156],
[173,129,202,168],
[178,142,209,177],
[177,152,211,191],
[179,162,213,197],
[175,177,211,211],
[173,185,211,222],
[175,192,211,225],
[178,197,212,228],
[183,203,215,227],
[188,209,211,228],
[148,210,177,230],
[153,205,180,229],
[157,199,184,226],
[160,191,189,222],
[163,185,198,216],
[165,181,194,210],
[168,172,197,213],
[166,166,199,202],
[172,162,199,200],
[171,158,201,195],
[175,153,208,187],
[167,145,201,182],
[160,134,197,173],
[158,127,190,163],
[151,120,186,154],
[150,109,185,148],
[149,100,178,133],
[143,95,180,136],
[148,88,182,125],
[148,79,186,119],
[146,77,179,112],
[147,69,182,107],
[150,62,185,103],
[153,54,185,97],
[153,48,188,88],
[149,38,185,78],
[156,34,186,69],
[159,35,192,61],
[157,34,194,55],
[150,33,178,53],
[148,32,178,56],
[146,35,180,64],
[144,44,176,79],
[141,51,175,89],
[141,61,170,96],
[143,64,171,102],
[143,67,175,111],
[145,76,177,122],
[146,85,178,132],
[149,91,177,138],
[151,106,182,140],
[150,109,183,151],
[158,123,186,163],
[159,129,186,169],
[158,137,195,179],
[170,151,202,192],
[168,157,200,197],
[169,164,201,205],
[169,171,200,213],
[168,185,197,218],
[162,194,195,225],
[161,198,195,227],
[158,204,194,229],
[175,209,200,229],
[176,203,206,226],
[177,197,207,226],
[184,192,209,223],
[175,183,207,221],
[177,174,208,214],
[179,166,207,204],
[177,155,203,195],
[175,148,205,186],
[175,144,202,176],
[164,133,202,172],
[160,120,198,161],
[160,118,194,150],
[159,113,193,146],
[159,105,193,137],
[165,98,196,132],
[164,89,198,126],
[163,88,196,119],
[166,76,199,112],
[168,67,200,101],
[169,57,206,95],
[168,48,209,79],
[178,38,214,68],
[180,32,216,58],
[178,32,218,54],
[184,32,216,50],
[181,32,212,51],
[180,33,214,56],
[182,31,218,66],
[187,34,220,74],
[188,43,222,87],
[190,49,227,96],
[200,61,231,107],
[204,70,238,113],
[208,84,242,124],
[211,95,242,133],
[207,104,241,146],
[202,118,234,157],
[203,125,231,167],
[200,135,232,179],
[195,141,228,186],
[191,151,228,194],
[184,160,228,201],
[183,167,228,211],
[181,178,225,217],
[185,187,226,226],
[187,194,230,225],
[190,193,235,225],
[197,203,232,224],
[153,207,176,226],
[157,191,181,219],
[157,174,186,206],
[158,160,190,194],
[165,149,195,181],
[162,134,197,166],
[165,121,197,155],
[160,105,197,139],
[158,94,196,130],
[160,83,194,116],
[159,73,197,108],
[153,60,194,97],
[153,47,190,85],
[159,41,189,75],
[165,35,193,65],
[176,210,202,226],
[184,203,206,224],
[188,200,213,222],
[189,191,218,218],
[192,184,218,213],
[191,175,221,212],
[188,172,219,203],
[192,165,219,196],
[175,152,215,187],
[179,145,213,179],
[178,137,211,172],
[179,131,215,168],
[181,127,213,157],
[189,120,217,151],
[192,108,218,137],
[190,96,223,130],
[196,85,226,118],
[198,79,232,112],
[197,72,227,104],
[195,63,230,98],
[191,52,229,87],
[201,50,230,79],
[200,44,228,71],
[193,34,228,61],
[188,33,222,51],
[135,33,165,51],
[137,32,172,54],
[138,35,176,63],
[142,36,176,67],
[142,40,178,79],
[137,42,174,84],
[142,57,177,96],
[142,57,182,101],
[146,69,183,110],
[153,81,186,116],
[153,88,188,125],
[159,99,191,131],
[159,104,193,137],
[165,115,198,145],
[165,119,199,151],
[170,121,199,155],
[169,126,205,161],
[168,131,205,166],
[165,141,199,172],
[168,146,198,176],
[171,154,207,186],
[160,165,200,195],
[158,173,192,201],
[157,178,194,205],
[155,188,185,215],
[155,191,188,221],
[149,197,186,225],
[149,204,180,229],
[147,202,178,228],
[144,209,183,232],
[149,209,176,231],
[231,203,261,218],
[229,203,260,220],
[228,201,260,219],
[228,200,261,218],
[229,199,262,220],
[229,199,258,219],
[223,195,258,219],
[227,196,256,218],
[219,191,254,215],
[224,194,256,219],
[221,191,251,217],
[219,187,247,217],
[218,187,246,213],
[216,185,244,211],
[214,181,245,212],
[209,178,242,209],
[208,175,240,207],
[210,175,240,205],
[210,172,238,200],
[207,168,237,198],
[204,164,238,197],
[207,164,237,196],
[204,158,236,191],
[205,157,234,185],
[203,153,235,185],
[196,148,233,181],
[200,148,231,181],
[196,141,231,172],
[194,141,229,171],
[191,137,226,168],
[196,135,227,167],
[196,134,225,162],
[193,128,222,159],
[147,212,178,229],
[154,213,183,232],
[152,210,184,229],
[154,207,180,228],
[150,201,181,227],
[150,204,180,225],
[150,202,177,227],
[151,204,175,227],
[148,205,177,230],
[145,210,176,231],
[141,215,165,230],
[188,97,218,128],
[186,95,214,126],
[185,90,218,122],
[180,89,216,118],
[181,86,217,119],
[183,89,218,116],
[183,87,216,114],
[181,82,216,112],
[180,79,210,106],
[177,77,214,107],
[182,79,216,107],
[182,81,219,107],
[176,74,217,105],
[179,78,212,103],
[177,71,213,103],
[180,76,211,100],
[178,74,216,101],
[176,72,213,100],
[181,76,214,101],
[178,73,211,99],
[175,72,214,99],
[180,74,212,99],
[178,71,210,97],
[176,72,210,97],
[179,73,211,99],
[179,71,217,98],
[179,73,213,97],
[179,73,213,101],
[177,72,213,101],
[176,73,212,100],
[178,72,210,98],
[172,71,210,98],
[179,72,211,97],
[177,73,210,99],
[184,74,212,97],
[174,70,213,100],
[178,73,210,98],
[179,73,212,99],
[182,73,216,101],
[177,70,212,98],
[182,73,210,97],
[176,70,213,100],
[178,71,213,95],
[174,72,209,97],
[179,72,210,98],
[180,70,211,98],
[179,73,215,99],
[183,72,214,98],
[177,70,213,98],
[179,70,212,96],
[175,69,212,98],
[179,73,211,97],
[176,74,210,99],
[169,69,210,97],
[178,77,207,99],
[177,77,208,102],
[176,79,210,105],
[175,79,205,106],
[169,81,205,109],
[175,85,202,112],
[168,85,198,110],
[168,84,205,111],
[172,86,202,112],
[168,84,201,111],
[167,84,202,111],
[171,87,201,110],
[171,86,205,111],
[170,82,206,115],
[170,81,205,116],
[169,80,207,114],
[172,79,205,111],
[172,80,206,111],
[174,83,203,111],
[174,82,204,112],
[176,86,208,116],
[176,85,207,119],
[174,86,208,119],
[175,88,207,118],
[174,87,209,119],
[175,89,207,121],
[173,88,204,120],
[173,89,210,121],
[176,89,205,119],
[175,91,207,118],
[176,87,207,119],
[176,87,207,117],
[176,86,212,117],
[175,86,214,118],
[180,86,211,118],
[181,87,215,115],
[180,84,214,116],
[181,84,216,115],
[184,82,215,115],
[186,85,220,116],
[187,83,218,114],
[190,84,223,114],
[192,81,223,113],
[194,82,227,112],
[197,79,226,112],
[197,79,230,111],
[198,79,230,110],
[200,79,233,111],
[201,78,231,110],
[201,78,234,112],
[198,79,231,110],
[198,79,233,113],
[198,81,233,113],
[199,81,233,115],
[196,82,231,114],
[195,82,230,113],
[196,84,235,117],
[194,82,231,114],
[194,83,234,117],
[196,84,231,114],
[198,84,230,112],
[192,78,229,113],
[193,81,235,113],
[191,81,230,114],
[193,82,229,113],
[195,83,230,114],
[193,79,228,113],
[193,79,228,111],
[193,81,226,111],
[195,82,228,110],
[193,80,225,109],
[193,80,227,110],
[195,83,229,111],
[195,83,228,112],
[191,83,227,112],
[192,85,231,111],
[128,217,154,231],
[195,85,229,115],
[195,85,232,116],
[195,85,232,113],
[194,85,230,114],
[194,85,228,114],
[193,85,229,116],
[191,83,226,115],
[193,82,227,114],
[190,83,228,114],
[188,81,225,113],
[190,81,228,113],
[191,86,228,114],
[189,86,228,115],
[192,83,228,115],
[194,83,227,112],
[191,82,226,113],
[192,80,228,113],
[192,80,227,113],
[191,80,227,114],
[191,78,227,111],
[191,75,226,112],
[192,78,225,108],
[192,77,223,109]
])
np.random.shuffle(X)
return kmeans(X, K=9, iters=100)
def overlap(a1, a2, b1, b2):
l1 = a1 - a2/2.
l2 = b1 - b2/2.
left = l1 if l1 > l2 else l2
r1 = a1 + a2/2.
r2 = b1 + b2/2.
right = r1 if r1 < r2 else r2
return right - left
def intersection(a, b):
return overlap(a[0], a[2], b[0], b[2]) * overlap(a[1], a[3], b[1], b[3])
def area(x):
return x[2]*x[3]
def union(a, b):
return area(a) + area(b) - intersection(a, b)
def iou(a, b):
return intersection(a, b) / union(a, b)
def niou(a, b):
global iou_vals
global iou_count
iou_val = iou(a,b)
niou_val = 1. - iou_val
iou_vals.append(niou_val)
iou_count += 1
return niou_val
def uoi(a, b):
return union(a, b) / intersection(a, b)
def kmeans(X, K, iters=10):
if (K > len(X)):
return np.array()
C = X[np.random.choice(np.arange(len(X)), K, replace=False)]
for i in range(iters):
cost = np.array([np.argmin([niou(x_i, c_i) for c_i in C]) for x_i in X])
C = [X[cost == k].mean(axis=0) for k in range(K)]
return np.array(C)
def main():
# print "overlap: %s" % Test.overlap()
# print "intersection: %f" % Test.intersection()
# print "area: %f" % Test.area()
# print "union: %f" % Test.union()
# print "iou: %f" % Test.iou()
# print "uoi: %f" % Test.uoi()
test_kmeans = Test.kmeans()
print "kmeans output format is:\n\t[[x1 y1, w1, h1], ..., [xn yn, wn, hn]]"
print "\nkmeans, clustering with original dimensions:\n%s" % test_kmeans
print "\nkmeans, clustering considering [208x208, 13x13 => 16]:\n%s" % (test_kmeans * 13/208)
print "\nkmeans, clustering considering [416x416, 13x13 => 32]:\n%s" % (test_kmeans * 13/416)
print "\navg_iou: %.2f%%" % (100.*sum(iou_vals) / iou_count)
if __name__ == "__main__":
main()
@Jumabek
Copy link

Jumabek commented Feb 13, 2017

One question, where did you get this numbers in X array Cuz, in the yolov2 paper author says he computed anchors from VOC and COCO bounding boxes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment