minhhn2910 / conga2022

Code for using Qtorch+ and reproduce Conga2022 results
MIT License
0 stars 1 forks source link

YOLO v5 model is not working with Qtorch+ when replacing the object detection used models (Faster/Mask R-CNN) #2

Open MEssam711 opened 1 year ago

MEssam711 commented 1 year ago

Hello,

Could you help me to fix the issues in the below script to be able to check YOLO results instead of (Faster/Mask R-CNN)?

I want to check YOLO-V5 which is developed using this community: https://pytorch.org/hub/ultralytics_yolov5/

So, I tried to replace the model in the script of torchbench_coco-posit.py but it gives me some errors.

Could you help me regarding this if the code needs to customize for this model. I thought as mentioned in the Qtorch+ paper that I can do this as simple as switching between the Pytorch models?

The used Script:

`from torchbench.object_detection import COCO from torchbench.utils import send_model_to_device from torchbench.object_detection.transforms import Compose, ConvertCocoPolysToMask, ToTensor import torchvision import PIL

import torch.nn as nn import qtorch_plus from qtorch_plus.quant import configurable_table_quantize, posit_quantize

import torch

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

def coco_data_to_device(input, target, device: str = "cuda", non_blocking: bool = True): input = list(inp.to(device=device, non_blocking=non_blocking) for inp in input) target = [{k: v.to(device=device, non_blocking=non_blocking) for k, v in t.items()} for t in target] return input, target

def coco_collate_fn(batch): return tuple(zip(*batch))

def coco_output_transform(output, target): output = [{k: v.to("cpu") for k, v in t.items()} for t in output] return output, target

transforms = Compose([ConvertCocoPolysToMask(), ToTensor()])

model_name = 'fasterrcnn_resnet50_fpn'

model_name = 'maskrcnn_resnet50_fpn'

model = torchvision.models.detection.dict[model_name](num_classes=91, pretrained=True)

def other_weight(input): input = posit_quantize(input, nsize=16, es=1) return input

def other_activation(input):

input = posit_quantize(input, nsize=16, es=1)
return input

def linear_weight(input): input = posit_quantize(input, nsize=8, es=1, scale= 4.0) return input def linear_activation(input): global act_data input = posit_quantize(input, nsize=8, es=1, scale= 0.5) return input

def forward_pre_hook_other(m, input): return (other_activation(input[0]),)

def forward_pre_hook_linear(m, input):

return (linear_activation(input[0]),)

layer_count = 0 total_layer = 0

for name, module in model.named_modules(): if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear) : module.weight.data = linear_weight(module.weight.data) module.register_forward_pre_hook(forward_pre_hook_linear)

    total_layer+=1
    layer_count +=1

else: #should use fixedpoint or posit 16 for other layers 'weight
    if hasattr(module, 'weight'):
        total_layer +=1
        module.weight.data = other_weight(module.weight.data)
        module.register_forward_pre_hook(forward_pre_hook_other)

        #pass

print ("total %d layers ; using posit on %d conv/linear layers"%(total_layer, layer_count))

Run the benchmark

"""COCO.benchmark( model=model, paper_model_name='Mask R-CNN (ResNet-50-FPN)', paper_arxiv_id='1703.06870', transforms=transforms, model_output_transform=coco_output_transform, send_data_to_device=coco_data_to_device, collate_fn=coco_collate_fn, batch_size=4, num_gpu=1 )"""

COCO.benchmark( model=model, paper_model_name='Yolo', transforms=transforms, model_output_transform=coco_output_transform, send_data_to_device=coco_data_to_device, collate_fn=coco_collate_fn, batch_size=4, num_gpu=1 )`

The appeared ERROR: Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N) ninja: no work to do. Loading extension module quant_cpu... Using /home/hanysalah/.cache/torch_extensions/py310_cu117 as PyTorch extensions root... Detected CUDA files, patching ldflags Emitting ninja build file /home/hanysalah/.cache/torch_extensions/py310_cu117/quant_cuda/build.ninja... Building extension module quant_cuda... Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N) ninja: no work to do. Loading extension module quant_cuda... Using cache found in /home/hanysalah/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2023-1-23 Python-3.10.6 torch-1.13.0+cu117 CUDA:0 (NVIDIA GeForce GTX 1650, 4096MiB)

Fusing layers... YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients Adding AutoShape... total 60 layers ; using posit on 60 conv/linear layers loading annotations into memory... Done (t=0.74s) creating index... index created! Evaluation: 0%| | 0/1250 [00:00<?, ?it/s] Traceback (most recent call last): File "/home/hanysalah/technical/posits/conga2022/torchbench_coco-posit.py", line 83, in COCO.benchmark( File "/home/hanysalah/technical/posits/torchbench/torchbench/object_detection/coco.py", line 220, in benchmark test_results, speed_mem_metrics, run_hash = evaluate_detection_coco( File "/home/hanysalah/technical/posits/torchbench/torchbench/object_detection/utils.py", line 209, in evaluate_detection_coco original_output = model(input) File "/home/hanysalah/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl return forward_call(*input, *kwargs) File "/home/hanysalah/.local/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context return func(args, **kwargs) File "/home/hanysalah/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 690, in forward im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1) TypeError: transpose() received an invalid combination of arguments - got (tuple), but expected one of:

Thanks in advance.

Best Regards, Mohammed Essam

minhhn2910 commented 1 year ago

Hi, I tried using their tutorial and it's supported. You can see my google colab running this yolov5 with Qtorch+ using posit6 and posit 8 totally ok here : https://colab.research.google.com/drive/1-HGcOtrxjfnkEDEvbP7hxr0YCcbIZ_y4?usp=sharing

I think the main problem you have is to run the original yolov5 with coco dataset from sotabench. And personally I think it should be an issue on yolov5 repo https://github.com/ultralytics/yolov5 . The better way is to find the tutorial code on https://github.com/ultralytics/yolov5 or ask them how to run validation with COCO dataset. Since they are quite active developing right now, they should be able to answer you very soon. Then you can start copying the qtorch+ code in the colab link I post above.

Nonetheless, if you successfully crafted the code to test coco from scratch after contacting them, please make a pull request to this repo or the original qtorch+ repo Your contribution is greatly appreciated :)