test / resnet_model /configuration_resnet.py
thinh-researcher's picture
Update
db7d3b8
from typing import List
from transformers import PretrainedConfig
"""
The configuration of a model is an object that
will contain all the necessary information to build the model.
The three important things to remember when writing you own configuration are the following:
- you have to inherit from PretrainedConfig,
- the __init__ of your PretrainedConfig must accept any kwargs,
- those kwargs need to be passed to the superclass __init__.
"""
class ResnetConfig(PretrainedConfig):
"""
Defining a model_type for your configuration (here model_type="resnet") is not mandatory,
unless you want to register your model with the auto classes (see last section)."""
model_type = "rgbdsod-resnet"
def __init__(
self,
block_type="bottleneck",
layers: List[int] = [3, 4, 6, 3],
num_classes: int = 1000,
input_channels: int = 3,
cardinality: int = 1,
base_width: int = 64,
stem_width: int = 64,
stem_type: str = "",
avg_down: bool = False,
**kwargs,
):
if block_type not in ["basic", "bottleneck"]:
raise ValueError(
f"`block_type` must be 'basic' or bottleneck', got {block_type}."
)
if stem_type not in ["", "deep", "deep-tiered"]:
raise ValueError(
f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}."
)
self.block_type = block_type
self.layers = layers
self.num_classes = num_classes
self.input_channels = input_channels
self.cardinality = cardinality
self.base_width = base_width
self.stem_width = stem_width
self.stem_type = stem_type
self.avg_down = avg_down
super().__init__(**kwargs)
if __name__ == "__main__":
"""
With this done, you can easily create and save your configuration like
you would do with any other model config of the library.
Here is how we can create a resnet50d config and save it:
"""
resnet50d_config = ResnetConfig(
block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True
)
resnet50d_config.save_pretrained("custom-resnet")
"""
This will save a file named config.json inside the folder custom-resnet.
You can then reload your config with the from_pretrained method:
"""
resnet50d_config = ResnetConfig.from_pretrained("custom-resnet")
"""
You can also use any other method of the PretrainedConfig class,
like push_to_hub() to directly upload your config to the Hub.
"""