ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
48.37k stars 15.87k forks source link

How can I process the features during inference? #13161

Open Yangchen-nudt opened 2 days ago

Yangchen-nudt commented 2 days ago

Search before asking

Question

So much thank if developers can see my question and chat with me :) I use yolov5 project with ByteTrack(which is a two stage method: detect, then associate) to achieve multi-object tracking. But I found that there existing some missed detection: 2024-07-03 17-27-01屏幕截图 As shown in the pic, the car in the Bottom Right side cannot be detected (maybe due to the shadow cast on the car) However, i can inform the yolov5 algorithm the probable position of the undetected car, because it's detected in the previous tracking. So i think maybe i can enhance the three feature maps before the detect head. Specifically speaking, I generate one Gaussian distribution heatmap(the probable position is the peak point), and element-wise multiply the heatmap with the feature map. In this case, I want to let the yolov5 pay more attention to the probable position. Then when it comes to the pratical coding, I meet some problems cause I'm not that familiar with pytorch. I don't know how to extract the features before the Detect Head during inference, process them and them feed them back to the final Detect Head. I notice before the non_max_suppression, the detected result is given by: # Inference with dt[1]: visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False if model.xml and im.shape[0] > 1: pred = None for image in ims: if pred is None: pred = model(image, augment=augment, visualize=visualize).unsqueeze(0) else: pred = torch.cat((pred, model(image, augment=augment, visualize=visualize).unsqueeze(0)), dim=0) pred = [pred, None] else: pred = model(im, augment=augment, visualize=visualize) and the model is loaded with my trained weight. What should I do if i want to extract the feature map and then feed it back to the final Detect head?

I'll appreciate it for any instructions given to me. Long for your reply

Additional

No response

github-actions[bot] commented 2 days ago

👋 Hello @Yangchen-nudt, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 2 days ago

@Yangchen-nudt hello,

Thank you for your detailed question and for providing context on your use case with ByteTrack and YOLOv5. Enhancing feature maps during inference is an interesting approach to address missed detections.

To achieve this, you will need to modify the YOLOv5 model to extract and manipulate the feature maps before they are passed to the detection head. Here’s a step-by-step guide to help you get started:

  1. Modify the YOLOv5 Model: You will need to modify the model.py file to extract the feature maps. Specifically, you can hook into the forward pass of the model to access the intermediate feature maps.

  2. Extract Feature Maps: You can use PyTorch hooks to extract the feature maps. Here’s an example of how you can do this:

    import torch
    from models.yolo import Model
    
    # Load your model
    model = Model('path/to/your/yolov5.yaml', ch=3, nc=80)
    model.load_state_dict(torch.load('path/to/your/weights.pt')['model'])
    
    # Register hooks to extract feature maps
    feature_maps = []
    
    def hook_fn(module, input, output):
       feature_maps.append(output)
    
    hooks = []
    for layer in model.model:
       if isinstance(layer, torch.nn.Conv2d):
           hooks.append(layer.register_forward_hook(hook_fn))
    
    # Perform inference
    img = torch.randn(1, 3, 640, 640)  # Example input
    with torch.no_grad():
       pred = model(img)
    
    # Remove hooks
    for hook in hooks:
       hook.remove()
    
    # Now feature_maps contains the intermediate feature maps
  3. Enhance Feature Maps: Once you have the feature maps, you can enhance them using your Gaussian heatmap. Here’s an example of how you might do this:

    import torch.nn.functional as F
    
    # Generate Gaussian heatmap
    heatmap = torch.zeros_like(feature_maps[0])
    center = (320, 320)  # Example center
    sigma = 10
    for i in range(heatmap.shape[2]):
       for j in range(heatmap.shape[3]):
           heatmap[0, 0, i, j] = torch.exp(-((i - center[0]) ** 2 + (j - center[1]) ** 2) / (2 * sigma ** 2))
    
    # Enhance feature maps
    enhanced_feature_maps = [fm * heatmap for fm in feature_maps]
  4. Feed Enhanced Feature Maps to Detection Head: Finally, you need to modify the forward pass of the model to use the enhanced feature maps. This will require deeper changes to the model’s code to ensure the enhanced feature maps are used in the detection head.

Please ensure you are using the latest versions of torch and https://github.com/ultralytics/yolov5 to avoid any compatibility issues. If you encounter any specific errors or need further assistance, please provide a minimum reproducible code example as outlined in our documentation.

I hope this helps! If you have any further questions, feel free to ask.