def build_model(): #import here, such that our global variables are not overridden! import j6_2ch_128mm, j6_4ch meta_2ch = j6_2ch_128mm.build_model() meta_4ch = j6_4ch.build_model() l_age = nn.layers.InputLayer(data_sizes["sliced:meta:PatientAge"]) l_sex = nn.layers.InputLayer(data_sizes["sliced:meta:PatientSex"]) l_meta_2ch_systole = meta_2ch["meta_outputs"]["systole"] l_meta_2ch_diastole = meta_2ch["meta_outputs"]["diastole"] l_meta_4ch_systole = meta_4ch["meta_outputs"]["systole"] l_meta_4ch_diastole = meta_4ch["meta_outputs"]["diastole"] l_meta_systole = nn.layers.ConcatLayer([l_age, l_sex, l_meta_2ch_systole, l_meta_4ch_systole]) l_meta_diastole = nn.layers.ConcatLayer([l_age, l_sex, l_meta_2ch_diastole, l_meta_4ch_diastole]) ldsys1 = nn.layers.DenseLayer(l_meta_systole, 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) ldsys3 = nn.layers.DenseLayer(ldsys2drop, num_units=600, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.softmax) ldsys3drop = nn.layers.dropout(ldsys3, p=0.5) # dropout at the output might encourage adjacent neurons to correllate ldsys3dropnorm = layers.NormalisationLayer(ldsys3drop) l_systole = layers.CumSumLayer(ldsys3dropnorm) lddia1 = nn.layers.DenseLayer(l_meta_diastole, 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) lddia3 = nn.layers.DenseLayer(lddia2drop, num_units=600, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.softmax) lddia3drop = nn.layers.dropout(lddia3, p=0.5) # dropout at the output might encourage adjacent neurons to correllate lddia3dropnorm = layers.NormalisationLayer(lddia3drop) l_diastole = layers.CumSumLayer(lddia3dropnorm) submodels = [meta_2ch, meta_4ch] return { "inputs": dict({ "sliced:meta:PatientAge": l_age, "sliced:meta:PatientSex": l_sex, }, **{ k: v for d in [model["inputs"] for model in submodels] for k, v in d.items() } ), "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":{ j6_2ch_128mm.__name__: meta_2ch["outputs"], j6_4ch.__name__: meta_4ch["outputs"], }, "cutoff_gradients": [ ] + [ v for d in [model["meta_outputs"] for model in submodels if "meta_outputs" in model] for v in d.values() ] }
def build_model(): #import here, such that our global variables are not overridden! import j6_2ch_128mm, j6_4ch, je_ss_jonisc64small_360 sax_input = nn.layers.InputLayer(data_sizes["sliced:data:sax"]) sax_slices = nn.layers.ReshapeLayer(sax_input, (-1, [2], [3], [4])) meta_sax = je_ss_jonisc64small_360.build_model(input_layer = sax_slices) #reduce the number of parameters BEFORE reshaping! Keep 16 numbers per slice. meta_sax_systole_reduced = nn.layers.DenseLayer(meta_sax["meta_outputs"]["systole"], num_units=16, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify) meta_sax_diastole_reduced = nn.layers.DenseLayer(meta_sax["meta_outputs"]["diastole"], num_units=16, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify) l_sax_systole = nn.layers.ReshapeLayer(meta_sax_systole_reduced, (-1, nr_slices, [1])) l_sax_diastole = nn.layers.ReshapeLayer(meta_sax_diastole_reduced, (-1, nr_slices, [1])) l_sax_systole_flat = nn.layers.DenseLayer(l_sax_systole, num_units=64, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify) l_sax_diastole_flat = nn.layers.DenseLayer(l_sax_diastole, num_units=64, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify) meta_2ch = j6_2ch_128mm.build_model() meta_4ch = j6_4ch.build_model() l_age = nn.layers.InputLayer(data_sizes["sliced:meta:PatientAge"]) l_sex = nn.layers.InputLayer(data_sizes["sliced:meta:PatientSex"]) l_locations = nn.layers.InputLayer(data_sizes["sliced:data:sax:locations"]) l_meta_2ch_systole = nn.layers.DenseLayer(meta_2ch["meta_outputs"]["systole"], num_units=64, W=nn.init.Orthogonal("relu"), 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("relu"), 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("relu"), 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("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.rectify) l_meta_systole = nn.layers.ConcatLayer([l_age, l_sex, l_meta_2ch_systole, l_meta_4ch_systole, l_locations, l_sax_systole_flat]) l_meta_diastole = nn.layers.ConcatLayer([l_age, l_sex, l_meta_2ch_diastole, l_meta_4ch_diastole, l_locations, l_sax_diastole_flat]) ldsys1 = nn.layers.DenseLayer(l_meta_systole, 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) ldsys3 = nn.layers.DenseLayer(ldsys2drop, num_units=600, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.softmax) ldsys3drop = nn.layers.dropout(ldsys3, p=0.5) # dropout at the output might encourage adjacent neurons to correllate ldsys3dropnorm = layers.NormalisationLayer(ldsys3drop) l_systole = layers.CumSumLayer(ldsys3dropnorm) lddia1 = nn.layers.DenseLayer(l_meta_diastole, 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) lddia3 = nn.layers.DenseLayer(lddia2drop, num_units=600, W=nn.init.Orthogonal("relu"), b=nn.init.Constant(0.1), nonlinearity=nn.nonlinearities.softmax) lddia3drop = nn.layers.dropout(lddia3, p=0.5) # dropout at the output might encourage adjacent neurons to correllate lddia3dropnorm = layers.NormalisationLayer(lddia3drop) l_diastole = layers.CumSumLayer(lddia3dropnorm) submodels = [meta_2ch, meta_4ch, meta_sax] return { "inputs": dict({ "sliced:data:sax": sax_input, "sliced:meta:PatientAge": l_age, "sliced:meta:PatientSex": l_sex, "sliced:data:sax:locations": l_locations, }, **{ k: v for d in [model["inputs"] for model in [meta_2ch, meta_4ch]] for k, v in d.items() } ), "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":{ j6_2ch_128mm.__name__: meta_2ch["outputs"], j6_4ch.__name__: meta_4ch["outputs"], je_ss_jonisc64small_360.__name__: meta_sax["outputs"], }, "cutoff_gradients": [ ] + [ v for d in [model["meta_outputs"] for model in submodels if "meta_outputs" in model] for v in d.values() ] }