File size: 7,764 Bytes
0d2aee9 |
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 |
from typing import Optional
from dataclasses import dataclass
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.modeling_outputs import ModelOutput
from transformers.modeling_utils import PreTrainedModel
from .configuration_fcn4flare import FCN4FlareConfig
class MaskDiceLoss(nn.Module):
r"""
Computes the Mask Dice Loss between the predicted and target tensors.
$$
\text{loss} = 1 - \frac{2 \times \text{intersection} + \epsilon}{\text{predicted} + \text{target} + \epsilon}
$$
Args:
maskdice_threshold (float): Threshold value for the predicted tensor.
Returns:
loss (float): Computed Mask Dice Loss.
"""
def __init__(self, maskdice_threshold):
super().__init__()
self.maskdice_threshold = maskdice_threshold
def forward(self, inputs, targets):
"""
Computes the forward pass of the Mask Dice Loss.
Args:
inputs (torch.Tensor): Predicted tensor.
targets (torch.Tensor): Target tensor.
Returns:
loss (float): Computed Mask Dice Loss.
"""
n = targets.size(0)
smooth = 1e-8
# Apply thresholding to inputs
inputs_act = torch.gt(inputs, self.maskdice_threshold)
inputs_act = inputs_act.long()
inputs = inputs * inputs_act
intersection = inputs * targets
dice_diff = (2 * intersection.sum(1) + smooth) / (inputs.sum(1) + targets.sum(1) + smooth * n)
loss = 1 - dice_diff.mean()
return loss
class NaNMask(nn.Module):
def __init__(self):
super().__init__()
def forward(self, inputs):
# Create a mask where NaNs are marked as 1
nan_mask = torch.isnan(inputs).float()
# Replace NaNs with 0 in the input tensor
inputs = torch.nan_to_num(inputs, nan=0.0)
# Concatenate the input tensor with the NaN mask
return torch.cat([inputs, nan_mask], dim=-1)
class SamePadConv(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size, dilation=1):
super().__init__()
self.receptive_field = (kernel_size - 1) * dilation + 1
padding = self.receptive_field // 2
self.conv = nn.Conv1d(
input_dim, output_dim, kernel_size,
padding=padding,
dilation=dilation
)
self.batchnorm = nn.BatchNorm1d(output_dim)
self.remove = 1 if self.receptive_field % 2 == 0 else 0
def forward(self, x):
x = self.conv(x)
x = self.batchnorm(x)
x = F.gelu(x)
if self.remove > 0:
x = x[:, :, : -self.remove]
return x
class ConvBlock(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size, dilation):
super().__init__()
self.conv1 = SamePadConv(input_dim, output_dim, kernel_size, dilation=dilation)
self.conv2 = SamePadConv(output_dim, output_dim, kernel_size, dilation=dilation)
def forward(self, x):
residual = x
x = self.conv1(x)
x = self.conv2(x)
return x + residual
class Backbone(nn.Module):
def __init__(self, input_dim, dim_list, dilation, kernel_size):
super().__init__()
self.net = nn.Sequential(*[
ConvBlock(
dim_list[i-1] if i > 0 else input_dim,
dim_list[i],
kernel_size=kernel_size,
dilation=dilation[i]
)
for i in range(len(dim_list))
])
def forward(self, x):
return self.net(x)
class LightCurveEncoder(nn.Module):
def __init__(self, input_dim, output_dim, depth, dilation):
super().__init__()
self.mapping = nn.Conv1d(input_dim + 1, output_dim, 1) # +1 for NaN mask
self.backbone = Backbone(
output_dim,
[output_dim] * depth,
dilation,
kernel_size=3
)
self.repr_dropout = nn.Dropout(p=0.1)
def forward(self, x):
x = x.transpose(1, 2) # B x Ci x T
x = self.mapping(x) # B x Ch x T
x = self.backbone(x) # B x Co x T
x = self.repr_dropout(x)
return x
class SegHead(nn.Module):
def __init__(self, input_dim, output_dim):
super().__init__()
self.conv = SamePadConv(input_dim, input_dim, 3)
self.projector = nn.Conv1d(input_dim, output_dim, 1)
def forward(self, x):
# x: B x Ci x T
x = self.conv(x) # B x Ci x T
x = self.projector(x) # B x Co x T
x = x.transpose(1, 2) # B x T x Co
return x
class FCN4FlarePreTrainedModel(PreTrainedModel):
"""
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
"""
config_class = FCN4FlareConfig
base_model_prefix = "fcn4flare"
supports_gradient_checkpointing = True
def _init_weights(self, module):
if isinstance(module, nn.Conv1d):
nn.init.kaiming_normal_(module.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(module, nn.BatchNorm1d):
nn.init.constant_(module.weight, 1)
nn.init.constant_(module.bias, 0)
@dataclass
class FCN4FlareOutput(ModelOutput):
"""
Output type of FCN4Flare.
Args:
loss (`Optional[torch.FloatTensor]` of shape `(1,)`, *optional*):
Mask Dice loss if labels provided, None otherwise.
logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, output_dim)`):
Prediction scores of the model.
hidden_states (`torch.FloatTensor` of shape `(batch_size, hidden_dim, sequence_length)`):
Hidden states from the encoder.
"""
loss: Optional[torch.FloatTensor] = None
logits: torch.FloatTensor = None
hidden_states: torch.FloatTensor = None
class FCN4FlareModel(FCN4FlarePreTrainedModel):
def __init__(self, config: FCN4FlareConfig):
super().__init__(config)
self.nan_mask = NaNMask()
self.encoder = LightCurveEncoder(
config.input_dim,
config.hidden_dim,
config.depth,
config.dilation
)
self.seghead = SegHead(config.hidden_dim, config.output_dim)
# Initialize weights and apply final processing
self.post_init()
def forward(
self,
input_features,
sequence_mask=None,
labels=None,
return_dict=True,
):
# Apply NaN masking
inputs_with_mask = self.nan_mask(input_features)
# Encoder and segmentation head
outputs = self.encoder(inputs_with_mask)
logits = self.seghead(outputs)
# Loss calculation
loss = None
if labels is not None:
loss_fct = MaskDiceLoss(self.config.maskdice_threshold)
logits_sigmoid = torch.sigmoid(logits).squeeze(-1)
if sequence_mask is not None:
# Copy labels and replace padding positions with zeros
labels_for_loss = labels.clone()
labels_for_loss = torch.nan_to_num(labels_for_loss, nan=0.0)
labels_for_loss = labels_for_loss * sequence_mask
logits_sigmoid = logits_sigmoid * sequence_mask
loss = loss_fct(logits_sigmoid, labels_for_loss)
else:
loss = loss_fct(logits_sigmoid, labels)
if not return_dict:
output = (logits,)
return ((loss,) + output) if loss is not None else output
return FCN4FlareOutput(
loss=loss,
logits=logits,
hidden_states=outputs
)
|