experiencor / keras-yolo3

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

Increasing number of anchors for training #230

Open AceMcAwesome opened 5 years ago

AceMcAwesome commented 5 years ago

I would like to increase the number of anchors from 9 to 18 for training, since I read this can improve accuracy (I am not concerned about model runtime, and it is very important for the box to fit tightly to the boundary of the objects for this project). I used gen_anchors.py to create 18 anchors, and copied these into the config.json file. I also doubled the anchor values in the YoloLayer definitions within the yolo.py file:

loss_yolo_1 = YoloLayer(anchors[24:], [1*num for num in max_grid], batch_size, warmup_batches, ignore_thresh, grid_scales[0], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, pred_yolo_1, true_yolo_1, true_boxes])

loss_yolo_2 = YoloLayer(anchors[12:24], 
                        [2*num for num in max_grid], 
                        batch_size, 
                        warmup_batches, 
                        ignore_thresh, 
                        grid_scales[1],
                        obj_scale,
                        noobj_scale,
                        xywh_scale,
                        class_scale)([input_image, pred_yolo_2, true_yolo_2, true_boxes])

loss_yolo_3 = YoloLayer(anchors[:12], 
                        [4*num for num in max_grid], 
                        batch_size, 
                        warmup_batches, 
                        ignore_thresh, 
                        grid_scales[2],
                        obj_scale,
                        noobj_scale,
                        xywh_scale,
                        class_scale)([input_image, pred_yolo_3, true_yolo_3, true_boxes]) 

(the original values were 12:, 6:12, :6 instead of 24:, 12:24, :12). I assume this must be done because the anchors vector is twice as long (tell me if I'm wrong). However, now I get this error:

Traceback (most recent call last): File "train.py", line 282, in main(args) File "train.py", line 244, in main class_scale = config['train']['class_scale'], File "train.py", line 131, in create_model class_scale = class_scale File "/home/VRC/robert.harris/keras_yolo3_tube_sub/yolo.py", line 334, in create_yolov3_model class_scale)([input_image, pred_yolo_1, true_yolo_1, true_boxes]) File "/home/VRC/robert.harris/keras_yolo3_tube_sub/yolo.py", line 15, in init self.anchors = tf.constant(anchors, dtype='float', shape=[1,1,1,3,2]) File "/home/VRC/robert.harris/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 207, in constant value, dtype=dtype, shape=shape, verify_shape=verify_shape)) File "/home/VRC/robert.harris/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 497, in make_tensor_proto (shape_size, nparray.size)) ValueError: Too many elements provided. Needed at most 6, but received 12

mainly, it doesn't like this line: self.anchors = tf.constant(anchors, dtype='float', shape=[1,1,1,3,2])

I get that the tensor no longer has only 6 values but I'm not sure what to do to fix this. I tried playing around with increasing the vector in that line to self.anchors = tf.constant(anchors, dtype='float', shape=[1,1,1,6,2]) so that it would have 12 total numbers to fit to, but then I get errors down in this area:

pred_xy = tf.expand_dims(pred_box_xy / grid_factor, 4) pred_wh = tf.expand_dims(tf.exp(pred_box_wh) * self.anchors / net_factor, 4)

    pred_wh_half = pred_wh / 2.
    pred_mins    = pred_xy - pred_wh_half
    pred_maxes   = pred_xy + pred_wh_half  

In general, what is the proper way to run training with more than 9 anchors? Thanks!

ruichengnew commented 4 years ago

Meet the same problem, have you done this?