Preparing .npy Data of a Caffe Model

Prerequisites

  • To obtain the .npy data file of a quantized Caffe model, refer to Quantized Original Model and Quantization Information File to obtain the original model first.
  • 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 dump 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 how to install AMCT, see Tool Installation in the AMCT Instructions.

    Go to the $HOME/Ascend/ascend-toolkit/latest/tools/operator_cmp/compare directory and run the following command to remove in-place:

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

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

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

.npy Data File Generation

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.

How to prepare the .npy file of a Caffe model is not detailed herein. You can prepare it by yourself.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    #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.

The .npy data file is named in the format of {op_name}.{output_index}.{timestamp}.npy. Ensure that the {output_index} field is set to 0. Otherwise, no comparison result is generated. This is because the accuracy comparison starts from the first data piece whose output_index value is 0 by default.