Skip to content
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

Focus module? #3850

Closed
HuKai97 opened this issue Jul 1, 2021 · 4 comments
Closed

Focus module? #3850

HuKai97 opened this issue Jul 1, 2021 · 4 comments
Labels
question Further information is requested

Comments

@HuKai97
Copy link

HuKai97 commented Jul 1, 2021

Hello, which paper is your focus module from? Or did you make it yourself? What's the theory?
image

@HuKai97 HuKai97 added the question Further information is requested label Jul 1, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jul 1, 2021

👋 Hello @coderhukai, 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://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

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), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 1, 2021

@coderhukai I developed the Focus() module myself for YOLOv5. For details see the Focus() layer discussion #3181

I've received lots of interest in the YOLOv5 🚀 Focus layer, so I've written a short documentation here on it. I created the Focus layer myself when evolving the YOLOv3 architecture into YOLOv5, and did not adopt it from other sources. The main purpose of the Focus layer is to reduce layers, reduce parameters, reduce FLOPS, reduce CUDA memory, increase forward and backward speed while minimally impacting mAP.

yolov5/models/common.py

Lines 163 to 171 in 17b0f71

class Focus(nn.Module):
# Focus wh information into c-space
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
super(Focus, self).__init__()
self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
# self.contract = Contract(gain=2)
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))

The YOLOv5 Focus layer replaces the first 3 YOLOv3 layers with a single layer:
Focus Layer

I settled on the current Focus layer design after a significant effort profiling alternative designs to the YOLOv3 input layers, both for immediate forward/backward/memory profiling results, and also comparing full 300 epoch COCO trainings to determine the effect on mAP. You can profile the Focus layer very easily against the YOLOv3 original layers replaced using the YOLOv5 profile() function:

import torch.nn as nn
from models.common import Focus, Conv, Bottleneck
from utils.torch_utils import profile 

m1 = Focus(3, 64, 3)  # YOLOv5 single layer
m2 = nn.Sequential(Conv(3, 32, 3, 1), Conv(32, 64, 3, 2), Bottleneck(64, 64))  # YOLOv3 equivalent layers

profile(x=torch.randn(16, 3, 640, 640), ops=[m1, m2], n=100)  # profile both 100 times at batch-size 16

In our YOLOv5 Google Colab notebook with a V100 GPU I get the following result:

1.8.1+cu101 cuda _CudaDeviceProperties(name='Tesla V100-SXM2-16GB', major=7, minor=0, total_memory=16160MB, multi_processor_count=80)

      Params      GFLOPS    forward (ms)   backward (ms)                   input                  output
        7040       23.07           4.958           12.76       (16, 3, 640, 640)      (16, 64, 320, 320)  # Focus()
       40160       140.7           26.15             125       (16, 3, 640, 640)      (16, 64, 320, 320)  # YOLOv3 layers

@HuKai97
Copy link
Author

HuKai97 commented Jul 1, 2021

Thank you! 😃

@HuKai97 HuKai97 closed this as completed Jul 1, 2021
@GitHubChrischen
Copy link

does anyone tried 5x5 with stride= 2 convolution layer ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants