Visualize this architecture: class DualEncoder(torch.nn.Module): def init(self,…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Visualize this architecture:
class DualEncoder(torch.nn.Module): def init(self, in_channels=6, out_channels=3, base_channels=32, norm_type="batch"): """ norm_type: "batch" (BatchNorm3d) lub "instance" (InstanceNorm3d) """ super(DualEncoder, self).init() assert norm_type in ("batch", "instance"), "norm_type must be 'batch' or 'instance'" self.norm_type = norm_type
self.encoder_phantom = self.build_encoder(in_channels, base_channels)
self.encoder_patient = self.build_encoder(in_channels, base_channels)
self.decoder = self.build_decoder(out_channels, base_channels)
def build_encoder(self, in_channels, base_channels):
return torch.nn.Sequential(
self.conv_block(in_channels, base_channels),
torch.nn.MaxPool3d(2),
self.conv_block(base_channels, base_channels * 2),
torch.nn.MaxPool3d(2),
self.conv_block(base_channels * 2, base_channels * 4)
)
def build_decoder(self, out_channels, base_channels):
return torch.nn.Sequential(
torch.nn.Upsample(scale_factor=2, mode='trilinear', align_corners=False),
self.conv_block(base_channels * 4, base_channels * 2),
torch.nn.Upsample(scale_factor=2, mode='trilinear', align_corners=False),
self.conv_block(base_channels * 2, base_channels),
torch.nn.Conv3d(base_channels, out_channels, kernel_size=1)
)
def get_norm_layer(self, num_features):
if self.norm_type == "batch":
return torch.nn.BatchNorm3d(num_features)
elif self.norm_type == "instance":
return torch.nn.InstanceNorm3d(num_features, affine=True)
def conv_block(self, in_ch, out_ch):
return torch.nn.Sequential(
torch.nn.Conv3d(in_ch, out_ch, kernel_size=3, padding=1),
self.get_norm_layer(out_ch),
torch.nn.ReLU(inplace=True),
torch.nn.Conv3d(out_ch, out_ch, kernel_size=3, padding=1),
self.get_norm_layer(out_ch),
torch.nn.ReLU(inplace=True)
)
def forward(self, x, source_flags):
latent = torch.zeros(
(x.shape[0], 128, x.shape[2] // 4, x.shape[3] // 4, x.shape[4] // 4),
device=x.device
)
phantom_idx = (source_flags == 0).nonzero(as_tuple=True)[0]
patient_idx = (source_flags == 1).nonzero(as_tuple=True)[0]
latents = torch.zeros_like(latent)
if phantom_idx.numel() > 0:
latent[phantom_idx] = self.encoder_phantom(x[phantom_idx])
latents[phantom_idx] = latent[phantom_idx]
if patient_idx.numel() > 0:
latent[patient_idx] = self.encoder_patient(x[patient_idx])
latents[patient_idx] = latent[patient_idx]
output = self.decoder(latent)
output = torch.nn.functional.interpolate(
output, size=x.shape[2:], mode="trilinear", align_corners=False
)
return output, latents
Input (6 channels, 3D volume)
|
|---- source_flags determines path ----
| |
↓ ↓
Phantom Encoder Patient Encoder
| |
↓ ↓
[Conv3D → Norm → ReLU → Conv3D → Norm → ReLU] (32 channels)
| |
↓ ↓
MaxPool3D (2×) MaxPool3D (2×)
| |
↓ ↓
[Conv3D → Norm → ReLU → Conv3D → Norm → ReLU] (64 channels)
| |
↓ ↓
MaxPool3D (2×) MaxPool3D (2×)
| |
↓ ↓
[Conv3D → Norm → ReLU → Conv3D → Norm → ReLU] (128 channels)
| |
\-----------------+-----------------/
↓
Latent Space
(128 channels)
|
↓
Decoder
|
↓
Upsample (2×)
|
↓
[Conv3D → Norm → ReLU → Conv3D → Norm → ReLU] (64 channels)
|
↓
Upsample (2×)
|
↓
[Conv3D → Norm → ReLU → Conv3D → Norm → ReLU] (32 channels)
|
↓
Conv3D (1×1)
|
↓
Output (3 channels, same size as input)
Would you like me to explain or break down the code?