-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Architecture): added ConvNeXt - closes #101
- Loading branch information
Showing
4 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
aucmedi/neural_network/architectures/image/convnext_base.py
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,85 @@ | ||
#==============================================================================# | ||
# Author: Dominik Müller # | ||
# Copyright: 2022 IT-Infrastructure for Translational Medical Research, # | ||
# University of Augsburg # | ||
# # | ||
# This program is free software: you can redistribute it and/or modify # | ||
# it under the terms of the GNU General Public License as published by # | ||
# the Free Software Foundation, either version 3 of the License, or # | ||
# (at your option) any later version. # | ||
# # | ||
# This program is distributed in the hope that it will be useful, # | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
# GNU General Public License for more details. # | ||
# # | ||
# You should have received a copy of the GNU General Public License # | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. # | ||
#==============================================================================# | ||
#-----------------------------------------------------# | ||
# Documentation # | ||
#-----------------------------------------------------# | ||
""" The classification variant of the ConvNeXt Base architecture. | ||
| Architecture Variable | Value | | ||
| ------------------------ | -------------------------- | | ||
| Key in architecture_dict | "2D.ConvNeXtBase" | | ||
| Input_shape | (224, 224) | | ||
| Standardization | None | | ||
Recommended alternative `Input_shape` is 384x384 pixels. | ||
!!! warning | ||
ConvNeXt models expect their inputs to be float or uint8 tensors of pixels with values in the [0-255] range. | ||
Standardization is applied inside the architecture. | ||
???+ abstract "Reference - Implementation" | ||
[https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext](https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext) <br> | ||
???+ abstract "Reference - Publication" | ||
Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. | ||
10 Jan 2022. A ConvNet for the 2020s. | ||
<br> | ||
[https://arxiv.org/abs/2201.03545](https://arxiv.org/abs/2201.03545) | ||
""" | ||
#-----------------------------------------------------# | ||
# Library imports # | ||
#-----------------------------------------------------# | ||
# External libraries | ||
from tensorflow.keras.applications.convnext import ConvNeXtBase as BaseModel | ||
# Internal libraries | ||
from aucmedi.neural_network.architectures import Architecture_Base | ||
|
||
#-----------------------------------------------------# | ||
# Architecture class: ConvNeXtBase # | ||
#-----------------------------------------------------# | ||
class ConvNeXtBase(Architecture_Base): | ||
#---------------------------------------------# | ||
# Initialization # | ||
#---------------------------------------------# | ||
def __init__(self, classification_head, channels, input_shape=(224, 224), | ||
pretrained_weights=False): | ||
self.classifier = classification_head | ||
self.input = input_shape + (channels,) | ||
self.pretrained_weights = pretrained_weights | ||
|
||
#---------------------------------------------# | ||
# Create Model # | ||
#---------------------------------------------# | ||
def create_model(self): | ||
# Get pretrained image weights from imagenet if desired | ||
if self.pretrained_weights : model_weights = "imagenet" | ||
else : model_weights = None | ||
|
||
# Obtain ResNet50 as base model | ||
base_model = BaseModel(include_top=False, weights=model_weights, | ||
input_tensor=None, input_shape=self.input, | ||
pooling=None) | ||
top_model = base_model.output | ||
|
||
# Add classification head | ||
model = self.classifier.build(model_input=base_model.input, | ||
model_output=top_model) | ||
|
||
# Return created model | ||
return model |
85 changes: 85 additions & 0 deletions
85
aucmedi/neural_network/architectures/image/convnext_large.py
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,85 @@ | ||
#==============================================================================# | ||
# Author: Dominik Müller # | ||
# Copyright: 2022 IT-Infrastructure for Translational Medical Research, # | ||
# University of Augsburg # | ||
# # | ||
# This program is free software: you can redistribute it and/or modify # | ||
# it under the terms of the GNU General Public License as published by # | ||
# the Free Software Foundation, either version 3 of the License, or # | ||
# (at your option) any later version. # | ||
# # | ||
# This program is distributed in the hope that it will be useful, # | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
# GNU General Public License for more details. # | ||
# # | ||
# You should have received a copy of the GNU General Public License # | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. # | ||
#==============================================================================# | ||
#-----------------------------------------------------# | ||
# Documentation # | ||
#-----------------------------------------------------# | ||
""" The classification variant of the ConvNeXt Large architecture. | ||
| Architecture Variable | Value | | ||
| ------------------------ | -------------------------- | | ||
| Key in architecture_dict | "2D.ConvNeXtLarge" | | ||
| Input_shape | (384x384) | | ||
| Standardization | None | | ||
Recommended alternative `Input_shape` is 224x224 pixels. | ||
!!! warning | ||
ConvNeXt models expect their inputs to be float or uint8 tensors of pixels with values in the [0-255] range. | ||
Standardization is applied inside the architecture. | ||
???+ abstract "Reference - Implementation" | ||
[https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext](https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext) <br> | ||
???+ abstract "Reference - Publication" | ||
Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. | ||
10 Jan 2022. A ConvNet for the 2020s. | ||
<br> | ||
[https://arxiv.org/abs/2201.03545](https://arxiv.org/abs/2201.03545) | ||
""" | ||
#-----------------------------------------------------# | ||
# Library imports # | ||
#-----------------------------------------------------# | ||
# External libraries | ||
from tensorflow.keras.applications.convnext import ConvNeXtLarge as BaseModel | ||
# Internal libraries | ||
from aucmedi.neural_network.architectures import Architecture_Base | ||
|
||
#-----------------------------------------------------# | ||
# Architecture class: ConvNeXtLarge # | ||
#-----------------------------------------------------# | ||
class ConvNeXtLarge(Architecture_Base): | ||
#---------------------------------------------# | ||
# Initialization # | ||
#---------------------------------------------# | ||
def __init__(self, classification_head, channels, input_shape=(224, 224), | ||
pretrained_weights=False): | ||
self.classifier = classification_head | ||
self.input = input_shape + (channels,) | ||
self.pretrained_weights = pretrained_weights | ||
|
||
#---------------------------------------------# | ||
# Create Model # | ||
#---------------------------------------------# | ||
def create_model(self): | ||
# Get pretrained image weights from imagenet if desired | ||
if self.pretrained_weights : model_weights = "imagenet" | ||
else : model_weights = None | ||
|
||
# Obtain ResNet50 as base model | ||
base_model = BaseModel(include_top=False, weights=model_weights, | ||
input_tensor=None, input_shape=self.input, | ||
pooling=None) | ||
top_model = base_model.output | ||
|
||
# Add classification head | ||
model = self.classifier.build(model_input=base_model.input, | ||
model_output=top_model) | ||
|
||
# Return created model | ||
return model |
83 changes: 83 additions & 0 deletions
83
aucmedi/neural_network/architectures/image/convnext_small.py
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,83 @@ | ||
#==============================================================================# | ||
# Author: Dominik Müller # | ||
# Copyright: 2022 IT-Infrastructure for Translational Medical Research, # | ||
# University of Augsburg # | ||
# # | ||
# This program is free software: you can redistribute it and/or modify # | ||
# it under the terms of the GNU General Public License as published by # | ||
# the Free Software Foundation, either version 3 of the License, or # | ||
# (at your option) any later version. # | ||
# # | ||
# This program is distributed in the hope that it will be useful, # | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
# GNU General Public License for more details. # | ||
# # | ||
# You should have received a copy of the GNU General Public License # | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. # | ||
#==============================================================================# | ||
#-----------------------------------------------------# | ||
# Documentation # | ||
#-----------------------------------------------------# | ||
""" The classification variant of the ConvNeXt Small architecture. | ||
| Architecture Variable | Value | | ||
| ------------------------ | -------------------------- | | ||
| Key in architecture_dict | "2D.ConvNeXtSmall" | | ||
| Input_shape | (224, 224) | | ||
| Standardization | None | | ||
!!! warning | ||
ConvNeXt models expect their inputs to be float or uint8 tensors of pixels with values in the [0-255] range. | ||
Standardization is applied inside the architecture. | ||
???+ abstract "Reference - Implementation" | ||
[https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext](https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext) <br> | ||
???+ abstract "Reference - Publication" | ||
Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. | ||
10 Jan 2022. A ConvNet for the 2020s. | ||
<br> | ||
[https://arxiv.org/abs/2201.03545](https://arxiv.org/abs/2201.03545) | ||
""" | ||
#-----------------------------------------------------# | ||
# Library imports # | ||
#-----------------------------------------------------# | ||
# External libraries | ||
from tensorflow.keras.applications.convnext import ConvNeXtSmall as BaseModel | ||
# Internal libraries | ||
from aucmedi.neural_network.architectures import Architecture_Base | ||
|
||
#-----------------------------------------------------# | ||
# Architecture class: ConvNeXtSmall # | ||
#-----------------------------------------------------# | ||
class ConvNeXtSmall(Architecture_Base): | ||
#---------------------------------------------# | ||
# Initialization # | ||
#---------------------------------------------# | ||
def __init__(self, classification_head, channels, input_shape=(224, 224), | ||
pretrained_weights=False): | ||
self.classifier = classification_head | ||
self.input = input_shape + (channels,) | ||
self.pretrained_weights = pretrained_weights | ||
|
||
#---------------------------------------------# | ||
# Create Model # | ||
#---------------------------------------------# | ||
def create_model(self): | ||
# Get pretrained image weights from imagenet if desired | ||
if self.pretrained_weights : model_weights = "imagenet" | ||
else : model_weights = None | ||
|
||
# Obtain ResNet50 as base model | ||
base_model = BaseModel(include_top=False, weights=model_weights, | ||
input_tensor=None, input_shape=self.input, | ||
pooling=None) | ||
top_model = base_model.output | ||
|
||
# Add classification head | ||
model = self.classifier.build(model_input=base_model.input, | ||
model_output=top_model) | ||
|
||
# Return created model | ||
return model |
83 changes: 83 additions & 0 deletions
83
aucmedi/neural_network/architectures/image/convnext_tiny.py
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,83 @@ | ||
#==============================================================================# | ||
# Author: Dominik Müller # | ||
# Copyright: 2022 IT-Infrastructure for Translational Medical Research, # | ||
# University of Augsburg # | ||
# # | ||
# This program is free software: you can redistribute it and/or modify # | ||
# it under the terms of the GNU General Public License as published by # | ||
# the Free Software Foundation, either version 3 of the License, or # | ||
# (at your option) any later version. # | ||
# # | ||
# This program is distributed in the hope that it will be useful, # | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
# GNU General Public License for more details. # | ||
# # | ||
# You should have received a copy of the GNU General Public License # | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. # | ||
#==============================================================================# | ||
#-----------------------------------------------------# | ||
# Documentation # | ||
#-----------------------------------------------------# | ||
""" The classification variant of the ConvNeXt Tiny architecture. | ||
| Architecture Variable | Value | | ||
| ------------------------ | -------------------------- | | ||
| Key in architecture_dict | "2D.ConvNeXtTiny" | | ||
| Input_shape | (224, 224) | | ||
| Standardization | None | | ||
!!! warning | ||
ConvNeXt models expect their inputs to be float or uint8 tensors of pixels with values in the [0-255] range. | ||
Standardization is applied inside the architecture. | ||
???+ abstract "Reference - Implementation" | ||
[https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext](https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext) <br> | ||
???+ abstract "Reference - Publication" | ||
Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. | ||
10 Jan 2022. A ConvNet for the 2020s. | ||
<br> | ||
[https://arxiv.org/abs/2201.03545](https://arxiv.org/abs/2201.03545) | ||
""" | ||
#-----------------------------------------------------# | ||
# Library imports # | ||
#-----------------------------------------------------# | ||
# External libraries | ||
from tensorflow.keras.applications.convnext import ConvNeXtTiny as BaseModel | ||
# Internal libraries | ||
from aucmedi.neural_network.architectures import Architecture_Base | ||
|
||
#-----------------------------------------------------# | ||
# Architecture class: ConvNeXtTiny # | ||
#-----------------------------------------------------# | ||
class ConvNeXtTiny(Architecture_Base): | ||
#---------------------------------------------# | ||
# Initialization # | ||
#---------------------------------------------# | ||
def __init__(self, classification_head, channels, input_shape=(224, 224), | ||
pretrained_weights=False): | ||
self.classifier = classification_head | ||
self.input = input_shape + (channels,) | ||
self.pretrained_weights = pretrained_weights | ||
|
||
#---------------------------------------------# | ||
# Create Model # | ||
#---------------------------------------------# | ||
def create_model(self): | ||
# Get pretrained image weights from imagenet if desired | ||
if self.pretrained_weights : model_weights = "imagenet" | ||
else : model_weights = None | ||
|
||
# Obtain ResNet50 as base model | ||
base_model = BaseModel(include_top=False, weights=model_weights, | ||
input_tensor=None, input_shape=self.input, | ||
pooling=None) | ||
top_model = base_model.output | ||
|
||
# Add classification head | ||
model = self.classifier.build(model_input=base_model.input, | ||
model_output=top_model) | ||
|
||
# Return created model | ||
return model |