-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Update TNT-(S/B) model weights and add feature extraction support #2480
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
base: main
Are you sure you want to change the base?
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
if intermediates_only: | ||
return intermediates | ||
|
||
patch_embed = self.norm(patch_embed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rwightman,
I'm not sure if my understanding is correct — but I think we may need a last_idx
check in forward_intermediates()
to align with the behavior in forward_features()
.
In particular, if the final output should go through the norm
layer, perhaps it should be handled like this:
- FastViT
pytorch-image-models/timm/models/fastvit.py
Lines 1293 to 1294 in c8c4f25
if feat_idx == last_idx: | |
x = self.final_conv(x) |
- EfficientFormer
pytorch-image-models/timm/models/efficientformer.py
Lines 492 to 513 in c8c4f25
last_idx = self.num_stages - 1 | |
if torch.jit.is_scripting() or not stop_early: # can't slice blocks in torchscript | |
stages = self.stages | |
else: | |
stages = self.stages[:max_index + 1] | |
feat_idx = 0 | |
for feat_idx, stage in enumerate(stages): | |
x = stage(x) | |
if feat_idx < last_idx: | |
B, C, H, W = x.shape | |
if feat_idx in take_indices: | |
if feat_idx == last_idx: | |
x_inter = self.norm(x) if norm else x | |
intermediates.append(x_inter.reshape(B, H // 2, W // 2, -1).permute(0, 3, 1, 2)) | |
else: | |
intermediates.append(x) | |
if intermediates_only: | |
return intermediates | |
if feat_idx == last_idx: | |
x = self.norm(x) |
Otherwise, in cases where
stop_early=True
or feat_idx != last_idx
, we would skip the norm
layer which I assume is the intended behavior for intermediate outputs.
last_idx = len(self.blocks) - 1
for i, blk in enumerate(blocks):
pixel_embed, patch_embed = blk(pixel_embed, patch_embed)
if i in take_indices:
# normalize intermediates with final norm layer if enabled
intermediates.append(self.norm(patch_embed) if norm and i == last_idx else patch_embed)
if intermediates_only:
return intermediates
if i == last_idx:
patch_embed = self.norm(patch_embed)
return patch_embed, intermediates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brianhou0208 yeah, there are some compromises for models that aren't 'uniform' like ViT where it only makes sense for a final norm/conv layer to apply to the very last output and would break if you tried to force it on intermediate output. That's why some of the modles have those checks on last_idx to ensure if the stop early is used or the model was truncated that those last layers aren't applied.
For vit models, applying a final norm or projection to a block output that isn't the last actually works reasonably and gives you useable results (accuracy falls off obviously as you prune further back), so if the sizing matches that's why the final norm is applied when possible.
Other sanity check, with
feat = model.forward_features(inpt)
feat2, intermediates = model.forward_intermediates(inpt, intermediates_only=False)
torch.allclose(feat, feat2)
should hold
and either one can be passed through classifier_out = model.forward_head(feat)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should probably add that check to the forward_intermediates unit test...
The main updates are as follows:
Block
&PixelEmbed
), and thelegacy
parameter is used to maintain backward compatibility.features_only
parameters andforward_intermediates
functionExample
Example(forward_intermediates)
Example(features_only)
Result
test code
Reference
official PyTorch implement: https://github.com/huawei-noah/Efficient-AI-Backbones/tree/master/tnt_pytorch