File size: 2,607 Bytes
9d17be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db7d3b8
9d17be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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.
    """