Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 5643849

Browse files
mannatsinghfacebook-github-bot
authored andcommitted
Add support for profiling EfficientNet (#483)
Summary: Pull Request resolved: #483 - Add `flops` and `activations` to `Conv2dSamePadding` - The EfficientNet forward squeezed the final output - this works fine for batches with > 1 elements, but for unit batches, it removes the batch dimension as well. This resulted in incorrect profiler behavior and has been fixed. Reviewed By: vreis Differential Revision: D21094291 fbshipit-source-id: e76a7c5398c521c11e00bca4d428138430bc7344
1 parent c2b3ec7 commit 5643849

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

classy_vision/models/efficientnet.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def __init__(
143143
pad_w = max(
144144
(out_w - 1) * self.stride[1] + (kernel_w - 1) * dilation_w + 1 - image_w, 0
145145
)
146+
self.out_h = out_h
147+
self.out_w = out_w
146148
self.same_padding = None
147149
if pad_h > 0 or pad_w > 0:
148150
self.same_padding = nn.ZeroPad2d(
@@ -168,6 +170,22 @@ def forward(self, x):
168170
)
169171
return x
170172

173+
def flops(self, x):
174+
batchsize_per_replica = x.size()[0]
175+
return (
176+
batchsize_per_replica
177+
* self.in_channels
178+
* self.out_channels
179+
* self.kernel_size[0]
180+
* self.kernel_size[1]
181+
* self.out_h
182+
* self.out_w
183+
/ self.groups
184+
)
185+
186+
def activations(self, x, out):
187+
return out.numel()
188+
171189

172190
class MBConvBlock(nn.Module):
173191
"""
@@ -527,7 +545,7 @@ def forward(self, inputs):
527545
outputs = self.trunk_output(outputs)
528546

529547
# Average Pooling
530-
outputs = self.avg_pooling(outputs).squeeze()
548+
outputs = self.avg_pooling(outputs).view(outputs.size(0), -1)
531549

532550
# Dropout
533551
if self.dropout is not None:

0 commit comments

Comments
 (0)