mirror of
https://github.com/ultralytics/yolov5.git
synced 2025-06-03 14:49:29 +08:00
* Add license line to .github/ISSUE_TEMPLATE/bug-report.yml * Add license line to .github/ISSUE_TEMPLATE/config.yml * Add license line to .github/ISSUE_TEMPLATE/feature-request.yml * Add license line to .github/ISSUE_TEMPLATE/question.yml * Add license line to .github/dependabot.yml * Add license line to .github/workflows/ci-testing.yml * Add license line to .github/workflows/cla.yml * Add license line to .github/workflows/codeql-analysis.yml * Add license line to .github/workflows/docker.yml * Add license line to .github/workflows/format.yml * Add license line to .github/workflows/greetings.yml * Add license line to .github/workflows/links.yml * Add license line to .github/workflows/merge-main-into-prs.yml * Add license line to .github/workflows/stale.yml * Add license line to benchmarks.py * Add license line to classify/predict.py * Add license line to classify/train.py * Add license line to classify/val.py * Add license line to data/Argoverse.yaml * Add license line to data/GlobalWheat2020.yaml * Add license line to data/ImageNet.yaml * Add license line to data/ImageNet10.yaml * Add license line to data/ImageNet100.yaml * Add license line to data/ImageNet1000.yaml * Add license line to data/Objects365.yaml * Add license line to data/SKU-110K.yaml * Add license line to data/VOC.yaml * Add license line to data/VisDrone.yaml * Add license line to data/coco.yaml * Add license line to data/coco128-seg.yaml * Add license line to data/coco128.yaml * Add license line to data/hyps/hyp.Objects365.yaml * Add license line to data/hyps/hyp.VOC.yaml * Add license line to data/hyps/hyp.no-augmentation.yaml * Add license line to data/hyps/hyp.scratch-high.yaml * Add license line to data/hyps/hyp.scratch-low.yaml * Add license line to data/hyps/hyp.scratch-med.yaml * Add license line to data/xView.yaml * Add license line to detect.py * Add license line to export.py * Add license line to hubconf.py * Add license line to models/common.py * Add license line to models/experimental.py * Add license line to models/hub/anchors.yaml * Add license line to models/hub/yolov3-spp.yaml * Add license line to models/hub/yolov3-tiny.yaml * Add license line to models/hub/yolov3.yaml * Add license line to models/hub/yolov5-bifpn.yaml * Add license line to models/hub/yolov5-fpn.yaml * Add license line to models/hub/yolov5-p2.yaml * Add license line to models/hub/yolov5-p34.yaml * Add license line to models/hub/yolov5-p6.yaml * Add license line to models/hub/yolov5-p7.yaml * Add license line to models/hub/yolov5-panet.yaml * Add license line to models/hub/yolov5l6.yaml * Add license line to models/hub/yolov5m6.yaml * Add license line to models/hub/yolov5n6.yaml * Add license line to models/hub/yolov5s-LeakyReLU.yaml * Add license line to models/hub/yolov5s-ghost.yaml * Add license line to models/hub/yolov5s-transformer.yaml * Add license line to models/hub/yolov5s6.yaml * Add license line to models/hub/yolov5x6.yaml * Add license line to models/segment/yolov5l-seg.yaml * Add license line to models/segment/yolov5m-seg.yaml * Add license line to models/segment/yolov5n-seg.yaml * Add license line to models/segment/yolov5s-seg.yaml * Add license line to models/segment/yolov5x-seg.yaml * Add license line to models/tf.py * Add license line to models/yolo.py * Add license line to models/yolov5l.yaml * Add license line to models/yolov5m.yaml * Add license line to models/yolov5n.yaml * Add license line to models/yolov5s.yaml * Add license line to models/yolov5x.yaml * Add license line to pyproject.toml * Add license line to segment/predict.py * Add license line to segment/train.py * Add license line to segment/val.py * Add license line to train.py * Add license line to utils/__init__.py * Add license line to utils/activations.py * Add license line to utils/augmentations.py * Add license line to utils/autoanchor.py * Add license line to utils/autobatch.py * Add license line to utils/aws/resume.py * Add license line to utils/callbacks.py * Add license line to utils/dataloaders.py * Add license line to utils/downloads.py * Add license line to utils/flask_rest_api/example_request.py * Add license line to utils/flask_rest_api/restapi.py * Add license line to utils/general.py * Add license line to utils/google_app_engine/app.yaml * Add license line to utils/loggers/__init__.py * Add license line to utils/loggers/clearml/clearml_utils.py * Add license line to utils/loggers/clearml/hpo.py * Add license line to utils/loggers/comet/__init__.py * Add license line to utils/loggers/comet/comet_utils.py * Add license line to utils/loggers/comet/hpo.py * Add license line to utils/loggers/wandb/wandb_utils.py * Add license line to utils/loss.py * Add license line to utils/metrics.py * Add license line to utils/plots.py * Add license line to utils/segment/augmentations.py * Add license line to utils/segment/dataloaders.py * Add license line to utils/segment/general.py * Add license line to utils/segment/loss.py * Add license line to utils/segment/metrics.py * Add license line to utils/segment/plots.py * Add license line to utils/torch_utils.py * Add license line to utils/triton.py * Add license line to val.py * Auto-format by https://ultralytics.com/actions * Update ImageNet1000.yaml Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> * Auto-format by https://ultralytics.com/actions --------- Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
127 lines
4.5 KiB
Python
127 lines
4.5 KiB
Python
# Ultralytics YOLOv5 🚀, AGPL-3.0 license
|
|
"""Activation functions."""
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
|
|
|
|
class SiLU(nn.Module):
|
|
@staticmethod
|
|
def forward(x):
|
|
"""
|
|
Applies the Sigmoid-weighted Linear Unit (SiLU) activation function.
|
|
|
|
https://arxiv.org/pdf/1606.08415.pdf.
|
|
"""
|
|
return x * torch.sigmoid(x)
|
|
|
|
|
|
class Hardswish(nn.Module):
|
|
@staticmethod
|
|
def forward(x):
|
|
"""
|
|
Applies the Hardswish activation function, compatible with TorchScript, CoreML, and ONNX.
|
|
|
|
Equivalent to x * F.hardsigmoid(x)
|
|
"""
|
|
return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX
|
|
|
|
|
|
class Mish(nn.Module):
|
|
"""Mish activation https://github.com/digantamisra98/Mish."""
|
|
|
|
@staticmethod
|
|
def forward(x):
|
|
"""Applies the Mish activation function, a smooth alternative to ReLU."""
|
|
return x * F.softplus(x).tanh()
|
|
|
|
|
|
class MemoryEfficientMish(nn.Module):
|
|
class F(torch.autograd.Function):
|
|
@staticmethod
|
|
def forward(ctx, x):
|
|
"""Applies the Mish activation function, a smooth ReLU alternative, to the input tensor `x`."""
|
|
ctx.save_for_backward(x)
|
|
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
|
|
|
|
@staticmethod
|
|
def backward(ctx, grad_output):
|
|
"""Computes the gradient of the Mish activation function with respect to input `x`."""
|
|
x = ctx.saved_tensors[0]
|
|
sx = torch.sigmoid(x)
|
|
fx = F.softplus(x).tanh()
|
|
return grad_output * (fx + x * sx * (1 - fx * fx))
|
|
|
|
def forward(self, x):
|
|
"""Applies the Mish activation function to the input tensor `x`."""
|
|
return self.F.apply(x)
|
|
|
|
|
|
class FReLU(nn.Module):
|
|
"""FReLU activation https://arxiv.org/abs/2007.11824."""
|
|
|
|
def __init__(self, c1, k=3): # ch_in, kernel
|
|
"""Initializes FReLU activation with channel `c1` and kernel size `k`."""
|
|
super().__init__()
|
|
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
|
|
self.bn = nn.BatchNorm2d(c1)
|
|
|
|
def forward(self, x):
|
|
"""
|
|
Applies FReLU activation with max operation between input and BN-convolved input.
|
|
|
|
https://arxiv.org/abs/2007.11824
|
|
"""
|
|
return torch.max(x, self.bn(self.conv(x)))
|
|
|
|
|
|
class AconC(nn.Module):
|
|
"""
|
|
ACON activation (activate or not) function.
|
|
|
|
AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
|
|
See "Activate or Not: Learning Customized Activation" https://arxiv.org/pdf/2009.04759.pdf.
|
|
"""
|
|
|
|
def __init__(self, c1):
|
|
"""Initializes AconC with learnable parameters p1, p2, and beta for channel-wise activation control."""
|
|
super().__init__()
|
|
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
|
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
|
self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))
|
|
|
|
def forward(self, x):
|
|
"""Applies AconC activation function with learnable parameters for channel-wise control on input tensor x."""
|
|
dpx = (self.p1 - self.p2) * x
|
|
return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x
|
|
|
|
|
|
class MetaAconC(nn.Module):
|
|
"""
|
|
ACON activation (activate or not) function.
|
|
|
|
AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
|
|
See "Activate or Not: Learning Customized Activation" https://arxiv.org/pdf/2009.04759.pdf.
|
|
"""
|
|
|
|
def __init__(self, c1, k=1, s=1, r=16):
|
|
"""Initializes MetaAconC with params: channel_in (c1), kernel size (k=1), stride (s=1), reduction (r=16)."""
|
|
super().__init__()
|
|
c2 = max(r, c1 // r)
|
|
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
|
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
|
self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)
|
|
self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)
|
|
# self.bn1 = nn.BatchNorm2d(c2)
|
|
# self.bn2 = nn.BatchNorm2d(c1)
|
|
|
|
def forward(self, x):
|
|
"""Applies a forward pass transforming input `x` using learnable parameters and sigmoid activation."""
|
|
y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)
|
|
# batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891
|
|
# beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable
|
|
beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed
|
|
dpx = (self.p1 - self.p2) * x
|
|
return dpx * torch.sigmoid(beta * dpx) + self.p2 * x
|