Exemple #1
0
 def __init__(self,
              previous_in_channels,
              out_channels,
              kernel_size,
              sampling_scale,
              conv_type_key='vanilla'):
     super(DecoderX, self).__init__(
         previous_in_channels,
         out_channels,
         kernel_size,
         conv_type=CONV_TYPES[conv_type_key],
         pre_output=AnisotropicUpsample(scale_factor=sampling_scale))
def get_sampler(scale_factor):
    assert isinstance(scale_factor, (int, list, tuple))
    if isinstance(scale_factor, (list, tuple)):
        assert len(scale_factor) == 3
        # we need to make sure that the scale factor conforms with the single value
        # that AnisotropicPool expects
        assert scale_factor[0] == 1
        sampler = AnisotropicUpsample(scale_factor=scale_factor[1])
    else:
        if scale_factor > 0:
            sampler = Upsample(scale_factor=scale_factor)
        else:
            sampler = None
    return sampler
Exemple #3
0
 def __init__(self, in_channels, out_channels, kernel_size, scale_factor=2, conv_type=ConvELU3D):
     assert isinstance(scale_factor, (int, tuple))
     if isinstance(scale_factor, tuple):
         assert len(scale_factor) == 3
         # we need to make sure that the scale factor conforms with the single value
         # that AnisotropicPool expects
         assert scale_factor[0] == 1
         assert scale_factor[1] == scale_factor[2]
         sampler = AnisotropicUpsample(scale_factor=scale_factor[1])
     else:
         sampler = nn.Upsample(scale_factor=scale_factor)
     super(Decoder, self).__init__(in_channels, out_channels, kernel_size,
                                   conv_type=conv_type,
                                   pre_output=sampler)
Exemple #4
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 N=16,
                 final_activation='auto',
                 conv_type_key='vanilla',
                 encoder_type_key='X',
                 decoder_type_key='X',
                 base_type_key='X',
                 return_hypercolumns=False,
                 sampling_scale=3):
        # Set attributes
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.N = N
        self.sampling_scale = sampling_scale
        # Get encoder and decoder types
        Encoder = ENCODERS[encoder_type_key]
        Decoder = DECODERS[decoder_type_key]
        Base = BASES[base_type_key]
        # Build encoders
        encoders = [
            # Hard code the first encoder to vanilla, because this takes the major part of the time
            Encoder([in_channels],
                    N,
                    3,
                    self.sampling_scale,
                    conv_type_key='vanilla'),
            Encoder([in_channels, N],
                    2 * N,
                    3,
                    self.sampling_scale,
                    conv_type_key=conv_type_key),
            Encoder([in_channels, N, 2 * N],
                    4 * N,
                    3,
                    self.sampling_scale,
                    conv_type_key=conv_type_key)
        ]

        # different poolings:
        pooling_scales = [
            self.sampling_scale**(i + 1) for i in range(len(encoders))
        ]

        # Build poolers
        poolers = [
            AnisotropicPool(downscale_factor=scale) for scale in pooling_scales
        ]
        # Build base
        base = Base([in_channels, N, 2 * N, 4 * N],
                    4 * N,
                    3,
                    conv_type_key=conv_type_key)
        # Build upsamplers
        upsamplers = [
            AnisotropicUpsample(scale_factor=scale) for scale in pooling_scales
        ]
        # Build decoders
        decoders = [
            Decoder([in_channels, N, 2 * N, 4 * N, 4 * N],
                    2 * N,
                    3,
                    self.sampling_scale,
                    conv_type_key=conv_type_key),
            Decoder([in_channels, N, 2 * N, 4 * N, 4 * N, 2 * N],
                    N,
                    3,
                    self.sampling_scale,
                    conv_type_key=conv_type_key),
            Decoder([in_channels, N, 2 * N, 4 * N, 4 * N, 2 * N, N],
                    N,
                    3,
                    self.sampling_scale,
                    conv_type_key=conv_type_key)
        ]
        # Build output
        output = Output([in_channels, N, 2 * N, 4 * N, 4 * N, 2 * N, N, N],
                        out_channels, 3)
        # Parse final activation
        if final_activation == 'auto':
            final_activation = nn.Sigmoid()
        assert final_activation != 'Softmax2d'
        assert not return_hypercolumns, "No hypercolumns for 3D networks yet."
        # dundundun
        super(DUNet, self).__init__(encoders=encoders,
                                    poolers=poolers,
                                    base=base,
                                    upsamplers=upsamplers,
                                    decoders=decoders,
                                    output=output,
                                    final_activation=final_activation,
                                    return_hypercolumns=return_hypercolumns)