Quantization-aware training with the API
See original GitHub issueInstructions To Reproduce the 🐛 Bug:
I tried to add quantization-aware training to the d2go_beginner.ipynb notebook, but I couldn’t get it to work.
Code:
from d2go.runner import Detectron2GoRunner
def prepare_for_launch():
runner = Detectron2GoRunner()
cfg = runner.get_default_cfg()
cfg.merge_from_file(model_zoo.get_config_file("faster_rcnn_fbnetv3a_C4.yaml"))
cfg.MODEL_EMA.ENABLED = False
cfg.DATASETS.TRAIN = ("balloon_train",)
cfg.DATASETS.TEST = ("balloon_val",)
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("faster_rcnn_fbnetv3a_C4.yaml") # Let training initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.00025 # pick a good LR
cfg.SOLVER.MAX_ITER = 600 # 300 iterations seems good enough for this toy dataset; you will need to train longer for a practical dataset
cfg.SOLVER.STEPS = [] # do not decay learning rate
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128 # faster, and good enough for this toy dataset (default: 512)
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1 # only has one class (ballon). (see https://detectron2.readthedocs.io/tutorials/datasets.html#update-the-config-for-new-datasets)
# NOTE: this config means the number of classes, but a few popular unofficial tutorials incorrect uses num_classes+1 here.
# quantization-aware training
cfg.QUANTIZATION.BACKEND = "qnnpack"
cfg.QUANTIZATION.QAT.ENABLED = True
cfg.QUANTIZATION.QAT.START_ITER = 0
cfg.QUANTIZATION.QAT.ENABLE_OBSERVER_ITER = 0
cfg.QUANTIZATION.QAT.DISABLE_OBSERVER_ITER = 5
cfg.QUANTIZATION.QAT.FREEZE_BN_ITER = 7
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
return cfg, runner
cfg, runner = prepare_for_launch()
print(cfg)
model = runner.build_model(cfg)
runner.do_train(cfg, model, resume=False)
Error message:
AssertionError Traceback (most recent call last)
<ipython-input-11-327fe2b2a9ce> in <module>()
32 cfg, runner = prepare_for_launch()
33 print(cfg)
---> 34 model = runner.build_model(cfg)
35 runner.do_train(cfg, model, resume=False)
15 frames
/usr/local/lib/python3.7/dist-packages/torch/quantization/fuser_method_mappings.py in get_fuser_method(op_list, additional_fuser_method_mapping)
129 additional_fuser_method_mapping)
130 fuser_method = all_mappings.get(op_list, None)
--> 131 assert fuser_method is not None, "did not find fuser method for: {} ".format(op_list)
132 return fuser_method
AssertionError: did not find fuser method for: (<class 'torch.nn.modules.conv.Conv2d'>, <class 'mobile_cv.arch.layers.batch_norm.NaiveSyncBatchNorm'>, <class 'torch.nn.modules.activation.ReLU'>)
Expected behavior:
Quantization-aware training should work with the API.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Quantization aware training - Model optimization - TensorFlow
Experiment with quantization algorithms that span Keras layers or require the training step. Stabilize APIs. Results. Image classification with ...
Read more >TF2 Object Detect API Quantization Aware Training #8935
Great to see the Tensorflow 2 Object Detect API has been released. One feature I'm very interested in is quantization aware training (as...
Read more >vai_q_tensorflow Quantization Aware Training - 2.5 English
Quantization aware training (QAT) is similar to float model training/finetuning, but in QAT, the vai_q_tensorflow APIs are used to rewrite the float graph ......
Read more >Transfer Learning with Quantization Aware Training using ...
I'd like to understand what is the correct way to perform quantization-aware-training in this case? python · tensorflow · machine-learning ...
Read more >Quantization of TensorFlow Object Detection API Models
Quantization -aware training gives less accuracy drop compared to post-training quantization and allows us to recover most of the accuracy loss ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for you guy’s contribution!!
I am wondering is there any further update?
According to my experiments, I was using the
balloon
dataset to do QAT experiments and the results were quite bad. To be precise, the results were strange that all score values were the same (all are 50%) indeed.When I switched the model from QAT-model to normal model which was trained by the code from the demo jupyter notebook, the predictions were more correct.
In addition, I compared the inference time between QAT-mdoel and the normal model, the inference time of both models were the same. I anticipate that the speed will be accelerated even the predicting ability is not good.
In addition, I also expect that the QAT-model size could be smaller than the normal model but it was not actually.
Thank you.
I now tried multiple sets of hyperparameters, and I achieved a decent total_loss with the following hyperparameters:
Output:
But if I try to make predictions on the validation set, nothing is getting detected. Do I need to pre-process the images differently now?
Code:
Full notebook: https://colab.research.google.com/drive/1BSa319b6QCfX4yEJ-Zedi5KGnT8ldZ_w?usp=sharing