def build_model():

    #################
    # Regular model #
    #################
    input_size = data_sizes["sliced:data:sax"]
    input_size_mask = data_sizes["sliced:data:sax:is_not_padded"]
    input_size_locations = data_sizes["sliced:data:sax:locations"]

    l0 = nn.layers.InputLayer(input_size)
    lin_slice_mask = nn.layers.InputLayer(input_size_mask)
    lin_slice_locations = nn.layers.InputLayer(input_size_locations)

    # PREPROCESS SLICES SEPERATELY
    # Convolutional layers and some dense layers are defined in a submodel
    l0_slices = nn.layers.ReshapeLayer(l0, (-1, [2], [3], [4]))

    import je_ss_jonisc80_leaky_convroll_augzoombright
    submodel = je_ss_jonisc80_leaky_convroll_augzoombright.build_model(
        l0_slices)

    # Systole Dense layers
    l_sys_mu = submodel["meta_outputs"]["systole:mu"]
    l_sys_sigma = submodel["meta_outputs"]["systole:sigma"]
    # Diastole Dense layers
    l_dia_mu = submodel["meta_outputs"]["diastole:mu"]
    l_dia_sigma = submodel["meta_outputs"]["diastole:sigma"]

    # AGGREGATE SLICES PER PATIENT
    l_scaled_slice_locations = layers.TrainableScaleLayer(
        lin_slice_locations, scale=nn.init.Constant(0.1), trainable=False)

    # Systole
    l_pat_sys_ss_mu = nn.layers.ReshapeLayer(l_sys_mu, (-1, nr_slices))
    l_pat_sys_ss_sigma = nn.layers.ReshapeLayer(l_sys_sigma, (-1, nr_slices))
    l_pat_sys_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_sys_ss_mu, l_pat_sys_ss_sigma, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=100.)

    l_systole = layers.MuSigmaErfLayer(l_pat_sys_aggr_mu_sigma)

    # Diastole
    l_pat_dia_ss_mu = nn.layers.ReshapeLayer(l_dia_mu, (-1, nr_slices))
    l_pat_dia_ss_sigma = nn.layers.ReshapeLayer(l_dia_sigma, (-1, nr_slices))
    l_pat_dia_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_dia_ss_mu, l_pat_dia_ss_sigma, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=100.)

    l_diastole = layers.MuSigmaErfLayer(l_pat_dia_aggr_mu_sigma)

    submodels = [submodel]
    return {
        "inputs": {
            "sliced:data:sax": l0,
            "sliced:data:sax:is_not_padded": lin_slice_mask,
            "sliced:data:sax:locations": lin_slice_locations,
        },
        "outputs": {
            "systole": l_systole,
            "diastole": l_diastole,
        },
        "regularizable":
        dict({}, **{
            k: v
            for d in [
                model["regularizable"] for model in submodels
                if "regularizable" in model
            ] for k, v in d.items()
        }),
        "pretrained": {
            je_ss_jonisc80_leaky_convroll_augzoombright.__name__:
            submodel["outputs"],
        }
    }
def build_model():

    #################
    # Regular model #
    #################
    input_size = data_sizes["sliced:data:sax"]
    input_size_mask = data_sizes["sliced:data:sax:is_not_padded"]
    input_size_locations = data_sizes["sliced:data:sax:locations"]

    l0 = nn.layers.InputLayer(input_size)
    lin_slice_mask = nn.layers.InputLayer(input_size_mask)
    lin_slice_locations = nn.layers.InputLayer(input_size_locations)

    # PREPROCESS SLICES SEPERATELY
    l0_slices = nn.layers.ReshapeLayer(l0, (batch_size * nr_slices, 30, patch_px, patch_px)) # (bxs, t, i, j)
    subsample_factor = 2
    l0_slices_subsampled = nn.layers.SliceLayer(l0_slices, axis=1, indices=slice(0, 30, subsample_factor))
    nr_frames_subsampled = 30 / subsample_factor

    # PREPROCESS FRAMES SEPERATELY
    l0_frames = nn.layers.ReshapeLayer(l0_slices_subsampled, (batch_size * nr_slices * nr_frames_subsampled, 1, patch_px, patch_px))  # (bxsxt, 1, i, j)

    l1a = nn.layers.dnn.Conv2DDNNLayer(l0_frames,  W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=16, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l1b = nn.layers.dnn.Conv2DDNNLayer(l1a, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=16, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l1c = nn.layers.dnn.Conv2DDNNLayer(l1b, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=16, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l1 = nn.layers.dnn.MaxPool2DDNNLayer(l1c, pool_size=(2,2), stride=(2,2))

    l2a = nn.layers.dnn.Conv2DDNNLayer(l1,  W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=32, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l2b = nn.layers.dnn.Conv2DDNNLayer(l2a, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=32, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l2c = nn.layers.dnn.Conv2DDNNLayer(l2b, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=32, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l2 = nn.layers.dnn.MaxPool2DDNNLayer(l2c, pool_size=(2,2), stride=(2,2))

    l3a = nn.layers.dnn.Conv2DDNNLayer(l2,  W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=64, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l3b = nn.layers.dnn.Conv2DDNNLayer(l3a, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=64, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l3c = nn.layers.dnn.Conv2DDNNLayer(l3b, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=64, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l3d = nn.layers.dnn.Conv2DDNNLayer(l3c, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=64, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l3 = nn.layers.dnn.MaxPool2DDNNLayer(l3d, pool_size=(2,2), stride=(2,2))

    l4a = nn.layers.dnn.Conv2DDNNLayer(l3,  W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l4b = nn.layers.dnn.Conv2DDNNLayer(l4a, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l4c = nn.layers.dnn.Conv2DDNNLayer(l4b, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l4d = nn.layers.dnn.Conv2DDNNLayer(l4c, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l4 = nn.layers.dnn.MaxPool2DDNNLayer(l4d, pool_size=(2,2), stride=(2,2))

    l5a = nn.layers.dnn.Conv2DDNNLayer(l4,  W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l5b = nn.layers.dnn.Conv2DDNNLayer(l5a, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l5c = nn.layers.dnn.Conv2DDNNLayer(l5b, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l5d = nn.layers.dnn.Conv2DDNNLayer(l5c, W=nn.init.Orthogonal("relu"), filter_size=(3,3), num_filters=128, stride=(1,1), pad="same", nonlinearity=nn.nonlinearities.rectify)
    l5 = nn.layers.dnn.MaxPool2DDNNLayer(l5d, pool_size=(2,2), stride=(2,2))

    l5drop = nn.layers.dropout(l5, p=0.5)
    ld1 = nn.layers.DenseLayer(l5drop, num_units=256, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify)

    ld1drop = nn.layers.dropout(ld1, p=0.5)
    ld2 = nn.layers.DenseLayer(ld1drop, num_units=256, W=nn.init.Orthogonal("relu"),b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify)

    ld2drop = nn.layers.dropout(ld2, p=0.5)

    ld3mu = nn.layers.DenseLayer(ld2drop, num_units=1, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(15.0), nonlinearity=None)
    ld3sigma = nn.layers.DenseLayer(ld2drop, num_units=1, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(5.0), nonlinearity=lb_softplus(.3))
    ld3musigma = nn.layers.ConcatLayer([ld3mu, ld3sigma], axis=1)

    # Go back to a per slice model
    l_slices_musigma = nn.layers.ReshapeLayer(ld3musigma, (batch_size * nr_slices, nr_frames_subsampled, 2))  # (bxs, t, 2)
    l_slices_musigma_sys = layers.ArgmaxAndMaxLayer(l_slices_musigma, mode='min')  # (bxs, 2)
    l_slices_musigma_dia = layers.ArgmaxAndMaxLayer(l_slices_musigma, mode='max')  # (bxs, 2)

    # AGGREGATE SLICES PER PATIENT
    l_scaled_slice_locations = layers.TrainableScaleLayer(lin_slice_locations, scale=nn.init.Constant(0.1), trainable=False)

    # Systole
    l_pat_sys_ss_musigma = nn.layers.ReshapeLayer(l_slices_musigma_sys, (batch_size, nr_slices, 2))
    l_pat_sys_ss_mu = nn.layers.SliceLayer(l_pat_sys_ss_musigma, indices=0, axis=-1)
    l_pat_sys_ss_sigma = nn.layers.SliceLayer(l_pat_sys_ss_musigma, indices=1, axis=-1)
    l_pat_sys_aggr_mu_sigma = layers.JeroenLayer([l_pat_sys_ss_mu, l_pat_sys_ss_sigma, lin_slice_mask, l_scaled_slice_locations], rescale_input=1.)

    l_systole = layers.MuSigmaErfLayer(l_pat_sys_aggr_mu_sigma)

    # Diastole
    l_pat_dia_ss_musigma = nn.layers.ReshapeLayer(l_slices_musigma_dia, (batch_size, nr_slices, 2))
    l_pat_dia_ss_mu = nn.layers.SliceLayer(l_pat_dia_ss_musigma, indices=0, axis=-1)
    l_pat_dia_ss_sigma = nn.layers.SliceLayer(l_pat_dia_ss_musigma, indices=1, axis=-1)
    l_pat_dia_aggr_mu_sigma = layers.JeroenLayer([l_pat_dia_ss_mu, l_pat_dia_ss_sigma, lin_slice_mask, l_scaled_slice_locations], rescale_input=1.)

    l_diastole = layers.MuSigmaErfLayer(l_pat_dia_aggr_mu_sigma)


    return {
        "inputs":{
            "sliced:data:sax": l0,
            "sliced:data:sax:is_not_padded": lin_slice_mask,
            "sliced:data:sax:locations": lin_slice_locations,
        },
        "outputs": {
            "systole": l_systole,
            "diastole": l_diastole,
        },
        "regularizable": {
        },
    }
예제 #3
0
def build_model():

    #################
    # Regular model #
    #################
    input_size = data_sizes["sliced:data:sax"]
    input_size_mask = data_sizes["sliced:data:sax:is_not_padded"]
    input_size_locations = data_sizes["sliced:data:sax:locations"]

    l0 = nn.layers.InputLayer(input_size)
    lin_slice_mask = nn.layers.InputLayer(input_size_mask)
    lin_slice_locations = nn.layers.InputLayer(input_size_locations)

    # PREPROCESS SLICES SEPERATELY
    # Convolutional layers and some dense layers are defined in a submodel
    l0_slices = nn.layers.ReshapeLayer(l0, (-1, [2], [3], [4]))

    l1a = nn.layers.dnn.Conv2DDNNLayer(l0_slices,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=64,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l1b = nn.layers.dnn.Conv2DDNNLayer(l1a,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=64,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l1 = nn.layers.dnn.MaxPool2DDNNLayer(l1b, pool_size=(2, 2), stride=(2, 2))

    l2a = nn.layers.dnn.Conv2DDNNLayer(l1,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=128,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l2b = nn.layers.dnn.Conv2DDNNLayer(l2a,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=128,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l2 = nn.layers.dnn.MaxPool2DDNNLayer(l2b, pool_size=(2, 2), stride=(2, 2))

    l3a = nn.layers.dnn.Conv2DDNNLayer(l2,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=256,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l3b = nn.layers.dnn.Conv2DDNNLayer(l3a,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=256,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l3c = nn.layers.dnn.Conv2DDNNLayer(l3b,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=256,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l3 = nn.layers.dnn.MaxPool2DDNNLayer(l3c, pool_size=(2, 2), stride=(2, 2))

    l4a = nn.layers.dnn.Conv2DDNNLayer(l3,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=512,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l4b = nn.layers.dnn.Conv2DDNNLayer(l4a,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=512,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l4c = nn.layers.dnn.Conv2DDNNLayer(l4b,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=512,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l4 = nn.layers.dnn.MaxPool2DDNNLayer(l4c, pool_size=(2, 2), stride=(2, 2))

    l5a = nn.layers.dnn.Conv2DDNNLayer(l4,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=512,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l5b = nn.layers.dnn.Conv2DDNNLayer(l5a,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=512,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l5c = nn.layers.dnn.Conv2DDNNLayer(l5b,
                                       W=nn.init.Orthogonal("relu"),
                                       filter_size=(3, 3),
                                       num_filters=512,
                                       stride=(1, 1),
                                       pad="same",
                                       nonlinearity=nn.nonlinearities.rectify)
    l5 = nn.layers.dnn.MaxPool2DDNNLayer(l5c, pool_size=(2, 2), stride=(2, 2))

    # Systole Dense layers
    ldsys1 = nn.layers.DenseLayer(l5,
                                  num_units=512,
                                  W=nn.init.Orthogonal("relu"),
                                  b=nn.init.Constant(0.1),
                                  nonlinearity=nn.nonlinearities.rectify)

    ldsys1drop = nn.layers.dropout(ldsys1, p=0.5)
    ldsys2 = nn.layers.DenseLayer(ldsys1drop,
                                  num_units=512,
                                  W=nn.init.Orthogonal("relu"),
                                  b=nn.init.Constant(0.1),
                                  nonlinearity=nn.nonlinearities.rectify)

    ldsys2drop = nn.layers.dropout(ldsys2, p=0.5)
    l_sys_mu = nn.layers.DenseLayer(ldsys2drop,
                                    num_units=1,
                                    W=nn.init.Orthogonal("relu"),
                                    b=nn.init.Constant(10.0),
                                    nonlinearity=None)
    l_sys_sigma = nn.layers.DenseLayer(ldsys2drop,
                                       num_units=1,
                                       W=nn.init.Orthogonal("relu"),
                                       b=nn.init.Constant(3.),
                                       nonlinearity=lb_softplus(.1))

    # Diastole Dense layers
    lddia1 = nn.layers.DenseLayer(l5,
                                  num_units=512,
                                  W=nn.init.Orthogonal("relu"),
                                  b=nn.init.Constant(0.1),
                                  nonlinearity=nn.nonlinearities.rectify)

    lddia1drop = nn.layers.dropout(lddia1, p=0.5)
    lddia2 = nn.layers.DenseLayer(lddia1drop,
                                  num_units=512,
                                  W=nn.init.Orthogonal("relu"),
                                  b=nn.init.Constant(0.1),
                                  nonlinearity=nn.nonlinearities.rectify)

    lddia2drop = nn.layers.dropout(lddia2, p=0.5)
    l_dia_mu = nn.layers.DenseLayer(lddia2drop,
                                    num_units=1,
                                    W=nn.init.Orthogonal("relu"),
                                    b=nn.init.Constant(10.0),
                                    nonlinearity=None)
    l_dia_sigma = nn.layers.DenseLayer(lddia2drop,
                                       num_units=1,
                                       W=nn.init.Orthogonal("relu"),
                                       b=nn.init.Constant(3.),
                                       nonlinearity=lb_softplus(.1))

    # AGGREGATE SLICES PER PATIENT
    l_scaled_slice_locations = layers.TrainableScaleLayer(
        lin_slice_locations, scale=nn.init.Constant(0.1), trainable=False)

    # Systole
    l_pat_sys_ss_mu = nn.layers.ReshapeLayer(l_sys_mu, (-1, nr_slices))
    l_pat_sys_ss_sigma = nn.layers.ReshapeLayer(l_sys_sigma, (-1, nr_slices))
    l_pat_sys_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_sys_ss_mu, l_pat_sys_ss_sigma, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=1.)

    l_systole = layers.MuSigmaErfLayer(l_pat_sys_aggr_mu_sigma)

    # Diastole
    l_pat_dia_ss_mu = nn.layers.ReshapeLayer(l_dia_mu, (-1, nr_slices))
    l_pat_dia_ss_sigma = nn.layers.ReshapeLayer(l_dia_sigma, (-1, nr_slices))
    l_pat_dia_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_dia_ss_mu, l_pat_dia_ss_sigma, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=1.)

    l_diastole = layers.MuSigmaErfLayer(l_pat_dia_aggr_mu_sigma)

    return {
        "inputs": {
            "sliced:data:sax": l0,
            "sliced:data:sax:is_not_padded": lin_slice_mask,
            "sliced:data:sax:locations": lin_slice_locations,
        },
        "outputs": {
            "systole": l_systole,
            "diastole": l_diastole,
        },
        "regularizable": {
            ldsys1: l2_weight,
            ldsys2: l2_weight,
            l_sys_mu: l2_weight_out,
            l_sys_sigma: l2_weight_out,
            lddia1: l2_weight,
            lddia2: l2_weight,
            l_dia_mu: l2_weight_out,
            l_dia_sigma: l2_weight_out,
        },
    }
예제 #4
0
def build_model():

    #################
    # Regular model #
    #################
    input_size = data_sizes["sliced:data:sax"]
    input_size_mask = data_sizes["sliced:data:sax:is_not_padded"]
    input_size_locations = data_sizes["sliced:data:sax:locations"]

    l0 = nn.layers.InputLayer(input_size)
    lin_slice_mask = nn.layers.InputLayer(input_size_mask)
    lin_slice_locations = nn.layers.InputLayer(input_size_locations)

    # PREPROCESS SLICES SEPERATELY
    l0_slices = nn.layers.ReshapeLayer(
        l0, (batch_size * nr_slices, 30, patch_px, patch_px))  # (bxs, t, i, j)
    subsample_factor = 2
    l0_slices_subsampled = nn.layers.SliceLayer(l0_slices,
                                                axis=1,
                                                indices=slice(
                                                    0, 30, subsample_factor))
    nr_frames_subsampled = 30 / subsample_factor

    # PREPROCESS FRAMES SEPERATELY
    l0_frames = nn.layers.ReshapeLayer(
        l0_slices_subsampled, (batch_size * nr_slices * nr_frames_subsampled,
                               1, patch_px, patch_px))  # (bxsxt, 1, i, j)

    # downsample
    downsample = lambda incoming: nn.layers.dnn.Pool2DDNNLayer(
        incoming, pool_size=(2, 2), stride=(2, 2), mode='average_inc_pad')
    upsample = lambda incoming: nn.layers.Upscale2DLayer(incoming,
                                                         scale_factor=2)
    l0_frames_d0 = l0_frames
    l0_frames_d1 = downsample(l0_frames_d0)
    l0_frames_d2 = downsample(l0_frames_d1)
    l0_frames_d3 = downsample(l0_frames_d2)

    ld3a = nn.layers.dnn.Conv2DDNNLayer(l0_frames_d3,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(3, 3),
                                        num_filters=16,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld3b = nn.layers.dnn.Conv2DDNNLayer(ld3a,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(3, 3),
                                        num_filters=16,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld3c = nn.layers.dnn.Conv2DDNNLayer(ld3b,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(3, 3),
                                        num_filters=16,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld3o = nn.layers.dnn.Conv2DDNNLayer(ld3c,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(3, 3),
                                        num_filters=16,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)

    ld2i = nn.layers.ConcatLayer([l0_frames_d2, upsample(ld3o)], axis=1)
    ld2a = nn.layers.dnn.Conv2DDNNLayer(ld2i,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld2b = nn.layers.dnn.Conv2DDNNLayer(ld2a,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld2c = nn.layers.dnn.Conv2DDNNLayer(ld2b,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld2d = nn.layers.dnn.Conv2DDNNLayer(ld2c,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld2o = nn.layers.dnn.Conv2DDNNLayer(ld2d,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)

    ld1i = nn.layers.ConcatLayer([l0_frames_d1, upsample(ld2o)], axis=1)
    ld1a = nn.layers.dnn.Conv2DDNNLayer(ld1i,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld1b = nn.layers.dnn.Conv2DDNNLayer(ld1a,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld1c = nn.layers.dnn.Conv2DDNNLayer(ld1b,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld1d = nn.layers.dnn.Conv2DDNNLayer(ld1c,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld1o = nn.layers.dnn.Conv2DDNNLayer(ld1d,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)

    ld0i = nn.layers.ConcatLayer([l0_frames_d0, upsample(ld1o)], axis=1)
    ld0a = nn.layers.dnn.Conv2DDNNLayer(ld0i,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld0b = nn.layers.dnn.Conv2DDNNLayer(ld0a,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld0c = nn.layers.dnn.Conv2DDNNLayer(ld0b,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld0d = nn.layers.dnn.Conv2DDNNLayer(ld0c,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=32,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.rectify)
    ld0o = nn.layers.dnn.Conv2DDNNLayer(ld0d,
                                        W=nn.init.Orthogonal("relu"),
                                        filter_size=(5, 5),
                                        num_filters=1,
                                        stride=(1, 1),
                                        pad="same",
                                        nonlinearity=nn.nonlinearities.sigmoid)
    ld0r = nn.layers.ReshapeLayer(
        ld0o,
        (batch_size * nr_slices * nr_frames_subsampled, patch_px, patch_px))

    l_frames_musigma = layers.IntegrateAreaLayer(ld0r,
                                                 sigma_mode='scale',
                                                 sigma_scale=.1)
    area_per_pixel_cm = (float(patch_mm) / float(patch_px))**2 / 100.0
    l_frames_musigma_cm = layers.TrainableScaleLayer(
        l_frames_musigma,
        scale=nn.init.Constant(area_per_pixel_cm),
        trainable=False)

    # Go back to a per slice model
    l_slices_musigma_cm = nn.layers.ReshapeLayer(
        l_frames_musigma_cm,
        (batch_size * nr_slices, nr_frames_subsampled, 2))  # (bxs, t, 2)
    l_slices_musigma_cm_sys = layers.ArgmaxAndMaxLayer(l_slices_musigma_cm,
                                                       mode='min')  # (bxs, 2)
    l_slices_musigma_cm_dia = layers.ArgmaxAndMaxLayer(l_slices_musigma_cm,
                                                       mode='max')  # (bxs, 2)
    l_slices_musigma_cm_avg = layers.ArgmaxAndMaxLayer(l_slices_musigma_cm,
                                                       mode='mean')

    # AGGREGATE SLICES PER PATIENT
    l_scaled_slice_locations = layers.TrainableScaleLayer(
        lin_slice_locations, scale=nn.init.Constant(0.1), trainable=False)

    # Systole
    l_pat_sys_ss_musigma_cm = nn.layers.ReshapeLayer(
        l_slices_musigma_cm_sys, (batch_size, nr_slices, 2))
    l_pat_sys_ss_mu_cm = nn.layers.SliceLayer(l_pat_sys_ss_musigma_cm,
                                              indices=0,
                                              axis=-1)
    l_pat_sys_ss_sigma_cm = nn.layers.SliceLayer(l_pat_sys_ss_musigma_cm,
                                                 indices=1,
                                                 axis=-1)
    l_pat_sys_ss_sigma_cm = nn.layers.TrainableScaleLayer(
        l_pat_sys_ss_sigma_cm)
    l_pat_sys_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_sys_ss_mu_cm, l_pat_sys_ss_sigma_cm, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=1.)

    l_systole = layers.MuSigmaErfLayer(l_pat_sys_aggr_mu_sigma)

    # Diastole
    l_pat_dia_ss_musigma_cm = nn.layers.ReshapeLayer(
        l_slices_musigma_cm_dia, (batch_size, nr_slices, 2))
    l_pat_dia_ss_mu_cm = nn.layers.SliceLayer(l_pat_dia_ss_musigma_cm,
                                              indices=0,
                                              axis=-1)
    l_pat_dia_ss_sigma_cm = nn.layers.SliceLayer(l_pat_dia_ss_musigma_cm,
                                                 indices=1,
                                                 axis=-1)
    l_pat_dia_ss_sigma_cm = nn.layers.TrainableScaleLayer(
        l_pat_dia_ss_sigma_cm)
    l_pat_dia_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_dia_ss_mu_cm, l_pat_dia_ss_sigma_cm, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=1.)

    l_diastole = layers.MuSigmaErfLayer(l_pat_dia_aggr_mu_sigma)

    # Average
    l_pat_avg_ss_musigma_cm = nn.layers.ReshapeLayer(
        l_slices_musigma_cm_avg, (batch_size, nr_slices, 2))
    l_pat_avg_ss_mu_cm = nn.layers.SliceLayer(l_pat_avg_ss_musigma_cm,
                                              indices=0,
                                              axis=-1)
    l_pat_avg_ss_sigma_cm = nn.layers.SliceLayer(l_pat_avg_ss_musigma_cm,
                                                 indices=1,
                                                 axis=-1)
    l_pat_avg_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_avg_ss_mu_cm, l_pat_avg_ss_sigma_cm, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=1.)

    l_mean = layers.MuSigmaErfLayer(l_pat_avg_aggr_mu_sigma)

    return {
        "inputs": {
            "sliced:data:sax": l0,
            "sliced:data:sax:is_not_padded": lin_slice_mask,
            "sliced:data:sax:locations": lin_slice_locations,
        },
        "outputs": {
            "systole": l_systole,
            "diastole": l_diastole,
            "average": l_mean,
        },
        "regularizable": {},
    }
예제 #5
0
def build_model():

    import j6_2ch_gauss, j6_4ch_gauss
    meta_2ch = j6_2ch_gauss.build_model()
    meta_4ch = j6_4ch_gauss.build_model()

    l_meta_2ch_systole = nn.layers.DenseLayer(
        meta_2ch["meta_outputs"]["systole"],
        num_units=64,
        W=nn.init.Orthogonal(),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.rectify)
    l_meta_2ch_diastole = nn.layers.DenseLayer(
        meta_2ch["meta_outputs"]["diastole"],
        num_units=64,
        W=nn.init.Orthogonal(),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.rectify)

    l_meta_4ch_systole = nn.layers.DenseLayer(
        meta_4ch["meta_outputs"]["systole"],
        num_units=64,
        W=nn.init.Orthogonal(),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.rectify)
    l_meta_4ch_diastole = nn.layers.DenseLayer(
        meta_4ch["meta_outputs"]["diastole"],
        num_units=64,
        W=nn.init.Orthogonal(),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.rectify)

    #################
    # Regular model #
    #################
    input_size = data_sizes["sliced:data:sax"]
    input_size_mask = data_sizes["sliced:data:sax:is_not_padded"]
    input_size_locations = data_sizes["sliced:data:sax:locations"]

    l0 = nn.layers.InputLayer(input_size)
    lin_slice_mask = nn.layers.InputLayer(input_size_mask)
    lin_slice_locations = nn.layers.InputLayer(input_size_locations)

    # PREPROCESS SLICES SEPERATELY
    # Convolutional layers and some dense layers are defined in a submodel
    l0_slices = nn.layers.ReshapeLayer(l0, (-1, [2], [3], [4]))

    import je_ss_jonisc64small_360_gauss_longer
    submodel = je_ss_jonisc64small_360_gauss_longer.build_model(l0_slices)

    # Systole Dense layers
    l_sys_mu = submodel["meta_outputs"]["systole:mu"]
    l_sys_sigma = submodel["meta_outputs"]["systole:sigma"]
    l_sys_meta = submodel["meta_outputs"]["systole"]
    # Diastole Dense layers
    l_dia_mu = submodel["meta_outputs"]["diastole:mu"]
    l_dia_sigma = submodel["meta_outputs"]["diastole:sigma"]
    l_dia_meta = submodel["meta_outputs"]["diastole"]

    # AGGREGATE SLICES PER PATIENT
    l_scaled_slice_locations = layers.TrainableScaleLayer(
        lin_slice_locations, scale=nn.init.Constant(0.1), trainable=False)

    # Systole
    l_pat_sys_ss_mu = nn.layers.ReshapeLayer(l_sys_mu, (-1, nr_slices))
    l_pat_sys_ss_sigma = nn.layers.ReshapeLayer(l_sys_sigma, (-1, nr_slices))
    l_pat_sys_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_sys_ss_mu, l_pat_sys_ss_sigma, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=100.)

    l_systole = layers.MuSigmaErfLayer(l_pat_sys_aggr_mu_sigma)

    l_sys_meta = nn.layers.DenseLayer(nn.layers.ReshapeLayer(
        l_sys_meta, (-1, nr_slices, 512)),
                                      num_units=64,
                                      W=nn.init.Orthogonal(),
                                      b=nn.init.Constant(0.1),
                                      nonlinearity=nn.nonlinearities.rectify)

    l_meta_systole = nn.layers.ConcatLayer(
        [l_meta_2ch_systole, l_meta_4ch_systole, l_sys_meta])
    l_weights = nn.layers.DenseLayer(l_meta_systole,
                                     num_units=512,
                                     W=nn.init.Orthogonal(),
                                     b=nn.init.Constant(0.1),
                                     nonlinearity=nn.nonlinearities.rectify)
    l_weights = nn.layers.DenseLayer(l_weights,
                                     num_units=3,
                                     W=nn.init.Orthogonal(),
                                     b=nn.init.Constant(0.1),
                                     nonlinearity=nn.nonlinearities.rectify)
    systole_output = layers.WeightedMeanLayer(l_weights, [
        l_systole, meta_2ch["outputs"]["systole"],
        meta_4ch["outputs"]["systole"]
    ])

    # Diastole
    l_pat_dia_ss_mu = nn.layers.ReshapeLayer(l_dia_mu, (-1, nr_slices))
    l_pat_dia_ss_sigma = nn.layers.ReshapeLayer(l_dia_sigma, (-1, nr_slices))
    l_pat_dia_aggr_mu_sigma = layers.JeroenLayer([
        l_pat_dia_ss_mu, l_pat_dia_ss_sigma, lin_slice_mask,
        l_scaled_slice_locations
    ],
                                                 rescale_input=100.)

    l_diastole = layers.MuSigmaErfLayer(l_pat_dia_aggr_mu_sigma)

    l_dia_meta = nn.layers.DenseLayer(nn.layers.ReshapeLayer(
        l_dia_meta, (-1, nr_slices, 512)),
                                      num_units=64,
                                      W=nn.init.Orthogonal(),
                                      b=nn.init.Constant(0.1),
                                      nonlinearity=nn.nonlinearities.rectify)

    l_meta_diastole = nn.layers.ConcatLayer(
        [l_meta_2ch_diastole, l_meta_4ch_diastole, l_dia_meta])
    l_weights = nn.layers.DenseLayer(l_meta_diastole,
                                     num_units=512,
                                     W=nn.init.Orthogonal(),
                                     b=nn.init.Constant(0.1),
                                     nonlinearity=nn.nonlinearities.rectify)
    l_weights = nn.layers.DenseLayer(l_weights,
                                     num_units=3,
                                     W=nn.init.Orthogonal(),
                                     b=nn.init.Constant(0.1),
                                     nonlinearity=nn.nonlinearities.identity)
    diastole_output = layers.WeightedMeanLayer(l_weights, [
        l_diastole, meta_2ch["outputs"]["diastole"],
        meta_4ch["outputs"]["diastole"]
    ])

    submodels = [submodel, meta_2ch, meta_4ch]

    return {
        "inputs":
        dict(
            {
                "sliced:data:sax": l0,
                "sliced:data:sax:is_not_padded": lin_slice_mask,
                "sliced:data:sax:locations": lin_slice_locations,
            }, **{
                k: v
                for d in [model["inputs"] for model in [meta_2ch, meta_4ch]]
                for k, v in d.items()
            }),
        "outputs": {
            "systole": systole_output,
            "diastole": diastole_output,
        },
        "regularizable":
        dict({}, **{
            k: v
            for d in [
                model["regularizable"] for model in submodels
                if "regularizable" in model
            ] for k, v in d.items()
        }),
        "pretrained": {
            je_ss_jonisc64small_360_gauss_longer.__name__: submodel["outputs"],
            j6_2ch_gauss.__name__: meta_2ch["outputs"],
            j6_4ch_gauss.__name__: meta_4ch["outputs"],
        },
        #"cutoff_gradients": [
        #] + [ v for d in [model["meta_outputs"] for model in [meta_2ch, meta_4ch] if "meta_outputs" in model]
        #       for v in d.values() ]
    }