Preparing .npy Data of a Caffe Model

Requirements for Preparing Raw Caffe Data Files

Prepare the .npy file as follows:

  • Save the file content in NumPy (.npy) format.
  • Name the file in the format of {op_name}.{output_index}.{timestamp}.npy. Set the output_index field in the NumPy data file name to 0 and ensure that the output_index value of the dump data generated after conversion is also 0. Otherwise, no comparison result is available. This is because the accuracy comparison starts from the first data piece whose output_index value is 0 by default.
  • To ensure that the .npy file is correctly named, remove in-place on the Caffe model file to generate a new .prototxt model file for .npy file generation. For example, if there are four fused operators A, B, C, and D whose in-place is not removed, the output data dumping result is that of operator D, while the file name is prefixed with operator A. For quantization scenarios, install AMCT in the environment before running the command to remove in-place. For details about the installation, see the AMCT (Caffe).

    Go to the Ascend-CANN-Toolkit installation directory/ascend-toolkit/latest/tools/operator_cmp/compare directory and run a command to remove in-place. The following is a command example:

    python3 inplace_layer_process.py -i /home/HwHiAiUser/resnet50.prototxt

    After the command is executed, the new_resnet50.prototxt file with in-place removed is generated in the /home/HwHiAiUser directory.

  • For quantization scenarios, to ensure accuracy, data preprocessing during Caffe model inference must be the same as that during Caffe model compression.

Generating the .npy File

This version does not support the generation of NumPy files of a Caffe model. You need to install the Caffe environment and prepare the source data .npy files in advance. This section provides only a Caffe .npy file example that meets the accuracy comparison requirements.

Details about how to prepare the .npy file of a Caffe model are not described herein. You can prepare it by yourself.

To generate an .npy file that meets the accuracy comparison requirements, add similar code as follows after the inference is complete.

    #read prototxt file
    net_param = caffe_pb2.NetParameter()        
    with open(self.model_file_path, 'rb') as model_file:
        google.protobuf.text_format.Parse(model_file.read(), net_param)

        # save data to numpy file
        for layer in net_param.layer: 
            name = layer.name.replace("/", "_").replace(".", "_")
            index = 0
            for top in layer.top: 
                data = net.blobs[top].data[...]
                file_name = name + "." + str(index) + "." + str( 
                    round(time.time() * 1000000)) + ".npy"
                output_dump_path = os.path.join(self.output_path, file_name)
                np.save(output_dump_path, data)
                print('The dump data of "' + layer.name
                      + '" has been saved to "' + output_dump_path + '".')
                index += 1

After the preceding code is added, run the application project of the Caffe model to generate an .npy file that meets the requirements.