experiencor / keras-yolo3

Training and Detecting Objects with YOLO3
MIT License
1.6k stars 862 forks source link

Different number of anchors #144

Open deraltefritz opened 6 years ago

deraltefritz commented 6 years ago

Hi,

has anyone ever tried to use more or less anchors than the default (=9)?

Even though "gen_anchors.py" allows for more anchors to be computed, "yolo.py" seems to be hard coded to exactly 9 anchors.

It's clear that the number of anchors must be divisible by 3 (each 'YoloLayer' is passed one third of the anchors). So I tried changing the calls to YoloLayer: loss_yolo_1 = YoloLayer(anchors_yolo1, #anchors[12:] ... loss_yolo_2 = YoloLayer(anchors_yolo2,#anchors[6:12] ... loss_yolo_3 = YoloLayer(anchors_yolo3,#anchors[:6] ... and made sure that "anchors_yolo1" etc. contain one third of the anchors: anchors = [4, 11, 4, 72, 14, 113, 17, 329, 18, 22, 27, 48, 40, 88, 54, 278, 85, 36, 92, 99, 259, 107, 321, 313] anchors_yolo1 = [85, 36, 92, 99, 259, 107, 321, 313] anchors_yolo2 = [18, 22, 27, 48, 40, 88, 54, 278] anchors_yolo3 = [4, 11, 4, 72, 14, 113, 17, 329]

But then I ran into the following error:

InvalidArgumentError: Dimensions must be equal, but are 3 and 4 for 'yolo_layer_1/mul' (op: 'Mul') with input shapes: [?,?,?,3,?], [1,1,1,4,2]. ---> 94 pred_wh = tf.expand_dims(tf.exp(pred_box_wh) * self.anchors / net_factor, 4)

I have a hunch that the 3 in this line \# adjust the shape of the y_predict [batch, grid_h, grid_w, 3, 4+1+nb_class] y_pred = tf.reshape(y_pred, tf.concat([tf.shape(y_pred)[:3], tf.constant([3, -1])], axis=0)) relates to the default 3 anchors per layer, and tried changing it to the number of anchors per layer (4 in the example above). But that just leads to further errors:

InvalidArgumentError: Dimensions must be equal, but are 3 and 4 for 'yolo_layer_1/add' (op: 'Add') with input shapes: [4,?,?,3,2], [?,?,?,18,4,?]. ---> 66 pred_box_xy = (self.cell_grid[:,:grid_h,:grid_w,:,:] + tf.sigmoid(y_pred[..., :2])) # sigma(t_xy) + c_xy

Could anyone tell me what changes are necessary to have an arbitrary (divisible by 3) number of anchors?

Thank you!

undertakerweian commented 5 years ago

Hi, deraltefritz!

you have changing line 58.59.60.119.120.121.122 in "generator.py" at first, in my case, i want changing the number of anchors box from 18 into 6, so i have modify "generator.py" line58.59.60 from len(self.anchors)//3 into len(self.anchors)//1 and line 119.120.121.122 from max_index%3 into max_index%1

afterall, you have changing "yolo.py" in line 296 from anchors[12:] into anchors[4:] in line 234.235.236 from len(anchors)//6 into len(anchors)//2 in line 322 from anchors[6:12] into anchors[2:4] in line 346 from anchors[:6] into anchors[:2]

look4pritam commented 5 years ago

What are the changes required to run YOLO3 with 4 anchors per scale?