-
-
Notifications
You must be signed in to change notification settings - Fork 15.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I process the features during inference? #13161
Comments
👋 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. RequirementsPython>=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 EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf 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 |
@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:
Please ensure you are using the latest versions of I hope this helps! If you have any further questions, feel free to ask. |
Hello @aybukesakaci, Thank you for reaching out with your interesting project on unsupervised domain adaptation using YOLOv5x. Here’s a step-by-step guide to help you integrate an attention module into YOLOv5x: 1. Extract Features with YOLOv5xTo extract features from an intermediate layer of YOLOv5x, you can use PyTorch hooks. Here’s an example: import torch
from models.yolo import Model
# Load your model
model = Model('path/to/your/yolov5x.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 2. Pass Through GRL and DiscriminatorYou will need to implement a Gradient Reversal Layer (GRL) and a discriminator. Here’s a basic implementation: import torch.nn as nn
import torch.autograd as autograd
class GRL(autograd.Function):
@staticmethod
def forward(ctx, x):
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
return grad_output.neg()
class Discriminator(nn.Module):
def __init__(self, input_dim):
super(Discriminator, self).__init__()
self.fc = nn.Sequential(
nn.Linear(input_dim, 1024),
nn.ReLU(),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Linear(1024, 1),
nn.Sigmoid()
)
def forward(self, x):
x = GRL.apply(x)
return self.fc(x) 3. Modulate Features with Attention WeightsPass the extracted features through the GRL and discriminator to get attention weights, then modulate the features: # Assuming feature_maps[0] is the extracted feature map
features = feature_maps[0]
discriminator = Discriminator(features.shape[1])
attention_weights = discriminator(features.view(features.size(0), -1))
attention_weights = attention_weights.view_as(features)
# Modulate features
modulated_features = features * attention_weights 4. Feed Modulated Features Back to YOLOv5xTo feed the modulated features back into YOLOv5x, you will need to modify the forward pass of the model to accept these features. This requires deeper changes to the model’s code. Additional Steps
I hope this helps! If you have any further questions or run into any issues, feel free to ask. Good luck with your project! 🚀 |
Hello again, I have successfully completed the first three steps. I have the modulated features. How do I integrate this modulated features into the backbone? Should i change the backbone? How can I use new features without changing the backbone? Is that possible? Thanks in advance! |
Hello @aybukesakaci, Great to hear that you've successfully completed the first three steps! Integrating the modulated features back into the YOLOv5x model can indeed be done without changing the backbone. Here’s how you can proceed: 1. Integrate Modulated FeaturesYou can integrate the modulated features by modifying the forward pass of the YOLOv5 model to use these features. Here’s an example of how you can do this:
import torch
import torch.nn as nn
from models.yolo import Model
class CustomYOLOv5(Model):
def forward(self, x, modulated_features=None, augment=False, profile=False, visualize=False):
# Original forward pass
y, dt = [], []
for m in self.model:
if m.f != -1: # if not from previous layer
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]
if modulated_features is not None and isinstance(m, nn.Conv2d):
x = x + modulated_features # Integrate modulated features
x = m(x)
y.append(x if m.i in self.save else None)
return x
# Load your custom model
model = CustomYOLOv5('path/to/your/yolov5x.yaml', ch=3, nc=80)
model.load_state_dict(torch.load('path/to/your/weights.pt')['model'])
# Perform inference with modulated features
img = torch.randn(1, 3, 640, 640) # Example input
with torch.no_grad():
pred = model(img, modulated_features=modulated_features) 2. Ensure CompatibilityMake sure you are using the latest versions of 3. Testing and ValidationAfter integrating the modulated features, thoroughly test and validate the model to ensure it performs as expected. If you encounter any specific issues or need further assistance, feel free to ask. The YOLO community and the Ultralytics team are here to help! 😊 Best of luck with your project! |
Search before asking
Question
So much thank if developers can see my question and chat with me :)
![2024-07-03 17-27-01屏幕截图](https://private-user-images.githubusercontent.com/155145106/345364780-3f54527b-140f-4797-9760-b0f406dfb5bb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjExMzk3NDcsIm5iZiI6MTcyMTEzOTQ0NywicGF0aCI6Ii8xNTUxNDUxMDYvMzQ1MzY0NzgwLTNmNTQ1MjdiLTE0MGYtNDc5Ny05NzYwLWIwZjQwNmRmYjViYi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzE2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcxNlQxNDE3MjdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jYzMxYWU2ZWQ4Nzk4ZTk1ZjUwMjkzYTJiZmRkNDU2OTIwZDg1YWE3Zjk4NDg2OWRmMWViYzY2ZDg3MzNkNTNiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8pd77gsaQmoLDA7QBcRKhpsqBqd2Jq56sY5qR_5h994)
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:
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
The text was updated successfully, but these errors were encountered: