def end(net, x): channels_out = 3 x = L.Convolution(x, kernel_size=[1], stride=[1], dilation=[1], num_output=channels_out, pad=[0], param=[dict(lr_mult=1.0), dict(lr_mult=2.0)], weight_filler=dict(type='msra'), bias_filler=dict(type='constant')) # Choose output activation functions net.prob = L.Sigmoid(x, ntop=1, in_place=False) # Choose a loss function and input data, label and scale inputs. Only include it during the training phase (phase = 0) net.euclid_loss = L.EuclideanLoss( net.prob, net.label, net.scale, ntop=0, loss_weight=1.0, include=[dict(phase=0, stage='euclid')]) net.malis_loss = L.MalisLoss(net.prob, net.label, net.components, net.nhood, ntop=0, loss_weight=1.0, include=[dict(phase=0, stage='malis')]) return net
def caffenet(netmode): # Start Caffe proto net net = caffe.NetSpec() # Specify input data structures if netmode == caffe_pb2.TEST: if netconf.loss_function == 'malis': fmaps_end = 11 if netconf.loss_function == 'euclid': fmaps_end = 11 if netconf.loss_function == 'softmax': fmaps_end = 2 net.data, net.datai = data_layer([1, 1, 44, 132, 132]) net.silence = L.Silence(net.datai, ntop=0) # Shape specs: # 00. Convolution buffer size # 01. Weight memory size # 03. Num. channels # 04. [d] parameter running value # 05. [w] parameter running value run_shape_in = [[0, 0, 1, [1, 1, 1], [44, 132, 132]]] run_shape_out = run_shape_in last_blob = implement_usknet(net, run_shape_out, 64, fmaps_end) # Implement the prediction layer if netconf.loss_function == 'malis': net.prob = L.Sigmoid(last_blob, ntop=1) if netconf.loss_function == 'euclid': net.prob = L.Sigmoid(last_blob, ntop=1) if netconf.loss_function == 'softmax': net.prob = L.Softmax(last_blob, ntop=1) for i in range(0, len(run_shape_out)): print(run_shape_out[i]) print("Max. memory requirements: %s B" % (compute_memory_buffers(run_shape_out) + compute_memory_weights(run_shape_out) + compute_memory_blobs(run_shape_out))) print("Weight memory: %s B" % compute_memory_weights(run_shape_out)) print("Max. conv buffer: %s B" % compute_memory_buffers(run_shape_out)) else: if netconf.loss_function == 'malis': net.data, net.datai = data_layer([1, 1, 44, 132, 132]) net.label, net.labeli = data_layer([1, 1, 16, 44, 44]) net.label_affinity, net.label_affinityi = data_layer( [1, 11, 16, 44, 44]) net.affinity_edges, net.affinity_edgesi = data_layer([1, 1, 11, 3]) net.silence = L.Silence(net.datai, net.labeli, net.label_affinityi, net.affinity_edgesi, ntop=0) fmaps_end = 11 if netconf.loss_function == 'euclid': net.data, net.datai = data_layer([1, 1, 44, 132, 132]) net.label, net.labeli = data_layer([1, 11, 16, 44, 44]) net.scale, net.scalei = data_layer([1, 11, 16, 44, 44]) net.silence = L.Silence(net.datai, net.labeli, net.scalei, ntop=0) fmaps_end = 11 if netconf.loss_function == 'softmax': net.data, net.datai = data_layer([1, 1, 44, 132, 132]) # Currently only supports binary classification net.label, net.labeli = data_layer([1, 1, 16, 44, 44]) net.silence = L.Silence(net.datai, net.labeli, ntop=0) fmaps_end = 2 run_shape_in = [[0, 1, 1, [1, 1, 1], [44, 132, 132]]] run_shape_out = run_shape_in # Start the actual network last_blob = implement_usknet(net, run_shape_out, 64, fmaps_end) for i in range(0, len(run_shape_out)): print(run_shape_out[i]) print("Max. memory requirements: %s B" % (compute_memory_buffers(run_shape_out) + compute_memory_weights(run_shape_out) + 2 * compute_memory_blobs(run_shape_out))) print("Weight memory: %s B" % compute_memory_weights(run_shape_out)) print("Max. conv buffer: %s B" % compute_memory_buffers(run_shape_out)) # Implement the loss if netconf.loss_function == 'malis': last_blob = L.Sigmoid(last_blob, in_place=True) net.loss = L.MalisLoss(last_blob, net.label_affinity, net.label, net.affinity_edges, ntop=0) if netconf.loss_function == 'euclid': last_blob = L.Sigmoid(last_blob, in_place=True) net.loss = L.EuclideanLoss(last_blob, net.label, net.scale, ntop=0) if netconf.loss_function == 'softmax': net.loss = L.SoftmaxWithLoss(last_blob, net.label, ntop=0) # Return the protocol buffer of the generated network return net.to_proto()
def caffenet(netconf, netmode): # Start Caffe proto net net = caffe.NetSpec() # Specify input data structures dims = len(netconf.input_shape) run_shape = RunShape(None, None) run_shape.shape = netconf.input_shape run_shape.dilation = [1 for i in range(0, dims)] run_shape.fmaps = 1 run_shape_in = [run_shape] run_shape_out = run_shape_in offsets = [0, 0, 0] offsets[0] = (netconf.input_shape3d[-3] - netconf.output_shape3d[-3]) / 2 - 1 offsets[1] = (netconf.input_shape3d[-2] - netconf.output_shape3d[-2]) / 2 - 1 offsets[2] = (netconf.input_shape3d[-1] - netconf.output_shape3d[-1]) / 2 - 1 sizes = netconf.output_shape3d param = {"offsets": offsets, "sizes": sizes} param_json = json.dumps(param) if netmode == caffe_pb2.TEST: netgen = NetworkGenerator(netconf, netmode) net.data, net.datai = netgen.data_layer([1] + [netconf.fmap_input] + netconf.input_shape3d) net.silence = L.Silence(net.datai, ntop=0) # Chained blob list to construct the network (forward direction) blobs = [] # All networks start with data #blobs = blobs + [net.data] #blobs, run_shape_out = netgen.implement_usknet(netconf, net, run_shape_out, blobs, netconf.fmap_start, netconf.fmap_output) net_blobs, loss_flag = implement_parallel_unets( netconf, netgen, net, netmode) blobs = blobs + net_blobs last_blob = blobs[-1] # Implement the prediction layer if netconf.loss_function == 'malis': net.prob = L.Sigmoid(last_blob, ntop=1) if netconf.loss_function == 'euclid': net.prob = L.Sigmoid(last_blob, ntop=1) if netconf.loss_function == 'softmax': net.prob = L.Softmax(last_blob, ntop=1) else: netgen = NetworkGenerator(netconf, netmode) net.data, net.datai = netgen.data_layer([1] + [netconf.fmap_input] + netconf.input_shape3d) if netconf.loss_function == 'malis': net.label, net.labeli = netgen.data_layer([1] + [netconf.fmap_output] + netconf.output_shape) net.components, net.componentsi = netgen.data_layer( [1, 1] + netconf.output_shape) net.nhood, net.nhoodi = netgen.data_layer([1, 1] + [netconf.fmap_output] + [3]) net.silence = L.Silence(net.datai, net.labeli, net.componentsi, net.nhoodi, ntop=0) if netconf.loss_function == 'euclid': net.label, net.labeli = netgen.data_layer([1] + [netconf.fmap_output3d] + netconf.output_shape3d) net.scale, net.scalei = netgen.data_layer([1] + [netconf.fmap_output3d] + netconf.output_shape3d) net.silence = L.Silence(net.datai, net.labeli, net.scalei, ntop=0) if netconf.loss_function == 'softmax': # net.label, net.labeli = netgen.data_layer([1]+[netconf.fmap_output]+netconf.output_shape) net.label, net.labeli = netgen.data_layer([1] + [1] + netconf.output_shape) net.silence = L.Silence(net.datai, net.labeli, ntop=0) # Start the actual network # Chained blob list to construct the network (forward direction) blobs = [] # All networks start with data #blobs = blobs + [net.data] net_blobs, loss_flag = implement_parallel_unets( netconf, netgen, net, netmode) blobs = blobs + net_blobs last_blob = blobs[-1] # Implement the loss if netconf.loss_function == 'malis': last_blob = L.Sigmoid(last_blob, in_place=True) net.loss = L.MalisLoss(last_blob, net.label, net.components, net.nhood, ntop=0) if netconf.loss_function == 'euclid': last_blob = L.Sigmoid(last_blob, in_place=True) #net.loss = L.EuclideanLoss(last_blob, net.label, net.scale, ntop=0) net.loss = L.GatedEuclideanLoss(last_blob, net.label, net.scale, loss_flag, ntop=0) if netconf.loss_function == 'softmax': net.loss = L.SoftmaxWithLoss(last_blob, net.label, ntop=0) # Return the protocol buffer of the generated network return net.to_proto()
# Scale input layer net.scale = L.MemoryData(dim=[1, 3], ntop=1, include=[dict(phase=0, stage='euclid')]) # Silence the not needed data and label integer values net.nhood = L.MemoryData(dim=[1, 1, 3, 3], ntop=1, include=[dict(phase=0, stage='malis')]) # USK-Net metalayer net.unet = ML.UNet(net.data, fmap_start=12, depth=3, fmap_inc_rule = lambda fmaps: int(math.ceil(float(fmaps) * 5)), fmap_dec_rule = lambda fmaps: int(math.ceil(float(fmaps) / 5)), downsampling_strategy = [[2,2,2],[2,2,2],[3,3,3]], dropout = 0.0, use_deconv_uppath=False, use_stable_upconv=True) net.aff_out = L.Convolution(net.unet, kernel_size=[1], num_output=3, param=[dict(lr_mult=1),dict(lr_mult=2)], weight_filler=dict(type='msra'), bias_filler=dict(type='constant')) # Choose output activation functions net.aff_pred = L.Sigmoid(net.aff_out, ntop=1, in_place=False) # Choose a loss function and input data, label and scale inputs. Only include it during the training phase (phase = 0) net.euclid_loss = L.EuclideanLoss(net.aff_pred, net.aff_label, net.scale, ntop=0, loss_weight=1.0, include=[dict(phase=0, stage='euclid')]) net.malis_loss = L.MalisLoss(net.aff_pred, net.aff_label, net.comp_label, net.nhood, ntop=0, loss_weight=1.0, include=[dict(phase=0, stage='malis')]) # Fix the spatial input dimensions. Note that only spatial dimensions get modified, the minibatch size # and the channels/feature maps must be set correctly by the user (since this code can definitely not # figure out the user's intent). If the code does not seem to terminate, then the issue is most likely # a wrong number of feature maps / channels in either the MemoryData-layers or the network output. # This function takes as input: # - The network # - A list of other inputs to test (note: the nhood input is static and not spatially testable, thus excluded here) # - A list of the maximal shapes for each input # - A list of spatial dependencies; here [-1, 0] means the Y axis is a free parameter, and the X axis should be identical to the Y axis. caffe.fix_input_dims(net, [net.data, net.aff_label, net.comp_label, net.scale], max_shapes = [[200,200,200],[100,100,100],[100,100,100],[100,100,100]], shape_coupled = [-1, -1, 1])
def caffenet(netconf, netmode): # Start Caffe proto net net = caffe.NetSpec() # Specify input data structures dims = len(netconf.input_shape) run_shape = RunShape(None, None) run_shape.shape = netconf.input_shape run_shape.dilation = [1 for i in range(0,dims)] run_shape.fmaps = 1 run_shape_in = [run_shape] run_shape_out = run_shape_in if netmode == caffe_pb2.TEST: netgen = NetworkGenerator(netconf, netmode); net.data, net.datai = netgen.data_layer([1]+[netconf.fmap_input]+netconf.input_shape) net.silence = L.Silence(net.datai, ntop=0) # Chained blob list to construct the network (forward direction) blobs = [] # All networks start with data blobs = blobs + [net.data] blobs, run_shape_out = netgen.implement_usknet(netconf, net, run_shape_out, blobs, netconf.fmap_start, netconf.fmap_output) last_blob = blobs[-1] # Implement the prediction layer if netconf.loss_function == 'malis': net.prob = netgen.implement_loss_activation(last_blob, False) if netconf.loss_function == 'euclid': net.prob = netgen.implement_loss_activation(last_blob, False) if netconf.loss_function == 'softmax': net.prob = L.Softmax(last_blob, ntop=1) for i in range(0,len(run_shape_out)): print("Shape: [%s]" % i) run_shape_out[i].print() print("Max. memory requirements: %s B" % (netgen.compute_memory_buffers(run_shape_out)+netgen.compute_memory_weights(run_shape_out)+netgen.compute_memory_blobs(run_shape_out))) print("Weight memory: %s B" % netgen.compute_memory_weights(run_shape_out)) print("Max. conv buffer: %s B" % netgen.compute_memory_buffers(run_shape_out)) else: netgen = NetworkGenerator(netconf, netmode); net.data, net.datai = netgen.data_layer([1]+[netconf.fmap_input]+netconf.input_shape) if netconf.loss_function == 'malis': net.label, net.labeli = netgen.data_layer([1]+[netconf.fmap_output]+netconf.output_shape) n_component_channels = 2 if netconf.malis_split_component_phases else 1 components_shape = [1, n_component_channels] + netconf.output_shape net.components, net.componentsi = netgen.data_layer(components_shape) net.nhood, net.nhoodi = netgen.data_layer([1,1]+[netconf.fmap_output]+[3]) net.silence = L.Silence(net.datai, net.labeli, net.componentsi, net.nhoodi, ntop=0) if netconf.loss_function == 'euclid': net.label, net.labeli = netgen.data_layer([1]+[netconf.fmap_output]+netconf.output_shape) net.scale, net.scalei = netgen.data_layer([1]+[netconf.fmap_output]+netconf.output_shape) net.silence = L.Silence(net.datai, net.labeli, net.scalei, ntop=0) if netconf.loss_function == 'softmax': net.label, net.labeli = netgen.data_layer([1]+[netconf.fmap_output]+netconf.output_shape) net.silence = L.Silence(net.datai, net.labeli, ntop=0) # Start the actual network # Chained blob list to construct the network (forward direction) blobs = [] # All networks start with data blobs = blobs + [net.data] blobs, run_shape_out = netgen.implement_usknet(netconf, net, run_shape_out, blobs, netconf.fmap_start, netconf.fmap_output) last_blob = blobs[-1] for i in range(0,len(run_shape_out)): print("Shape: [%s]" % i) run_shape_out[i].print() print("Max. memory requirements: %s B" % (netgen.compute_memory_buffers(run_shape_out)+netgen.compute_memory_weights(run_shape_out)+2*netgen.compute_memory_blobs(run_shape_out))) print("Weight memory: %s B" % netgen.compute_memory_weights(run_shape_out)) print("Max. conv buffer: %s B" % netgen.compute_memory_buffers(run_shape_out)) # Implement the loss if netconf.loss_function == 'malis': last_blob = netgen.implement_loss_activation(last_blob, True) net.loss = L.MalisLoss(last_blob, net.label, net.components, net.nhood, ntop=0) if netconf.loss_function == 'euclid': last_blob = netgen.implement_loss_activation(last_blob, True) net.loss = L.EuclideanLoss(last_blob, net.label, net.scale, ntop=0) if netconf.loss_function == 'softmax': net.loss = L.SoftmaxWithLoss(last_blob, net.label, ntop=0) # Return the protocol buffer of the generated network protonet = net.to_proto() protonet.name = 'NET_' + ('TEST' if (netmode == caffe_pb2.TEST) else 'TRAIN') return protonet
def long_range_unet(name): # Start a network net = caffe.NetSpec() # Data input layer net.data = L.MemoryData(dim=[1, 1], ntop=1) n_channels = 12 # TODO # Label input layer # I guess the second number is the number of channels net.aff_label = L.MemoryData(dim=[1, n_channels], ntop=1, include=[dict(phase=0)]) # Components label layer # No idea about this one... net.comp_label = L.MemoryData(dim=[1, 2], ntop=1, include=[dict(phase=0, stage='malis')]) # Scale input layer # again second = channels ?! net.scale = L.MemoryData(dim=[1, n_channels], ntop=1, include=[dict(phase=0, stage='euclid')]) # Silence the not needed data and label integer values # is this correct ???? net.nhood = L.MemoryData(dim=[1, 1, n_channels, 3], ntop=1, include=[dict(phase=0, stage='malis')]) # USK-Net metalayer net.unet = ML.UNet( net.data, fmap_start=12, depth=3, fmap_inc_rule=lambda fmaps: int(math.ceil(float(fmaps) * 5)), fmap_dec_rule=lambda fmaps: int(math.ceil(float(fmaps) / 5)), downsampling_strategy=[[1, 3, 3], [1, 3, 3], [1, 3, 3]], dropout=0.0, use_deconv_uppath=False, use_stable_upconv=True) net.aff_out = L.Convolution(net.unet, kernel_size=[1], num_output=n_channels, param=[dict(lr_mult=1), dict(lr_mult=2)], weight_filler=dict(type='msra'), bias_filler=dict(type='constant')) # Choose output activation functions net.aff_pred = L.Sigmoid(net.aff_out, ntop=1, in_place=False) # Choose a loss function and input data, label and scale inputs. Only include it during the training phase (phase = 0) net.euclid_loss = L.EuclideanLoss(net.aff_pred, net.aff_label, net.scale, ntop=0, loss_weight=1.0, include=[dict(phase=0, stage='euclid')]) net.malis_loss = L.MalisLoss(net.aff_pred, net.aff_label, net.comp_label, net.nhood, ntop=0, loss_weight=1.0, include=[dict(phase=0, stage='malis')]) # Fix the spatial input dimensions. Note that only spatial dimensions get modified, the minibatch size # and the channels/feature maps must be set correctly by the user (since this code can definitely not # figure out the user's intent). If the code does not seem to terminate, then the issue is most likely # a wrong number of feature maps / channels in either the MemoryData-layers or the network output. # This function takes as input: # - The network # - A list of other inputs to test (note: the nhood input is static and not spatially testable, thus excluded here) # - A list of the maximal shapes for each input # - A list of spatial dependencies; here [-1, 0] means the Y axis is a free parameter, and the X axis should be identical to the Y axis. caffe.fix_input_dims(net, [net.data, net.aff_label, net.comp_label, net.scale], max_shapes=[[84, 268, 268], [100, 100, 100], [100, 100, 100], [100, 100, 100]], shape_coupled=[-1, -1, 1]) protonet = net.to_proto() protonet.name = name # Store the network as prototxt with open(protonet.name + '.prototxt', 'w') as f: print(protonet, file=f)