Erebustsai
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    > 1. https://medium.com/ai-blog-tw/efficientnet-v2%E7%9A%84%E8%83%8C%E5%BE%8C-%E9%87%8B%E6%94%BEmobilenet%E5%9C%A8gpu-tpu%E4%B8%8A%E7%9A%84%E6%95%88%E7%8E%87-f19abde55b05 > 2. https://www.zhihu.com/question/343343895 > 3. https://medium.com/ai-academy-taiwan/efficient-cnn-%E4%BB%8B%E7%B4%B9-%E4%B8%80-mobilenetv1-304c96f5eb7e > 4. https://medium.com/ai-academy-taiwan/efficient-cnn-%E4%BB%8B%E7%B4%B9-%E4%BA%8C-mobilenetv2-7809721f0bc8 > 5. https://www.youtube.com/watch?v=_OZsGQHB41s > 6. https://www.youtube.com/watch?v=fR_0o25kigM&list=PLhhyoLH6IjfxeoooqP9rhU3HJIAVAJ3Vz&index=20 ## MobileNet V1 ### Depth-wise Separable Convolution Traditional convolution operations consider the correlation of both space and channel in the kernel. ![](https://hackmd.io/_uploads/SkvIYjsc3.png) :::info :bulb: **The Amount of Computation** *Ksize * Ksize * out_featureH * out_featureW (One element in output feature need Ksize * Ksize computation) * in_channel * out_channel* ::: Separable convolutions use **depth-wise convolution** to fetch correlated **spatial information** and use pointwise convolution to fetch **channel-related information**. ![](https://hackmd.io/_uploads/BJc39siqn.png) :::info :bulb: **The Amount of Computation** *Ksize * Ksize * in_channel * out_featureH * out_featureW + in_channel * out_channel * out_featureH * out_featureW* ::: ## MobileNet V2 ### Linear Bottlenecks There is redundant or not important information in feature maps output by the model’s layers. To trim out unnecessary information and keep the model accuracy, we can reduce the channel size of each layer so that it can fit in all the important information. However, if ReLU is applied, some information (value less than 0) in the already reduced feature map will be loss. We can conclude that ReLU can be applied when the models have enough redundancy to keep important information. All in all, a linear bottleneck produces a simplified feature while keeping all the important information ![](https://hackmd.io/_uploads/rkJtjoo92.png) **(b):** Depth-wise -> Point-wise (Channel increased) **\(c\) and (d):** Depth-wise -> ReLU -> Point-wise (Channel reduced and Produces the linear bottleneck) -> Point-wise -> Relu -> Depth-wise (repeated) :::success ***TLDR: Reduce channel size, don’t use ReLU*** ::: ### Inverted Residual Block ![](https://hackmd.io/_uploads/BJJbnss53.png) The biggest difference between the residual block and the inverted residual block is that the connection happened in the linear bottleneck layer. Since the linear bottleneck should contend all the important information, the connection only connects important information and the expansion layer only for reducing the impact of ReLU. :::info :bulb: **Implementation Notes:** 1. Residual connection only happened when **input_channel == output_channel** and **stride == 1**. (*Intuition: Only do connections when the sizes of the feature maps are equal otherwise is not possible*) 3. Use ReLU6 to cap the maximum feature map value to 6.0. 4. **Depth-wise layers** will not change the channel size but it can change the feature map size. 5. **Point-wise layers** will not change the feature map size but it can change the channel size. ::: ### Code Example: (OpenCL) ```cpp // 2 model.add(new ocl::pwconv2d(model.back(), 96, false)); //9 model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::relu6(model.back(), true)); model.add(new ocl::dwconv2d(model.back(), 3, 2, 1, false)); model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::relu6(model.back(), true)); model.add(new ocl::pwconv2d(model.back(), 24, false)); model.add(new ocl::batchNorm(model.back(), 1e-5, true)); // 3 model.add(new ocl::pwconv2d(model.back(), 144, false)); //17 model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::relu6(model.back(), true)); model.add(new ocl::dwconv2d(model.back(), 3, 1, 1, false)); model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::relu6(model.back(), true)); model.add(new ocl::pwconv2d(model.back(), 24, false)); model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::add(model.back(), model[16], true)); //The third block has a connection because the input channel size is 24 and equal to the output channel size // 4 model.add(new ocl::pwconv2d(model.back(), 144, false)); //26 model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::relu6(model.back(), true)); model.add(new ocl::dwconv2d(model.back(), 3, 2, 1, false)); model.add(new ocl::batchNorm(model.back(), 1e-5, true)); model.add(new ocl::relu6(model.back(), true)); model.add(new ocl::pwconv2d(model.back(), 32, false)); model.add(new ocl::batchNorm(model.back(), 1e-5, true)); ``` ### Possible Pitfall: MobileNet reduces the amount of computation needed compared to the normal convolutional layer. However, the following diagram shows that the inference time of the mobilenet is comparably more than other models. > ![](https://hackmd.io/_uploads/H1QEAio5h.png) > https://www.zhihu.com/question/343343895 We can assume that GPU is able to run models’ layers in parallel completely. The more layers in a model, the more memory transfer GPU is going to take. In mobilenet, we slice one single convolution layer into two depth-wise and point-wise convolution layers so the number of layers is increased. The layer computation is depended on its previous one so it’s required to finish computing the previous layer in order to do the next. Therefore, as mobilenet increase the number of layers in the model, memory transfer is also increased thus causing the entire computation to be memory bound. On the other hand, CPU is only bounded by the amount of computation needed and can benefit from the separable convolution. ## EfficientNet V1 Provide a constant ratio to scale up the convolutional neural network. The computational resources needed will be increased following this ratio. Therefore, the total computational cost can be estimated and modified according to the hardware we have or according to the performance that we want. :::info :information_source: **There are three independent ways to scale up a convolution model** 1. Increase the input image resolution. 2. Increase the model’s width (channels size) 3. Increase the model’s depth (number of layers) :bulb: **The compound scaling method** **depth:** 𝑑 = 𝛼𝜑 **width:** 𝑤 = 𝛽𝜑 **resolution:** 𝑟 = 𝛾𝜑 𝛼 = 1.2, 𝛽 = 1.1, 𝛾 = 1.15 ::: :::success ***TLDR: A systematic way to scale up the convolutional neural network.*** ::: ### Use of inverted residual block (mobile inverted bottleneck MBConv), Squeeze & excitation The grid search is applied to find the constant values and coefficients. ![](https://hackmd.io/_uploads/BktjJ3o9n.png) The following python list and dictionary shows the model architecture configuration. ```python # expand_ratio, out_channels, repeats, stride, kernel_size base_model = [ [1, 16, 1, 1, 3], [6, 24, 2, 2, 3], [6, 40, 2, 2, 5], [6, 80, 3, 2, 3], [6, 112, 3, 1, 5], [6, 192, 4, 2, 5], [6, 320, 1, 1, 3], ] # phi_value, resolution, drop_rate phi_values = { "b0": (0, 224, 0.2), "b1": (0.5, 240, 0.2), "b2": (1, 260, 0.3), "b3": (2, 300, 0.3), "b4": (3, 380, 0.4), "b5": (4, 456, 0.4), "b6": (5, 528, 0.5), "b7": (6, 600, 0.5), } ``` ### Squeeze & excitation (channel attention) > https://towardsdatascience.com/squeeze-and-excitation-networks-9ef5e71eacd7 ![](https://hackmd.io/_uploads/BJbQ-2j5h.png) Average pooling is applied to each channel and we have a vector with a size of C. This value will be passed through some 1 * 1 convolution and a sigmoid layer to a set of numbers with value 0 ~ 1. This value represents the percentage for a certain channel and can be considered as the amount of attention needed for a certain channel. ```python # Compute the attention score for each channels class SqueezeExcitation(nn.Module): def __init__(self, in_channels, reduced_dim): super(SqueezeExcitation, self).__init__() self.se = nn.Sequential( nn.AdaptiveAvgPool2d(1), # C x H x W -> C x 1 x 1 nn.Conv2d(in_channels, reduced_dim, 1), nn.SiLU(), nn.Conv2d(reduced_dim, in_channels, 1), nn.Sigmoid(), # output will be [0, 1] ) def forward(self, x): return x * self.se(x) ``` ### Stochastic Depth Similar to dropout but dropping layers. ![](https://hackmd.io/_uploads/ByzoZ3j53.png) This force the model to learn more generally. However, this action will only be applied when the model is under training. ### Inverted Residual Block Implementation notes ```cpp (features): Sequential( (0): CNNBlock( (cnn): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (silu): SiLU() ) (1): InvertedResidualBlock( (conv): Sequential( (0): CNNBlock( (cnn): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32, bias=False) (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (silu): SiLU() ) (1): SqueezeExcitation( (se): Sequential( (0): AdaptiveAvgPool2d(output_size=1) (1): Conv2d(32, 8, kernel_size=(1, 1), stride=(1, 1)) (2): SiLU() (3): Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1)) (4): Sigmoid() ) ) (2): Conv2d(32, 16, kernel_size=(1, 1), stride=(1, 1), bias=False) (3): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) ... ) ``` The above print output of the efficientnetv1 shows that the first inverted residual block will not expand and maintain a 32 in channel size to a 32 out channel size in the first depth-wise convolution. In the end of this inverted residual block, the channel size is reduced by a point-wise convolution to create a linear bottleneck. ## EfficientnetV2 EfficientnetV1 is not suitable to run on GPU and most of the performance gain can only be noticed when running on the sequential processor. The following bullet points shows the modification of EfficientnetV2 to the original EfficientnetV1. 1. Replace depth-wise separable convolution. 2. Neural Architecture Search is applied to find architecture suitable for GPU. 3. Use non-uniformed scaling which means each stage has different scaling factors. 4. Gradually increase image resolution during training and provider higher regularization. ### Replace depth-wise separable convolution with fused version The following MBConv is same as a bottleneck blocks in mobilenetv2 ![](https://hackmd.io/_uploads/Bkg5mhj9n.png =500x) ### Model Architecture ![](https://hackmd.io/_uploads/B1cqX2oc2.png) :::info :bulb: **Implementation Notes** 1. ***Fused-MBConv*** is just **replace the separable depth-wise convolution with traditional convolution layer**. 2. Squeeze and excitation layer is only applied on MBConv series of layers as the block used in MobilenetV3 3. The first Fused-MBConv does not expand on the first convolution layer since the expand factor is 1. 4. The batch normalization **use 1e-3 for avoid zero division instead of 1e-5** which is default for Pytorch. ::: ### Fused-MBConv building block ```cpp (0): FusedMBConv( (block): Sequential( (0): Conv2dNormActivation( (0): Conv2d(24, 96, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) (1): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True) (2): SiLU(inplace=True) ) (1): Conv2dNormActivation( (0): Conv2d(96, 48, kernel_size=(1, 1), stride=(1, 1), bias=False) (1): BatchNorm2d(48, eps=0.001, momentum=0.1, affine=True, track_running_stats=True) ) ) (stochastic_depth): StochasticDepth(p=0.01, mode=row) ) ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully