def climate_test_1_3d(args={"sigma": 0.}): conv = InputLayer((None, 16, 8, 96, 96)) conv = GaussianNoiseLayer(conv, sigma=args["sigma"]) conv = dnn.Conv3DDNNLayer(conv, num_filters=128, filter_size=(4, 5, 5), stride=(1, 2, 2), nonlinearity=rectify) conv = dnn.Conv3DDNNLayer(conv, num_filters=256, filter_size=(3, 5, 5), stride=(1, 2, 2), nonlinearity=rectify) conv = dnn.Conv3DDNNLayer(conv, num_filters=512, filter_size=(3, 5, 5), stride=(1, 2, 2), nonlinearity=rectify) if "bottleneck" in args: conv = DenseLayer(conv, num_units=args["bottleneck"], nonlinearity=rectify) l_out = conv print_network(l_out) final_out, decoder_layers, encoder_layers = get_encoder_and_decoder(l_out) ladder = [] for a, b in zip(decoder_layers, encoder_layers[::-1]): ladder += [a, b] return final_out, ladder
def full_image_net_1_3d_v3b(args): conv = InputLayer((None, 16, 8, 768, 1152)) conv = GaussianNoiseLayer(conv, sigma=args["sigma"]) encoder_layers = [] conv = dnn.Conv3DDNNLayer(conv, num_filters=196, filter_size=(3, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) conv = dnn.Conv3DDNNLayer(conv, num_filters=256, filter_size=(3, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) #conv = dnn.Conv3DDNNLayer(conv, num_filters=512, filter_size=(3,5,5), stride=(1,2,2)); encoder_layers.append(conv) #conv = dnn.Conv3DDNNLayer(conv, num_filters=1024, filter_size=(2,5,5), stride=(1,2,2)); encoder_layers.append(conv) decoder_layers = [] for layer in get_all_layers(conv)[::-1]: if isinstance(layer, InputLayer): break conv = InverseLayer(conv, layer) decoder_layers.append(conv) l_out = conv # bug: get_encoder_and_decoder doesn't work for this #final_out, decoder_layers, encoder_layers = get_encoder_and_decoder(l_out) ladder = [] for a, b in zip(decoder_layers, encoder_layers[::-1]): ladder += [a, b] print_network(l_out) return l_out, ladder
def build_model(): l_in = nn.layers.InputLayer((None, 1,) + p_transform['patch_size']) l_target = nn.layers.InputLayer((None, 1)) l = dnn.Conv3DDNNLayer(l_in, num_filters=64, filter_size=3, pad='valid', W=nn.init.Orthogonal(), b=nn.init.Constant(0.01), nonlinearity=nn.nonlinearities.very_leaky_rectify) l = inrn_v2_red(l) l = inrn_v2(l) l = inrn_v2_red(l) l = inrn_v2(l) l = inrn_v2_red(l) l = inrn_v2_red(l) l = dense(drop(l), 128) l_out = nn.layers.DenseLayer(l, num_units=10, W=lasagne.init.Orthogonal(), b=lasagne.init.Constant(0.1), nonlinearity=nn.nonlinearities.softmax) return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
def build_model(): l_in = nn.layers.InputLayer((None, n_candidates_per_patient, 1,) + p_transform['patch_size']) l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size']) l_target = nn.layers.InputLayer((batch_size,)) l = dnn.Conv3DDNNLayer(l_in_rshp, filter_size=3, num_filters=64, pad='valid', W=nn.init.Orthogonal(), nonlinearity=nn.nonlinearities.very_leaky_rectify) print 'l1', l.output_shape l = inrn_v2_red(l) print 'l2', l.output_shape l = inrn_v2(l) print 'l3', l.output_shape l = inrn_v2_red(l) print 'l4', l.output_shape l = inrn_v2(l) l = inrn_v2_red(l) l = inrn_v2_red(l) l = dense(drop(l), 256) l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(), nonlinearity=None) l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1)) l_out = nn_lung.AggAllBenignExp(l) return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
def build_model(): l_in = nn.layers.InputLayer((None, 1,) + p_transform['patch_size']) l_target = nn.layers.InputLayer((None, 1,) + p_transform['patch_size']) net = {} base_n_filters = 64 net['contr_1_1'] = conv_prelu_layer(l_in, base_n_filters) net['contr_1_2'] = conv_prelu_layer(net['contr_1_1'], base_n_filters) net['contr_1_3'] = conv_prelu_layer(net['contr_1_2'], base_n_filters) net['pool1'] = max_pool3d(net['contr_1_3']) net['encode_1'] = conv_prelu_layer(net['pool1'], base_n_filters) net['encode_2'] = conv_prelu_layer(net['encode_1'], base_n_filters) net['encode_3'] = conv_prelu_layer(net['encode_2'], base_n_filters) net['encode_4'] = conv_prelu_layer(net['encode_3'], base_n_filters) net['upscale1'] = nn.layers.Upscale3DLayer(net['encode_4'], 2) net['concat1'] = nn.layers.ConcatLayer([net['upscale1'], net['contr_1_3']], cropping=(None, None, "center", "center", "center")) net['expand_1_1'] = conv_prelu_layer(net['concat1'], base_n_filters) net['expand_1_2'] = conv_prelu_layer(net['expand_1_1'], base_n_filters) net['expand_1_3'] = conv_prelu_layer(net['expand_1_2'], base_n_filters) net['expand_1_4'] = conv_prelu_layer(net['expand_1_3'], base_n_filters) net['expand_1_5'] = conv_prelu_layer(net['expand_1_4'], base_n_filters) l_out = dnn.Conv3DDNNLayer(net['expand_1_5'], num_filters=1, filter_size=1, nonlinearity=nn.nonlinearities.sigmoid) return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
def build_model(image_size=(IMAGE_SIZE, IMAGE_SIZE, IMAGE_SIZE)): l_in = lasagne.layers.InputLayer(shape=(None, ) + image_size) l0 = lasagne.layers.DimshuffleLayer(l_in, pattern=[0, 'x', 1, 2, 3]) net = {} base_n_filters = 64 net['contr_1_1'] = conv3d(l0, base_n_filters) net['contr_1_1'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_1']) net['contr_1_2'] = conv3d(net['contr_1_1'], base_n_filters) net['contr_1_2'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_2']) net['contr_1_3'] = conv3d(net['contr_1_2'], base_n_filters) net['contr_1_3'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_3']) net['pool1'] = max_pool3d(net['contr_1_3']) net['encode_1'] = conv3d(net['pool1'], base_n_filters) net['encode_1'] = lasagne.layers.ParametricRectifierLayer(net['encode_1']) net['encode_2'] = conv3d(net['encode_1'], base_n_filters) net['encode_2'] = lasagne.layers.ParametricRectifierLayer(net['encode_2']) net['encode_3'] = conv3d(net['encode_2'], base_n_filters) net['encode_3'] = lasagne.layers.ParametricRectifierLayer(net['encode_3']) net['encode_4'] = conv3d(net['encode_3'], base_n_filters) net['encode_4'] = lasagne.layers.ParametricRectifierLayer(net['encode_4']) net['upscale1'] = lasagne.layers.Upscale3DLayer(net['encode_4'], 2) net['concat1'] = lasagne.layers.ConcatLayer( [net['upscale1'], net['contr_1_3']], cropping=(None, None, "center", "center", "center")) net['expand_1_1'] = conv3d(net['concat1'], 2 * base_n_filters) net['expand_1_1'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_1']) net['expand_1_2'] = conv3d(net['expand_1_1'], 2 * base_n_filters) net['expand_1_2'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_2']) net['expand_1_3'] = conv3d(net['expand_1_2'], base_n_filters) net['expand_1_3'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_3']) net['output_segmentation'] = dnn.Conv3DDNNLayer( net['expand_1_3'], num_filters=1, filter_size=1, nonlinearity=lasagne.nonlinearities.sigmoid) l_out = lasagne.layers.SliceLayer(net['output_segmentation'], indices=0, axis=1) return { "inputs": { "luna:3d": l_in, }, "outputs": { "predicted_segmentation": l_out }, }
def build_model(patch_size=None): patch_size = p_transform['patch_size'] if patch_size is None else patch_size l_in = nn.layers.InputLayer(( None, 1, ) + patch_size) l_target = nn.layers.InputLayer(( None, 1, ) + patch_size) net = {} base_n_filters = 128 net['contr_1_1'] = conv3d(l_in, base_n_filters) net['contr_1_1'] = nn.layers.ParametricRectifierLayer(net['contr_1_1']) net['contr_1_2'] = conv3d(net['contr_1_1'], base_n_filters) net['contr_1_2'] = nn.layers.ParametricRectifierLayer(net['contr_1_2']) net['contr_1_3'] = conv3d(net['contr_1_2'], base_n_filters) net['contr_1_3'] = nn.layers.ParametricRectifierLayer(net['contr_1_3']) net['pool1'] = max_pool3d(net['contr_1_3']) net['encode_1'] = conv3d(net['pool1'], base_n_filters) net['encode_1'] = nn.layers.ParametricRectifierLayer(net['encode_1']) net['encode_2'] = conv3d(net['encode_1'], base_n_filters) net['encode_2'] = nn.layers.ParametricRectifierLayer(net['encode_2']) net['encode_3'] = conv3d(net['encode_2'], base_n_filters) net['encode_3'] = nn.layers.ParametricRectifierLayer(net['encode_3']) net['encode_4'] = conv3d(net['encode_3'], base_n_filters) net['encode_4'] = nn.layers.ParametricRectifierLayer(net['encode_4']) net['dropout_1'] = nn.layers.DropoutLayer(net['encode_4']) net['upscale1'] = nn.layers.Upscale3DLayer(net['dropout_1'], 2) net['concat1'] = nn.layers.ConcatLayer([net['upscale1'], net['contr_1_3']], cropping=(None, None, "center", "center", "center")) net['expand_1_1'] = conv3d(net['concat1'], 2 * base_n_filters) net['expand_1_1'] = nn.layers.ParametricRectifierLayer(net['expand_1_1']) net['expand_1_2'] = conv3d(net['expand_1_1'], 2 * base_n_filters) net['expand_1_2'] = nn.layers.ParametricRectifierLayer(net['expand_1_2']) net['expand_1_3'] = conv3d(net['expand_1_2'], base_n_filters) net['expand_1_3'] = nn.layers.ParametricRectifierLayer(net['expand_1_3']) l_out = dnn.Conv3DDNNLayer(net['expand_1_3'], num_filters=1, filter_size=1, nonlinearity=nn.nonlinearities.sigmoid) return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
def build_model(): l_in = lasagne.layers.InputLayer(shape=(None, IMAGE_SIZE, IMAGE_SIZE, IMAGE_SIZE)) l0 = lasagne.layers.DimshuffleLayer(l_in, pattern=[0, 'x', 1, 2, 3]) net = {} base_n_filters = 256 net['contr_1_1'] = conv3d(l0, base_n_filters) net['contr_1_2'] = conv3d(net['contr_1_1'], base_n_filters) net['pool1'] = max_pool3d(net['contr_1_2']) net['contr_2_1'] = conv3d(net['pool1'], base_n_filters * 2) net['contr_2_2'] = conv3d(net['contr_2_1'], base_n_filters * 2) net['expand_3_1'] = conv3d(net['contr_2_2'], base_n_filters * 2) net['expand_3_2'] = conv3d(net['expand_3_1'], base_n_filters * 2) net['upscale4'] = Upscale3DLayer(net['expand_3_2'], 2) net['concat4'] = lasagne.layers.ConcatLayer( [net['upscale4'], net['contr_1_2']], cropping=(None, None, "center", "center", "center")) net['expand_4_1'] = conv3d(net['concat4'], base_n_filters) net['expand_4_2'] = conv3d(net['expand_4_1'], base_n_filters) net['output_segmentation'] = dnn.Conv3DDNNLayer( net['expand_4_2'], num_filters=1, filter_size=1, W=lasagne.init.Constant(0), b=None, nonlinearity=lasagne.nonlinearities.sigmoid) l_out = lasagne.layers.SliceLayer(net['output_segmentation'], indices=0, axis=1) return { "inputs": { "luna:3d": l_in, }, "outputs": { "predicted_segmentation": l_out }, }
def build_layers(input_var, nk): '''nk: network_kwargs''' '''conv, extra_convs, pool multiple times then fc with dropout, fc with dropout and softmax then reshape''' '''total number of conv layers is num_convpool * (1 + num_extra_conv)''' filter_dim = nk['filter_dim'] num_layers = nk['num_layers'] filters_list = [128, 256, 512, 768, 1024, 1280] conv = lasagne.layers.InputLayer(shape=nk['input_shape']) for i in range(num_layers): num_filters = int(nk["filters_scale"] * filters_list[i]) if nk["im_dim"] == 3: conv = dnn.Conv3DDNNLayer(conv, num_filters=num_filters, filter_size=(3,5,5),pad=(1,2,2), stride=(1,2,2)) else: #print nk["filters_scale"] #print num_filters conv = Conv2DLayer(conv, num_filters=num_filters, filter_size=nk['filter_dim'], pad=nk['filter_dim'] / 2, stride=2, W=HeNormal(), nonlinearity=LeakyRectify(0.1)) encoder = conv hid_fmap = conv if nk["yolo_batch_norm"]: encoder = BatchNormLayer(encoder) if nk["im_dim"] == 3: #encoder = FeaturePoolLayer(encoder, pool_size=640, axis=1) box_conf = dnn.Conv3DDNNLayer(encoder, num_filters=2, filter_size=(3,3,3), stride=(2,1,1), pad=(1,1,1), nonlinearity=softmax4D) #box_conf = NonlinearityLayer(box_conf, softmax4D) class_conf = dnn.Conv3DDNNLayer(encoder, num_filters=nk['num_classes'], filter_size=(3,3,3), stride=(2,1,1), pad=(1,1,1), nonlinearity=softmax4D) #class_conf = NonlinearityLayer(class_conf, softmax4D) coord_net = dnn.Conv3DDNNLayer(encoder, num_filters=4, filter_size=(3,3,3), stride=(2,1,1), pad=(1,1,1), W=nk['w_init'], nonlinearity=rectify) #outputs a batch_size x 10 x 4 x 12 x 18 bbox_reg = ConcatLayer([coord_net,box_conf, class_conf]) #print get_output_shape(bbox_reg,input_shapes=(1,16,8,768,1152)) s = get_output_shape(bbox_reg) # reshape to be like the 2D case -> batch_size*time_steps(4) x 10 x 12 x 18 net = ExpressionLayer(bbox_reg, function=lambda g: g.transpose((0,2,1,3,4)), output_shape=(nk["batch_size"],s[2], s[1],s[3], s[4])) #print get_output_shape(net,input_shapes=(1,16,8,768,1152)) # after transpose s = get_output_shape(net) bbox_reg = ReshapeLayer(net, shape=(nk["batch_size"],s[1], s[2],s[3], s[4])) #print get_output_shape(bbox_reg,input_shapes=(1,16,8,768,1152)) else: box_conf = Conv2DLayer(encoder, num_filters=2, filter_size=3, pad=1, nonlinearity=linear) box_conf = NonlinearityLayer(box_conf, softmax3D) class_conf = Conv2DLayer(encoder, num_filters=nk['num_classes'], filter_size=3, pad=1, nonlinearity=linear) class_conf = NonlinearityLayer(class_conf, softmax3D) coord_net = Conv2DLayer(encoder, num_filters=4, filter_size=3,pad=1, W=nk['w_init'], nonlinearity=rectify) bbox_reg = ConcatLayer([coord_net,box_conf, class_conf]) for layer in get_all_layers(conv)[::-1]: if nk["batch_norm"]: conv = batch_norm(conv) if isinstance(layer, InputLayer): break conv = InverseLayer(conv, layer) return {'yolo': bbox_reg, 'ae':conv, 'hid_fmap':hid_fmap}#, "decoder":decoder_layers}
def getmodel(X1, X2, X3, X4, seg, p_drop_hidden, inputsize, inputsize3D, nrofinputs, classes): #67x67 orthogonal input segnetwork1 = lasagne.layers.InputLayer(shape=(None, nrofinputs, inputsize, inputsize), input_var=X1) segnetwork2 = lasagne.layers.InputLayer(shape=(None, nrofinputs, inputsize, inputsize), input_var=X2) segnetwork3 = lasagne.layers.InputLayer(shape=(None, nrofinputs, inputsize, inputsize), input_var=X3) #25x25x25 input segnetwork4 = lasagne.layers.InputLayer(shape=(None, nrofinputs, inputsize3D, inputsize3D, inputsize3D), input_var=X4) segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(2, 2), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(2, 2), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(2, 2), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(4, 4), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(4, 4), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(4, 4), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(8, 8), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(8, 8), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(8, 8), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(16, 16), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(16, 16), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(16, 16), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape segnetwork1 = lasagne.layers.DilatedConv2DLayer( segnetwork1, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork1 = lasagne.layers.batch_norm(segnetwork1) segnetwork2 = lasagne.layers.DilatedConv2DLayer( segnetwork2, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork2 = lasagne.layers.batch_norm(segnetwork2) segnetwork3 = lasagne.layers.DilatedConv2DLayer( segnetwork3, num_filters=32, filter_size=(3, 3), dilation=(1, 1), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork3 = lasagne.layers.batch_norm(segnetwork3) print segnetwork1.output_shape for i in xrange(int((inputsize3D - 1) / 2)): segnetwork4 = dnn.Conv3DDNNLayer( segnetwork4, num_filters=32, filter_size=(3, 3, 3), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) segnetwork4 = lasagne.layers.batch_norm(segnetwork4) print segnetwork4.output_shape segnetwork4 = lasagne.layers.ReshapeLayer(segnetwork4, ((-1, 32, 1, 1))) print segnetwork4.output_shape segnetwork = lasagne.layers.concat( (segnetwork1, segnetwork2, segnetwork3, segnetwork4)) print segnetwork.output_shape segnetwork = lasagne.layers.dropout(segnetwork, p=p_drop_hidden) segnetwork = lasagne.layers.DilatedConv2DLayer( segnetwork, num_filters=classes, filter_size=(1, 1), dilation=(1, 1), nonlinearity=softmax, W=lasagne.init.GlorotUniform()) return segnetwork1, segnetwork2, segnetwork3, segnetwork4, segnetwork
def build_model(): #l_in = lasagne.layers.InputLayer((None, 1,) + p_transform['patch_size']) # l_target = lasagne.layers.InputLayer((None, 1,) + p_transform['patch_size']) l_in = lasagne.layers.InputLayer(shape=(None, 128, 128, 128)) l0 = lasagne.layers.DimshuffleLayer(l_in, pattern=[0, 'x', 1, 2, 3]) net = {} base_n_filters = 64 net['contr_1_1'] = conv3d(l0, base_n_filters) net['contr_1_1'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_1']) net['contr_1_2'] = conv3d(net['contr_1_1'], base_n_filters) net['contr_1_2'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_2']) net['contr_1_3'] = conv3d(net['contr_1_2'], base_n_filters) net['contr_1_3'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_3']) net['pool1'] = max_pool3d(net['contr_1_3']) net['encode_1'] = conv3d(net['pool1'], base_n_filters) net['encode_1'] = lasagne.layers.ParametricRectifierLayer(net['encode_1']) net['encode_2'] = conv3d(net['encode_1'], base_n_filters) net['encode_2'] = lasagne.layers.ParametricRectifierLayer(net['encode_2']) net['encode_3'] = conv3d(net['encode_2'], base_n_filters) net['encode_3'] = lasagne.layers.ParametricRectifierLayer(net['encode_3']) net['upscale1'] = lasagne.layers.Upscale3DLayer(net['encode_2'], 2) net['concat1'] = lasagne.layers.ConcatLayer( [net['upscale1'], net['contr_1_3']], cropping=(None, None, "center", "center", "center")) net['expand_1_1'] = conv3d(net['concat1'], 2 * base_n_filters) net['expand_1_1'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_1']) net['expand_1_2'] = conv3d(net['expand_1_1'], 2 * base_n_filters) net['expand_1_2'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_2']) net['expand_1_3'] = conv3d(net['expand_1_2'], base_n_filters) net['expand_1_3'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_3']) net['output_segmentation'] = dnn.Conv3DDNNLayer( net['expand_1_3'], num_filters=1, filter_size=1, nonlinearity=lasagne.nonlinearities.sigmoid) l_out = lasagne.layers.SliceLayer(net['output_segmentation'], indices=0, axis=1) #return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target) return { "inputs": { "luna:3d": l_in, }, "outputs": { "predicted_segmentation": l_out }, }
def full_image_net_1_3d_segmenter(args): """ Returns: encoder_out, segmentation_out, decoder_out """ # ############### # X encoder part # ############### conv = InputLayer((None, 16, 4, 768, 1152)) conv = GaussianNoiseLayer(conv, sigma=args["sigma"]) encoder_layers = [] conv = dnn.Conv3DDNNLayer(conv, num_filters=128, filter_size=(3, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) conv = dnn.Conv3DDNNLayer(conv, num_filters=256, filter_size=(2, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) conv = dnn.Conv3DDNNLayer(conv, num_filters=512, filter_size=(1, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) conv = dnn.Conv3DDNNLayer(conv, num_filters=768, filter_size=(1, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) conv = dnn.Conv3DDNNLayer(conv, num_filters=1024, filter_size=(1, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) conv = dnn.Conv3DDNNLayer(conv, num_filters=1280, filter_size=(1, 5, 5), stride=(1, 2, 2)) encoder_layers.append(conv) # ############### # classifier part # ############### conv2 = ReshapeLayer(conv, (-1, 1280, 9, 15)) # prep it before the 2d conv conv2 = Conv2DLayer(conv2, num_filters=(3 * 4), filter_size=(1, 1)) # we need masks for every time step conv2 = ReshapeLayer(conv2, (-1, 3, 4, 9, 15)) # reshaping l_out_for_X = NonlinearityLayer(conv2, nonlinearity=sigmoid) print "classifier part:" print_network(l_out_for_X) # ########### # Y decimator # ########### yconv = InputLayer((None, 3, 4, 768, 1152)) yconv = ReshapeLayer(yconv, (-1, 3 * 4, 768, 1152)) num_decimation_layers = 6 for i in range(num_decimation_layers): yconv = MaxPool2DLayer(yconv, pool_size=5, stride=2) yconv = ReshapeLayer(yconv, (-1, 3, 4, 9, 15)) l_out_for_Y = yconv print "y decimator part:" print_network(l_out_for_Y) # ############## # X decoder part # ############## conv3 = conv for layer in get_all_layers(conv)[::-1]: if isinstance(layer, InputLayer): break conv3 = InverseLayer(conv3, layer) l_out_for_decoder = conv3 print "decoder part:" print_network(l_out_for_decoder) return l_out_for_X, l_out_for_Y, l_out_for_decoder
def build_model(image_size=(IMAGE_SIZE, IMAGE_SIZE, IMAGE_SIZE)): l_in = lasagne.layers.InputLayer(shape=(None, ) + image_size) #preprocessing layers l_norm = Hu2normHULayer(l_in, min_hu=-1000, max_hu=400) l_norm = ZMUVLayer(l_norm, mean=0.36, std=0.31) l0 = lasagne.layers.DimshuffleLayer(l_norm, pattern=[0, 'x', 1, 2, 3]) net = {} base_n_filters = 128 net['contr_1_1'] = conv3d(l0, base_n_filters) net['contr_1_1'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_1']) net['contr_1_2'] = conv3d(net['contr_1_1'], base_n_filters) net['contr_1_2'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_2']) net['contr_1_3'] = conv3d(net['contr_1_2'], base_n_filters) net['contr_1_3'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_3']) net['contr_1_4'] = conv3d(net['contr_1_3'], base_n_filters) net['contr_1_4'] = lasagne.layers.ParametricRectifierLayer( net['contr_1_4']) net['pool1'] = max_pool3d(net['contr_1_4']) net['contr_2_1'] = conv3d(net['pool1'], base_n_filters) net['contr_2_1'] = lasagne.layers.ParametricRectifierLayer( net['contr_2_1']) net['contr_2_2'] = conv3d(net['contr_2_1'], base_n_filters) net['contr_2_2'] = lasagne.layers.ParametricRectifierLayer( net['contr_2_2']) net['pool2'] = max_pool3d(net['contr_2_2']) net['contr_3_1'] = conv3d(net['pool2'], base_n_filters) net['contr_3_1'] = lasagne.layers.ParametricRectifierLayer( net['contr_3_1']) net['dropout_1'] = lasagne.layers.DropoutLayer(net['contr_3_1']) net['upscale1'] = lasagne.layers.Upscale3DLayer(net['dropout_1'], 2) net['concat1'] = lasagne.layers.ConcatLayer( [net['upscale1'], net['contr_2_2']], cropping=(None, None, "center", "center", "center")) net['expand_1_1'] = conv3d(net['concat1'], base_n_filters) net['expand_1_1'] = lasagne.layers.ParametricRectifierLayer( net['expand_1_1']) net['upscale1'] = lasagne.layers.Upscale3DLayer(net['expand_1_1'], 2) net['concat2'] = lasagne.layers.ConcatLayer( [net['upscale1'], net['contr_1_4']], cropping=(None, None, "center", "center", "center")) net['expand_2_1'] = conv3d(net['concat2'], base_n_filters) net['expand_2_1'] = lasagne.layers.ParametricRectifierLayer( net['expand_2_1']) net['expand_2_2'] = conv3d(net['expand_2_1'], base_n_filters) net['expand_2_2'] = lasagne.layers.ParametricRectifierLayer( net['expand_2_2']) net['output_segmentation'] = dnn.Conv3DDNNLayer( net['expand_2_2'], num_filters=1, filter_size=1, nonlinearity=lasagne.nonlinearities.sigmoid) l_out = lasagne.layers.SliceLayer(net['output_segmentation'], indices=0, axis=1) return { "inputs": { "luna:3d": l_in, }, "outputs": { "predicted_segmentation": l_out }, }
import theano from theano import tensor as T import lasagne from lasagne.layers import * from lasagne.layers import dnn import sys sys.path.append("..") import common l_conv = InputLayer((None,16,8,96,96)) l_conv = dnn.Conv3DDNNLayer(l_conv, num_filters=128, filter_size=(4,5,5), stride=(1,2,2)) l_conv = dnn.Conv3DDNNLayer(l_conv, num_filters=256, filter_size=(3,5,5), stride=(1,2,2)) l_conv = dnn.Conv3DDNNLayer(l_conv, num_filters=512, filter_size=(3,5,5), stride=(1,2,2)) for layer in get_all_layers(l_conv)[::-1]: if isinstance(layer, InputLayer): break l_conv = InverseLayer(l_conv, layer) for layer in get_all_layers(l_conv): print layer, layer.output_shape print count_params(l_conv) for x, _ in common.data_iterator(32, "/storeSSD/cbeckham/nersc/big_images/", start_day=1, end_day=1, img_size=96, time_chunks_per_example=8): # right now it is: (bs, time, nchannels, height, width) # needs to be: (bs, nchannels, time, height, width) print x.shape x = x.reshape((x.shape[0], x.shape[2], x.shape[1], x.shape[3], x.shape[4])) print x.shape # break