helper2424 commited on
Commit
4f7f30b
·
verified ·
1 Parent(s): 4a8c1df

Upload ResNet10

Browse files
Files changed (3) hide show
  1. config.json +6 -1
  2. model.safetensors +2 -2
  3. modeling_resnet.py +30 -37
config.json CHANGED
@@ -1,6 +1,10 @@
1
  {
 
 
 
2
  "auto_map": {
3
- "AutoConfig": "configuration_resnet.ResNet10Config"
 
4
  },
5
  "depths": [
6
  1,
@@ -8,6 +12,7 @@
8
  1,
9
  1
10
  ],
 
11
  "embedding_size": 64,
12
  "hidden_act": "relu",
13
  "hidden_sizes": [
 
1
  {
2
+ "architectures": [
3
+ "ResNet10"
4
+ ],
5
  "auto_map": {
6
+ "AutoConfig": "configuration_resnet.ResNet10Config",
7
+ "AutoModel": "modeling_resnet.ResNet10"
8
  },
9
  "depths": [
10
  1,
 
12
  1,
13
  1
14
  ],
15
+ "dtype": "float32",
16
  "embedding_size": 64,
17
  "hidden_act": "relu",
18
  "hidden_sizes": [
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:10f7d125770aa256bd45ec9e4f586ca1157e29380fa1306d14a025664ae173d0
3
- size 19626736
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1be729cd30827ec597a7601d7b10ce1d6a50729b79119142761fbb9bba090d5f
3
+ size 19627000
modeling_resnet.py CHANGED
@@ -123,6 +123,21 @@ class Conv2dJax(nn.Module):
123
  return self.conv(x)
124
 
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  class BasicBlock(nn.Module):
127
  def __init__(self, in_channels, out_channels, activation, stride=1, norm_groups=4):
128
  super().__init__()
@@ -134,17 +149,17 @@ class BasicBlock(nn.Module):
134
  stride=stride,
135
  bias=False,
136
  )
137
- self.norm1 = nn.GroupNorm(num_groups=norm_groups, num_channels=out_channels)
138
  self.act1 = ACT2FN[activation]
139
  self.act2 = ACT2FN[activation]
140
  self.conv2 = Conv2dJax(out_channels, out_channels, kernel_size=3, stride=1, bias=False)
141
- self.norm2 = nn.GroupNorm(num_groups=norm_groups, num_channels=out_channels)
142
 
143
  self.shortcut = None
144
  if in_channels != out_channels:
145
  self.shortcut = nn.Sequential(
146
  Conv2dJax(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
147
- nn.GroupNorm(num_groups=norm_groups, num_channels=out_channels),
148
  )
149
 
150
  def forward(self, x):
@@ -190,18 +205,18 @@ class Encoder(nn.Module):
190
  )
191
 
192
  def forward(self, hidden_state: Tensor, output_hidden_states: bool = False) -> BaseModelOutputWithNoAttention:
193
- hidden_states = () if output_hidden_states else None
194
 
195
  for stage in self.stages:
196
  if output_hidden_states:
197
- hidden_states = hidden_states + (hidden_state,)
198
 
199
  hidden_state = stage(hidden_state)
200
 
201
  if output_hidden_states:
202
- hidden_states = hidden_states + (hidden_state,)
203
 
204
- return BaseModelOutputWithNoAttention(
205
  last_hidden_state=hidden_state,
206
  hidden_states=hidden_states,
207
  )
@@ -222,47 +237,22 @@ class ResNet10(PreTrainedModel):
222
  padding=3,
223
  bias=False,
224
  ),
225
- # The original code has a small trick -
226
- # https://github.com/rail-berkeley/hil-serl/blob/main/serl_launcher/serl_launcher/vision/resnet_v1.py#L119
227
- # class MyGroupNorm(nn.GroupNorm):
228
- # def __call__(self, x):
229
- # if x.ndim == 3:
230
- # x = x[jnp.newaxis]
231
- # x = super().__call__(x)
232
- # return x[0]
233
- # else:
234
- # return super().__call__(x)
235
- nn.GroupNorm(num_groups=4, eps=1e-5, num_channels=self.config.embedding_size),
236
  ACT2FN[self.config.hidden_act],
237
  MaxPool2dJax(kernel_size=3, stride=2),
238
  )
239
 
240
  self.encoder = Encoder(self.config)
241
- self.pooler = nn.AdaptiveAvgPool2d(output_size=1)
 
242
 
243
  def _init_pooler(self):
244
  if self.config.pooler == "avg":
245
  self.pooler = nn.AdaptiveAvgPool2d(output_size=1)
246
  elif self.config.pooler == "max":
247
  self.pooler = nn.MaxPool2d(kernel_size=3, stride=2)
248
- elif self.config.pooler == "spatial_learned_embeddings":
249
- raise ValueError("Invalid pooler, it exist in the hil serl version, but weights are missing")
250
-
251
- # In the original HIl-SERL code is used SpatialLearnedEmbeddings as pooliing method
252
- # Check https://github.com/rail-berkeley/hil-serl/blob/7d17d13560d85abffbd45facec17c4f9189c29c0/serl_launcher/serl_launcher/agents/continuous/sac.py#L490
253
- # But weights for this custom layer are missing
254
- # Probably it means that pretrained weights used other way of pooling - probably it's AvgPool2d
255
- # self.pooler = nn.Sequential(
256
- # SpatialLearnedEmbeddings(
257
- # height=height,
258
- # width=width,
259
- # channel=channel,
260
- # num_features=self.num_spatial_blocks,
261
- # ),
262
- # nn.Dropout(0.1, deterministic=not train),
263
- # )
264
  else:
265
- raise ValueError(f"Invalid pooler: {self.config.pooler}")
266
 
267
  def forward(self, x: Tensor, output_hidden_states: Optional[bool] = None) -> BaseModelOutputWithNoAttention:
268
  output_hidden_states = (
@@ -271,7 +261,10 @@ class ResNet10(PreTrainedModel):
271
  embedding_output = self.embedder(x)
272
  encoder_outputs = self.encoder(embedding_output, output_hidden_states=output_hidden_states)
273
 
274
- pooler_output = self.pooler(encoder_outputs.last_hidden_state)
 
 
 
275
 
276
  return BaseModelOutputWithPoolingAndNoAttention(
277
  last_hidden_state=encoder_outputs.last_hidden_state,
 
123
  return self.conv(x)
124
 
125
 
126
+ class MyGroupNorm(nn.Module):
127
+ def __init__(self, num_groups, num_channels, eps=1e-5, affine=True):
128
+ super().__init__()
129
+ self.group_norm = nn.GroupNorm(num_groups, num_channels, eps, affine)
130
+
131
+ def forward(self, x):
132
+ if x.ndim == 3:
133
+ x = x.unsqueeze(0)
134
+ x = self.group_norm(x)
135
+ x = x.squeeze(0)
136
+ else:
137
+ x = self.group_norm(x)
138
+ return x
139
+
140
+
141
  class BasicBlock(nn.Module):
142
  def __init__(self, in_channels, out_channels, activation, stride=1, norm_groups=4):
143
  super().__init__()
 
149
  stride=stride,
150
  bias=False,
151
  )
152
+ self.norm1 = MyGroupNorm(num_groups=norm_groups, num_channels=out_channels)
153
  self.act1 = ACT2FN[activation]
154
  self.act2 = ACT2FN[activation]
155
  self.conv2 = Conv2dJax(out_channels, out_channels, kernel_size=3, stride=1, bias=False)
156
+ self.norm2 = MyGroupNorm(num_groups=norm_groups, num_channels=out_channels)
157
 
158
  self.shortcut = None
159
  if in_channels != out_channels:
160
  self.shortcut = nn.Sequential(
161
  Conv2dJax(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
162
+ MyGroupNorm(num_groups=norm_groups, num_channels=out_channels),
163
  )
164
 
165
  def forward(self, x):
 
205
  )
206
 
207
  def forward(self, hidden_state: Tensor, output_hidden_states: bool = False) -> BaseModelOutputWithNoAttention:
208
+ hidden_states: Optional[tuple[Tensor, ...]] = () if output_hidden_states else None
209
 
210
  for stage in self.stages:
211
  if output_hidden_states:
212
+ hidden_states = hidden_states + (hidden_state,) # type: ignore
213
 
214
  hidden_state = stage(hidden_state)
215
 
216
  if output_hidden_states:
217
+ hidden_states = hidden_states + (hidden_state,) # type: ignore
218
 
219
+ return BaseModelOutputWithPoolingAndNoAttention(
220
  last_hidden_state=hidden_state,
221
  hidden_states=hidden_states,
222
  )
 
237
  padding=3,
238
  bias=False,
239
  ),
240
+ MyGroupNorm(num_groups=4, eps=1e-5, num_channels=self.config.embedding_size),
 
 
 
 
 
 
 
 
 
 
241
  ACT2FN[self.config.hidden_act],
242
  MaxPool2dJax(kernel_size=3, stride=2),
243
  )
244
 
245
  self.encoder = Encoder(self.config)
246
+ self._init_pooler()
247
+ self.post_init()
248
 
249
  def _init_pooler(self):
250
  if self.config.pooler == "avg":
251
  self.pooler = nn.AdaptiveAvgPool2d(output_size=1)
252
  elif self.config.pooler == "max":
253
  self.pooler = nn.MaxPool2d(kernel_size=3, stride=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  else:
255
+ self.pooler = None
256
 
257
  def forward(self, x: Tensor, output_hidden_states: Optional[bool] = None) -> BaseModelOutputWithNoAttention:
258
  output_hidden_states = (
 
261
  embedding_output = self.embedder(x)
262
  encoder_outputs = self.encoder(embedding_output, output_hidden_states=output_hidden_states)
263
 
264
+ if self.pooler is not None:
265
+ pooler_output = self.pooler(encoder_outputs.last_hidden_state)
266
+ else:
267
+ pooler_output = None
268
 
269
  return BaseModelOutputWithPoolingAndNoAttention(
270
  last_hidden_state=encoder_outputs.last_hidden_state,