Modifying YOLOv2 Prototxt

Do not directly copy the code samples in this section to your network model. Adjust the parameters to suit your use case. For example, the bottom and top parameters must match those in the corresponding network model, and the sequence of the bottom and top parameters is fixed.

  1. Modify the Region operator.

    The YOLO and DetectionOutput operators complete the postprocessing logic of the feature detection network. Before adding the YoloV2DetectionOutput operator, replace the Region operator with a YOLO operator.

    The YOLO operator has one input and three outputs as described in Supported Caffe Operators. Figure 1 shows the .prototxt files before and after operator modification for adapting to the Ascend AI Processor.

    Figure 1 Prototxt file before and after modification 2

    A code example is as follows.

    layer {
    	bottom: "layer31-conv"
    	top: "yolo_coords"
    	top: "yolo_obj"
    	top: "yolo_classes"
    	name: "yolo"
    	type: "Yolo"
    	yolo_param {
    		boxes: 5
    		coords: 4
    		classes: 80  
    		yolo_version: "V2"
    		softmax: true
    		background: false
        }
    }

    For details about the parameters, see Supported Caffe Operators.

  2. Add a YoloV2DetectionOutput operator to the output layer.

    On the YOLOv2 network, add a postprocessing layer YoloV2DetectionOutput to the end of the original .prototxt file by referring to List of Custom Operators. The YoloV2DetectionOutput operator has four inputs and two outputs as described in Supported Caffe Operators. An example of the constructed operator code is as follows.

    layer {
    name: "detection_out2"
    type: "YoloV2DetectionOutput"
    bottom: "yolo_coords"
    bottom: "yolo_obj"
    bottom: "yolo_classes"
    bottom: "img_info"
    top: "box_out"
    top: "box_out_num"
    yolov2_detection_output_param {
      boxes: 5
      classes: 80
      relative: true
      obj_threshold: 0.5
      score_threshold: 0.5
      iou_threshold: 0.45
      pre_nms_topn: 512
      post_nms_topn: 1024
      biases: 0.572730
      biases: 0.677385
      biases: 1.874460
      biases: 2.062530
      biases: 3.338430
      biases: 5.474340
      biases: 7.882820
      biases: 3.527780
      biases: 9.770520
      biases: 9.168280
      }
    }

    For details about the parameters, see Supported Caffe Operators.

  3. Add the input.

    The YoloV2DetectionOutput operator has the img_info input. Add img_info to model inputs. Figure 2 shows the .prototxt file before and after modification for adapting to the Ascend AI Processor.

    Figure 2 Prototxt file before and after modification 1

    The following is a code example. The shape is [batch, 4], where 4 is formatted [netH, netW, scaleH, scaleW]. netH and netW are H and W of the network model input, and scaleH and scaleW are H and W of the source image.

    input: "img_info"
    input_shape {
      dim: 1
      dim: 4
    }