File size: 9,837 Bytes
4c27541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Numpy reference forward pass of a ResNet-18 image classifier on uint8 images (model_spec.json describes it).

Requires numpy only. Call ``forward(params, x_uint8)``:

* ``params``: dict of float arrays keyed by parameter name, PyTorch layout (conv weight [out, in, kh, kw]).
* ``x_uint8``: uint8 RGB images of shape [N, *INPUT_SHAPE] (NCHW), already resized and center cropped to
  INPUT_SHAPE (model_spec.json "preprocessing").
* returns float32 logits [N, N_CLASSES]; the prediction is the argmax over axis 1.

Preprocessing inside this file: x / 255, then per channel (x - MEAN[c]) / STD[c]. It has no parameters.

Architecture (fixed; ``expected_shapes()`` lists every parameter, and any other name or shape is refused):

* stem: ``conv1`` (7x7, stride 2, padding 3, no bias, 3 -> 64), batch norm ``bn1``, ReLU, then max pooling over 3x3
  windows with stride 2 and padding 1 (padded positions never win).
* four stages ``layer1`` .. ``layer4`` of BLOCKS_PER_STAGE basic blocks each, with STAGE_CHANNELS channels. The first
  block of ``layer2``, ``layer3`` and ``layer4`` has stride 2; every other convolution has stride 1. A basic block
  ``layerL.i`` computes out = ReLU(bn2(conv2(ReLU(bn1(conv1(x))))) + shortcut(x)), with 3x3 convolutions (padding 1,
  no bias). The shortcut is the identity, except in ``layer2.0``, ``layer3.0`` and ``layer4.0``, where it is
  ``downsample.1(downsample.0(x))``: a 1x1 convolution with stride 2 (no bias) and a batch norm.
* global average pooling over the spatial positions gives the features f [N, 512].
* linear head: logits = f @ ``fc.weight``.T + ``fc.bias``, ``fc.weight`` [N_CLASSES, 512] and ``fc.bias``
  [N_CLASSES].

