-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredNet.py
More file actions
60 lines (49 loc) · 1.77 KB
/
PredNet.py
File metadata and controls
60 lines (49 loc) · 1.77 KB
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
from functools import partial
from typing import Any, Callable
import jax.numpy as jnp
import flax.linen as nn
ModuleDef = Any
dtypedef = Any
class PredNetBlock(nn.Module):
cnn_channels: int
norm: ModuleDef
dtype: dtypedef
kernel_init: Callable
@nn.compact
def __call__(self, x):
x = nn.Conv(features=self.cnn_channels, kernel_size=(3, 3), dtype=self.dtype, kernel_init=self.kernel_init)(x)
x = self.norm()(x)
x = nn.relu(x)
return x
class Classifier(nn.Module):
cnn_channels: int
num_blocks: int
num_classes: int
dtype: dtypedef
kernel_init: Callable
@nn.compact
def __call__(self, x, train):
norm = partial(nn.BatchNorm, use_running_average=not train, dtype=self.dtype)
for _ in range(self.num_blocks):
x = PredNetBlock(cnn_channels=self.cnn_channels, norm=norm, dtype=self.dtype, kernel_init=self.kernel_init)(x)
x = x.reshape(x.shape[0], -1)
x = nn.Dense(features=self.num_classes, dtype=self.dtype, kernel_init=self.kernel_init)(x)
return x
class PredNet(nn.Module):
backbone: nn.Module
cnn_channels: int
num_blocks_classifier: int
num_classes: int
dtype: dtypedef = jnp.float32
kernel_init: Callable = nn.initializers.glorot_uniform()
def setup(self):
self.classifier = Classifier(self.cnn_channels, self.num_blocks_classifier, self.num_classes, self.dtype, self.kernel_init)
def __call__(self, x, train):
x = self.backbone(x, train)
x = self.classifier(x, train)
return x
def prednet_constructor(model_arch, backbone):
cnn_channels = 128
num_blocks_classifier = int(model_arch[7])
num_classes = 10
return PredNet(backbone, cnn_channels, num_blocks_classifier, num_classes)