Case Study

Description

For the Fast R-CNN network, the default high-performance implementation mode and force_fp16 precision mode are retained for model conversion. However, the inference result is wrong, and the mAP result is 0.

Then, precision_mode=allow_fp32_to_fp16 is configured in the atc command line to generate a high-precision offline model. The inference accuracy with this new offline model is satisfactory.

Analysis

  1. During model conversion, retain the default high-performance mode and force_fp16 precision mode. Then, run inference on the new offline model to obtain dump data.
  2. During model re-conversion, set the high-precision mode (precision_mode=allow_fp32_to_fp16). Then, run inference again on the new offline model to obtain dump data.
  3. Use the Model Accuracy Analyzer to compare the dump data obtained in 1 and 2.

    The following is an example of the comparison result.

  4. The CosineSimilarity column shows the cosine similarity comparison result. The value range is [–1, +1]. A value closer to 1 indicates higher similarity. A value closer to –1 indicates lower similarity. For most operators, cosine similarity lower than 0.95 implies inaccuracy.

    As shown in the preceding figure, the cosine similarity of the AddN operator is as low as 0.72. Further analyze the dump data file of output 0 of the operator in high-precision mode (obtained in 2).

  5. Dump data files cannot be viewed using a text tool. Therefore, before analyzing a dump data file, convert the dump data file to a NumPy file and then convert the NumPy file to a TXT file. For details, see Viewing Dump Data Files in Accuracy Analyzer.

    When converting a NumPy file into a TXT file, you can obtain the maximum and minimum values of output 0 of the AddN operator. The following is a command example (****.npy indicates the path of the NumPy file):

    $ python3
    Python 3 (default, Mar  5 2020, 16:07:54)[GCC 5.4.0 20160609] on linuxType ....
    >>> import numpy as np
    >>> a = np.load("****.npy")
    >>> a.max()
    >>> 109508.0
    >>> a.min()
    >>> 70683.0
  6. According to the maximum and minimum values of output 0 of the AddN operator obtained from 5, the maximum output tensor of the AddN operator is 109508.0 in high-precision mode and 65504.0 in high-performance mode (fp16). The maximum value range of FP16 is -65505 to 65504. Therefore, the output value of the AddN operator in high-precision mode is greater than the expression range of the fp16 type field. Therefore, you need to configure the operator to use the high-precision mode. For details, see Preserving the Precision of Selected Operators.