Convolution is 2-D cross-correlation as in torch.nn.functional.conv2d: out[n, o, i, j] = sum_{c, u, v} weight[o, c, u,
v] * xpad[n, c, i * stride + u, j * stride + v], with xpad zero-padded on each spatial side. Batch norm is in
evaluation mode: (x - running_mean) / sqrt(running_var + BN_EPS) * weight + bias, per channel. Internally the
activations are kept channels-last ([N, H, W, C]) and convolutions use im2col (columns ordered kernel row, kernel
column, input channel). All arithmetic, the preprocessing included, runs in float64 and the logits are cast once to float32. Inputs are processed
in chunks of at most CHUNK images, which bounds the working memory (about 0.6 GB at CHUNK 16 and 224 x 224 inputs).
"""

from __future__ import annotations

import numpy as np

INPUT_SHAPE = (3, 224, 224)
MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float64)
STD = np.array([0.229, 0.224, 0.225], dtype=np.float64)
N_CLASSES = 1000
BN_EPS = 1e-05
STEM_CHANNELS = 64
STAGE_CHANNELS = (64, 128, 256, 512)
STAGE_STRIDES = (1, 2, 2, 2)
BLOCKS_PER_STAGE = 2
BN_FIELDS = ("weight", "bias", "running_mean", "running_var")
CHUNK = 16


def _bn_shapes(name: str, channels: int) -> dict[str, tuple[int, ...]]:
    return {f"{name}.{field}": (channels,) for field in BN_FIELDS}


def _blocks() -> list[tuple[str, int, int, int, bool]]:
    """(name, in channels, out channels, stride, has the projection shortcut) of every basic block in forward order."""
    blocks, channels_in = [], STEM_CHANNELS
    for stage, (channels, stride) in enumerate(zip(STAGE_CHANNELS, STAGE_STRIDES), start=1):
        for index in range(BLOCKS_PER_STAGE):
            block_stride = stride if index == 0 else 1
            projection = block_stride != 1 or channels_in != channels
            blocks.append((f"layer{stage}.{index}", channels_in, channels, block_stride, projection))
            channels_in = channels
    return blocks


def expected_shapes() -> dict[str, tuple[int, ...]]:
    """Every parameter name of the model with its shape, in forward order."""
    shapes: dict[str, tuple[int, ...]] = {"conv1.weight": (STEM_CHANNELS, INPUT_SHAPE[0], 7, 7)}
    shapes.update(_bn_shapes("bn1", STEM_CHANNELS))
    for name, channels_in, channels, _stride, projection in _blocks():
        shapes[f"{name}.conv1.weight"] = (channels, channels_in, 3, 3)
        shapes.update(_bn_shapes(f"{name}.bn1", channels))
        shapes[f"{name}.conv2.weight"] = (channels, channels, 3, 3)
        shapes.update(_bn_shapes(f"{name}.bn2", channels))
        if projection:
            shapes[f"{name}.downsample.0.weight"] = (channels, channels_in, 1, 1)
            shapes.update(_bn_shapes(f"{name}.downsample.1", channels))
    shapes["fc.weight"] = (N_CLASSES, STAGE_CHANNELS[-1])
    shapes["fc.bias"] = (N_CLASSES,)
    return shapes


def _checked(params: dict) -> dict[str, np.ndarray]:
    expected = expected_shapes()
    names = {str(key) for key in params}
    if names != set(expected):
        missing, extra = sorted(set(expected) - names), sorted(names - set(expected))
        raise KeyError(f"parameter names differ from the architecture: missing {missing}, unexpected {extra}")
    tensors = {}
    for name, shape in expected.items():
        value = np.asarray(params[name])
        if not np.issubdtype(value.dtype, np.floating):
            raise TypeError(f"{name} must be a float array, got {value.dtype}")
        if tuple(value.shape) != shape:
            raise ValueError(f"{name} has shape {tuple(value.shape)}, expected {shape}")
        tensors[name] = value.astype(np.float64)
    return tensors


def _conv2d(h: np.ndarray, weight: np.ndarray, stride: int, pad: int) -> np.ndarray:
    """Convolution of channels-last activations h [N, H, W, C] by im2col; returns [N, Ho, Wo, out]."""
    n, height, width, channels = h.shape
    out_channels, in_channels, kh, kw = weight.shape
    if in_channels != channels:
        raise ValueError(f"conv weight {weight.shape} does not fit input {h.shape}")
    padded = np.pad(h, ((0, 0), (pad, pad), (pad, pad), (0, 0))) if pad else h
    h_out = (height + 2 * pad - kh) // stride + 1
    w_out = (width + 2 * pad - kw) // stride + 1
    cols = np.empty((n, h_out, w_out, kh, kw, channels), dtype=np.float64)
    for u in range(kh):
        for v in range(kw):
            cols[:, :, :, u, v, :] = padded[:, u : u + (h_out - 1) * stride + 1 : stride,
                                            v : v + (w_out - 1) * stride + 1 : stride, :]
    matrix = weight.transpose(2, 3, 1, 0).reshape(kh * kw * channels, out_channels)
    out = cols.reshape(n * h_out * w_out, kh * kw * channels) @ matrix
    return out.reshape(n, h_out, w_out, out_channels)


def _bn(h: np.ndarray, tensors: dict[str, np.ndarray], name: str, relu: bool = False) -> np.ndarray:
    """Batch norm in evaluation mode on [N, H, W, C], in place on ``h`` (a fresh convolution output), then ReLU if
    ``relu``."""
    scale = tensors[f"{name}.weight"] / np.sqrt(tensors[f"{name}.running_var"] + BN_EPS)
    shift = tensors[f"{name}.bias"] - tensors[f"{name}.running_mean"] * scale
    h *= scale
    h += shift
    if relu:
        np.maximum(h, 0.0, out=h)
    return h


def _maxpool(h: np.ndarray) -> np.ndarray:
    """3x3 max pooling with stride 2 and padding 1 on [N, H, W, C]."""
    n, height, width, channels = h.shape
    padded = np.pad(h, ((0, 0), (1, 1), (1, 1), (0, 0)), constant_values=-np.inf)
    h_out, w_out = (height - 1) // 2 + 1, (width - 1) // 2 + 1
    out = None
    for u in range(3):
        for v in range(3):
            window = padded[:, u : u + 2 * (h_out - 1) + 1 : 2, v : v + 2 * (w_out - 1) + 1 : 2, :]
            out = window.copy() if out is None else np.maximum(out, window)
    return out


def _normalised(x_uint8: np.ndarray) -> np.ndarray:
    """Channels-last float64 network input [N, H, W, C]: x / 255, then (x - MEAN[c]) / STD[c]."""
    h = np.ascontiguousarray(x_uint8.transpose(0, 2, 3, 1), dtype=np.float64)
    h /= 255.0
    h -= MEAN
    h /= STD
    return h


def _features(tensors: dict[str, np.ndarray], x: np.ndarray) -> np.ndarray:
    h = _normalised(x)
    h = _bn(_conv2d(h, tensors["conv1.weight"], 2, 3), tensors, "bn1", relu=True)
    h = _maxpool(h)
    for name, _channels_in, _channels, stride, projection in _blocks():
        if projection:
            shortcut = _bn(_conv2d(h, tensors[f"{name}.downsample.0.weight"], stride, 0), tensors,
                           f"{name}.downsample.1")
        else:
            shortcut = h
        out = _bn(_conv2d(h, tensors[f"{name}.conv1.weight"], stride, 1), tensors, f"{name}.bn1", relu=True)
        out = _bn(_conv2d(out, tensors[f"{name}.conv2.weight"], 1, 1), tensors, f"{name}.bn2")
        out += shortcut
        h = np.maximum(out, 0.0, out=out)
    return h.mean(axis=(1, 2))


def _images(x_uint8: np.ndarray) -> np.ndarray:
    x = np.asarray(x_uint8)
    if x.dtype != np.uint8:
        raise TypeError(f"images must be uint8, got {x.dtype}")
    if x.ndim != 4 or tuple(x.shape[1:]) != INPUT_SHAPE:
        raise ValueError(f"images must have shape [N, {', '.join(map(str, INPUT_SHAPE))}], got {list(x.shape)}")
    return x


def features(params: dict[str, np.ndarray], x_uint8: np.ndarray) -> np.ndarray:
    """float64 features f [N, 512] (the pooled encoder output, before the head) for uint8 images."""
    x = _images(x_uint8)
    tensors = _checked(params)
    if x.shape[0] == 0:
        return np.zeros((0, STAGE_CHANNELS[-1]), dtype=np.float64)
    return np.concatenate([_features(tensors, x[start : start + CHUNK]) for start in range(0, x.shape[0], CHUNK)])


def forward(params: dict[str, np.ndarray], x_uint8: np.ndarray) -> np.ndarray:
    """Logits float32 [N, N_CLASSES] for uint8 images [N, *INPUT_SHAPE]."""
    x = _images(x_uint8)
    tensors = _checked(params)
    if x.shape[0] == 0:
        return np.zeros((0, N_CLASSES), dtype=np.float32)
    chunks = []
    for start in range(0, x.shape[0], CHUNK):
        f = _features(tensors, x[start : start + CHUNK])
        chunks.append((f @ tensors["fc.weight"].T + tensors["fc.bias"]).astype(np.float32))
    return np.concatenate(chunks, axis=0)