making efficientnet own class that efficientnetconfig calls
Browse files- app.py +3 -3
- model_utils/efficientnet_config.py +163 -1
app.py
CHANGED
@@ -85,11 +85,11 @@ def get_activations(model, image: list, model_name: str,
|
|
85 |
|
86 |
|
87 |
layer_outputs = {}
|
88 |
-
for i in range(len(model.features)):
|
89 |
-
image = model.features[i](image)
|
90 |
layer_outputs[i] = image
|
91 |
print(i, layer_outputs[i].shape)
|
92 |
-
output = model(image).detach().cpu().numpy()
|
93 |
output_1 = activation_indices[model_name].detach().cpu().numpy()
|
94 |
output_2 = activation_indices[model_name].detach().cpu().numpy()
|
95 |
|
|
|
85 |
|
86 |
|
87 |
layer_outputs = {}
|
88 |
+
for i in range(len(model.model.features)):
|
89 |
+
image = model.model.features[i](image)
|
90 |
layer_outputs[i] = image
|
91 |
print(i, layer_outputs[i].shape)
|
92 |
+
output = model.model(image).detach().cpu().numpy()
|
93 |
output_1 = activation_indices[model_name].detach().cpu().numpy()
|
94 |
output_2 = activation_indices[model_name].detach().cpu().numpy()
|
95 |
|
model_utils/efficientnet_config.py
CHANGED
@@ -269,8 +269,170 @@ class EfficientNetConfig(PretrainedConfig):
|
|
269 |
norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
|
270 |
last_channel (int): The number of channels on the penultimate layer
|
271 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
# super().__init__()
|
273 |
# _log_api_usage_once(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
inverted_residual_setting, last_channel = _efficientnet_conf(
|
276 |
"efficientnet_%s" % (size), width_mult=width_mult, depth_mult=depth_mult
|
@@ -373,7 +535,7 @@ class EfficientNetConfig(PretrainedConfig):
|
|
373 |
nn.init.uniform_(m.weight, -init_range, init_range)
|
374 |
nn.init.zeros_(m.bias)
|
375 |
|
376 |
-
super().__init__(**kwargs)
|
377 |
|
378 |
def _forward_impl(self, x: Tensor) -> Tensor:
|
379 |
x = self.features(x)
|
|
|
269 |
norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
|
270 |
last_channel (int): The number of channels on the penultimate layer
|
271 |
"""
|
272 |
+
|
273 |
+
|
274 |
+
self.model = EfficientNet(
|
275 |
+
dropout,
|
276 |
+
num_channels=num_channels,
|
277 |
+
num_classes=num_classes,
|
278 |
+
size=size,
|
279 |
+
stochastic_depth_prob=stochastic_depth_prob,
|
280 |
+
width_mult=width_mult,
|
281 |
+
depth_mult=depth_mult,
|
282 |
+
)
|
283 |
+
|
284 |
+
super().__init__(**kwargs)
|
285 |
+
|
286 |
# super().__init__()
|
287 |
# _log_api_usage_once(self)
|
288 |
+
# inverted_residual_setting, last_channel = _efficientnet_conf(
|
289 |
+
# "efficientnet_%s" % (size), width_mult=width_mult, depth_mult=depth_mult
|
290 |
+
# )
|
291 |
+
|
292 |
+
# if not inverted_residual_setting:
|
293 |
+
# raise ValueError("The inverted_residual_setting should not be empty")
|
294 |
+
# elif not (
|
295 |
+
# isinstance(inverted_residual_setting, Sequence)
|
296 |
+
# and all([isinstance(s, _MBConvConfig) for s in inverted_residual_setting])
|
297 |
+
# ):
|
298 |
+
# raise TypeError(
|
299 |
+
# "The inverted_residual_setting should be List[MBConvConfig]"
|
300 |
+
# )
|
301 |
+
|
302 |
+
# if "block" in kwargs:
|
303 |
+
# warnings.warn(
|
304 |
+
# "The parameter 'block' is deprecated since 0.13 and will be removed 0.15. "
|
305 |
+
# "Please pass this information on 'MBConvConfig.block' instead."
|
306 |
+
# )
|
307 |
+
# if kwargs["block"] is not None:
|
308 |
+
# for s in inverted_residual_setting:
|
309 |
+
# if isinstance(s, MBConvConfig):
|
310 |
+
# s.block = kwargs["block"]
|
311 |
+
|
312 |
+
# if norm_layer is None:
|
313 |
+
# norm_layer = nn.BatchNorm2d
|
314 |
+
|
315 |
+
# layers: List[nn.Module] = []
|
316 |
+
|
317 |
+
# # building first layer
|
318 |
+
# firstconv_output_channels = inverted_residual_setting[0].input_channels
|
319 |
+
# layers.append(
|
320 |
+
# Conv2dNormActivation(
|
321 |
+
# num_channels,
|
322 |
+
# firstconv_output_channels,
|
323 |
+
# kernel_size=3,
|
324 |
+
# stride=2,
|
325 |
+
# norm_layer=norm_layer,
|
326 |
+
# activation_layer=nn.SiLU,
|
327 |
+
# )
|
328 |
+
# )
|
329 |
+
|
330 |
+
# # building inverted residual blocks
|
331 |
+
# total_stage_blocks = sum(cnf.num_layers for cnf in inverted_residual_setting)
|
332 |
+
# stage_block_id = 0
|
333 |
+
# for cnf in inverted_residual_setting:
|
334 |
+
# stage: List[nn.Module] = []
|
335 |
+
# for _ in range(cnf.num_layers):
|
336 |
+
# # copy to avoid modifications. shallow copy is enough
|
337 |
+
# block_cnf = copy.copy(cnf)
|
338 |
+
|
339 |
+
# # overwrite info if not the first conv in the stage
|
340 |
+
# if stage:
|
341 |
+
# block_cnf.input_channels = block_cnf.out_channels
|
342 |
+
# block_cnf.stride = 1
|
343 |
+
|
344 |
+
# # adjust stochastic depth probability based on the depth of the stage block
|
345 |
+
# sd_prob = (
|
346 |
+
# stochastic_depth_prob * float(stage_block_id) / total_stage_blocks
|
347 |
+
# )
|
348 |
+
|
349 |
+
# stage.append(block_cnf.block(block_cnf, sd_prob, norm_layer))
|
350 |
+
# stage_block_id += 1
|
351 |
+
|
352 |
+
# layers.append(nn.Sequential(*stage))
|
353 |
+
|
354 |
+
# # building last several layers
|
355 |
+
# lastconv_input_channels = inverted_residual_setting[-1].out_channels
|
356 |
+
# lastconv_output_channels = (
|
357 |
+
# last_channel if last_channel is not None else 4 * lastconv_input_channels
|
358 |
+
# )
|
359 |
+
# layers.append(
|
360 |
+
# Conv2dNormActivation(
|
361 |
+
# lastconv_input_channels,
|
362 |
+
# lastconv_output_channels,
|
363 |
+
# kernel_size=1,
|
364 |
+
# norm_layer=norm_layer,
|
365 |
+
# activation_layer=nn.SiLU,
|
366 |
+
# )
|
367 |
+
# )
|
368 |
+
|
369 |
+
# self.features = nn.Sequential(*layers)
|
370 |
+
# self.avgpool = nn.AdaptiveAvgPool2d(1)
|
371 |
+
# self.classifier = nn.Sequential(
|
372 |
+
# nn.Dropout(p=dropout, inplace=True),
|
373 |
+
# nn.Linear(lastconv_output_channels, num_classes),
|
374 |
+
# )
|
375 |
+
|
376 |
+
# for m in self.modules():
|
377 |
+
# if isinstance(m, nn.Conv2d):
|
378 |
+
# nn.init.kaiming_normal_(m.weight, mode="fan_out")
|
379 |
+
# if m.bias is not None:
|
380 |
+
# nn.init.zeros_(m.bias)
|
381 |
+
# elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
|
382 |
+
# nn.init.ones_(m.weight)
|
383 |
+
# nn.init.zeros_(m.bias)
|
384 |
+
# elif isinstance(m, nn.Linear):
|
385 |
+
# init_range = 1.0 / math.sqrt(m.out_features)
|
386 |
+
# nn.init.uniform_(m.weight, -init_range, init_range)
|
387 |
+
# nn.init.zeros_(m.bias)
|
388 |
+
|
389 |
+
# super().__init__(**kwargs)
|
390 |
+
|
391 |
+
# def _forward_impl(self, x: Tensor) -> Tensor:
|
392 |
+
# x = self.features(x)
|
393 |
+
|
394 |
+
# x = self.avgpool(x)
|
395 |
+
# x = torch.flatten(x, 1)
|
396 |
+
|
397 |
+
# x = self.classifier(x)
|
398 |
+
|
399 |
+
# return x
|
400 |
+
|
401 |
+
# def forward(self, x: Tensor) -> Tensor:
|
402 |
+
# return self._forward_impl(x)
|
403 |
+
|
404 |
+
|
405 |
+
class EfficientNet(nn.Module):
|
406 |
+
|
407 |
+
model_type = "efficientnet"
|
408 |
+
|
409 |
+
def __init__(
|
410 |
+
self,
|
411 |
+
# inverted_residual_setting: Sequence[Union[MBConvConfig, FusedMBConvConfig]],
|
412 |
+
dropout: float,
|
413 |
+
num_channels: int = 61,
|
414 |
+
stochastic_depth_prob: float = 0.2,
|
415 |
+
num_classes: int = 2,
|
416 |
+
norm_layer: Optional[Callable[..., nn.Module]] = None,
|
417 |
+
# last_channel: Optional[int] = None,
|
418 |
+
size: str='v2_s',
|
419 |
+
width_mult: float = 1.0,
|
420 |
+
depth_mult: float = 1.0,
|
421 |
+
**kwargs: Any,
|
422 |
+
) -> None:
|
423 |
+
"""
|
424 |
+
EfficientNet V1 and V2 main class
|
425 |
+
|
426 |
+
Args:
|
427 |
+
inverted_residual_setting (Sequence[Union[MBConvConfig, FusedMBConvConfig]]): Network structure
|
428 |
+
dropout (float): The droupout probability
|
429 |
+
stochastic_depth_prob (float): The stochastic depth probability
|
430 |
+
num_classes (int): Number of classes
|
431 |
+
norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
|
432 |
+
last_channel (int): The number of channels on the penultimate layer
|
433 |
+
"""
|
434 |
+
super().__init__()
|
435 |
+
# _log_api_usage_once(self)
|
436 |
|
437 |
inverted_residual_setting, last_channel = _efficientnet_conf(
|
438 |
"efficientnet_%s" % (size), width_mult=width_mult, depth_mult=depth_mult
|
|
|
535 |
nn.init.uniform_(m.weight, -init_range, init_range)
|
536 |
nn.init.zeros_(m.bias)
|
537 |
|
538 |
+
# super().__init__(**kwargs)
|
539 |
|
540 |
def _forward_impl(self, x: Tensor) -> Tensor:
|
541 |
x = self.features(x)
|