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
The DualEncoder architecture processes 3D medical images using two distinct encoders (for phantom/patient inputs) and a shared decoder. Here's a structured visualization:
Input (3D Volume) → [Split by source_flags] → Phantom Encoder / Patient Encoder → Shared Decoder → Output (Reconstructed 3D Volume)
Input (6 channels)
↓
ConvBlock(6 → 32) → MaxPool3d(2) → ConvBlock(32 → 64) → MaxPool3d(2) → ConvBlock(64 → 128)
Latent (128 channels)
↓
Upsample(2) → ConvBlock(128 → 64) → Upsample(2) → ConvBlock(64 → 32) → Conv3d(32 → 3)
[B, 6, D, H, W]).encoder_phantom.encoder_patient.[B, 128, D/4, H/4, W/4].[B, 3, D, H, W].Phantom Input (0) → [ConvBlock] → [MaxPool] → [ConvBlock] → [MaxPool] → [ConvBlock] →
↘
Patient Input (1) → [ConvBlock] → [MaxPool] → [ConvBlock] → [MaxPool] → [ConvBlock] → [Decoder] → Output
Each encoder path is independent, and the decoder merges their latent representations during training.