Dynamic Image Size
The section describes how to support the dynamic image size function at model build time.
Overview
The dynamic image size feature is introduced to support the scenario where the image width and height are not determinable until runtime.
Procedure
- In Data operator definition, set the H and W dimensions in the shape to -1.
1 2 3 4 5
auto shape_data = vector<int64_t>({ 8,3,-1,-1 }); TensorDesc desc_data(ge::Shape(shape_data), FORMAT_ND, DT_FLOAT); auto data = op::Data("data"); data.update_input_desc_data(desc_data); data.update_output_desc_out(desc_data);
-
At model build time, set INPUT_SHAPE and INPUT_FORMAT in the options argument passed to the aclgrphBuildModel call and specify the dynamic image size profiles by setting DYNAMIC_IMAGE_SIZE.
- INPUT_FORMAT must be consistent with the format of each Data operator, and only NCHW and NHWC are supported. Otherwise, model build fails.
- INPUT_SHAPE is optional. If this parameter is not set, the shape of the corresponding Data nodes is used by default. Otherwise, the passed argument is used and updated to those of the corresponding Data nodes.
1 2 3 4 5 6 7
void PrepareOptions(std::map<AscendString, AscendString>& options) { options.insert({ {ge::ir_option::INPUT_FORMAT, "NCHW"}, {ge::ir_option::INPUT_SHAPE, "data: 8,3,-1,-1"}, {ge::ir_option::DYNAMIC_IMAGE_SIZE, "416,416;832,832"} // Set the H and W dimension size profiles. This setting indicates that both 416 x 416 and 832 x 832 images are supported. }); }
Precautions
- This function is exclusive with dynamic batch size and dynamic dimension size.
- Too large image sizes or too many image size profiles will cause model build failures.
- If this option is used to set the dynamic image size during model build, you need to perform the following operations before calling the model execution APIs to run an application project for inference:
- Use the aclmdlSetDynamicHWSize API provided by AscendCL to set the runtime image size. The size of the dataset images used for inference must match the runtime image size in use.
- If aclmdlSetDynamicHWSize is not called, values are assigned based on the maximum width and height within the dynamic image size range by default during model execution.
- If you have set the dynamic image size as well as static AIPP (by setting INSERT_OP_FILE), you must disable the cropping and padding functions, and set src_image_size_w and src_image_size_h to 0 in the configuration file.
- If you have set the dynamic image size as well as dynamic AIPP (by setting INSERT_OP_FILE):
In your inference code, call the aclmdlSetInputAIPP API provided by AscendCL to set dynamic AIPP parameters. Ensure that cropping and padding are disabled. In this scenario, ensure that the width and height configured by calling aclmdlSetInputAIPP are the same as those configured by calling aclmdlSetDynamicHWSize. That is, the width and height must be set to the maximum dynamic image size.
- The offline model generated with this option included is configured with the dynamic image size feature, which might have structure differences from that generated without this option and therefore shows different inference performance.
- If dynamic image size is enabled, the size of the dataset images used for inference must match the runtime image size in use.
- In the scenario where you have set too large image sizes or too many image size profiles, you are advised to run the swapoff -a command to disable the use of swap space as memory to prevent slow operating environment.