Repository navigation
Expand file tree
/
Copy pathencoder.py
More file actions
214 lines (177 loc) · 5.91 KB
/
Copy pathencoder.py
File metadata and controls
214 lines (177 loc) · 5.91 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#encoder.py
import jax
import jax.numpy as jnp
import flax.linen as nn
from typing import Tuple, Optional, Dict, Any
from functools import partial
class ConvBNAct(nn.Module):
"""Convolution-BatchNorm-Activation block"""
features: int
kernel_size: Tuple[int, int] = (3, 3)
strides: Tuple[int, int] = (1, 1)
padding: str = 'SAME'
use_bias: bool = False
@nn.compact
def __call__(self, x: jnp.ndarray, training: bool = True) -> jnp.ndarray:
x = nn.Conv(
features=self.features,
kernel_size=self.kernel_size,
strides=self.strides,
padding=self.padding,
use_bias=self.use_bias
)(x)
x = nn.BatchNorm(
use_running_average=not training,
momentum=0.9,
epsilon=1e-5
)(x)
return nn.relu(x)
class SEBlock(nn.Module):
"""Squeeze-and-Excitation block for channel attention"""
features: int
reduction_ratio: int = 4
@nn.compact
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
input_channels = x.shape[-1]
# Global Average Pooling
pooled = jnp.mean(x, axis=(1, 2), keepdims=True)
# Reduction
reduced = nn.Conv(
features=max(1, input_channels // self.reduction_ratio),
kernel_size=(1, 1)
)(pooled)
reduced = nn.relu(reduced)
# Expansion
expanded = nn.Conv(
features=input_channels,
kernel_size=(1, 1)
)(reduced)
weights = nn.sigmoid(expanded)
return x * weights
class InvertedResidual(nn.Module):
"""MobileNetV3 Inverted Residual block"""
features: int
stride: int
expand_ratio: int = 6
use_se: bool = True
@nn.compact
def __call__(self, x: jnp.ndarray, training: bool = True) -> jnp.ndarray:
input_channels = x.shape[-1]
expanded_features = input_channels * self.expand_ratio
# Expansion
if self.expand_ratio != 1:
residual = ConvBNAct(
features=expanded_features,
kernel_size=(1, 1)
)(x, training=training)
else:
residual = x
# Depthwise
residual = nn.Conv(
features=expanded_features,
kernel_size=(3, 3),
strides=(self.stride, self.stride),
padding='SAME',
feature_group_count=expanded_features
)(residual)
residual = nn.BatchNorm(use_running_average=not training)(residual)
residual = nn.relu(residual)
# SE block
if self.use_se:
residual = SEBlock(features=expanded_features)(residual)
# Projection
residual = ConvBNAct(
features=self.features,
kernel_size=(1, 1)
)(residual, training=training)
# Skip connection
if self.stride == 1 and input_channels == self.features:
return x + residual
return residual
class BitmapEncoder(nn.Module):
"""Encodes bitmap images into fixed-dimensional embeddings"""
embed_dim: int = 256
@nn.compact
def __call__(self, bitmap: jnp.ndarray, training: bool = True) -> jnp.ndarray:
# Ensure input has correct shape
if bitmap.ndim == 3:
bitmap = jnp.expand_dims(bitmap, axis=-1)
# Initial conv to get to 3 channels
x = nn.Conv(features=3, kernel_size=(1, 1))(bitmap)
# Early layers adapted for 28x28 input
x = ConvBNAct(
features=16,
kernel_size=(3, 3),
strides=(2, 2)
)(x, training=training)
x = InvertedResidual(
features=24,
stride=2,
expand_ratio=4,
use_se=True
)(x, training=training)
x = InvertedResidual(
features=32,
stride=1,
expand_ratio=4,
use_se=True
)(x, training=training)
# Feature processor
x = ConvBNAct(
features=64,
kernel_size=(1, 1)
)(x, training=training)
x = ConvBNAct(
features=self.embed_dim,
kernel_size=(1, 1)
)(x, training=training)
# Global average pooling
x = jnp.mean(x, axis=(1, 2))
return x
def create_encoder_params(
key: jnp.ndarray,
batch_size: int = 1,
image_size: Tuple[int, int] = (28, 28),
embed_dim: int = 256
) -> Dict[str, Any]:
"""Initialize encoder parameters"""
encoder = BitmapEncoder(embed_dim=embed_dim)
dummy_bitmap = jnp.ones((batch_size, *image_size))
variables = encoder.init(
{'params': key, 'batch_stats': key},
dummy_bitmap,
training=False
)
return variables
def test_encoder():
"""Test the bitmap encoder implementation"""
import jax.random as random
# Create random key
key = random.PRNGKey(0)
key, subkey = random.split(key)
# Create dummy batch
batch_size = 4
image_size = (28, 28)
embed_dim = 256
# Initialize model
encoder = BitmapEncoder(embed_dim=embed_dim)
variables = create_encoder_params(subkey, batch_size, image_size, embed_dim)
# Create dummy batch
dummy_batch = jnp.ones((batch_size, *image_size))
# Forward pass
output = encoder.apply(
variables,
dummy_batch,
training=False,
mutable=['batch_stats']
)
# When training=True, output is a tuple (output, mutated_variables)
# When training=False, output is just the tensor
if isinstance(output, tuple):
output, updated_variables = output
print(f"Input shape: {dummy_batch.shape}")
print(f"Output shape: {output.shape}")
print(f"Output embedding dimension: {output.shape[-1]}")
print("Test successful!")
if __name__ == "__main__":
test_encoder()