ultralytics / yolov5

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

Extract training or validation losses for use in another function #9317

Closed ayindemalik closed 2 years ago

ayindemalik commented 2 years ago

Search before asking

Question

Hello, I am working on a project related to handwritten detection. apart from normal training, I need to perform also adversarial training using adversarial samples. However, to generate adversarial samples, training or validation losses are needed. So my issue is how I could extract or save the losses during the training process and be able to use them in generating adversarial images using functions like FGSM (Fast Gradient Sign Method) or PGD (Projected Gradient Descent).

Any help is appreciated.

Additional

No response

github-actions[bot] commented 2 years ago

👋 Hello @ayinemalik, 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 screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. 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

CI CPU testing

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

glenn-jocher commented 2 years ago

Losses are computed in train.py here: https://github.com/ultralytics/yolov5/blob/5a134e06530a8c24fdb9774c2c4ab0b513b08260/train.py#L307

ayindemalik commented 2 years ago

thanks Mr. glenn-jocker for your helped reply. I still need an additional htlp tip

sopose I want to use the yolov5 model during the training to generate adversarial samples images and include them through the training process like it has been processed in the code picture below for image classification model.

So question is at with which process and line of code I could get the model and apply it to fast_gradient_method(torch_model, xs, eps=0.3, norm=np.inf, clip_min=0., clip_max=1.) method in yolov5. or any additional tips will be apreciated. image

Thanks in advance.

github-actions[bot] commented 2 years ago

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

glenn-jocher commented 1 year ago

@ayindemalik you can apply adversarial attacks directly to the YOLOv5 model by accessing the underlying PyTorch model. In train.py, after line 307, insert the following code to generate adversarial samples:

import torch
import numpy as np
from advertorch.attacks import GradientSignAttack

# Access PyTorch model from YOLOv5 model
torch_model = model.model[-1]  # Access the underlying PyTorch model

# Generate adversarial samples
adversary = GradientSignAttack(torch_model)
adversarial_samples = adversary.perturb(xs, ys)

This code snippet accesses the PyTorch model from YOLOv5 and generates adversarial samples using the FGSM method. Remember to adjust the attack method and parameters according to your requirements.