def build(conf, name='Enc_Anatomy'):
    """
    Build a UNet based encoder to extract anatomical information from the image.
    """
    spatial_encoder = UNet(conf)
    spatial_encoder.input = Input(shape=conf.input_shape)
    l1_down = spatial_encoder.unet_downsample(
        spatial_encoder.input, spatial_encoder.normalise)  # downsample
    spatial_encoder.unet_bottleneck(l1_down,
                                    spatial_encoder.normalise)  # bottleneck
    l2_up = spatial_encoder.unet_upsample(
        spatial_encoder.bottleneck, spatial_encoder.normalise)  # upsample

    anatomy = Conv2D(conf.out_channels,
                     1,
                     padding='same',
                     activation='softmax',
                     name='conv_anatomy')(l2_up)
    if conf.rounding:
        anatomy = Rounding()(anatomy)

    model = Model(inputs=spatial_encoder.input, outputs=anatomy, name=name)
    log.info('Enc_Anatomy')
    model.summary(print_fn=log.info)
    return model
    def build(self, conf):
        # build encoder1
        encoder1 = UNet(conf)
        encoder1.input = Input(shape=conf.input_shape)
        l1 = encoder1.unet_downsample(encoder1.input, encoder1.normalise)

        # build encoder2
        encoder2 = UNet(conf)
        encoder2.input = Input(shape=conf.input_shape)
        l2 = encoder2.unet_downsample(encoder2.input, encoder2.normalise)

        self.build_decoder(conf)

        d1_l3 = encoder1.d_l3 if conf.downsample > 3 else None
        d2_l3 = encoder2.d_l3 if conf.downsample > 3 else None
        anatomy_output1 = self.evaluate_decoder(conf, l1, d1_l3, encoder1.d_l2,
                                                encoder1.d_l1, encoder1.d_l0)
        anatomy_output2 = self.evaluate_decoder(conf, l2, d2_l3, encoder2.d_l2,
                                                encoder2.d_l1, encoder2.d_l0)

        # build shared layer
        shr_lay4 = Conv2D(conf.out_channels,
                          1,
                          padding='same',
                          activation='softmax',
                          name='conv_anatomy')

        # connect models
        encoder1_output = shr_lay4(anatomy_output1)
        encoder2_output = shr_lay4(anatomy_output2)

        if conf.rounding:
            encoder1_output = Rounding()(encoder1_output)
            encoder2_output = Rounding()(encoder2_output)

        encoder1 = Model(inputs=encoder1.input,
                         outputs=encoder1_output,
                         name='Enc_Anatomy_%s' % self.modalities[0])
        encoder2 = Model(inputs=encoder2.input,
                         outputs=encoder2_output,
                         name='Enc_Anatomy_%s' % self.modalities[1])

        return [encoder1, encoder2]
예제 #3
0
    def build_anatomy_encoder(self):
        """
        Build an encoder to extract anatomical information from the image.
        """
        # Manually build UNet to add Rounding as a last layer
        spatial_encoder = UNet(self.conf.anatomy_encoder_params)
        spatial_encoder.input = Input(shape=self.conf.input_shape)
        l1 = spatial_encoder.unet_downsample(spatial_encoder.input,
                                             spatial_encoder.normalise)
        spatial_encoder.unet_bottleneck(l1, spatial_encoder.normalise)
        l2 = spatial_encoder.unet_upsample(spatial_encoder.bottleneck,
                                           spatial_encoder.normalise)
        anatomy = spatial_encoder.out(l2, out_activ='softmax')
        if self.conf.rounding == 'encoder':
            anatomy = Rounding()(anatomy)

        self.Enc_Anatomy = Model(inputs=spatial_encoder.input,
                                 outputs=anatomy,
                                 name='Enc_Anatomy')
        log.info('Enc_Anatomy')
        self.Enc_Anatomy.summary(print_fn=log.info)