This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Open source vision transformers (#646)
Summary: Pull Request resolved: #646 Open source Vision Transformers from https://arxiv.org/abs/2010.11929 Reviewed By: vreis Differential Revision: D24840754 (7c58f6d) fbshipit-source-id: 77148f3beb54afe8234b9a4d5c0a190ddfb76136
- Loading branch information
1 parent
49a3762
commit 07d4ffa
Showing
6 changed files
with
615 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
""" | ||
Vision Transformer head implementation from https://arxiv.org/abs/2010.11929. | ||
References: | ||
https://github.com/google-research/vision_transformer | ||
https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py | ||
""" | ||
|
||
import copy | ||
from collections import OrderedDict | ||
|
||
import torch.nn as nn | ||
from classy_vision.heads import ClassyHead, register_head | ||
|
||
from ..models.lecun_normal_init import lecun_normal_init | ||
|
||
|
||
@register_head("vision_transformer_head") | ||
class VisionTransformerHead(ClassyHead): | ||
def __init__( | ||
self, | ||
in_plane, | ||
num_classes, | ||
hidden_dim=None, | ||
): | ||
super().__init__() | ||
if hidden_dim is None: | ||
layers = [("head", nn.Linear(in_plane, num_classes))] | ||
else: | ||
layers = [ | ||
("pre_logits", nn.Linear(in_plane, hidden_dim)), | ||
("act", nn.Tanh()), | ||
("head", nn.Linear(hidden_dim, num_classes)), | ||
] | ||
self.layers = nn.Sequential(OrderedDict(layers)) | ||
self.init_weights() | ||
|
||
def init_weights(self): | ||
if hasattr(self.layers, "pre_logits"): | ||
lecun_normal_init( | ||
self.layers.pre_logits.weight, fan_in=self.layers.pre_logits.in_features | ||
) | ||
nn.init.zeros_(self.layers.pre_logits.bias) | ||
nn.init.zeros_(self.layers.head.weight) | ||
nn.init.zeros_(self.layers.head.bias) | ||
|
||
@classmethod | ||
def from_config(cls, config): | ||
config = copy.deepcopy(config) | ||
config.pop("unique_id") | ||
return cls(**config) | ||
|
||
def forward(self, x): | ||
return self.layers(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import torch.nn as nn | ||
import math | ||
|
||
|
||
def lecun_normal_init(tensor, fan_in): | ||
nn.init.trunc_normal_(tensor, std=math.sqrt(1 / fan_in)) |
Oops, something went wrong.