def joinBlock(name, opn, n, inputA, inputB, train=True): trainparam = [] trainparam2 = [] if train: trainparam = [dict(lr_mult=1, decay_mult= 1),dict(lr_mult=1, decay_mult= 1)] trainparam2 = [dict(lr_mult=1, decay_mult= 1),dict(lr_mult=1, decay_mult= 1),dict(lr_mult=1, decay_mult= 1)] else: trainparam = [dict(lr_mult=0, decay_mult= 0),dict(lr_mult=0, decay_mult= 0)] trainparam2 = [dict(lr_mult=0, decay_mult= 0),dict(lr_mult=0, decay_mult= 0),dict(lr_mult=0, decay_mult= 0)] #if global_stat: #trainparam2 = [dict(lr_mult=0, decay_mult= 0),dict(lr_mult=0, decay_mult= 0),dict(lr_mult=0, decay_mult= 0)] #TRAINABLE???? TODO s = list(name) del s[0] if s[0] == 'B': del s[0] nname = "".join(s) if upsample == False: convolution_param = dict(num_output=opn, kernel_size=2, stride=2, pad=0, weight_filler = dict(type='xavier')) n["upsample"+name] = L.Deconvolution(n[inputA], convolution_param=convolution_param, param=trainparam, name="upsampleparam"+nname) else: convolution_param = dict(num_output=opn, kernel_size=2, stride=2, pad=0, weight_filler = dict(type='constant', value=0.0), bias_term=False) n["upsample"+name] = L.Deconvolution(n[inputA], convolution_param=convolution_param ,param=[dict(lr_mult=0, decay_mult= 0)], name="upsampleparam"+nname) n["upsampleB"+name] = L.BatchNorm(n["upsample"+name], use_global_stats=global_stat, param=trainparam2, name="upsampleBparam"+nname)#, param=[{"lr_mult":0},{"lr_mult":0},{"lr_mult":0}]) n[inputB] = L.BatchNorm(n[inputB], use_global_stats=global_stat, param=trainparam2, name=nname+"param")#, param=[{"lr_mult":0},{"lr_mult":0},{"lr_mult":0}]) #ACHTUNG BEI BATCHNORM upsampleB genau da drunter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! n["concat"+name] = L.Concat(n["upsampleB"+name], n[inputB], concat_param=dict(axis=1)) return n, "concat"+name
def Upsample(self, bottom, name='upsample', factor=2, n_out=3, lr_mult=None, decay_mult=None): def get_kernel_size(f): return 2 * f - f % 2 def get_pad(f): return int( np.ceil( (f-1) / 2.) ) up_k = get_kernel_size(factor) up_s = factor up_p = get_pad(factor) conv_params = {'num_output': n_out, 'group': n_out, \ 'kernel_size': up_k, 'pad': up_p, 'stride': up_s, \ 'weight_filler': {'type': 'bilinear'}, 'bias_term': False} if lr_mult is not None: params = [] for lr, decay in zip(lr_mult, decay_mult): p = self.layer_param.copy() p['lr_mult'] = lr p['decay_mult'] = decay params.append(p) return L.Deconvolution( bottom, name=name, param=params, convolution_param=conv_params) else: return L.Deconvolution( bottom, name=name, convolution_param=conv_params)
def fcn(split, tops): n = caffe.NetSpec() n.data, n.label = L.Python(module='nyud_layers', layer='NYUDSegDataLayer', ntop=2, param_str=str(dict(nyud_dir='../data/nyud', split=split, tops=tops, seed=1337))) # the base net n.conv1_1, n.relu1_1 = conv_relu(n.data, 64, pad=100) n.conv1_2, n.relu1_2 = conv_relu(n.relu1_1, 64) n.pool1 = max_pool(n.relu1_2) n.conv2_1, n.relu2_1 = conv_relu(n.pool1, 128) n.conv2_2, n.relu2_2 = conv_relu(n.relu2_1, 128) n.pool2 = max_pool(n.relu2_2) n.conv3_1, n.relu3_1 = conv_relu(n.pool2, 256) n.conv3_2, n.relu3_2 = conv_relu(n.relu3_1, 256) n.conv3_3, n.relu3_3 = conv_relu(n.relu3_2, 256) n.pool3 = max_pool(n.relu3_3) n.conv4_1, n.relu4_1 = conv_relu(n.pool3, 512) n.conv4_2, n.relu4_2 = conv_relu(n.relu4_1, 512) n.conv4_3, n.relu4_3 = conv_relu(n.relu4_2, 512) n.pool4 = max_pool(n.relu4_3) n.conv5_1, n.relu5_1 = conv_relu(n.pool4, 512) n.conv5_2, n.relu5_2 = conv_relu(n.relu5_1, 512) n.conv5_3, n.relu5_3 = conv_relu(n.relu5_2, 512) n.pool5 = max_pool(n.relu5_3) # fully conv n.fc6, n.relu6 = conv_relu(n.pool5, 4096, ks=7, pad=0) n.drop6 = L.Dropout(n.relu6, dropout_ratio=0.5, in_place=True) n.fc7, n.relu7 = conv_relu(n.drop6, 4096, ks=1, pad=0) n.drop7 = L.Dropout(n.relu7, dropout_ratio=0.5, in_place=True) n.score_fr = L.Convolution(n.drop7, num_output=40, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.upscore2 = L.Deconvolution(n.score_fr, convolution_param=dict(num_output=40, kernel_size=4, stride=2, bias_term=False), param=[dict(lr_mult=0)]) n.score_pool4 = L.Convolution(n.pool4, num_output=40, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.score_pool4c = crop(n.score_pool4, n.upscore2) n.fuse_pool4 = L.Eltwise(n.upscore2, n.score_pool4c, operation=P.Eltwise.SUM) n.upscore16 = L.Deconvolution(n.fuse_pool4, convolution_param=dict(num_output=40, kernel_size=32, stride=16, bias_term=False), param=[dict(lr_mult=0)]) n.score = crop(n.upscore16, n.data) n.loss = L.SoftmaxWithLoss(n.score, n.label, loss_param=dict(normalize=False, ignore_label=255)) return n.to_proto()
def fcn(split): n = caffe.NetSpec() n.data, n.sem, n.geo = L.Python(module='siftflow_layers', layer='SIFTFlowSegDataLayer', ntop=3, param_str=str(dict(siftflow_dir='../data/sift-flow', split=split, seed=1337))) # the base net n.conv1_1, n.relu1_1 = conv_relu(n.data, 64, pad=100) n.conv1_2, n.relu1_2 = conv_relu(n.relu1_1, 64) n.pool1 = max_pool(n.relu1_2) n.conv2_1, n.relu2_1 = conv_relu(n.pool1, 128) n.conv2_2, n.relu2_2 = conv_relu(n.relu2_1, 128) n.pool2 = max_pool(n.relu2_2) n.conv3_1, n.relu3_1 = conv_relu(n.pool2, 256) n.conv3_2, n.relu3_2 = conv_relu(n.relu3_1, 256) n.conv3_3, n.relu3_3 = conv_relu(n.relu3_2, 256) n.pool3 = max_pool(n.relu3_3) n.conv4_1, n.relu4_1 = conv_relu(n.pool3, 512) n.conv4_2, n.relu4_2 = conv_relu(n.relu4_1, 512) n.conv4_3, n.relu4_3 = conv_relu(n.relu4_2, 512) n.pool4 = max_pool(n.relu4_3) n.conv5_1, n.relu5_1 = conv_relu(n.pool4, 512) n.conv5_2, n.relu5_2 = conv_relu(n.relu5_1, 512) n.conv5_3, n.relu5_3 = conv_relu(n.relu5_2, 512) n.pool5 = max_pool(n.relu5_3) # fully conv n.fc6, n.relu6 = conv_relu(n.pool5, 4096, ks=7, pad=0) n.drop6 = L.Dropout(n.relu6, dropout_ratio=0.5, in_place=True) n.fc7, n.relu7 = conv_relu(n.drop6, 4096, ks=1, pad=0) n.drop7 = L.Dropout(n.relu7, dropout_ratio=0.5, in_place=True) n.score_fr_sem = L.Convolution(n.drop7, num_output=33, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.upscore_sem = L.Deconvolution(n.score_fr_sem, convolution_param=dict(num_output=33, kernel_size=64, stride=32, bias_term=False), param=[dict(lr_mult=0)]) n.score_sem = crop(n.upscore_sem, n.data) # losses to make score happy (o.w. loss_sem) n.loss = L.SoftmaxWithLoss(n.score_sem, n.sem, loss_param=dict(normalize=False, ignore_label=255)) n.score_fr_geo = L.Convolution(n.drop7, num_output=3, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.upscore_geo = L.Deconvolution(n.score_fr_geo, convolution_param=dict(num_output=3, kernel_size=64, stride=32, bias_term=False), param=[dict(lr_mult=0)]) n.score_geo = crop(n.upscore_geo, n.data) n.loss_geo = L.SoftmaxWithLoss(n.score_geo, n.geo, loss_param=dict(normalize=False, ignore_label=255)) return n.to_proto()
def fcn(split): n = caffe.NetSpec() pydata_params = dict(split=split, mean=(104.00699, 116.66877, 122.67892), seed=1337) if split == 'train': pydata_params['sbdd_dir'] = '../data/sbdd/dataset' pylayer = 'SBDDSegDataLayer' else: pydata_params['voc_dir'] = '../data/pascal/VOC2011' pylayer = 'VOCSegDataLayer' n.data, n.label = L.Python(module='voc_layers', layer=pylayer, ntop=2, param_str=str(pydata_params)) # the base net n.conv1, n.relu1 = conv_relu(n.data,96,ks=11,stride=4,pad=100) n.pool1 = max_pool(n.relu1) n.lrn1 = lrn(n.pool1) n.conv2, n.relu2 = conv_relu(n.lrn1, 256, ks=5, stride=1, pad=2, group=2) n.pool2 = max_pool(n.relu2) n.lrn2 = lrn(n.pool2) n.conv3, n.relu3 = conv_relu(n.lrn2, 384, ks=3, stride = 1, pad=1) n.conv4, n.relu4 = conv_relu(n.relu3, 384, ks=3, stride = 1, pad=1, group=2) n.conv5, n.relu5 = conv_relu(n.relu4, 256, ks=3, stride = 1, pad=1, group=2) n.pool5 = max_pool(n.relu5) # fully conv n.fc6, n.relu6 = conv_relu(n.pool5, 4096, ks=6, stride = 1, pad=0) n.drop6 = L.Dropout(n.relu6, dropout_ratio=0.5, in_place=True) n.fc7, n.relu7 = conv_relu(n.drop6, 4096, ks=1, stride = 1, pad=0) n.drop7 = L.Dropout(n.relu7, dropout_ratio=0.5, in_place=True) n.score_fr = L.Convolution(n.drop7, num_output=21, kernel_size=1, pad=0, stride = 1, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.upscore2 = L.Deconvolution(n.score_fr, convolution_param=dict(num_output=21, kernel_size=5, stride=2, bias_term=False), param=[dict(lr_mult=0)]) n.score_pool2 = L.Convolution(n.lrn2, num_output=21, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.score_pool2c = crop(n.score_pool2, n.upscore2) n.fuse_pool2 = L.Eltwise(n.upscore2, n.score_pool2c, operation=P.Eltwise.SUM) n.upscore16 = L.Deconvolution(n.fuse_pool2, convolution_param=dict(num_output=21, kernel_size=31, stride=16, bias_term=False), param=[dict(lr_mult=0)]) n.score = crop(n.upscore16, n.data) n.loss = L.SoftmaxWithLoss(n.score, n.label, loss_param=dict(normalize=True, ignore_label=255)) return n.to_proto()
def fcn(split, num_classes=None): n = caffe.NetSpec() n.data = L.Input(shape=[dict(dim=[1, 3, 500, 500])]) # conv1 n.conv1_1, n.relu1_1 = conv_relu(n.data, 64, pad=100) n.conv1_2, n.relu1_2 = conv_relu(n.relu1_1, 64) n.pool1 = max_pool(n.relu1_2) # conv2 n.conv2_1, n.relu2_1 = conv_relu(n.pool1, 128) n.conv2_2, n.relu2_2 = conv_relu(n.relu2_1, 128) n.pool2 = max_pool(n.relu2_2) # conv3 n.conv3_1, n.relu3_1 = conv_relu(n.pool2, 256) n.conv3_2, n.relu3_2 = conv_relu(n.relu3_1, 256) n.conv3_3, n.relu3_3 = conv_relu(n.relu3_2, 256) n.pool3 = max_pool(n.relu3_3) # score n.score_fr = L.Convolution(n.pool3, num_output=num_classes, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) # deconv : 8x n.upscore = L.Deconvolution(n.score_fr, convolution_param=dict(num_output=num_classes, kernel_size=16, stride=8, bias_term=False), param=[dict(lr_mult=0)]) n.score = crop(n.upscore, n.data) return n.to_proto()
def mynet(): data, label = L.DummyData( shape=[dict(dim=[8, 1, 28, 28]), dict(dim=[8, 1, 1, 1])], transform_param=dict(scale=1. / 255), ntop=2) # CAFFE = 1 # MKL2017 = 3 kwargs = {'engine': 3} conv1 = L.Convolution(data, kernel_size=[3, 4, 5], num_output=3, pad=[1, 2, 3]) bn1 = L.BatchNorm(conv1, **kwargs) relu1 = L.ReLU(bn1, **kwargs) convargs = { 'param': [dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=2)], 'convolution_param': dict(num_output=64, kernel_size=2, stride=2, engine=P.Convolution.CAFFE, bias_filler=dict(type='constant', value=0), weight_filler=dict(type='xavier')) } deconv1 = L.Deconvolution(relu1, **convargs) return to_proto(deconv1)
def deconv(netspec, input_node, num_output, scope, ks=4, stride=2, pad=1, std=0.01): """ :param input_node: :param num_output: :param ks: :param stride: :param pad: :param std: :return: """ n = netspec n.node = L.Deconvolution(input_node, convolution_param=dict( num_output=num_output, kernel_size=ks, stride=stride, pad=pad, weight_filler=dict(type='xavier'), bias_filler=dict(type='constant', value=0)), param=[dict(lr_mult=0)], name='%s' % scope) return n.node
def fpn_block(net): use_relu=False use_bn=False ConvBNLayer(net, 'stage2_tb', 'newC1', use_bn, use_relu, 128, 1, 0, 1) ConvBNLayer(net, 'stage3_tb', 'newC2', use_bn, use_relu, 128, 1, 0, 1) ConvBNLayer(net, 'stage4_tb', 'newC3', use_bn, use_relu, 128, 1, 0, 1) ConvBNLayer(net, 'ext1/fe1_2', 'p4', use_bn, use_relu, 128, 1, 0, 1) net.upP4 = L.Deconvolution(net.p4, convolution_param=dict(kernel_size=4, stride=2, group=128, num_output=128, pad=1, bias_term=False, weight_filler=dict(type='bilinear')), param=[dict(lr_mult=0, decay_mult=0)]) net.p3 = L.Eltwise(net['newC3'], net.upP4) ConvBNLayer(net, 'p3', 'p3_lateral', use_bn, use_relu, 128, 1, 0, 1) net.upP3 = L.Deconvolution(net.p3_lateral, convolution_param=dict(kernel_size=4, stride=2, group=128, num_output=128, pad=1, bias_term=False, weight_filler=dict(type='bilinear')),param=[dict(lr_mult=0, decay_mult=0)]) net.p2 = L.Eltwise(net['newC2'], net.upP3) ConvBNLayer(net, 'p2', 'p2_lateral', use_bn, use_relu, 128, 1, 0, 1) net.upP2 = L.Deconvolution(net.p2_lateral, convolution_param=dict(kernel_size=4, stride=2, group=128, num_output=128, pad=1, bias_term=False, weight_filler=dict(type='bilinear')),param=[dict(lr_mult=0, decay_mult=0)]) net.p1 = L.Eltwise(net['newC1'], net.upP2) return net
def test_nd_conv(self): """ ND conv maps the same way in more dimensions. """ n = caffe.NetSpec() # define data with 3 spatial dimensions, otherwise the same net n.data = L.Input(shape=dict(dim=[2, 3, 100, 100, 100])) n.conv = L.Convolution(n.data, num_output=10, kernel_size=[3, 3, 3], stride=[1, 1, 1], pad=[0, 1, 2]) n.pool = L.Pooling(n.conv, pool=P.Pooling.MAX, kernel_size=2, stride=2, pad=0) n.deconv = L.Deconvolution(n.pool, num_output=10, kernel_size=4, stride=2, pad=0) ax, a, b = coord_map_from_to(n.deconv, n.data) self.assertEquals(ax, 1) self.assertTrue(len(a) == len(b)) self.assertTrue(np.all(a == 1)) self.assertEquals(b[0] - 1, b[1]) self.assertEquals(b[1] - 1, b[2])
def upconv(self, run_shape, bottom, num_output_dec, num_output_conv, weight_std=0.01, kernel_size=[2], stride=[2]): # Shape update rules update = RunShapeUpdater() update.fmaps_update = lambda x: num_output_dec update.shape_update = lambda x: [ kernel_size[min(i, len(kernel_size) - 1)] * x[i] for i in range(0, len(run_shape[-1].shape)) ] self.update_shape(run_shape, update) deconv = L.Deconvolution(bottom, convolution_param=dict( num_output=num_output_dec, kernel_size=kernel_size, stride=stride, pad=[0], dilation=[1], group=num_output_dec, weight_filler=dict(type='constant', value=1), bias_term=False), param=dict(lr_mult=0, decay_mult=0)) # The convolution buffer and weight memory weight_mem = fsize * num_output_conv * num_output_dec conv_buff = fsize * run_shape[-1].fmaps for i in range(0, len(run_shape[-1].shape)): conv_buff *= 2 conv_buff *= run_shape[-1].shape[i] # Shape update rules update.conv_buffer_mem_update = lambda x: conv_buff update.weight_mem_update = lambda x: weight_mem update.fmaps_update = lambda x: num_output_conv update.shape_update = lambda x: [ x[i] for i in range(0, len(run_shape[-1].shape)) ] self.update_shape(run_shape, update) conv = L.Convolution(deconv, num_output=num_output_conv, kernel_size=[1], stride=[1], pad=[0], dilation=[1], group=1, param=[dict(lr_mult=1), dict(lr_mult=2)], weight_filler=dict(type='gaussian', std=weight_std), bias_filler=dict(type='constant')) return deconv, conv
def upsample(bottom, stride, nout=1, name=None): s, k, pad = stride, 2 * stride, int(ceil(stride-1)/2) if not name: name = "upsample%d"%s return L.Deconvolution(bottom, name=name, convolution_param=dict(num_output=nout, bias_term=False, kernel_size=k, stride=s, pad=pad, weight_filler = dict(type="bilinear"), group=nout), param=[dict(lr_mult=0, decay_mult=0)])
def fcn(split, tops): n = caffe.NetSpec() n.color, n.hha, n.label = L.Python(module='nyud_layers', layer='NYUDSegDataLayer', ntop=3, param_str=str( dict(nyud_dir='../data/nyud', split=split, tops=tops, seed=1337))) n = modality_fcn(n, 'color', 'color') n = modality_fcn(n, 'hha', 'hha') n.score_fused = L.Eltwise(n.score_frcolor, n.score_frhha, operation=P.Eltwise.SUM, coeff=[0.5, 0.5]) n.upscore = L.Deconvolution(n.score_fused, convolution_param=dict(num_output=40, kernel_size=64, stride=32, bias_term=False), param=[dict(lr_mult=0)]) n.score = crop(n.upscore, n.color) n.loss = L.SoftmaxWithLoss(n.score, n.label, loss_param=dict(normalize=False, ignore_label=255)) return n.to_proto()
def add_yolo_loss(net,out_layer=0,version='yolov2'): bottom = net.tops.keys()[-1] if version is 'yolov2' : net.conv_global = L.Convolution(net[bottom], kernel_size=1, stride=1,name='conv_global', num_output=125, pad=0, bias_term=True, weight_filler=dict(type='xavier'), bias_filler=dict(type='constant')) net.RegionLoss = L.RegionLoss(net.conv_global,net.label,num_class=20,coords=4,num=5,softmax=1,jitter=0.2,rescore=0,object_scale=5.0,noobject_scale=1.0,class_scale=1.0, coord_scale=1.0,absolute=1,thresh=0.6,random=0,biases=[1.08,1.19,3.42,4.41,6.63,11.38,9.42,5.11,16.62,10.52], include={'phase':caffe.TRAIN}) return net.conv_global,net.conv_global else : #net.conv_global1 = L.Convolution(net[bottom], kernel_size=1, stride=1,name='conv_global1', # num_output=75, pad=0, bias_term=True, weight_filler=dict(type='msra'), bias_filler=dict(type='constant')) net.Yolov3Loss1 = L.Yolov3(net[bottom],net.label,num_class=20,num=3,object_scale=5.0,noobject_scale=1.0,class_scale=1.0,side=13, coord_scale=1.0,thresh=0.6,anchors_scale=32,use_logic_gradient=False,mask=[3,4,5],biases=[10,14,23,27,37,58,81,82,135,169,344,319], include={'phase':caffe.TRAIN}) net.upsample = L.Deconvolution(net[bottom],param=dict(lr_mult=0, decay_mult=0),convolution_param=dict(kernel_size=4,stride=2,pad=1,group=75,bias_term=False,num_output=75,weight_filler=dict(type='bilinear'))) x = L.Eltwise(net.upsample, out_layer) #net.conv_global2 = L.Convolution(x, kernel_size=1, stride=1,name='conv_global2', # num_output=75, pad=0, bias_term=True, weight_filler=dict(type='msra'), bias_filler=dict(type='constant')) net.Yolov3Loss2 = L.Yolov3(x,net.label,num_class=20,num=3,object_scale=5.0,noobject_scale=1.0,class_scale=1.0,side=26, coord_scale=1.0,thresh=0.6,anchors_scale=16,use_logic_gradient=False,mask=[0,1,2],biases=[10,14,23,27,37,58,81,82,135,169,344,319], include={'phase':caffe.TRAIN}) return net[bottom],x
def unet2(bottom, ch, deconv): def block1(bottom): return unet_conv(bottom, 256, 128, se=True) def block2(bottom): conv1 = unet_conv(bottom, 64, 128, se=True) ub1 = unet_branch(conv1, block1, 128, 128, 4) conv2 = unet_conv(ub1, 64, 64, se=True) return conv2 conv1 = unet_conv(bottom, 32, 64, se=False) ub1 = unet_branch(conv1, block2, 64, 64, 16) conv2 = conv_relu(ub1, 64) if deconv: return L.Deconvolution(conv2, convolution_param=dict(num_output=ch, kernel_size=4, pad=3, stride=2)) else: return L.Convolution(conv2, kernel_size=3, stride=1, num_output=ch, pad=0)
def deconvolution_layer(net, inputlayer, layername, kernel_size, stride, pad, num_output, w_lr=1.0, b_lr=1.0, init_bias=0.0): parname_stem = layername deconv = L.Deconvolution(inputlayer, convolution_param=dict( num_output=num_output, group=num_output, kernel_size=kernel_size, stride=stride, pad=pad, weight_filler=dict(type="bilinear"), bias_filler=dict(type="constant", value=init_bias)), param=[ dict(name="par_%s_deconv_w" % (parname_stem), lr_mult=w_lr), dict(name="par_%s_conv_b" % (parname_stem), lr_mult=b_lr) ]) net.__setattr__(layername, deconv) return deconv
def Deconvolution(caffe_net, layer_idx, bottom_blob, out_planes, kernel_size=2, stride=2, padding=0, skip_connect=None): names = ['deconv{}'.format(layer_idx)] caffe_net[names[0]] = L.Deconvolution(bottom_blob, param={ "lr_mult": 1, "decay_mult": 1 }, convolution_param=dict( num_output=out_planes, bias_term=False, kernel_size=kernel_size, stride=stride, pad=padding)) bottom_blob = caffe_net[names[0]] if skip_connect is not None: caffe_net['res{}'.format(layer_idx)] = L.Eltwise( bottom_blob, skip_connect, operation=P.Eltwise.SUM, coeff=[1, 1]) bottom_blob = caffe_net['res{}'.format(layer_idx)] return bottom_blob, layer_idx + 1
def upconv(run_shape, bottom, num_output_dec, num_output_conv, weight_std=0.01, kernel_size=[2], stride=[2]): # Shape update rules update = [lambda x: 0, lambda x: 0, lambda x: num_output_dec] update += [[lambda x: x, lambda x: x, lambda x: x]] update += [[lambda x, i=i: kernel_size[min(i,len(kernel_size)-1)] * x for i in range(0,len(run_shape[-1][4]))]] update_shape(run_shape, update) deconv = L.Deconvolution(bottom, convolution_param=dict(num_output=num_output_dec, kernel_size=kernel_size, stride=stride, pad=[0], kstride=[1], group=num_output_dec, weight_filler=dict(type='constant', value=1), bias_term=False), param=dict(lr_mult=0, decay_mult=0)) # The convolution buffer and weight memory weight_mem = fsize * num_output_conv * num_output_dec conv_buff = fsize * run_shape[-1][2] for i in range(0,len(run_shape[-1][4])): conv_buff *= 2 conv_buff *= run_shape[-1][4][i] # Shape update rules update = [lambda x: conv_buff, lambda x: weight_mem, lambda x: num_output_conv] update += [[lambda x: x, lambda x: x, lambda x: x]] update += [[lambda x, i=i: x for i in range(0,len(run_shape[-1][4]))]] update_shape(run_shape, update) conv = L.Convolution(deconv, num_output=num_output_conv, kernel_size=[1], stride=[1], pad=[0], kstride=[1], group=1, param=[dict(lr_mult=1),dict(lr_mult=2)], weight_filler=dict(type='gaussian', std=weight_std), bias_filler=dict(type='constant')) return deconv, conv
def make_upsample(bottom, num_classes): return L.Deconvolution( bottom, param=[dict(lr_mult=0, decay_mult=0)], convolution_param=dict( bias_term=False, num_output=num_classes, kernel_size=16, stride=8, group=num_classes, pad=4, weight_filler=dict(type="bilinear")))
def upsample_merge(n, data, to_upsample, width, iters): deconv = L.Deconvolution(to_upsample, convolution_param=dict(num_output=width, kernel_size=2, stride=2, weight_filler=get_filler(), bias_filler=dict([ ('type', 'constant'), ('value', 0) ])), param=[{ "lr_mult": 1, "decay_mult": 1 }, { "lr_mult": 2, "decay_mult": 0 }]) prelu = L.PReLU(deconv) concat = L.Concat(data, prelu) netset(n, "concat_up_" + str(width), concat) left = left_branch(concat, width * 2, iters) netset(n, "left_branch_up_" + str(width), left) eltwise = L.Eltwise(concat, left, operation=P.Eltwise.SUM) netset(n, "elementwise_up_" + str(width), eltwise) return L.PReLU(eltwise, in_place=True)
def vgg16_ssd_seg(source, bbox_seg_data_param, kwargs, use_batchnorm=False, lr_mult=1): net = caffe.NetSpec() net.data, net.bbox, net.seg = L.BBoxSegData(name="data", annotated_data_param=bbox_seg_data_param, data_param=dict(batch_size=8, backend=P.Data.LMDB, source=source), ntop=3, **kwargs) net.cls_specific_bbox, net.binary_mask, net.cls = L.SelectBinary(net.bbox, net.seg, random_select=True, num_class=20, ntop=3) VGGNetBody(net, from_layer='data', fully_conv=True, reduced=True, dilated=True, dropout=False, pool_mask=True, freeze_all=True) AddExtraLayers(net, use_batchnorm, lr_mult=0) # class vector embedding deconvolution net for class-specific semantic segmentation net.cls_reshape = L.Reshape(net.cls, shape=dict(dim=[0, 0, 1, 1])) # add top-down deconvolution net # mbox_source_layers = ['conv4_3', 'fc7', 'conv6_2', 'conv7_2', 'conv8_2', 'conv9_2'] AddExtraTopDownLayers(net, use_batchnorm=True, lr_mult=1) DeVGGNetBody(net, from_layer='deconv6_1', fully_conv=True, reduced=True, dilated=True, dropout=False, pool_mask=True) dekwargs = { 'weight_filler': dict(type='xavier'), 'bias_filler': dict(type='constant', value=0)} deparam = {'param': [dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]} net.seg_score = L.Deconvolution(net.derelu1_1, convolution_param=dict(num_output=2, pad=1, kernel_size=3, **dekwargs), **deparam) net.seg_loss = L.SoftmaxWithLoss(net.seg_score, net.binary_mask, loss_param=dict(ignore_label=255)) return net.to_proto()
def coord_net_spec(ks=3, stride=1, pad=0, pool=2, dstride=2, dpad=0): """ Define net spec for simple conv-pool-deconv pattern common to all coordinate mapping tests. """ n = caffe.NetSpec() n.data = L.Input(shape=dict(dim=[2, 1, 100, 100])) n.aux = L.Input(shape=dict(dim=[2, 1, 20, 20])) n.conv = L.Convolution(n.data, num_output=10, kernel_size=ks, stride=stride, pad=pad) n.pool = L.Pooling(n.conv, pool=P.Pooling.MAX, kernel_size=pool, stride=pool, pad=0) # for upsampling kernel size is 2x stride try: deconv_ks = [s * 2 for s in dstride] except: deconv_ks = dstride * 2 n.deconv = L.Deconvolution(n.pool, num_output=10, kernel_size=deconv_ks, stride=dstride, pad=dpad) return n
def deconv(prev, nout, ks=4, stride=2, bias_term=False): return layers.Deconvolution(prev, convolution_param=dict(num_output=nout, kernel_size=ks, stride=stride, bias_term=bias_term), param=[dict(lr_mult=0)])
def upconv(netconf, bottom, num_output_dec, num_output_conv, kernel_size=[2], stride=[2], stable_mode=False): # Stable mode is the more numerically stable pathway if stable_mode: deconv = L.Deconvolution(bottom, convolution_param=dict(num_output=num_output_dec, kernel_size=kernel_size, stride=stride, pad=[0], group=num_output_dec, weight_filler=dict(type='constant', value=1), bias_term=False), param=dict(lr_mult=0, decay_mult=0)) conv = L.Convolution(deconv, num_output=num_output_conv, kernel_size=[1], stride=[1], pad=[0], group=1, param=[dict(lr_mult=1),dict(lr_mult=2)], weight_filler=dict(type='msra'), bias_filler=dict(type='constant')) return conv else: deconv = L.Deconvolution(bottom, convolution_param=dict(num_output=num_output_conv, kernel_size=kernel_size, stride=stride, pad=[0], group=1, weight_filler=dict(type='msra'), bias_filler=dict(type='constant')),param=[dict(lr_mult=1),dict(lr_mult=2)]) return deconv
def deconv_bn_relu(bottom, num_output, pad=0, kernel_size=3, stride=1): deconv = L.Deconvolution(bottom, param=[dict(lr_mult=1, decay_mult=1)], convolution_param=dict( num_output=num_output, pad=pad, kernel_size=kernel_size, stride=stride, weight_filler=dict(type='msra'), bias_term=0, engine=engine)) bn = L.BatchNorm(deconv, in_place=True, use_global_stats=use_global_stats, param=[dict(lr_mult=0), dict(lr_mult=0), dict(lr_mult=0)]) scale = L.Scale(bn, in_place=True, axis=1, filler=dict(type='constant', value=1), bias_term=1, bias_filler=dict(type='constant', value=0)) relu = L.ReLU(scale, in_place=True, engine=engine) return deconv, bn, scale, relu
def deconv_act(netconf, bottom, num_output, kernel_size=[3], stride=[1], pad=[0], dilation=[1], group=1, activation='relu'): deconv = L.Deconvolution(bottom, convolution_param=dict(kernel_size=kernel_size, stride=stride, dilation=dilation, num_output=num_output, pad=pad, group=group, weight_filler=dict(type='msra'), bias_filler=dict(type='constant')), param=[dict(lr_mult=1),dict(lr_mult=2)]) # Activation if activation == 'relu': relu = L.ReLU(deconv, in_place=True, negative_slope=netconf.relu_slope) last = relu if activation == 'tanh': tanh = L.Tanh(deconv, in_place=True) last = tanh if activation == 'sigmoid': sigm = L.Sigmoid(deconv, in_place=True) last = sigm if (netconf.dropout > 0): drop = L.Dropout(last, in_place=True, dropout_ratio=netconf.dropout) last = drop if (netconf.use_batchnorm == True): bnltrain = L.BatchNorm(last, in_place=True, include=[dict(phase=0)], param=[dict(lr_mult=0,decay_mult=0),dict(lr_mult=0,decay_mult=0),dict(lr_mult=0,decay_mult=0)], batch_norm_param=dict(use_global_stats=False, moving_average_fraction=netconf.batchnorm_maf)) bnltest = L.BatchNorm(last, in_place=True, include=[dict(phase=1)], param=[dict(lr_mult=0,decay_mult=0),dict(lr_mult=0,decay_mult=0),dict(lr_mult=0,decay_mult=0)], batch_norm_param=dict(use_global_stats=True, moving_average_fraction=netconf.batchnorm_maf)) last = {bnltrain, bnltest} return last
def deconv(input, num_output, kernel_size, stride, pad): return L.Deconvolution(input, convolution_param=dict(bias_term=False, num_output=num_output, kernel_size=kernel_size, stride=stride, pad=pad))
def conv1_autoencoder(split, batch_sz): n = caffe.NetSpec() n.data, n.label = L.ImageData(image_data_param=dict(source=split, batch_size=batch_sz, new_height=height, new_width=width, is_color=False), ntop=2) n.silence = L.Silence(n.label, ntop=0) n.flatdata_i = L.Flatten(n.data) n.conv1 = conv(n.data, 5, 5, 64, pad=2) n.bn1 = L.BatchNorm(n.conv1, use_global_stats=False, in_place=True, param=[{ "lr_mult": 0 }, { "lr_mult": 0 }, { "lr_mult": 0 }]) n.scale1 = L.Scale(n.bn1, bias_term=True, in_place=True) n.relu1 = L.ReLU(n.scale1, relu_param=dict(negative_slope=0.1)) n.pool1 = max_pool(n.relu1, 2, stride=2) n.code = conv(n.pool1, 5, 5, 64, pad=2) n.upsample1 = L.Deconvolution(n.code, param=dict(lr_mult=0, decay_mult=0), convolution_param=dict( group=64, num_output=64, kernel_size=4, stride=2, pad=1, bias_term=False, weight_filler=dict(type="bilinear"))) n.deconv1 = conv(n.upsample1, 5, 5, 1, pad=2) n.debn1 = L.BatchNorm(n.deconv1, use_global_stats=False, in_place=True, param=[{ "lr_mult": 0 }, { "lr_mult": 0 }, { "lr_mult": 0 }]) n.descale1 = L.Scale(n.debn1, bias_term=True, in_place=True) n.derelu1 = L.ReLU(n.descale1, relu_param=dict(negative_slope=0.1)) n.flatdata_o = L.Flatten(n.derelu1) n.loss_s = L.SigmoidCrossEntropyLoss(n.flatdata_o, n.flatdata_i, loss_weight=1) n.loss_e = L.EuclideanLoss(n.flatdata_o, n.flatdata_i, loss_weight=0) return str(n.to_proto())
def deconv_crop_layer(bottom, bottom2,num_out, size_kerbel,size_stride,num_offset): deconv1 = L.Deconvolution(bottom, convolution_param = dict(num_output = num_out, kernel_size = size_kerbel, stride = size_stride), param = [{'lr_mult':0,'decay_mult':1},{'lr_mult':0, 'decay_mult':0}]) feature_dsn = L.Crop(deconv1,bottom2, crop_param = dict(axis = 2, offset = num_offset)) return feature_dsn
def generate_net(): n = caffe.NetSpec() n.data = L.Data(source=TRAIN_LMDB_DATA_FILE, backend=P.Data.LMDB, batch_size=1, ntop=1, transform_param=dict(scale=1. / 255)) n.label = L.Data(source=TRAIN_LMDB_LABEL_FILE, backend=P.Data.LMDB, batch_size=1, ntop=1) # the base net n.conv1, n.relu1 = conv_relu(n.data, 11, 96, stride=4, pad=100) n.pool1 = max_pool(n.relu1, 3, stride=2) n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75) n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2) n.pool2 = max_pool(n.relu2, 3, stride=2) n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75) n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1) n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2) n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2) n.pool5 = max_pool(n.relu5, 3, stride=2) # fully conv n.fc6, n.relu6 = conv_relu(n.pool5, 6, 4096) n.drop6 = L.Dropout(n.relu6, dropout_ratio=0.5, in_place=True) n.fc7, n.relu7 = conv_relu(n.drop6, 1, 4096) n.drop7 = L.Dropout(n.relu7, dropout_ratio=0.5, in_place=True) # weight_filler=dict(type='gaussian', std=0.0001), bias_filler=dict(type='constant') n.score_fr_ = L.Convolution( n.drop7, num_output=2, kernel_size=1, pad=0, param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)]) n.upscore_ = L.Deconvolution(n.score_fr_, convolution_param=dict( num_output=2, kernel_size=63, stride=32, group=2, bias_term=False, weight_filler=dict(type='bilinear')), param=[dict(lr_mult=0)]) n.score = crop(n.upscore_, n.data) n.loss = L.SoftmaxWithLoss(n.score, n.label, loss_weight=1, loss_param=dict(normalize=False, ignore_label=255)) return n.to_proto()