-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from daniel-code/feat/pytorch_models
Feat/pytorch models
- Loading branch information
Showing
9 changed files
with
61 additions
and
77 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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from .resnet import ResNet | ||
from .resnext import ResNext | ||
from .swin import Swin | ||
from .base import ModelBase | ||
from .torch_model_wrapper import is_torch_builtin_models, TorchModelWrapper | ||
|
||
__all__ = ['ResNet', 'Swin', 'ResNext'] | ||
__all__ = ['ModelBase', 'TorchModelWrapper', 'is_torch_builtin_models'] |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
import types | ||
|
||
from .base import ModelBase | ||
from torch.nn import Module, Sequential, Linear | ||
|
||
|
||
def is_torch_builtin_models(model_type: str): | ||
model_type = model_type.lower() | ||
model_module = __import__('torchvision.models', fromlist=(model_type, ), level=0) | ||
if model_type in dir(model_module): | ||
model_func = getattr(model_module, model_type) | ||
return True if isinstance(model_func, types.FunctionType) else False | ||
|
||
return False | ||
|
||
|
||
class TorchModelWrapper(ModelBase): | ||
def _setup_model(self, model_type) -> Module: | ||
# dynamic import module | ||
model_module = __import__('torchvision.models', fromlist=(model_type, ), level=0) | ||
model_func = getattr(model_module, model_type) | ||
model = model_func(weights='DEFAULT' if self.user_pretrained_weight else None) | ||
|
||
# get last layer's name | ||
layer_name = list(model.named_children())[-1][0] | ||
|
||
# check last layer | ||
last_layer = model.get_submodule(layer_name) | ||
if isinstance(last_layer, Sequential): | ||
last_layer = last_layer[-1] | ||
in_features = last_layer.in_features | ||
|
||
# replace the last layer | ||
last_layer = getattr(model, layer_name) | ||
if isinstance(last_layer, Sequential): | ||
last_layer[-1] = Linear(in_features=in_features, out_features=self.num_classes) | ||
else: | ||
setattr(model, layer_name, Linear(in_features=in_features, out_features=self.num_classes)) | ||
|
||
return model |
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