Code Monkey home page Code Monkey logo

quantized-yolov5's Introduction

Low Precision Yolo

This repo contains code for training Yolo(v1, v3, v5) architectures using different quantization. It utilizes Brevitas which is a Pytorch research library for quantization-aware training (QAT) from Xilinx. The code is developed on top of YOLOV5.

Installation

$ git clone [email protected]:sefaburakokcu/quantized-yolov5.git
$ cd yolov5
$ pip install -r requirements.txt

Inference

detect.py runs inference on a variety of sources and saving results to runs/detect.

$ python detect.py --weights /path-to-weights-folder
		    --source 0  # webcam
		            file.jpg  # image 
		            file.mp4  # video
		            path/  # directory
		            path/*.jpg  # glob
		            'https://youtu.be/NUsoVlDFqZg'  # YouTube
		            'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream

Training

Run commands below to reproduce results on Widerface dataset.

$ python train.py --data widerface.yaml --cfg models/hub/yolov1-tiny-quant.yaml --weights '' --batch-size 128

Export

Sigmoid activation is used in detect layer when training whereas HardTanh is used when exporting the model for FINN.

$ python export.py --data widerface.yaml --weights runs/train/exp1/weights/best.pt --nodetect

Citation

If you utilize this repo in your research, please cite it as:

@inproceedings{Gunay_2022, series={EECSS’22},
   title={LPYOLO: Low Precision YOLO for Face Detection on FPGA},
   ISSN={2369-811X},
   url={http://dx.doi.org/10.11159/mvml22.108},
   DOI={10.11159/mvml22.108},
   booktitle={Proceedings of the 8th World Congress on Electrical Engineering and Computer Systems and Science},
   publisher={Avestia Publishing},
   author={Gunay, Bestami and Okcu, Sefa Burak and Bilge, Hasan Sakir},
   year={2022},
   month=jul, collection={EECSS’22} }

References

Authors

quantized-yolov5's People

Contributors

aehogan avatar albinxavi avatar alexstoken avatar anon-artist avatar ayushexel avatar borda avatar cristifati avatar dependabot[bot] avatar developer0hye avatar fcakyon avatar glenn-jocher avatar imyhxy avatar jdfr avatar kalenmike avatar kinoute avatar laughing-q avatar lorenzomammana avatar lornatang avatar nanocode012 avatar olehb avatar samfc10 avatar sefaburakokcu avatar skalskip avatar taoxiesz avatar tkianai avatar wanghaoyang0106 avatar yeric1789 avatar yxnong avatar zhiqwang avatar zldrobit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

quantized-yolov5's Issues

Some error in export to onnx

So I tried to train yolov5m-quant.yaml with coco128.yaml. When it's done, i want to export it but error come like this

Traceback (most recent call last):
File "/content/quantized-yolov5/models/experimental.py", line 97, in attempt_load
ema = ckpt['ema' if ckpt.get('ema') else 'model'].float()
KeyError: 'model'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/content/quantized-yolov5/export.py", line 418, in
main(opt)
File "/content/quantized-yolov5/export.py", line 406, in main
run(**vars(opt))
File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
File "/content/quantized-yolov5/export.py", line 317, in run
model = attempt_load(weights, map_location=device, inplace=True, fuse=True) # load FP32 model
File "/content/quantized-yolov5/models/experimental.py", line 105, in attempt_load
ema.load_state_dict(ckpt)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 2152, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for Model:
Missing key(s) in state_dict: "model.13.m.0.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.13.m.1.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.17.m.0.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.17.m.1.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.20.m.0.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.20.m.1.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.23.m.0.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value", "model.23.m.1.quant_identity.act_quant.fused_activation_quant_proxy.tensor_quant.scaling_impl.value".
size mismatch for model.24.m.0.weight: copying a param with shape torch.Size([255, 192, 1, 1]) from checkpoint, the shape in current model is torch.Size([18, 192, 1, 1]).
size mismatch for model.24.m.1.weight: copying a param with shape torch.Size([255, 384, 1, 1]) from checkpoint, the shape in current model is torch.Size([18, 384, 1, 1]).
size mismatch for model.24.m.2.weight: copying a param with shape torch.Size([255, 768, 1, 1]) from checkpoint, the shape in current model is torch.Size([18, 768, 1, 1]).

Do you have some advice or suggestion about this error? Thankyou

Question about which version of YOLO used

❔Question

I read the LPYOLO paper and it referenced the YOLOv3 version but this repo links to YOLOv5 for model training from scratch.
Which Ultralytic version would you recommend for full FPGA deployment? Thanks!

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

I encountered the following error while reproducing your code:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

The specific details are as follows:
Plotting labels...
Image sizes 640 train, 640 val
Using 8 dataloader workers
Logging results to runs/train/exp
Starting training for 300 epochs...

 Epoch   gpu_mem       box       obj       cls    labels  img_size

0%| | 0/183 [00:02<?, ?it/s]
Traceback (most recent call last):
File "/data/master21/zhujh/software/pythonProject/quantized-yolov5-quantized_yolo/train.py", line 653, in
main(opt)
File "/data/master21/zhujh/software/pythonProject/quantized-yolov5-quantized_yolo/train.py", line 545, in main
train(opt.hyp, opt, device, callbacks)
File "/data/master21/zhujh/software/pythonProject/quantized-yolov5-quantized_yolo/train.py", line 333, in train
pred = model(imgs) # forward
File "/home/zhujh/anaconda3/envs/yolov5/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "/data/master21/zhujh/software/pythonProject/quantized-yolov5-quantized_yolo/models/yolo.py", line 156, in forward
return self._forward_once(x, profile, visualize) # single-scale inference, train
File "/data/master21/zhujh/software/pythonProject/quantized-yolov5-quantized_yolo/models/yolo.py", line 179, in _forward_once
x = m(x) # run
File "/home/zhujh/anaconda3/envs/yolov5/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "/data/master21/zhujh/software/pythonProject/quantized-yolov5-quantized_yolo/models/common.py", line 150, in forward
x = self.conv(x)
File "/home/zhujh/anaconda3/envs/yolov5/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "/home/zhujh/anaconda3/envs/yolov5/lib/python3.7/site-packages/brevitas/nn/quant_conv.py", line 191, in forward
return self.forward_impl(input)
File "/home/zhujh/anaconda3/envs/yolov5/lib/python3.7/site-packages/brevitas/nn/quant_layer.py", line 311, in forward_impl
output_scale = output_scale * quant_input.scale.view(output_scale_shape)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

Process finished with exit code 1

May I ask if there is a good and effective solution,thanks.

Quantised YOLOv5

Hey, thank you for the repo. Can you add a quantised yolov5 model, where the user has individual control of the weight bit-widths for each layer and activation separately?

I have already seen the other issue mentioning yolov5. The quantised yolov1 is very good, but there is a difference with yolov5 that is non-trivial.

Thank you.

Additional context

Is quantized yolov5 available in the repo?

❔Question

Is quantized yolov5 available in the repo?

Additional context

I could see only the quantized yolov1 model using the quantized layers. So are quantized yolov5 models available in this repo or do I need to build them using the quantized layers?

Result reproduction

Hi, I'm trying to reproduce your results with yolov1-tiny model. How did you convert the data of widerface into a correct format? Cause when I download the zip it only contains images.

Thanks for the great work!

Supported FINN layers

          Hi!first of all, thank you very much for your selfless sharing and updates, which have been very helpful for my learning. You mentioned earlier that quantized YOLOv5 cannot be exported for use with FINN. What are the main layers that are difficult to implement in this context? Do you have any good ideas for replacing these layers?

Originally posted by @ramyen1 in #36 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.