Quantization Example
Prerequisites
- Model
Upload a TensorFlow model and its weight file to any directory on the Linux server as the AMCT installation user.
The following uses the sample/resnet_v1_50 model as an example. The pre_model/ResNet50_train.meta file is used for retrain. The pre_model/ResNet50_eval.meta file is used for validation.
- Image dataset
Because retrain needs to use a large amount of data to further optimize the quantization parameters, the retrain data needs to be consistent with that used to train the model. The ResNet-50 dataset is trained on the subset ILSVRC-2012-CLS of ImageNet. Therefore, you need to prepare a dataset in TFRecord format. To use a different dataset, you need to preprocess the data by yourself.
Procedure
- Retrain the resnet_v1_50 model.Go to the sample/resnet_v1_50 directory and run the following command to retrain the resnet_v1_50 model:
python3.7.5 resnet_v1_50_retrain_sample.py --train_set TRAIN_SET [--train_keyword TRAIN_KEYWORD] --eval_set EVAL_SET [--eval_keyword EVAL_KEYWORD] --train_model TRAIN_MODEL --eval_model EVAL_MODEL
The following table describes the available command-line options.
Table 4-18 Command-line options for retrainOption
Description
-h
- Optional.
- Displays help information.
--train_set TRAIN_SET
- Required.
- A string.
- Default: None
- Sets the directory of the image dataset.
--train_keyword TRAIN_KEYWORD
- Optional.
- A string.
- Default: None
- Selects files containing the keyword in the training dataset directory. If not specified, all files in the training dataset directory are used by default.
--eval_set EVAL_SET
- Required.
- A string.
- Default: None
- Sets the validation dataset directory.
--eval_keyword EVAL_KEYWORD
- Optional.
- A string.
- Default: None
- Selects files containing the keyword in the validation dataset directory. If not specified, all files in the validation dataset directory are used by default.
--train_model TRAIN_MODEL
- Required.
- A string.
- Default: ./pre_model/resnet_v1_50_train.meta
- Sets the directory of the model to retrain.
--eval_model EVAL_MODEL
- Required.
- A string.
- Default: ./pre_model/resnet_v1_50_eval.meta
- Sets the directory of the model for validation.
--num_parallel_reads NUM_PARALLEL_READS
- Optional.
- An int.
- Default: 4
- Sets the number of threads for reading datasets. Set the argument according to the hardware compute capability.
--buffer_size BUFFER_SIZE
- Optional.
- An int.
- Default: 1000
- Sets the size for dataset out-of-order buffer. Set this option according to the memory capacity.
--repeat_count REPEAT_COUNT
- Optional.
- An int.
- Default: 0
- Sets the number of repetitions of the dataset. The argument 0 indicates infinite repetition.
--batch_size BATCH_SIZE
- Optional.
- An int.
- Default: 32
- Sets the batch size of TensorFlow execution. Set the argument according to the memory or video RAM capacity.
--ckpt CKPT_PATH
- Optional.
- A string.
- Default: None.
- Sets the directory of the benchmark weight checkpoint file of the ResNet-50 v1 model. If not specified, the file is automatically downloaded.
--learning_rate LEARNING_RATE
- Optional.
- A float.
- Default: 1e-6
- Sets the learning rate.
--save_interval SAVE_INTERVAL
- Optional.
- An int.
- Default: 1000
- Sets the retrain save interval.
--momentum MOMENTUM
- Optional.
- A float.
- Default: 0.9
- Sets the momentum of the RMSPropOptimizer.
--train_iter TRAIN_ITER
- Optional.
- An int.
- Default: 1000
- Sets the number of training iterations.
An example is as follows:
python3.7.5 resnet_v1_50_retrain_sample.py --train_set /data/Datasets/imagenet_TF_trainset/ --eval_set /data/Datasets/imagenet_TF_evalset/
If the upload directory of the test dataset is the same as that of the validation dataset, add the --train_keyword TRAIN_KEYWORD and --eval_keyword EVAL_KEYWORD options to the quantization command to ensure that the --train_set option points to the test dataset and the --eval_set option points to the validation dataset.
If messages similar to the following are displayed, the retrain is successful:
The model after retraining top 1 accuracy = 68.14%. The model after retraining top 5 accuracy = 88.03%.
- View the retrain result.After the retrain is successful, find the following folders and files generated to the sample/resnet_v1_50 directory:
- amct_log: AMCT log folder, including the retrain log amct_tensorflow.log.
- results: retrain result folder, containing the quantized model file resnet_v1_50_quantized.pb.
This model can be used for accuracy simulation in the TensorFlow environment and can also be deployed on the Ascend AI Processor.
- tmp: retrain temporary folder, containing:
- checkpoint: checkpoints saved during TensorFlow training.
- config.json: retrain-based quantization configuration file that describes how to retrain each layer in the model. If a retrain configuration file already exists in the directory where the retrain script is located, when create_quant_retrain_config is called again, the existing retrain configuration file is overwritten if the name of the new retrain configuration file is the same as that of the existing file. Otherwise, a new retrain configuration file is generated.
- events.out.tfevents.xxxxxxxxxx.xxx: file that describes the retrain loss information, which can be viewed on the TensorBoard.
- record.txt: file that records quantization factors. For details about the prototype definition of the file, see Quantization Factor Record File.
- resnet_v1_50.pb: retrained model file.
- resnet_v1_50_retrain-0.data-00000-of-00001: weight file generated during retrain.
- resnet_v1_50_retrain-0.index: index of the weight file generated during retrain.
- resnet_v1_50_retrain-0.meta: model file to be retrained.
- resnet_v1_50_retrain-1.data-00000-of-00001: weight file generated during retrain.
- resnet_v1_50_retrain-1.index: index of the weight file generated during retrain.
- resnet_v1_50_retrain-1.meta: model file to be retrained.
When the model is retrained, the existing result files will be overwritten.