got rid of the bullshit and put in automodel
Browse files
app.py
CHANGED
@@ -7,39 +7,16 @@ import numpy as np
|
|
7 |
from PIL import Image
|
8 |
from scipy import special
|
9 |
import sys
|
10 |
-
|
11 |
from types import SimpleNamespace
|
12 |
# from transformers import AutoModel, pipeline
|
13 |
from transformers import AutoModelForImageClassification
|
14 |
import torch
|
15 |
-
from torch import Tensor, nn
|
16 |
-
from torch import Tensor
|
17 |
-
from torchvision.models._utils import _make_divisible
|
18 |
-
from torchvision.ops import StochasticDepth
|
19 |
|
20 |
-
|
21 |
# from utils import model_utils, train_utils, data_utils, run_utils
|
22 |
# from model_utils import jason_regnet_maker, jason_efficientnet_maker
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
from transformers import PretrainedConfig, PreTrainedModel
|
27 |
-
|
28 |
-
from typing import List
|
29 |
-
import copy
|
30 |
-
import math
|
31 |
-
import warnings
|
32 |
-
from dataclasses import dataclass
|
33 |
-
from functools import partial
|
34 |
-
import sys
|
35 |
-
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# sys.path.insert(1, "../")
|
40 |
-
# from utils.vision_modifications import Conv2dNormActivation, SqueezeExcitation
|
41 |
-
|
42 |
-
interpolate = torch.nn.functional.interpolate
|
43 |
|
44 |
model_path = 'chlab/'
|
45 |
# model_path = './models/'
|
@@ -73,847 +50,6 @@ effnet_hparams = {61: {
|
|
73 |
activation_indices = {'efficientnet': [0, 3]}
|
74 |
|
75 |
|
76 |
-
|
77 |
-
#### extra torchvision stuff ####
|
78 |
-
|
79 |
-
|
80 |
-
class FrozenBatchNorm2d(torch.nn.Module):
|
81 |
-
"""
|
82 |
-
BatchNorm2d where the batch statistics and the affine parameters are fixed
|
83 |
-
|
84 |
-
Args:
|
85 |
-
num_features (int): Number of features ``C`` from an expected input of size ``(N, C, H, W)``
|
86 |
-
eps (float): a value added to the denominator for numerical stability. Default: 1e-5
|
87 |
-
"""
|
88 |
-
|
89 |
-
def __init__(
|
90 |
-
self,
|
91 |
-
num_features: int,
|
92 |
-
eps: float = 1e-5,
|
93 |
-
):
|
94 |
-
super().__init__()
|
95 |
-
# _log_api_usage_once(self)
|
96 |
-
self.eps = eps
|
97 |
-
self.register_buffer("weight", torch.ones(num_features))
|
98 |
-
self.register_buffer("bias", torch.zeros(num_features))
|
99 |
-
self.register_buffer("running_mean", torch.zeros(num_features))
|
100 |
-
self.register_buffer("running_var", torch.ones(num_features))
|
101 |
-
|
102 |
-
def _load_from_state_dict(
|
103 |
-
self,
|
104 |
-
state_dict: dict,
|
105 |
-
prefix: str,
|
106 |
-
local_metadata: dict,
|
107 |
-
strict: bool,
|
108 |
-
missing_keys: List[str],
|
109 |
-
unexpected_keys: List[str],
|
110 |
-
error_msgs: List[str],
|
111 |
-
):
|
112 |
-
num_batches_tracked_key = prefix + "num_batches_tracked"
|
113 |
-
if num_batches_tracked_key in state_dict:
|
114 |
-
del state_dict[num_batches_tracked_key]
|
115 |
-
|
116 |
-
super()._load_from_state_dict(
|
117 |
-
state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs
|
118 |
-
)
|
119 |
-
|
120 |
-
def forward(self, x: Tensor) -> Tensor:
|
121 |
-
# move reshapes to the beginning
|
122 |
-
# to make it fuser-friendly
|
123 |
-
w = self.weight.reshape(1, -1, 1, 1)
|
124 |
-
b = self.bias.reshape(1, -1, 1, 1)
|
125 |
-
rv = self.running_var.reshape(1, -1, 1, 1)
|
126 |
-
rm = self.running_mean.reshape(1, -1, 1, 1)
|
127 |
-
scale = w * (rv + self.eps).rsqrt()
|
128 |
-
bias = b - rm * scale
|
129 |
-
return x * scale + bias
|
130 |
-
|
131 |
-
def __repr__(self) -> str:
|
132 |
-
return f"{self.__class__.__name__}({self.weight.shape[0]}, eps={self.eps})"
|
133 |
-
|
134 |
-
|
135 |
-
class ConvNormActivation(torch.nn.Sequential):
|
136 |
-
def __init__(
|
137 |
-
self,
|
138 |
-
in_channels: int,
|
139 |
-
out_channels: int,
|
140 |
-
kernel_size: int = 3,
|
141 |
-
stride: int = 1,
|
142 |
-
padding: Optional[int] = None,
|
143 |
-
groups: int = 1,
|
144 |
-
norm_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.BatchNorm2d,
|
145 |
-
activation_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.ReLU,
|
146 |
-
dilation: int = 1,
|
147 |
-
inplace: Optional[bool] = True,
|
148 |
-
bias: Optional[bool] = None,
|
149 |
-
conv_layer: Callable[..., torch.nn.Module] = torch.nn.Conv2d,
|
150 |
-
) -> None:
|
151 |
-
|
152 |
-
if padding is None:
|
153 |
-
padding = (kernel_size - 1) // 2 * dilation
|
154 |
-
if bias is None:
|
155 |
-
bias = norm_layer is None
|
156 |
-
|
157 |
-
layers = [
|
158 |
-
conv_layer(
|
159 |
-
in_channels,
|
160 |
-
out_channels,
|
161 |
-
kernel_size,
|
162 |
-
stride,
|
163 |
-
padding,
|
164 |
-
dilation=dilation,
|
165 |
-
groups=groups,
|
166 |
-
bias=bias,
|
167 |
-
)
|
168 |
-
]
|
169 |
-
|
170 |
-
if norm_layer is not None:
|
171 |
-
layers.append(norm_layer(out_channels))
|
172 |
-
|
173 |
-
if activation_layer is not None:
|
174 |
-
params = {} if inplace is None else {"inplace": inplace}
|
175 |
-
layers.append(activation_layer(**params))
|
176 |
-
super().__init__(*layers)
|
177 |
-
# _log_api_usage_once(self)
|
178 |
-
self.out_channels = out_channels
|
179 |
-
|
180 |
-
if self.__class__ == ConvNormActivation:
|
181 |
-
warnings.warn(
|
182 |
-
"Don't use ConvNormActivation directly, please use Conv2dNormActivation and Conv3dNormActivation instead."
|
183 |
-
)
|
184 |
-
|
185 |
-
|
186 |
-
class Conv2dNormActivation(ConvNormActivation):
|
187 |
-
"""
|
188 |
-
Configurable block used for Convolution2d-Normalization-Activation blocks.
|
189 |
-
|
190 |
-
Args:
|
191 |
-
in_channels (int): Number of channels in the input image
|
192 |
-
out_channels (int): Number of channels produced by the Convolution-Normalization-Activation block
|
193 |
-
kernel_size: (int, optional): Size of the convolving kernel. Default: 3
|
194 |
-
stride (int, optional): Stride of the convolution. Default: 1
|
195 |
-
padding (int, tuple or str, optional): Padding added to all four sides of the input. Default: None, in which case it will calculated as ``padding = (kernel_size - 1) // 2 * dilation``
|
196 |
-
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
|
197 |
-
norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the convolution layer. If ``None`` this layer wont be used. Default: ``torch.nn.BatchNorm2d``
|
198 |
-
activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the conv layer. If ``None`` this layer wont be used. Default: ``torch.nn.ReLU``
|
199 |
-
dilation (int): Spacing between kernel elements. Default: 1
|
200 |
-
inplace (bool): Parameter for the activation layer, which can optionally do the operation in-place. Default ``True``
|
201 |
-
bias (bool, optional): Whether to use bias in the convolution layer. By default, biases are included if ``norm_layer is None``.
|
202 |
-
|
203 |
-
"""
|
204 |
-
|
205 |
-
def __init__(
|
206 |
-
self,
|
207 |
-
in_channels: int,
|
208 |
-
out_channels: int,
|
209 |
-
kernel_size: int = 3,
|
210 |
-
stride: int = 1,
|
211 |
-
padding: Optional[int] = None,
|
212 |
-
groups: int = 1,
|
213 |
-
norm_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.BatchNorm2d,
|
214 |
-
activation_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.ReLU,
|
215 |
-
dilation: int = 1,
|
216 |
-
inplace: Optional[bool] = True,
|
217 |
-
bias: Optional[bool] = None,
|
218 |
-
) -> None:
|
219 |
-
|
220 |
-
super().__init__(
|
221 |
-
in_channels,
|
222 |
-
out_channels,
|
223 |
-
kernel_size,
|
224 |
-
stride,
|
225 |
-
padding,
|
226 |
-
groups,
|
227 |
-
norm_layer,
|
228 |
-
activation_layer,
|
229 |
-
dilation,
|
230 |
-
inplace,
|
231 |
-
bias,
|
232 |
-
torch.nn.Conv2d,
|
233 |
-
)
|
234 |
-
|
235 |
-
|
236 |
-
class Conv3dNormActivation(ConvNormActivation):
|
237 |
-
"""
|
238 |
-
Configurable block used for Convolution3d-Normalization-Activation blocks.
|
239 |
-
|
240 |
-
Args:
|
241 |
-
in_channels (int): Number of channels in the input video.
|
242 |
-
out_channels (int): Number of channels produced by the Convolution-Normalization-Activation block
|
243 |
-
kernel_size: (int, optional): Size of the convolving kernel. Default: 3
|
244 |
-
stride (int, optional): Stride of the convolution. Default: 1
|
245 |
-
padding (int, tuple or str, optional): Padding added to all four sides of the input. Default: None, in which case it will calculated as ``padding = (kernel_size - 1) // 2 * dilation``
|
246 |
-
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
|
247 |
-
norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the convolution layer. If ``None`` this layer wont be used. Default: ``torch.nn.BatchNorm3d``
|
248 |
-
activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the conv layer. If ``None`` this layer wont be used. Default: ``torch.nn.ReLU``
|
249 |
-
dilation (int): Spacing between kernel elements. Default: 1
|
250 |
-
inplace (bool): Parameter for the activation layer, which can optionally do the operation in-place. Default ``True``
|
251 |
-
bias (bool, optional): Whether to use bias in the convolution layer. By default, biases are included if ``norm_layer is None``.
|
252 |
-
"""
|
253 |
-
|
254 |
-
def __init__(
|
255 |
-
self,
|
256 |
-
in_channels: int,
|
257 |
-
out_channels: int,
|
258 |
-
kernel_size: int = 3,
|
259 |
-
stride: int = 1,
|
260 |
-
padding: Optional[int] = None,
|
261 |
-
groups: int = 1,
|
262 |
-
norm_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.BatchNorm3d,
|
263 |
-
activation_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.ReLU,
|
264 |
-
dilation: int = 1,
|
265 |
-
inplace: Optional[bool] = True,
|
266 |
-
bias: Optional[bool] = None,
|
267 |
-
) -> None:
|
268 |
-
|
269 |
-
super().__init__(
|
270 |
-
in_channels,
|
271 |
-
out_channels,
|
272 |
-
kernel_size,
|
273 |
-
stride,
|
274 |
-
padding,
|
275 |
-
groups,
|
276 |
-
norm_layer,
|
277 |
-
activation_layer,
|
278 |
-
dilation,
|
279 |
-
inplace,
|
280 |
-
bias,
|
281 |
-
torch.nn.Conv3d,
|
282 |
-
)
|
283 |
-
|
284 |
-
|
285 |
-
class SqueezeExcitation(torch.nn.Module):
|
286 |
-
"""
|
287 |
-
This block implements the Squeeze-and-Excitation block from https://arxiv.org/abs/1709.01507 (see Fig. 1).
|
288 |
-
Parameters ``activation``, and ``scale_activation`` correspond to ``delta`` and ``sigma`` in eq. 3.
|
289 |
-
|
290 |
-
Args:
|
291 |
-
input_channels (int): Number of channels in the input image
|
292 |
-
squeeze_channels (int): Number of squeeze channels
|
293 |
-
activation (Callable[..., torch.nn.Module], optional): ``delta`` activation. Default: ``torch.nn.ReLU``
|
294 |
-
scale_activation (Callable[..., torch.nn.Module]): ``sigma`` activation. Default: ``torch.nn.Sigmoid``
|
295 |
-
"""
|
296 |
-
|
297 |
-
def __init__(
|
298 |
-
self,
|
299 |
-
input_channels: int,
|
300 |
-
squeeze_channels: int,
|
301 |
-
activation: Callable[..., torch.nn.Module] = torch.nn.ReLU,
|
302 |
-
scale_activation: Callable[..., torch.nn.Module] = torch.nn.Sigmoid,
|
303 |
-
) -> None:
|
304 |
-
super().__init__()
|
305 |
-
# _log_api_usage_once(self)
|
306 |
-
self.avgpool = torch.nn.AdaptiveAvgPool2d(1)
|
307 |
-
self.fc1 = torch.nn.Conv2d(input_channels, squeeze_channels, 1)
|
308 |
-
self.fc2 = torch.nn.Conv2d(squeeze_channels, input_channels, 1)
|
309 |
-
self.activation = activation()
|
310 |
-
self.scale_activation = scale_activation()
|
311 |
-
|
312 |
-
def _scale(self, input: Tensor) -> Tensor:
|
313 |
-
scale = self.avgpool(input)
|
314 |
-
scale = self.fc1(scale)
|
315 |
-
scale = self.activation(scale)
|
316 |
-
scale = self.fc2(scale)
|
317 |
-
return self.scale_activation(scale)
|
318 |
-
|
319 |
-
def forward(self, input: Tensor) -> Tensor:
|
320 |
-
scale = self._scale(input)
|
321 |
-
return scale * input
|
322 |
-
|
323 |
-
|
324 |
-
class MLP(torch.nn.Sequential):
|
325 |
-
"""This block implements the multi-layer perceptron (MLP) module.
|
326 |
-
|
327 |
-
Args:
|
328 |
-
in_channels (int): Number of channels of the input
|
329 |
-
hidden_channels (List[int]): List of the hidden channel dimensions
|
330 |
-
norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the convolution layer. If ``None`` this layer wont be used. Default: ``None``
|
331 |
-
activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the conv layer. If ``None`` this layer wont be used. Default: ``torch.nn.ReLU``
|
332 |
-
inplace (bool): Parameter for the activation layer, which can optionally do the operation in-place. Default ``True``
|
333 |
-
bias (bool): Whether to use bias in the linear layer. Default ``True``
|
334 |
-
dropout (float): The probability for the dropout layer. Default: 0.0
|
335 |
-
"""
|
336 |
-
|
337 |
-
def __init__(
|
338 |
-
self,
|
339 |
-
in_channels: int,
|
340 |
-
hidden_channels: List[int],
|
341 |
-
norm_layer: Optional[Callable[..., torch.nn.Module]] = None,
|
342 |
-
activation_layer: Optional[Callable[..., torch.nn.Module]] = torch.nn.ReLU,
|
343 |
-
inplace: Optional[bool] = True,
|
344 |
-
bias: bool = True,
|
345 |
-
dropout: float = 0.0,
|
346 |
-
):
|
347 |
-
# The addition of `norm_layer` is inspired from the implementation of TorchMultimodal:
|
348 |
-
# https://github.com/facebookresearch/multimodal/blob/5dec8a/torchmultimodal/modules/layers/mlp.py
|
349 |
-
params = {} if inplace is None else {"inplace": inplace}
|
350 |
-
|
351 |
-
layers = []
|
352 |
-
in_dim = in_channels
|
353 |
-
for hidden_dim in hidden_channels[:-1]:
|
354 |
-
layers.append(torch.nn.Linear(in_dim, hidden_dim, bias=bias))
|
355 |
-
if norm_layer is not None:
|
356 |
-
layers.append(norm_layer(hidden_dim))
|
357 |
-
layers.append(activation_layer(**params))
|
358 |
-
layers.append(torch.nn.Dropout(dropout, **params))
|
359 |
-
in_dim = hidden_dim
|
360 |
-
|
361 |
-
layers.append(torch.nn.Linear(in_dim, hidden_channels[-1], bias=bias))
|
362 |
-
layers.append(torch.nn.Dropout(dropout, **params))
|
363 |
-
|
364 |
-
super().__init__(*layers)
|
365 |
-
# _log_api_usage_once(self)
|
366 |
-
|
367 |
-
|
368 |
-
class Permute(torch.nn.Module):
|
369 |
-
"""This module returns a view of the tensor input with its dimensions permuted.
|
370 |
-
|
371 |
-
Args:
|
372 |
-
dims (List[int]): The desired ordering of dimensions
|
373 |
-
"""
|
374 |
-
|
375 |
-
def __init__(self, dims: List[int]):
|
376 |
-
super().__init__()
|
377 |
-
self.dims = dims
|
378 |
-
|
379 |
-
def forward(self, x: Tensor) -> Tensor:
|
380 |
-
return torch.permute(x, self.dims)
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
########## EfficientNet ############
|
386 |
-
@dataclass
|
387 |
-
class _MBConvConfig:
|
388 |
-
expand_ratio: float
|
389 |
-
kernel: int
|
390 |
-
stride: int
|
391 |
-
input_channels: int
|
392 |
-
out_channels: int
|
393 |
-
num_layers: int
|
394 |
-
block: Callable[..., nn.Module]
|
395 |
-
|
396 |
-
@staticmethod
|
397 |
-
def adjust_channels(
|
398 |
-
channels: int, width_mult: float, min_value: Optional[int] = None
|
399 |
-
) -> int:
|
400 |
-
return _make_divisible(channels * width_mult, 8, min_value)
|
401 |
-
|
402 |
-
|
403 |
-
class MBConvConfig(_MBConvConfig):
|
404 |
-
# Stores information listed at Table 1 of the EfficientNet paper & Table 4 of the EfficientNetV2 paper
|
405 |
-
def __init__(
|
406 |
-
self,
|
407 |
-
expand_ratio: float,
|
408 |
-
kernel: int,
|
409 |
-
stride: int,
|
410 |
-
input_channels: int,
|
411 |
-
out_channels: int,
|
412 |
-
num_layers: int,
|
413 |
-
width_mult: float = 1.0,
|
414 |
-
depth_mult: float = 1.0,
|
415 |
-
block: Optional[Callable[..., nn.Module]] = None,
|
416 |
-
) -> None:
|
417 |
-
input_channels = self.adjust_channels(input_channels, width_mult)
|
418 |
-
out_channels = self.adjust_channels(out_channels, width_mult)
|
419 |
-
num_layers = self.adjust_depth(num_layers, depth_mult)
|
420 |
-
if block is None:
|
421 |
-
block = MBConv
|
422 |
-
super().__init__(
|
423 |
-
expand_ratio,
|
424 |
-
kernel,
|
425 |
-
stride,
|
426 |
-
input_channels,
|
427 |
-
out_channels,
|
428 |
-
num_layers,
|
429 |
-
block,
|
430 |
-
)
|
431 |
-
|
432 |
-
@staticmethod
|
433 |
-
def adjust_depth(num_layers: int, depth_mult: float):
|
434 |
-
return int(math.ceil(num_layers * depth_mult))
|
435 |
-
|
436 |
-
|
437 |
-
class FusedMBConvConfig(_MBConvConfig):
|
438 |
-
# Stores information listed at Table 4 of the EfficientNetV2 paper
|
439 |
-
def __init__(
|
440 |
-
self,
|
441 |
-
expand_ratio: float,
|
442 |
-
kernel: int,
|
443 |
-
stride: int,
|
444 |
-
input_channels: int,
|
445 |
-
out_channels: int,
|
446 |
-
num_layers: int,
|
447 |
-
block: Optional[Callable[..., nn.Module]] = None,
|
448 |
-
) -> None:
|
449 |
-
if block is None:
|
450 |
-
block = FusedMBConv
|
451 |
-
super().__init__(
|
452 |
-
expand_ratio,
|
453 |
-
kernel,
|
454 |
-
stride,
|
455 |
-
input_channels,
|
456 |
-
out_channels,
|
457 |
-
num_layers,
|
458 |
-
block,
|
459 |
-
)
|
460 |
-
|
461 |
-
|
462 |
-
class MBConv(nn.Module):
|
463 |
-
def __init__(
|
464 |
-
self,
|
465 |
-
cnf: MBConvConfig,
|
466 |
-
stochastic_depth_prob: float,
|
467 |
-
norm_layer: Callable[..., nn.Module],
|
468 |
-
se_layer: Callable[..., nn.Module] = SqueezeExcitation,
|
469 |
-
) -> None:
|
470 |
-
super().__init__()
|
471 |
-
|
472 |
-
if not (1 <= cnf.stride <= 2):
|
473 |
-
raise ValueError("illegal stride value")
|
474 |
-
|
475 |
-
self.use_res_connect = (
|
476 |
-
cnf.stride == 1 and cnf.input_channels == cnf.out_channels
|
477 |
-
)
|
478 |
-
|
479 |
-
layers: List[nn.Module] = []
|
480 |
-
activation_layer = nn.SiLU
|
481 |
-
|
482 |
-
# expand
|
483 |
-
expanded_channels = cnf.adjust_channels(cnf.input_channels, cnf.expand_ratio)
|
484 |
-
if expanded_channels != cnf.input_channels:
|
485 |
-
layers.append(
|
486 |
-
Conv2dNormActivation(
|
487 |
-
cnf.input_channels,
|
488 |
-
expanded_channels,
|
489 |
-
kernel_size=1,
|
490 |
-
norm_layer=norm_layer,
|
491 |
-
activation_layer=activation_layer,
|
492 |
-
)
|
493 |
-
)
|
494 |
-
|
495 |
-
# depthwise
|
496 |
-
layers.append(
|
497 |
-
Conv2dNormActivation(
|
498 |
-
expanded_channels,
|
499 |
-
expanded_channels,
|
500 |
-
kernel_size=cnf.kernel,
|
501 |
-
stride=cnf.stride,
|
502 |
-
groups=expanded_channels,
|
503 |
-
norm_layer=norm_layer,
|
504 |
-
activation_layer=activation_layer,
|
505 |
-
)
|
506 |
-
)
|
507 |
-
|
508 |
-
# squeeze and excitation
|
509 |
-
squeeze_channels = max(1, cnf.input_channels // 4)
|
510 |
-
layers.append(
|
511 |
-
se_layer(
|
512 |
-
expanded_channels,
|
513 |
-
squeeze_channels,
|
514 |
-
activation=partial(nn.SiLU, inplace=True),
|
515 |
-
)
|
516 |
-
)
|
517 |
-
|
518 |
-
# project
|
519 |
-
layers.append(
|
520 |
-
Conv2dNormActivation(
|
521 |
-
expanded_channels,
|
522 |
-
cnf.out_channels,
|
523 |
-
kernel_size=1,
|
524 |
-
norm_layer=norm_layer,
|
525 |
-
activation_layer=None,
|
526 |
-
)
|
527 |
-
)
|
528 |
-
|
529 |
-
self.block = nn.Sequential(*layers)
|
530 |
-
self.stochastic_depth = StochasticDepth(stochastic_depth_prob, "row")
|
531 |
-
self.out_channels = cnf.out_channels
|
532 |
-
|
533 |
-
def forward(self, input: Tensor) -> Tensor:
|
534 |
-
result = self.block(input)
|
535 |
-
if self.use_res_connect:
|
536 |
-
result = self.stochastic_depth(result)
|
537 |
-
result += input
|
538 |
-
return result
|
539 |
-
|
540 |
-
|
541 |
-
class FusedMBConv(nn.Module):
|
542 |
-
def __init__(
|
543 |
-
self,
|
544 |
-
cnf: FusedMBConvConfig,
|
545 |
-
stochastic_depth_prob: float,
|
546 |
-
norm_layer: Callable[..., nn.Module],
|
547 |
-
) -> None:
|
548 |
-
super().__init__()
|
549 |
-
|
550 |
-
if not (1 <= cnf.stride <= 2):
|
551 |
-
raise ValueError("illegal stride value")
|
552 |
-
|
553 |
-
self.use_res_connect = (
|
554 |
-
cnf.stride == 1 and cnf.input_channels == cnf.out_channels
|
555 |
-
)
|
556 |
-
|
557 |
-
layers: List[nn.Module] = []
|
558 |
-
activation_layer = nn.SiLU
|
559 |
-
|
560 |
-
expanded_channels = cnf.adjust_channels(cnf.input_channels, cnf.expand_ratio)
|
561 |
-
if expanded_channels != cnf.input_channels:
|
562 |
-
# fused expand
|
563 |
-
layers.append(
|
564 |
-
Conv2dNormActivation(
|
565 |
-
cnf.input_channels,
|
566 |
-
expanded_channels,
|
567 |
-
kernel_size=cnf.kernel,
|
568 |
-
stride=cnf.stride,
|
569 |
-
norm_layer=norm_layer,
|
570 |
-
activation_layer=activation_layer,
|
571 |
-
)
|
572 |
-
)
|
573 |
-
|
574 |
-
# project
|
575 |
-
layers.append(
|
576 |
-
Conv2dNormActivation(
|
577 |
-
expanded_channels,
|
578 |
-
cnf.out_channels,
|
579 |
-
kernel_size=1,
|
580 |
-
norm_layer=norm_layer,
|
581 |
-
activation_layer=None,
|
582 |
-
)
|
583 |
-
)
|
584 |
-
else:
|
585 |
-
layers.append(
|
586 |
-
Conv2dNormActivation(
|
587 |
-
cnf.input_channels,
|
588 |
-
cnf.out_channels,
|
589 |
-
kernel_size=cnf.kernel,
|
590 |
-
stride=cnf.stride,
|
591 |
-
norm_layer=norm_layer,
|
592 |
-
activation_layer=activation_layer,
|
593 |
-
)
|
594 |
-
)
|
595 |
-
|
596 |
-
self.block = nn.Sequential(*layers)
|
597 |
-
self.stochastic_depth = StochasticDepth(stochastic_depth_prob, "row")
|
598 |
-
self.out_channels = cnf.out_channels
|
599 |
-
|
600 |
-
def forward(self, input: Tensor) -> Tensor:
|
601 |
-
result = self.block(input)
|
602 |
-
if self.use_res_connect:
|
603 |
-
result = self.stochastic_depth(result)
|
604 |
-
result += input
|
605 |
-
return result
|
606 |
-
|
607 |
-
|
608 |
-
class EfficientNetConfig(PretrainedConfig):
|
609 |
-
|
610 |
-
model_type = "efficientnet"
|
611 |
-
|
612 |
-
def __init__(
|
613 |
-
self,
|
614 |
-
# inverted_residual_setting: Sequence[Union[MBConvConfig, FusedMBConvConfig]],
|
615 |
-
dropout: float=0.25,
|
616 |
-
num_channels: int = 61,
|
617 |
-
stochastic_depth_prob: float = 0.2,
|
618 |
-
num_classes: int = 2,
|
619 |
-
norm_layer: Optional[Callable[..., nn.Module]] = None,
|
620 |
-
# last_channel: Optional[int] = None,
|
621 |
-
size: str='v2_s',
|
622 |
-
width_mult: float = 1.0,
|
623 |
-
depth_mult: float = 1.0,
|
624 |
-
**kwargs: Any,
|
625 |
-
) -> None:
|
626 |
-
"""
|
627 |
-
EfficientNet V1 and V2 main class
|
628 |
-
|
629 |
-
Args:
|
630 |
-
inverted_residual_setting (Sequence[Union[MBConvConfig, FusedMBConvConfig]]): Network structure
|
631 |
-
dropout (float): The droupout probability
|
632 |
-
stochastic_depth_prob (float): The stochastic depth probability
|
633 |
-
num_classes (int): Number of classes
|
634 |
-
norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
|
635 |
-
last_channel (int): The number of channels on the penultimate layer
|
636 |
-
"""
|
637 |
-
|
638 |
-
|
639 |
-
# self.model = EfficientNet(
|
640 |
-
# dropout=dropout,
|
641 |
-
# num_channels=num_channels,
|
642 |
-
# num_classes=num_classes,
|
643 |
-
# size=size,
|
644 |
-
# stochastic_depth_prob=stochastic_depth_prob,
|
645 |
-
# width_mult=width_mult,
|
646 |
-
# depth_mult=depth_mult,
|
647 |
-
# )
|
648 |
-
|
649 |
-
#
|
650 |
-
self.dropout=dropout
|
651 |
-
self.num_channels=num_channels
|
652 |
-
self.num_classes=num_classes
|
653 |
-
self.size=size
|
654 |
-
self.stochastic_depth_prob=stochastic_depth_prob
|
655 |
-
self.width_mult=width_mult
|
656 |
-
self.depth_mult=depth_mult
|
657 |
-
|
658 |
-
super().__init__(**kwargs)
|
659 |
-
|
660 |
-
|
661 |
-
class EfficientNetPreTrained(PreTrainedModel):
|
662 |
-
|
663 |
-
config_class = EfficientNetConfig
|
664 |
-
|
665 |
-
def __init__(
|
666 |
-
self,
|
667 |
-
config
|
668 |
-
):
|
669 |
-
super().__init__(config)
|
670 |
-
self.model = EfficientNet( dropout=config.dropout,
|
671 |
-
num_channels=config.num_channels,
|
672 |
-
num_classes=config.num_classes,
|
673 |
-
size=config.size,
|
674 |
-
stochastic_depth_prob=config.stochastic_depth_prob,
|
675 |
-
width_mult=config.width_mult,
|
676 |
-
depth_mult=config.depth_mult,)
|
677 |
-
|
678 |
-
def forward(self, tensor):
|
679 |
-
return self.model.forward(tensor)
|
680 |
-
|
681 |
-
|
682 |
-
class EfficientNet(nn.Module):
|
683 |
-
|
684 |
-
|
685 |
-
def __init__(
|
686 |
-
self,
|
687 |
-
# inverted_residual_setting: Sequence[Union[MBConvConfig, FusedMBConvConfig]],
|
688 |
-
dropout: float=0.25,
|
689 |
-
num_channels: int = 61,
|
690 |
-
stochastic_depth_prob: float = 0.2,
|
691 |
-
num_classes: int = 2,
|
692 |
-
norm_layer: Optional[Callable[..., nn.Module]] = None,
|
693 |
-
# last_channel: Optional[int] = None,
|
694 |
-
size: str='v2_s',
|
695 |
-
width_mult: float = 1.0,
|
696 |
-
depth_mult: float = 1.0,
|
697 |
-
**kwargs: Any,
|
698 |
-
) -> None:
|
699 |
-
"""
|
700 |
-
EfficientNet V1 and V2 main class
|
701 |
-
|
702 |
-
Args:
|
703 |
-
inverted_residual_setting (Sequence[Union[MBConvConfig, FusedMBConvConfig]]): Network structure
|
704 |
-
dropout (float): The droupout probability
|
705 |
-
stochastic_depth_prob (float): The stochastic depth probability
|
706 |
-
num_classes (int): Number of classes
|
707 |
-
norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
|
708 |
-
last_channel (int): The number of channels on the penultimate layer
|
709 |
-
"""
|
710 |
-
super().__init__()
|
711 |
-
# _log_api_usage_once(self)
|
712 |
-
|
713 |
-
inverted_residual_setting, last_channel = _efficientnet_conf(
|
714 |
-
"efficientnet_%s" % (size), width_mult=width_mult, depth_mult=depth_mult
|
715 |
-
)
|
716 |
-
|
717 |
-
if not inverted_residual_setting:
|
718 |
-
raise ValueError("The inverted_residual_setting should not be empty")
|
719 |
-
elif not (
|
720 |
-
isinstance(inverted_residual_setting, Sequence)
|
721 |
-
and all([isinstance(s, _MBConvConfig) for s in inverted_residual_setting])
|
722 |
-
):
|
723 |
-
raise TypeError(
|
724 |
-
"The inverted_residual_setting should be List[MBConvConfig]"
|
725 |
-
)
|
726 |
-
|
727 |
-
if "block" in kwargs:
|
728 |
-
warnings.warn(
|
729 |
-
"The parameter 'block' is deprecated since 0.13 and will be removed 0.15. "
|
730 |
-
"Please pass this information on 'MBConvConfig.block' instead."
|
731 |
-
)
|
732 |
-
if kwargs["block"] is not None:
|
733 |
-
for s in inverted_residual_setting:
|
734 |
-
if isinstance(s, MBConvConfig):
|
735 |
-
s.block = kwargs["block"]
|
736 |
-
|
737 |
-
if norm_layer is None:
|
738 |
-
norm_layer = nn.BatchNorm2d
|
739 |
-
|
740 |
-
layers: List[nn.Module] = []
|
741 |
-
|
742 |
-
# building first layer
|
743 |
-
firstconv_output_channels = inverted_residual_setting[0].input_channels
|
744 |
-
layers.append(
|
745 |
-
Conv2dNormActivation(
|
746 |
-
num_channels,
|
747 |
-
firstconv_output_channels,
|
748 |
-
kernel_size=3,
|
749 |
-
stride=2,
|
750 |
-
norm_layer=norm_layer,
|
751 |
-
activation_layer=nn.SiLU,
|
752 |
-
)
|
753 |
-
)
|
754 |
-
|
755 |
-
# building inverted residual blocks
|
756 |
-
total_stage_blocks = sum(cnf.num_layers for cnf in inverted_residual_setting)
|
757 |
-
stage_block_id = 0
|
758 |
-
for cnf in inverted_residual_setting:
|
759 |
-
stage: List[nn.Module] = []
|
760 |
-
for _ in range(cnf.num_layers):
|
761 |
-
# copy to avoid modifications. shallow copy is enough
|
762 |
-
block_cnf = copy.copy(cnf)
|
763 |
-
|
764 |
-
# overwrite info if not the first conv in the stage
|
765 |
-
if stage:
|
766 |
-
block_cnf.input_channels = block_cnf.out_channels
|
767 |
-
block_cnf.stride = 1
|
768 |
-
|
769 |
-
# adjust stochastic depth probability based on the depth of the stage block
|
770 |
-
sd_prob = (
|
771 |
-
stochastic_depth_prob * float(stage_block_id) / total_stage_blocks
|
772 |
-
)
|
773 |
-
|
774 |
-
stage.append(block_cnf.block(block_cnf, sd_prob, norm_layer))
|
775 |
-
stage_block_id += 1
|
776 |
-
|
777 |
-
layers.append(nn.Sequential(*stage))
|
778 |
-
|
779 |
-
# building last several layers
|
780 |
-
lastconv_input_channels = inverted_residual_setting[-1].out_channels
|
781 |
-
lastconv_output_channels = (
|
782 |
-
last_channel if last_channel is not None else 4 * lastconv_input_channels
|
783 |
-
)
|
784 |
-
layers.append(
|
785 |
-
Conv2dNormActivation(
|
786 |
-
lastconv_input_channels,
|
787 |
-
lastconv_output_channels,
|
788 |
-
kernel_size=1,
|
789 |
-
norm_layer=norm_layer,
|
790 |
-
activation_layer=nn.SiLU,
|
791 |
-
)
|
792 |
-
)
|
793 |
-
|
794 |
-
self.features = nn.Sequential(*layers)
|
795 |
-
self.avgpool = nn.AdaptiveAvgPool2d(1)
|
796 |
-
self.classifier = nn.Sequential(
|
797 |
-
nn.Dropout(p=dropout, inplace=True),
|
798 |
-
nn.Linear(lastconv_output_channels, num_classes),
|
799 |
-
)
|
800 |
-
|
801 |
-
for m in self.modules():
|
802 |
-
if isinstance(m, nn.Conv2d):
|
803 |
-
nn.init.kaiming_normal_(m.weight, mode="fan_out")
|
804 |
-
if m.bias is not None:
|
805 |
-
nn.init.zeros_(m.bias)
|
806 |
-
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
|
807 |
-
nn.init.ones_(m.weight)
|
808 |
-
nn.init.zeros_(m.bias)
|
809 |
-
elif isinstance(m, nn.Linear):
|
810 |
-
init_range = 1.0 / math.sqrt(m.out_features)
|
811 |
-
nn.init.uniform_(m.weight, -init_range, init_range)
|
812 |
-
nn.init.zeros_(m.bias)
|
813 |
-
|
814 |
-
# super().__init__(**kwargs)
|
815 |
-
|
816 |
-
def _forward_impl(self, x: Tensor) -> Tensor:
|
817 |
-
x = self.features(x)
|
818 |
-
|
819 |
-
x = self.avgpool(x)
|
820 |
-
x = torch.flatten(x, 1)
|
821 |
-
|
822 |
-
x = self.classifier(x)
|
823 |
-
|
824 |
-
return x
|
825 |
-
|
826 |
-
def forward(self, x: Tensor) -> Tensor:
|
827 |
-
return self._forward_impl(x)
|
828 |
-
|
829 |
-
|
830 |
-
# def _efficientnet(
|
831 |
-
# inverted_residual_setting: Sequence[Union[MBConvConfig, FusedMBConvConfig]],
|
832 |
-
# dropout: float,
|
833 |
-
# last_channel: Optional[int],
|
834 |
-
# weights=None,
|
835 |
-
# num_channels: int = 61,
|
836 |
-
# stochastic_depth_prob: float = 0.2,
|
837 |
-
# progress: bool = True,
|
838 |
-
# num_classes: int = 2,
|
839 |
-
# **kwargs: Any,
|
840 |
-
# ) -> EfficientNetCongig:
|
841 |
-
|
842 |
-
# model = EfficientNetCongif(
|
843 |
-
# inverted_residual_setting,
|
844 |
-
# dropout,
|
845 |
-
# num_classes=num_classes,
|
846 |
-
# num_channels=num_channels,
|
847 |
-
# stochastic_depth_prob=stochastic_depth_prob,
|
848 |
-
# last_channel=last_channel,
|
849 |
-
# **kwargs,
|
850 |
-
# )
|
851 |
-
|
852 |
-
# return model
|
853 |
-
|
854 |
-
|
855 |
-
def _efficientnet_conf(
|
856 |
-
arch: str,
|
857 |
-
**kwargs: Any,
|
858 |
-
) -> Tuple[Sequence[Union[MBConvConfig, FusedMBConvConfig]], Optional[int]]:
|
859 |
-
inverted_residual_setting: Sequence[Union[MBConvConfig, FusedMBConvConfig]]
|
860 |
-
if arch.startswith("efficientnet_b"):
|
861 |
-
bneck_conf = partial(
|
862 |
-
MBConvConfig,
|
863 |
-
width_mult=kwargs.pop("width_mult"),
|
864 |
-
depth_mult=kwargs.pop("depth_mult"),
|
865 |
-
)
|
866 |
-
inverted_residual_setting = [
|
867 |
-
bneck_conf(1, 3, 1, 32, 16, 1),
|
868 |
-
bneck_conf(6, 3, 2, 16, 24, 2),
|
869 |
-
bneck_conf(6, 5, 2, 24, 40, 2),
|
870 |
-
bneck_conf(6, 3, 2, 40, 80, 3),
|
871 |
-
bneck_conf(6, 5, 1, 80, 112, 3),
|
872 |
-
bneck_conf(6, 5, 2, 112, 192, 4),
|
873 |
-
bneck_conf(6, 3, 1, 192, 320, 1),
|
874 |
-
]
|
875 |
-
last_channel = None
|
876 |
-
elif arch.startswith("efficientnet_v2_s"):
|
877 |
-
inverted_residual_setting = [
|
878 |
-
FusedMBConvConfig(1, 3, 1, 24, 24, 2),
|
879 |
-
FusedMBConvConfig(4, 3, 2, 24, 48, 4),
|
880 |
-
FusedMBConvConfig(4, 3, 2, 48, 64, 4),
|
881 |
-
MBConvConfig(4, 3, 2, 64, 128, 6),
|
882 |
-
MBConvConfig(6, 3, 1, 128, 160, 9),
|
883 |
-
MBConvConfig(6, 3, 2, 160, 256, 15),
|
884 |
-
]
|
885 |
-
last_channel = 1280
|
886 |
-
elif arch.startswith("efficientnet_v2_m"):
|
887 |
-
inverted_residual_setting = [
|
888 |
-
FusedMBConvConfig(1, 3, 1, 24, 24, 3),
|
889 |
-
FusedMBConvConfig(4, 3, 2, 24, 48, 5),
|
890 |
-
FusedMBConvConfig(4, 3, 2, 48, 80, 5),
|
891 |
-
MBConvConfig(4, 3, 2, 80, 160, 7),
|
892 |
-
MBConvConfig(6, 3, 1, 160, 176, 14),
|
893 |
-
MBConvConfig(6, 3, 2, 176, 304, 18),
|
894 |
-
MBConvConfig(6, 3, 1, 304, 512, 5),
|
895 |
-
]
|
896 |
-
last_channel = 1280
|
897 |
-
elif arch.startswith("efficientnet_v2_l"):
|
898 |
-
inverted_residual_setting = [
|
899 |
-
FusedMBConvConfig(1, 3, 1, 32, 32, 4),
|
900 |
-
FusedMBConvConfig(4, 3, 2, 32, 64, 7),
|
901 |
-
FusedMBConvConfig(4, 3, 2, 64, 96, 7),
|
902 |
-
MBConvConfig(4, 3, 2, 96, 192, 10),
|
903 |
-
MBConvConfig(6, 3, 1, 192, 224, 19),
|
904 |
-
MBConvConfig(6, 3, 2, 224, 384, 25),
|
905 |
-
MBConvConfig(6, 3, 1, 384, 640, 7),
|
906 |
-
]
|
907 |
-
last_channel = 1280
|
908 |
-
else:
|
909 |
-
raise ValueError(f"Unsupported model type {arch}")
|
910 |
-
|
911 |
-
return inverted_residual_setting, last_channel
|
912 |
-
|
913 |
-
|
914 |
-
|
915 |
-
##### normal stuff ####
|
916 |
-
|
917 |
def normalize_array(x: list):
|
918 |
|
919 |
'''Makes array between 0 and 1'''
|
@@ -1088,7 +224,8 @@ def predict_and_analyze(model_name, num_channels, dim, input_channel, image):
|
|
1088 |
config.save_pretrained(save_directory=model_loading_name)
|
1089 |
# config = EfficientNetConfig.from_pretrained(model_loading_name)
|
1090 |
|
1091 |
-
model = EfficientNetPreTrained.from_pretrained(model_loading_name)
|
|
|
1092 |
|
1093 |
# model = EfficientNetPreTrained(config)
|
1094 |
# config.register_for_auto_class()
|
|
|
7 |
from PIL import Image
|
8 |
from scipy import special
|
9 |
import sys
|
10 |
+
import timm
|
11 |
from types import SimpleNamespace
|
12 |
# from transformers import AutoModel, pipeline
|
13 |
from transformers import AutoModelForImageClassification
|
14 |
import torch
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
sys.path.insert(1, "../")
|
17 |
# from utils import model_utils, train_utils, data_utils, run_utils
|
18 |
# from model_utils import jason_regnet_maker, jason_efficientnet_maker
|
19 |
+
from model_utils.efficientnet_config import EfficientNetConfig, EfficientNetPreTrained
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
model_path = 'chlab/'
|
22 |
# model_path = './models/'
|
|
|
50 |
activation_indices = {'efficientnet': [0, 3]}
|
51 |
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def normalize_array(x: list):
|
54 |
|
55 |
'''Makes array between 0 and 1'''
|
|
|
224 |
config.save_pretrained(save_directory=model_loading_name)
|
225 |
# config = EfficientNetConfig.from_pretrained(model_loading_name)
|
226 |
|
227 |
+
# model = EfficientNetPreTrained.from_pretrained(model_loading_name)
|
228 |
+
model = AutoModelForImageClassification.from_pretrained(model_loading_name, trust_remote_code=True)
|
229 |
|
230 |
# model = EfficientNetPreTrained(config)
|
231 |
# config.register_for_auto_class()
|