Preparing .npy Data of a Caffe Model
This version does not support the generation of .npy files of a Caffe model. You need to install the Caffe environment and prepare the .npy file 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.
Prepare the .npy file as follows:
- Save the file content in NumPy format.
- Name the file in op_name.output_index.timestamp.npy format. Ensure that the output_index field is contained in the .npy file name and the output_index value of the generated dump data is 0. This is because by default, accuracy comparison starts from the first data whose output_index is 0. Otherwise, no comparison result is returned.
- 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 Ascend Model Compression Toolkit Instructions (Caffe).
Go to the /home/HwHiAiUser/Ascend/ascend-toolkit/latest/toolkit/tools/operator_cmp/compare directory and run the following command to remove in-place:
python3.7.5 inplace_layer_process.pyc -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.
- For quantization scenarios, to ensure accuracy, data pre-processing during Caffe model inference must be the same as that during Caffe model compression.
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) os.chmod(output_dump_path, FILE_PERMISSION_FLAG) 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.