MamaPearl commited on
Commit
f5cf82d
·
verified ·
1 Parent(s): 1a91252

Update config.py

Browse files
Files changed (1) hide show
  1. config.py +67 -0
config.py CHANGED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedConfig
2
+
3
+ class NULAConfig(PreTrainedConfig):
4
+ model_type="nula"
5
+ def __init__(
6
+ self, num_classes=10, in_channels=3, input_size=(3, 32, 32), block_channels=(128, 256, 512),
7
+ use_residual=True, use_se=True, use_spatial_attention=False, se_reduction=16, classifier_hidden_dim=256,
8
+ mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), id2label=None, label2id=None, **kwargs
9
+ ):
10
+ super().__init__(**kwargs)
11
+
12
+ self.num_classes = num_classes
13
+ self.in_channels = in_channels
14
+ self.input_size = input_size
15
+ self.block_channels = list(block_channels)
16
+
17
+ self.use_residual = use_residual
18
+ self.use_se = use_se
19
+ self.use_spatial_attention = use_spatial_attention
20
+ self.se_reduction = se_reduction
21
+
22
+ self.classifier_hidden_dim = classifier_hidden_dim
23
+ self.mean = list(mean)
24
+ self.std = list(std)
25
+
26
+ if id2label is None or label2id is None:
27
+ labels = [
28
+ "airplane", "automobile", "bird", "cat", "deer",
29
+ "dog", "frog", "horse", "ship", "truck"
30
+ ]
31
+ self.id2label = {i: label for i, label in enumerate(labels)}
32
+ self.label2id = {label: i for i, label in enumerate(labels)}
33
+ else:
34
+ self.id2label = id2label
35
+ self.label2id = label2id
36
+
37
+ self._val_invars()
38
+
39
+ def _val_invars(self):
40
+ if self.num_classes <= 0:
41
+ raise ValueError(f"num_classes must be positive, got {self.num_classes}")
42
+ if self.classifier_hidden_dim <= 0:
43
+ raise ValueError(
44
+ f"classifier_hidden_dim must be positive, got {self.classifier_hidden_dim}"
45
+ )
46
+ if len(self.input_size) != 3:
47
+ raise ValueError(f"input_size must be (C, H, W), got {self.input_size}")
48
+
49
+ C, H, W = self.input_size
50
+ if C != self.in_channels:
51
+ raise ValueError(f"channel mismatch: input_size[0]={C}, in_channels={self.in_channels}")
52
+ if H <= 0 or W <= 0:
53
+ raise ValueError(f"spatial dimensions must be positive, got H={H}, W={W}")
54
+
55
+ if len(self.block_channels) == 0:
56
+ raise ValueError("the model needs at least one layer!")
57
+ if any(c <= 0 for c in self.block_channels):
58
+ raise ValueError(f"invalid block_channels: {self.block_channels}")
59
+ if self.use_se and self.se_reduction <= 0:
60
+ raise ValueError(f"se_reduction must be positive, got {self.se_reduction}")
61
+ if len(self.mean) != self.in_channels or len(self.std) != self.in_channels:
62
+ raise ValueError("mean/std length must match in_channels")
63
+
64
+ if self.num_classes != len(self.id2label):
65
+ raise ValueError(f"num_classes ({self.num_classes}) != label count ({len(self.id2label)})")
66
+ if set(self.label2id.values()) != set(range(self.num_classes)):
67
+ raise ValueError("label2id values must cover [0, ..., num_classes-1]")