Modifying YOLOv3 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 upsample_param attribute of the Upsample operator.

    Change scale:2 in the .prototxt file of the original operator to scale:1 stride:2 by following instructions described in Supported Caffe Operators.

    Figure 1 shows the .prototxt file before and after modification for adapting to the Ascend AI Processor.

    Figure 1 Prototxt file before and after modification 4

    For details about the parameters, see Supported Caffe Operators.

  2. Add three YOLO operators.

    The YOLO and DetectionOutput operators complete the postprocessing logic of the feature detection network. According to the original operator .prototxt file, three YOLO operators should be added before adding the YoloV3DetectionOutput operator.

    The YOLO operator has one input and three outputs as described in Supported Caffe Operators. The code examples of the YOLO operators are provided as follows.
    • Code example of operator 1:
      layer {
      	bottom: "layer82-conv"
      	top: "yolo1_coords"
      	top: "yolo1_obj"
      	top: "yolo1_classes"
      	name: "yolo1"
      	type: "Yolo"
      	yolo_param {
      		boxes: 3
      		coords: 4
      		classes: 80  
      		yolo_version: "V3"
      		softmax: true
      		background: false
          }
      }
    • Code example of operator 2:
      layer {
      	bottom: "layer94-conv"
      	top: "yolo2_coords"
      	top: "yolo2_obj"
      	top: "yolo2_classes"
      	name: "yolo2"
      	type: "Yolo"
      	yolo_param {
      		boxes: 3
      		coords: 4
      		classes: 80  
      		yolo_version: "V3"
      		softmax: true
      		background: false
      	}
      }
    • Code example of operator 3:
      layer {
      	bottom: "layer106-conv"
      	top: "yolo3_coords"
      	top: "yolo3_obj"
      	top: "yolo3_classes"
      	name: "yolo3"
      	type: "Yolo"
      	yolo_param {
      		boxes: 3
      		coords: 4
      		classes: 80  
      		yolo_version: "V3"
      		softmax: true
      		background: false
      	}
      }

    For details about the parameters, see Supported Caffe Operators.

  3. Add a YoloV3DetectionOutput operator to the output layer.

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

    layer {
           name: "detection_out3"
           type: "YoloV3DetectionOutput"
           bottom: "yolo1_coords" 
           bottom: "yolo2_coords"
           bottom: "yolo3_coords"
           bottom: "yolo1_obj"
           bottom: "yolo2_obj" 
           bottom: "yolo3_obj"
           bottom: "yolo1_classes"
           bottom: "yolo2_classes"
           bottom: "yolo3_classes" 
           bottom: "img_info"
           top: "box_out"
           top: "box_out_num"
           yolov3_detection_output_param {
                               boxes: 3
                               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_high: 10
                               biases_high: 13
                               biases_high: 16
                               biases_high: 30
                               biases_high: 33
                               biases_high: 23
                               biases_mid: 30
                               biases_mid: 61  
                               biases_mid: 62
                               biases_mid: 45
                               biases_mid: 59
                               biases_mid: 119
                               biases_low: 116
                               biases_low: 90  
                               biases_low: 156
                               biases_low: 198
                               biases_low: 373
                               biases_low: 326
           }
    }

    For details about the parameters, see Supported Caffe Operators.

  4. Add the input.

    The YoloV3DetectionOutput 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 3

    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
    }