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

New TensorFlow TFDWConv() module #7824

Merged
merged 4 commits into from
May 15, 2022
Merged

New TensorFlow TFDWConv() module #7824

merged 4 commits into from
May 15, 2022

Conversation

glenn-jocher
Copy link
Member

@glenn-jocher glenn-jocher commented May 15, 2022

Analog to DWConv() module

yolov5/models/common.py

Lines 53 to 57 in 8aa2085

class DWConv(Conv):
# Depth-wise convolution class
def __init__(self, c1, c2, k=1, s=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), act=act)

πŸ› οΈ PR Summary

Made with ❀️ by Ultralytics Actions

🌟 Summary

Enhanced TensorFlow compatibility for YOLOv5 layers.

πŸ“Š Key Changes

  • Refined the Conv and DepthwiseConv2D layer parameter definitions for clarity and consistency.
  • Added an activations function to map PyTorch activation functions to their TensorFlow equivalents.
  • Reshaped the pixel shuffle operation to be more concise.
  • Included explicit handling of the activation functions for depthwise convolutions and regular convolutions.

🎯 Purpose & Impact

  • The changes aim to make the codebase more maintainable by improving readability and structure.
  • Users can expect more reliable cross-framework model behavior, with YOLOv5's TensorFlow version functioning closer to its PyTorch counterpart.
  • The update ensures activation functions are properly translated between PyTorch and TensorFlow, potentially leading to performance improvements and fewer discrepancies. πŸš€

@glenn-jocher glenn-jocher self-assigned this May 15, 2022
@glenn-jocher
Copy link
Member Author

@zldrobit this is my TF DWConv() implementation. Seems to work correctly. Tested with https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s-dw.pt that has one layer converted to DWConv.

@glenn-jocher glenn-jocher merged commit 4d59f65 into master May 15, 2022
@glenn-jocher glenn-jocher deleted the add/tfdwconv branch May 15, 2022 23:04
@glenn-jocher
Copy link
Member Author

glenn-jocher commented May 15, 2022

@zldrobit I noticed that the TF Conv2d docs show a groups argument. Do you know if TF 2.8 now supports this argument? It looks like maybe still not supported in https://stackoverflow.com/questions/66857534/group-conv-in-tensorflow-2

@glenn-jocher
Copy link
Member Author

glenn-jocher commented May 16, 2022

@zldrobit tests show a problem with TFDWConv(). To reproduce replace a few Conv() layers with DWConv() layers in yolov5s.yaml, i.e.:

# Parameters
nc: 80  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # layer channel multiple
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

# YOLOv5 v6.0 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Conv, [64, 6, 2, 2]],  # 0-P1/2
   [-1, 1, Conv, [128, 3, 2]],  # 1-P2/4
   [-1, 3, C3, [128]],
   [-1, 1, Conv, [256, 3, 2]],  # 3-P3/8
   [-1, 6, C3, [256]],
   [-1, 1, Conv, [512, 3, 2]],  # 5-P4/16
   [-1, 9, C3, [512]],
   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32
   [-1, 3, C3, [1024]],
   [-1, 1, SPPF, [1024, 5]],  # 9
  ]

# YOLOv5 v6.0 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4
   [-1, 3, C3, [512, False]],  # 13

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3
   [-1, 3, C3, [256, False]],  # 17 (P3/8-small)

   [-1, 1, DWConv, [256, 5, 2]],
   [[-1, 14], 1, Concat, [1]],  # cat head P4
   [-1, 3, C3, [512, False]],  # 20 (P4/16-medium)

   [-1, 1, DWConv, [512, 5, 2]],
   [[-1, 10], 1, Concat, [1]],  # cat head P5
   [-1, 3, C3, [1024, False]],  # 23 (P5/32-large)

   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]

and then train a model and observe lower val mAP on TF exports. No errors. Not sure where the problem lies. EDIT: fixed in #7845

!python train.py --weights yolov5s.pt --cfg yolov5s.yaml --epochs 300

!python export.py --weights runs/train/exp/weights/best.pt --include saved_model
!python val.py --weights runs/train/exp/weights/best.pt
!python val.py --weights runs/train/exp/weights/best_saved_model

@glenn-jocher
Copy link
Member Author

@zldrobit issue resolved in #7845

All new TF modules are validated! :)

tdhooghe pushed a commit to tdhooghe/yolov5 that referenced this pull request Jun 10, 2022
* New TensorFlow `TFDWConv()` module

Analog to DWConv() module:
https://github.com/ultralytics/yolov5/blob/8aa2085a7e7ae20a17a7548edefbdb2960f2b29b/models/common.py#L53-L57

* Fix and new activations() function

* Update tf.py
ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this pull request Sep 8, 2022
* New TensorFlow `TFDWConv()` module

Analog to DWConv() module:
https://github.com/ultralytics/yolov5/blob/8aa2085a7e7ae20a17a7548edefbdb2960f2b29b/models/common.py#L53-L57

* Fix and new activations() function

* Update tf.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant