def build_model():
    l_in = nn.layers.InputLayer((None, nslices, 30) + patch_size)
    l_in_slice_mask = nn.layers.InputLayer((None, nslices))
    l_in_slice_location = nn.layers.InputLayer((None, nslices, 1))
    l_in_sex_age = nn.layers.InputLayer((None, 2))
    l_ins = [l_in, l_in_slice_mask, l_in_slice_location, l_in_sex_age]

    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 30) + patch_size)  # (batch_size*nslices, 30, h,w)
    submodel = subconfig().build_model(l_in_rshp)

    # ------------------ systole
    l_mu0 = submodel.mu_layers[0]
    l_sigma0 = submodel.sigma_layers[0]

    l_mu0 = nn.layers.ReshapeLayer(l_mu0, (-1, nslices, [1]))
    l_sigma0 = nn.layers.ReshapeLayer(l_sigma0, (-1, nslices, [1]))

    l_volume_mu_sigma0 = nn_heart.JeroenLayer([nn.layers.flatten(l_mu0), nn.layers.flatten(l_sigma0),
                                               l_in_slice_mask, nn.layers.flatten(l_in_slice_location)],
                                              trainable_scale=False)

    l_volume_mu0 = nn.layers.SliceLayer(l_volume_mu_sigma0, indices=0, axis=-1)
    l_volume_sigma0 = nn.layers.SliceLayer(l_volume_mu_sigma0, indices=1, axis=-1)

    l_cdf0 = nn_heart.NormalCDFLayer(nn.layers.reshape(l_volume_mu0, ([0], 1)),
                                     nn.layers.reshape(l_volume_sigma0, ([0], 1)))

    # ------------------ diastole
    l_mu1 = submodel.mu_layers[1]
    l_sigma1 = submodel.sigma_layers[1]

    l_mu1 = nn.layers.ReshapeLayer(l_mu1, (-1, nslices, [1]))
    l_sigma1 = nn.layers.ReshapeLayer(l_sigma1, (-1, nslices, [1]))

    l_volume_mu_sigma1 = nn_heart.JeroenLayer([nn.layers.flatten(l_mu1), nn.layers.flatten(l_sigma1),
                                               l_in_slice_mask, nn.layers.flatten(l_in_slice_location)],
                                              trainable_scale=False)

    l_volume_mu1 = nn.layers.SliceLayer(l_volume_mu_sigma1, indices=0, axis=-1)
    l_volume_sigma1 = nn.layers.SliceLayer(l_volume_mu_sigma1, indices=1, axis=-1)

    l_cdf1 = nn_heart.NormalCDFLayer(nn.layers.reshape(l_volume_mu1, ([0], 1)),
                                     nn.layers.reshape(l_volume_sigma1, ([0], 1)))
    l_outs = [l_cdf0, l_cdf1]
    l_top = nn.layers.MergeLayer(l_outs)

    l_target_mu0 = nn.layers.InputLayer((None, 1))
    l_target_mu1 = nn.layers.InputLayer((None, 1))
    l_targets = [l_target_mu0, l_target_mu1]

    train_params = nn.layers.get_all_params(l_top)
    test_layes = [l_volume_mu_sigma0, l_volume_mu_sigma1]

    return namedtuple('Model', ['l_ins', 'l_outs', 'l_targets', 'l_top', 'train_params', 'submodel', 'test_layers'])(
        l_ins, l_outs,
        l_targets,
        l_top,
        train_params,
        submodel, test_layes)
예제 #2
0
def build_model(l_in=None):
    l_in = nn.layers.InputLayer((None, 30) + patch_size) if not l_in else l_in

    l = conv3(l_in, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l_w = nn.layers.DenseLayer(nn.layers.dropout(l), num_units=1024, W=nn.init.Orthogonal("relu"),
                               b=nn.init.Constant(0.1))

    l_d01 = nn.layers.DenseLayer(l, num_units=1024, W=nn.init.Orthogonal("relu"),
                                 b=nn.init.Constant(0.1))

    l_d02 = nn.layers.DenseLayer(nn.layers.dropout(l_d01), num_units=1024, W=nn.init.Orthogonal("relu"),
                                 b=nn.init.Constant(0.1))

    l_d02 = nn_heart.MultLayer(l_d02, l_w)

    mu0 = nn.layers.DenseLayer(nn.layers.dropout(l_d02), num_units=1, W=nn.init.Orthogonal(),
                               b=nn.init.Constant(50), nonlinearity=nn_heart.lb_softplus())
    sigma0 = nn.layers.DenseLayer(nn.layers.dropout(l_d02), num_units=1, W=nn.init.Orthogonal(),
                                  b=nn.init.Constant(10), nonlinearity=nn_heart.lb_softplus())
    l_cdf0 = nn_heart.NormalCDFLayer(mu0, sigma0, sigma_logscale=False, mu_logscale=False)

    # ---------------------------------------------------------------

    l_d11 = nn.layers.DenseLayer(l, num_units=1024, W=nn.init.Orthogonal("relu"),
                                 b=nn.init.Constant(0.1))
    l_d12 = nn.layers.DenseLayer(nn.layers.dropout(l_d11), num_units=1024, W=nn.init.Orthogonal("relu"),
                                 b=nn.init.Constant(0.1))

    l_d12 = nn_heart.MultLayer(l_d12, l_w)

    mu1 = nn.layers.DenseLayer(nn.layers.dropout(l_d12), num_units=1, W=nn.init.Orthogonal(),
                               b=nn.init.Constant(100), nonlinearity=nn_heart.lb_softplus())
    sigma1 = nn.layers.DenseLayer(nn.layers.dropout(l_d12), num_units=1, W=nn.init.Orthogonal(),
                                  b=nn.init.Constant(10), nonlinearity=nn_heart.lb_softplus())
    l_cdf1 = nn_heart.NormalCDFLayer(mu1, sigma1, sigma_logscale=False, mu_logscale=False)

    l_outs = [l_cdf0, l_cdf1]
    l_top = nn.layers.MergeLayer(l_outs)

    l_target_mu0 = nn.layers.InputLayer((None, 1))
    l_target_mu1 = nn.layers.InputLayer((None, 1))
    l_targets = [l_target_mu0, l_target_mu1]
    dense_layers = [l_w, l_d01, l_d02, l_d11, l_d12, mu0, sigma0, mu0, mu1]
    mu_layers = [mu0, mu1]
    sigma_layers = [sigma0, sigma1]

    return namedtuple('Model', ['l_ins', 'l_outs', 'l_targets', 'l_top', 'dense_layers', 'mu_layers', 'sigma_layers'])(
        [l_in], l_outs, l_targets,
        l_top, dense_layers, mu_layers, sigma_layers)
def build_model(l_in=None):
    l_in = nn.layers.InputLayer((None, 30) + patch_size) if not l_in else l_in

    l = conv3(l_in, num_filters=64)
    l = conv3(l, num_filters=64)

    l = max_pool(l)

    l = conv3(l, num_filters=128)
    l = conv3(l, num_filters=128)

    l = max_pool(l)

    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)
    l = conv3(l, num_filters=256)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)
    l = conv3(l, num_filters=512)

    l = max_pool(l)

    l_d01 = nn.layers.DenseLayer(
        l,
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)
    l_d02 = nn.layers.DenseLayer(
        nn.layers.dropout(l_d01, p=0.5),
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)

    mu0 = nn.layers.DenseLayer(nn.layers.dropout(l_d02, p=0.5),
                               num_units=1,
                               W=nn.init.Orthogonal(),
                               b=nn.init.Constant(100),
                               nonlinearity=nn_heart.lb_softplus())
    sigma0 = nn.layers.DenseLayer(nn.layers.dropout(l_d02, p=0.5),
                                  num_units=1,
                                  W=nn.init.Orthogonal(),
                                  b=nn.init.Constant(20),
                                  nonlinearity=nn_heart.lb_softplus())
    l_cdf0 = nn_heart.NormalCDFLayer(mu0, sigma0)

    # ---------------------------------------------------------------

    l_d11 = nn.layers.DenseLayer(
        l,
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)
    l_d12 = nn.layers.DenseLayer(
        nn.layers.dropout(l_d11, p=0.5),
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)

    mu1 = nn.layers.DenseLayer(nn.layers.dropout(l_d12, p=0.5),
                               num_units=1,
                               W=nn.init.Orthogonal(),
                               b=nn.init.Constant(150),
                               nonlinearity=nn_heart.lb_softplus())
    sigma1 = nn.layers.DenseLayer(nn.layers.dropout(l_d12, p=0.5),
                                  num_units=1,
                                  W=nn.init.Orthogonal(),
                                  b=nn.init.Constant(20),
                                  nonlinearity=nn_heart.lb_softplus())
    l_cdf1 = nn_heart.NormalCDFLayer(mu1, sigma1)

    l_outs = [l_cdf0, l_cdf1]
    l_top = nn.layers.MergeLayer(l_outs)

    l_target_mu0 = nn.layers.InputLayer((None, 1))
    l_target_mu1 = nn.layers.InputLayer((None, 1))
    l_targets = [l_target_mu0, l_target_mu1]
    mu_layers = [mu0, mu1]
    sigma_layers = [sigma0, sigma1]

    return namedtuple(
        'Model',
        ['l_ins', 'l_outs', 'l_targets', 'l_top', 'mu_layers', 'sigma_layers'
         ])([l_in], l_outs, l_targets, l_top, mu_layers, sigma_layers)
def build_model(l_in=None):
    stage_depths = [2, 4, 8, 10, 4]
    stage_sizes = [64, 96, 128, 192, 256]

    l_in = nn.layers.InputLayer((None, 30) + patch_size) if not l_in else l_in
    l = l_in
    for i in xrange(len(stage_sizes)):
        # First layer to change dimensions
        l = conv3(l, num_filters=stage_sizes[i])
        for _ in range(stage_depths[i] - 1):
            l = nn_heart.highway_conv3(l)
        l = max_pool(l)

    l_d01 = nn.layers.DenseLayer(
        l,
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)

    l_d02 = nn.layers.DenseLayer(
        nn.layers.dropout(l_d01),
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)

    mu0 = nn.layers.DenseLayer(nn.layers.dropout(l_d02),
                               num_units=1,
                               W=nn.init.Orthogonal(),
                               b=nn.init.Constant(50),
                               nonlinearity=nn_heart.lb_softplus())
    sigma0 = nn.layers.DenseLayer(nn.layers.dropout(l_d02),
                                  num_units=1,
                                  W=nn.init.Orthogonal(),
                                  b=nn.init.Constant(10),
                                  nonlinearity=nn_heart.lb_softplus())
    l_cdf0 = nn_heart.NormalCDFLayer(mu0,
                                     sigma0,
                                     sigma_logscale=False,
                                     mu_logscale=False)

    # ---------------------------------------------------------------

    l_d11 = nn.layers.DenseLayer(
        l,
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)
    l_d12 = nn.layers.DenseLayer(
        nn.layers.dropout(l_d11),
        num_units=512,
        W=nn.init.Orthogonal("relu"),
        b=nn.init.Constant(0.1),
        nonlinearity=nn.nonlinearities.very_leaky_rectify)

    mu1 = nn.layers.DenseLayer(nn.layers.dropout(l_d12),
                               num_units=1,
                               W=nn.init.Orthogonal(),
                               b=nn.init.Constant(100),
                               nonlinearity=nn_heart.lb_softplus())
    sigma1 = nn.layers.DenseLayer(nn.layers.dropout(l_d12),
                                  num_units=1,
                                  W=nn.init.Orthogonal(),
                                  b=nn.init.Constant(10),
                                  nonlinearity=nn_heart.lb_softplus())
    l_cdf1 = nn_heart.NormalCDFLayer(mu1,
                                     sigma1,
                                     sigma_logscale=False,
                                     mu_logscale=False)

    l_outs = [l_cdf0, l_cdf1]
    l_top = nn.layers.MergeLayer(l_outs)

    l_target_mu0 = nn.layers.InputLayer((None, 1))
    l_target_mu1 = nn.layers.InputLayer((None, 1))
    l_targets = [l_target_mu0, l_target_mu1]
    dense_layers = [l_d01, l_d02, l_d11, l_d12, mu0, sigma0, mu0, mu1]
    mu_layers = [mu0, mu1]
    sigma_layers = [sigma0, sigma1]

    return namedtuple('Model', [
        'l_ins', 'l_outs', 'l_targets', 'l_top', 'dense_layers', 'mu_layers',
        'sigma_layers'
    ])([l_in], l_outs, l_targets, l_top, dense_layers, mu_layers, sigma_layers)