File size: 9,658 Bytes
c319d57 |
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import TransformerEncoder, TransformerEncoderLayer
from transformers import PreTrainedModel
from configuration_neuroclr import NeuroCLRConfig
# --------------------------
# SSL Encoder (per-ROI)
# --------------------------
class NeuroCLR(nn.Module):
def __init__(self, config: NeuroCLRConfig):
super().__init__()
encoder_layer = TransformerEncoderLayer(
d_model=config.TSlength,
dim_feedforward=2 * config.TSlength,
nhead=config.nhead,
batch_first=True,
)
self.transformer_encoder = TransformerEncoder(encoder_layer, config.nlayer)
self.projector = nn.Sequential(
nn.Linear(config.TSlength, config.projector_out1),
nn.BatchNorm1d(config.projector_out1),
nn.ReLU(),
nn.Linear(config.projector_out1, config.projector_out2),
)
self.normalize_input = config.normalize_input
self.pooling = config.pooling
self.TSlength = config.TSlength
def forward(self, x):
# x: [B, 1, 128]
if self.normalize_input:
x = F.normalize(x, dim=-1)
x = self.transformer_encoder(x) # [B, 1, 128]
if self.pooling == "flatten":
h = x.reshape(x.shape[0], -1) # [B, 128]
elif self.pooling == "mean":
h = x.mean(dim=1)
elif self.pooling == "last":
h = x[:, -1, :]
else:
raise ValueError(f"Unknown pooling='{self.pooling}'")
if h.shape[1] != self.TSlength:
raise ValueError(f"h dim {h.shape[1]} != TSlength {self.TSlength}")
z = self.projector(h)
return h, z
# --------------------------
# Your ResNet1D head (verbatim)
# --------------------------
class MyConv1dPadSame(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, groups=1):
super().__init__()
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride, groups=groups)
self.kernel_size = kernel_size
self.stride = stride
def forward(self, x):
in_dim = x.shape[-1]
out_dim = (in_dim + self.stride - 1) // self.stride
p = max(0, (out_dim - 1) * self.stride + self.kernel_size - in_dim)
pad_left = p // 2
pad_right = p - pad_left
x = F.pad(x, (pad_left, pad_right), "constant", 0)
return self.conv(x)
class MyMaxPool1dPadSame(nn.Module):
def __init__(self, kernel_size):
super().__init__()
self.kernel_size = kernel_size
self.stride = 1
self.max_pool = nn.MaxPool1d(kernel_size=kernel_size)
def forward(self, x):
in_dim = x.shape[-1]
out_dim = (in_dim + self.stride - 1) // self.stride
p = max(0, (out_dim - 1) * self.stride + self.kernel_size - in_dim)
pad_left = p // 2
pad_right = p - pad_left
x = F.pad(x, (pad_left, pad_right), "constant", 0)
return self.max_pool(x)
class BasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, groups, downsample, use_bn, use_do, is_first_block=False):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.downsample = downsample
self.use_bn = use_bn
self.use_do = use_do
self.is_first_block = is_first_block
conv_stride = stride if downsample else 1
self.bn1 = nn.BatchNorm1d(in_channels)
self.relu1 = nn.ReLU()
self.do1 = nn.Dropout(p=0.75)
self.conv1 = MyConv1dPadSame(in_channels, out_channels, kernel_size, stride=conv_stride, groups=groups)
self.bn2 = nn.BatchNorm1d(out_channels)
self.relu2 = nn.ReLU()
self.do2 = nn.Dropout(p=0.75)
self.conv2 = MyConv1dPadSame(out_channels, out_channels, kernel_size, stride=1, groups=groups)
self.max_pool = MyMaxPool1dPadSame(kernel_size=conv_stride)
def forward(self, x):
identity = x
out = x
if not self.is_first_block:
if self.use_bn:
out = self.bn1(out)
out = self.relu1(out)
if self.use_do:
out = self.do1(out)
out = self.conv1(out)
if self.use_bn:
out = self.bn2(out)
out = self.relu2(out)
if self.use_do:
out = self.do2(out)
out = self.conv2(out)
if self.downsample:
identity = self.max_pool(identity)
if self.out_channels != self.in_channels:
identity = identity.transpose(-1, -2)
ch1 = (self.out_channels - self.in_channels) // 2
ch2 = self.out_channels - self.in_channels - ch1
identity = F.pad(identity, (ch1, ch2), "constant", 0)
identity = identity.transpose(-1, -2)
out += identity
return out
class ResNet1D(nn.Module):
def __init__(
self,
in_channels,
base_filters,
kernel_size,
stride,
groups,
n_block,
n_classes,
downsample_gap=2,
increasefilter_gap=4,
use_bn=True,
use_do=True,
verbose=False
):
super().__init__()
self.verbose = verbose
self.n_block = n_block
self.kernel_size = kernel_size
self.stride = stride
self.groups = groups
self.use_bn = use_bn
self.use_do = use_do
self.downsample_gap = downsample_gap
self.increasefilter_gap = increasefilter_gap
self.first_block_conv = MyConv1dPadSame(in_channels, base_filters, kernel_size=self.kernel_size, stride=1)
self.first_block_bn = nn.BatchNorm1d(base_filters)
self.first_block_relu = nn.ReLU()
out_channels = base_filters
self.basicblock_list = nn.ModuleList()
for i_block in range(self.n_block):
is_first_block = (i_block == 0)
downsample = (i_block % self.downsample_gap == 1)
if is_first_block:
in_ch = base_filters
out_ch = in_ch
else:
in_ch = int(base_filters * 2 ** ((i_block - 1) // self.increasefilter_gap))
if (i_block % self.increasefilter_gap == 0) and (i_block != 0):
out_ch = in_ch * 2
else:
out_ch = in_ch
block = BasicBlock(
in_channels=in_ch,
out_channels=out_ch,
kernel_size=self.kernel_size,
stride=self.stride,
groups=self.groups,
downsample=downsample,
use_bn=self.use_bn,
use_do=self.use_do,
is_first_block=is_first_block,
)
self.basicblock_list.append(block)
out_channels = out_ch
self.final_bn = nn.BatchNorm1d(out_channels)
self.final_relu = nn.ReLU(inplace=True)
self.dense = nn.Linear(out_channels, n_classes)
def forward(self, x):
out = self.first_block_conv(x)
if self.use_bn:
out = self.first_block_bn(out)
out = self.first_block_relu(out)
for block in self.basicblock_list:
out = block(out)
if self.use_bn:
out = self.final_bn(out)
out = self.final_relu(out)
out = out.mean(-1)
out = self.dense(out)
return out
# --------------------------
# HF model: encoder + ResNet1D head
# --------------------------
class NeuroCLRForSequenceClassification(PreTrainedModel):
"""
Expected input x: [B, 200, 128]
- runs encoder per ROI: [B,1,128] -> h_r [B,128]
- stacks into H: [B,200,128]
- feeds ResNet1D: [B,200,128] -> logits
"""
config_class = NeuroCLRConfig
base_model_prefix = "neuroclr"
def __init__(self, config: NeuroCLRConfig):
super().__init__(config)
self.encoder = NeuroCLR(config)
# Freeze the encoder
for p in self.encoder.parameters():
p.requires_grad = False
self.head = ResNet1D(
in_channels=config.n_rois,
base_filters=config.base_filters,
kernel_size=config.kernel_size,
stride=config.stride,
groups=config.groups,
n_block=config.n_block,
n_classes=config.num_labels,
downsample_gap=config.downsample_gap,
increasefilter_gap=config.increasefilter_gap,
use_bn=config.use_bn,
use_do=config.use_do,
)
self.post_init()
def forward(self, x: torch.Tensor, labels: torch.Tensor = None, **kwargs):
# x: [B, 200, 128]
if x.ndim != 3 or x.shape[1] != self.config.n_rois or x.shape[2] != self.config.TSlength:
raise ValueError(
f"Expected x shape [B,{self.config.n_rois},{self.config.TSlength}] but got {tuple(x.shape)}"
)
B, R, L = x.shape
# Encode each ROI independently (ROI-wise SSL)
hs = []
for r in range(R):
xr = x[:, r, :].unsqueeze(1) # [B,1,128]
with torch.no_grad():
h, _ = self.encoder(xr)
# h, _ = self.encoder(xr) # h: [B,128]
hs.append(h.unsqueeze(1)) # [B,1,128]
H = torch.cat(hs, dim=1) # [B,200,128]
logits = self.head(H) # head expects [B,200,128]
loss = None
if labels is not None:
loss = nn.CrossEntropyLoss()(logits, labels)
return {"loss": loss, "logits": logits}
|