Пример #1
0
def buildnet( inputdb, mean_file, batch_size, height, width, nchannels, net_type="train"):
    net = caffe.NetSpec()

    crop_size = -1
    if augment_data:
        crop_size = width

    train = False
    if net_type=="train":
        train = True

    data_layers,label = lt.data_layer_stacked( net, inputdb, mean_file, batch_size, net_type, height, width, nchannels, crop_size=crop_size )

    # First conv  layer
    conv1 = lt.convolution_layer( net, data_layers[0], "conv1", "conv1", 16, 2, 7, 3, 0.05, addbatchnorm=True, train=train )
    pool1 = lt.pool_layer( net, conv1, "pool1", 5, 3 )

    resnet2  = lt.resnet_module( net, pool1,   "resnet2", 16, 3, 1, 1,8,16, use_batch_norm, train)
    resnet3  = lt.resnet_module( net, resnet2, "resnet3", 16, 3, 1, 1,8,16, use_batch_norm, train)
    resnet4  = lt.resnet_module( net, resnet3, "resnet4", 16, 3, 1, 1,8,32, use_batch_norm, train)
    
    resnet5  = lt.resnet_module( net, resnet4, "resnet5", 32, 3, 1, 1,8,32, use_batch_norm, train)
    resnet6  = lt.resnet_module( net, resnet5, "resnet6", 32, 3, 1, 1,8,32, use_batch_norm, train)
    resnet7  = lt.resnet_module( net, resnet6, "resnet7", 32, 3, 1, 1,16,64, use_batch_norm, train)

    resnet8  = lt.resnet_module( net, resnet7, "resnet8", 64,  3, 1, 1,16,64, use_batch_norm, train)
    resnet9  = lt.resnet_module( net, resnet8, "resnet9", 64, 3, 1, 1, 16,64, use_batch_norm, train)
    resnet10 = lt.resnet_module( net, resnet9, "resnet10", 64, 3, 1, 1,32,128, use_batch_norm, train)
        
    net.lastpool = lt.pool_layer( net, resnet10, "lastpool", 7, 1, P.Pooling.AVE )
    lastpool_layer = net.lastpool

    if use_dropout:
        net.lastpool_dropout = L.Dropout(net.lastpool,
                                         in_place=True,
                                         dropout_param=dict(dropout_ratio=0.5))
        lastpool_layer = net.lastpool_dropout
    

    fc2 = lt.final_fully_connect( net, lastpool_layer )
    
    if train:
        net.loss = L.SoftmaxWithLoss(fc2, net.label )
        net.acc = L.Accuracy(fc2,net.label)
    else:
        net.probt = L.Softmax( fc2 )
        net.acc = L.Accuracy(fc2,net.label)

    return net
Пример #2
0
def inceptionB( net, corename, bot, addbatchnorm=True, train=True ):
    apa   = lt.pool_layer( net, bot, "IB_avepool_%s"%(corename), 3, 1, pooltype=P.Pooling.AVE, pad_w=1, pad_h=1 )
    conva = lt.convolution_layer( net, apa, "IB_conva_%s"%(corename), "IB_conva_%s"%(corename),
                                  128, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train )
    
    convb = lt.convolution_layer( net, bot, "IB_convb_%s"%(corename), "IB_convb_%s"%(corename),
                                  384, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train )
    
    convc1 = lt.convolution_layer( net, bot, "IB_convc1_%s"%(corename), "IB_convc1_%s"%(corename),
                                   192, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train )
    convc2 = lt.convolution_layer( net, convc1, "IB_convc2_%s"%(corename), "IB_convc2_%s"%(corename),
                                   224, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=1, kernel_w=7, pad_h=0, pad_w=3 )
    convc3 = lt.convolution_layer( net, convc2, "IB_convc3_%s"%(corename), "IB_convc3_%s"%(corename),
                                   256, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=7, kernel_w=1, pad_h=3, pad_w=0 )

    convd1 = lt.convolution_layer( net, bot, "IB_convd1_%s"%(corename), "IB_convd1_%s"%(corename),
                                   192, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train )
    convd2 = lt.convolution_layer( net, convd1, "IB_convd2_%s"%(corename),"IB_convd2_%s"%(corename),
                                   192, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=1, kernel_w=7, pad_h=0, pad_w=3 )
    convd3 = lt.convolution_layer( net, convd2, "IB_convd3_%s"%(corename),"IB_convd3_%s"%(corename),
                                   224, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=7, kernel_w=1, pad_h=3, pad_w=0 )
    convd4 = lt.convolution_layer( net, convd3, "IB_convd4_%s"%(corename),"IB_convd4_%s"%(corename),
                                   224, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=1, kernel_w=7, pad_h=0, pad_w=3 )
    convd5 = lt.convolution_layer( net, convd4, "IB_convd5_%s"%(corename),"IB_convd5_%s"%(corename),
                                   256, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=7, kernel_w=1, pad_h=3, pad_w=0 )

    ls = [ conva, convb, convc3, convd5 ]
    cat = lt.concat_layer( net, "inductB_concat_%s"%(corename), *ls )
    
    return cat
Пример #3
0
def build_stem( corename, net, data_top, addbatchnorm=True, train=True ):
    conv0 = lt.convolution_layer( net, data_top, "stem_conv0_%s"%(corename), "stem_conv0_%s"%(corename), 
                                  32, 3, 7, 3, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_h=7, kernel_w=7, pad_h=2, pad_w=2 )
    conv1 = lt.convolution_layer( net, conv0, "stem_conv1_%s"%(corename), "stem_conv1_%s"%(corename), 
                                  32, 2, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train )
    conv2 = lt.convolution_layer( net, conv1,    "stem_conv2_%s"%(corename), "stem_conv2_%s"%(corename), 
                                  32, 1, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train )
    conv3 = lt.convolution_layer( net, conv2,    "stem_conv3_%s"%(corename), "stem_conv3_%s"%(corename), 
                                  32, 1, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train )
    mp    = lt.pool_layer( net, conv3, "stem_mp1_%s"%(corename), 3, 2 )
    conv4 = lt.convolution_layer( net, conv3, "stem_conv4_%s"%(corename), "stem_conv4_%s"%(corename),
                                  32, 2, 3, 0, 0.0, addbatchnorm=addbatchnorm, train=train )
    ls    = [mp,conv4]
    cat   = lt.concat_layer( net, "stem_concat_%s"%(corename), *ls )

    # split 7s
    # use same padding up to 3x3
    conv_5a = lt.convolution_layer( net, cat,     "stem_conv5a_%s"%(corename), "stem_conv5a_%s"%(corename),
                                    64, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train )
    conv_5b = lt.convolution_layer( net, conv_5a, "stem_conv5b_%s"%(corename), "stem_conv5b_%s"%(corename),
                                    64, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_h=1, kernel_w=7, pad_h=0, pad_w=3 )
    conv_5c = lt.convolution_layer( net, conv_5b, "stem_conv5c_%s"%(corename), "stem_conv5c_%s"%(corename),
                                    64, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_h=7, kernel_w=1, pad_h=3, pad_w=0 )
    conv_5d = lt.convolution_layer( net, conv_5c, "stem_conv5d_%s"%(corename), "stem_conv5d_%s"%(corename),
                                    64, 1, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train )

    # split 3
    conv_6a = lt.convolution_layer( net, cat, "stem_conv6a_%s"%(corename), "stem_conv6a_%s"%(corename),
                                    96, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train )
    conv_6b = lt.convolution_layer( net, conv_6a, "stem_conv6b_%s"%(corename), "stem_conv6b_%s"%(corename),
                                    96, 1, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train )
    ls2  = [conv_5d, conv_6b]
    cat2 = lt.concat_layer( net, "stem_concat2_%s"%(corename), *ls2 )

    # split 2
    conv7  = lt.convolution_layer( net, cat2, "stem_conv7_%s"%(corename), "stem_conv7_%s"%(corename),
                                   192, 2, 3, 0, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_h=3, kernel_w=3, pad_h=1, pad_w=0 )
    mp7    = lt.pool_layer( net, cat2, "stem_mp7_%s"%(corename), 3, 2 )

    ls3 = [ conv7, mp7 ]
    cat3 = lt.concat_layer( net, "stem_concat3_%s"%(corename), *ls3 )

    nout = 352

    return cat3, nout
Пример #4
0
def buildnet( processcfg, batch_size, height, width, nchannels, user_batch_norm, net_type="train", targetplane=None):
    net = caffe.NetSpec()

    train = False
    if net_type=="train":
        train = True

    data_layers, label = root_data_layer_trimese( net, batch_size, processcfg, net_type, [1,2] )
    stems = []
    for n,data_layer in enumerate(data_layers):
        if targetplane is not None and n!=targetplane:
            stems.append( data_layer )
            continue
        outstem,nout = build_stem( "plane%d"%(n), net, data_layer, addbatchnorm=True, train=train )
        ia1     = inceptionA( net, "ia1_plane%d"%(n), outstem, nout, 256, 32, addbatchnorm=True, train=train )
        ia2     = inceptionA( net, "ia2_plane%d"%(n),     ia1, 256, 256, 32, addbatchnorm=True, train=train )
        ia3     = inceptionA( net, "ia3_plane%d"%(n),     ia2, 256, 256, 32, addbatchnorm=True, train=train )
        reda    = reductionA( net, "plane%d"%(n),       ia3, 32, 32, 32, 32, addbatchnorm=True, train=train )
        #ib1     = inceptionB( net, "ib1_plane%d"%(n), reda, addbatchnorm=True, train=train )
        #ib2     = inceptionB( net, "ib2_plane%d"%(n),  ib1, addbatchnorm=True, train=train )
        #ib3     = inceptionB( net, "ib3_plane%d"%(n),  ib2, addbatchnorm=True, train=train )
        ib1     = inceptionResB( net, "ib1_plane%d"%(n), reda, 320, 256, 32, addbatchnorm=True, train=train )
        ib2     = inceptionResB( net, "ib2_plane%d"%(n),  ib1, 256, 256, 32, addbatchnorm=True, train=train )
        ib3     = inceptionResB( net, "ib3_plane%d"%(n),  ib2, 256, 256, 32, addbatchnorm=True, train=train )
        redb    = reductionB( net, "plane%d"%(n), ib3 )
        ic1     = inceptionResC( net, "ic1_plane%d"%(n), redb, 320, 256, 192, addbatchnorm=True, train=train )
        ic2     = inceptionResC( net, "ic2_plane%d"%(n),  ic1, 256, 256, 192, addbatchnorm=True, train=train )
        ic3     = inceptionResC( net, "ic3_plane%d"%(n),  ic2, 256, 256, 192, addbatchnorm=True, train=train )
        avepool = lt.pool_layer( net, ic3, "lastpool_plane%d"%(n), 1, 1, P.Pooling.AVE )
        stems.append( avepool ) # no batch norm for stem. too many parameters!

    if targetplane is None:
        concat = lt.concat_layer( net, "mergeplanes", *stems )
        fc2 = lt.final_fully_connect( net, concat, "fc2", nclasses=2 )
    else:
        for n,stem in enumerate(stems):
            if n==targetplane:
                fc2 = lt.final_fully_connect( net, stems[n], "fc2_plane%donly"%(targetplane), nclasses=2 )
            else:
                sil = L.Silence( stems[n], ntop=0 )
                net.__setattr__( "silence_plane%d"%(n), sil )
    
    
    if train:
        net.loss = L.SoftmaxWithLoss(fc2, net.label )
        net.acc = L.Accuracy(fc2,net.label)
    else:
        net.probt = L.Softmax( fc2 )
        net.acc = L.Accuracy(fc2,net.label)

    return net
Пример #5
0
def reductionA( net, corename, bot, noutN, noutK, noutL, noutM, addbatchnorm=True, train=True ):
    mpa    = lt.pool_layer( net, bot, "reducA_mpA_%s"%(corename), 3, 2 )
    
    convb  = lt.convolution_layer( net, bot, "reducA_convb_%s"%(corename), "reducA_convb_%s"%(corename),
                                   noutN, 2, 3, 0, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_w=3, kernel_h=3, pad_w=0, pad_h=0 )

    convc1 = lt.convolution_layer( net, bot, "reducA_convc1_%s"%(corename), "reducA_convc1_%s"%(corename),
                                   noutK, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train )
    convc2 = lt.convolution_layer( net, convc1, "reducA_convc2_%s"%(corename), "reducA_convc2_%s"%(corename),
                                   noutL, 1, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train )
    convc3 = lt.convolution_layer( net, convc2, "reducA_convc3_%s"%(corename), "reducA_convc3_%s"%(corename),
                                   noutM, 2, 3, 0, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_w=3, kernel_h=3, pad_w=0, pad_h=0 )

    ls  = [mpa,convb,convc3]
    cat = lt.concat_layer( net, "reducA_concat_%s"%(corename), *ls )

    return cat
Пример #6
0
def reductionB( net, corename, bot, addbatchnorm=True, train=True ):
    mpa = lt.pool_layer( net, bot, "reducB_mpB_%s"%(corename), 3, 2, pad_w=0 )
    
    convb1 = lt.convolution_layer( net, bot, "reducB_convb1_%s"%(corename), "reducB_convb1_%s"%(corename), 
                                   192, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm, train=train )
    convb2 = lt.convolution_layer( net, convb1, "reducB_convb2_%s"%(corename), "reducB_convb2_%s"%(corename),
                                   192, 2, 3, 1, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_w=3, kernel_h=3, pad_w=0, pad_h=0 )
    
    convc1 = lt.convolution_layer( net, bot, "reducB_convc1_%s"%(corename), "reducB_convc1_%s"%(corename),
                                   256, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train )
    convc2 = lt.convolution_layer( net, convc1, "reducB_convc2_%s"%(corename), "reducB_convc2_%s"%(corename),
                                   256, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=1, kernel_w=7, pad_h=0, pad_w=3 )
    convc3 = lt.convolution_layer( net, convc2, "reducB_convc3_%s"%(corename), "reducB_convc3_%s"%(corename),
                                   320, 1, 1, 0, 0.0, addbatchnorm=addbatchnorm,train=train, kernel_h=7, kernel_w=1, pad_h=3, pad_w=0 )
    convc4 = lt.convolution_layer( net, convc3, "reducB_convc4_%s"%(corename), "reducB_convc4_%s"%(corename),
                                   320, 2, 3, 0, 0.0, addbatchnorm=addbatchnorm, train=train, kernel_w=3, kernel_h=3, pad_w=0, pad_h=0 )

    ls = [ mpa, convb2, convc4 ]
    cat = lt.concat_layer( net, "reducB_concat_%s"%(corename), *ls )

    return cat
Пример #7
0
def buildnet(inputdb,
             mean_file,
             batch_size,
             height,
             width,
             nchannels,
             net_type="train"):
    net = caffe.NetSpec()

    crop_size = -1
    if augment_data:
        crop_size = width

    train = False
    if net_type == "train":
        train = True

    data_layers, label = lt.data_layer_trimese(net,
                                               inputdb,
                                               mean_file,
                                               batch_size,
                                               net_type,
                                               height,
                                               width,
                                               nchannels, [4, 8],
                                               crop_size=768)

    # First conv  layer
    branch_ends = []
    for n, layer in enumerate(data_layers):
        conv1 = lt.convolution_layer(net,
                                     layer,
                                     "plane%d_conv1" % (n),
                                     "tri_conv1",
                                     32,
                                     2,
                                     7,
                                     3,
                                     0.05,
                                     addbatchnorm=True,
                                     train=train)
        pool1 = lt.pool_layer(net, conv1, "plane%d_pool1" % (n), 3, 1)

        conv2 = lt.convolution_layer(net,
                                     pool1,
                                     "plane%d_conv2" % (n),
                                     "tri_conv2",
                                     16,
                                     2,
                                     3,
                                     3,
                                     0.05,
                                     addbatchnorm=True,
                                     train=train)

        conv3 = lt.convolution_layer(net,
                                     conv2,
                                     "plane%d_conv3" % (n),
                                     "tri_conv3",
                                     16,
                                     2,
                                     3,
                                     3,
                                     0.05,
                                     addbatchnorm=True,
                                     train=train)

        pool3 = lt.pool_layer(net, conv3, "plane%d_pool3" % (n), 3, 1)

        branch_ends.append(pool3)

    concat = lt.concat_layer(net, "mergeplanes", *branch_ends)

    resnet1 = lt.resnet_module(net, concat, "resnet1", 16 * 3, 3, 1, 1, 8, 16,
                               use_batch_norm, train)
    resnet2 = lt.resnet_module(net, resnet1, "resnet2", 16, 3, 1, 1, 8, 16,
                               use_batch_norm, train)
    resnet3 = lt.resnet_module(net, resnet2, "resnet3", 16, 3, 1, 1, 8, 32,
                               use_batch_norm, train)

    resnet4 = lt.resnet_module(net, resnet3, "resnet4", 32, 3, 1, 1, 8, 32,
                               use_batch_norm, train)
    resnet5 = lt.resnet_module(net, resnet4, "resnet5", 32, 3, 1, 1, 8, 32,
                               use_batch_norm, train)
    resnet6 = lt.resnet_module(net, resnet5, "resnet6", 32, 3, 1, 1, 16, 64,
                               use_batch_norm, train)

    resnet7 = lt.resnet_module(net, resnet6, "resnet7", 64, 3, 1, 1, 16, 64,
                               use_batch_norm, train)
    resnet8 = lt.resnet_module(net, resnet7, "resnet8", 64, 3, 1, 1, 16, 64,
                               use_batch_norm, train)
    resnet9 = lt.resnet_module(net, resnet8, "resnet9", 64, 3, 1, 1, 32, 128,
                               use_batch_norm, train)

    net.lastpool = lt.pool_layer(net, resnet9, "lastpool", 7, 1, P.Pooling.AVE)
    lastpool_layer = net.lastpool

    if use_dropout:
        net.lastpool_dropout = L.Dropout(net.lastpool,
                                         in_place=True,
                                         dropout_param=dict(dropout_ratio=0.5))
        lastpool_layer = net.lastpool_dropout

    fc1 = lt.final_fully_connect(net, lastpool_layer, nclasses=256)
    fc2 = lt.final_fully_connect(net, fc1, nclasses=4096)
    fc3 = lt.final_fully_connect(net, fc2, nclasses=2)

    if train:
        net.loss = L.SoftmaxWithLoss(fc3, net.label)
        net.acc = L.Accuracy(fc3, net.label)
    else:
        net.probt = L.Softmax(fc3)
        net.acc = L.Accuracy(fc3, net.label)

    return net
Пример #8
0
def buildnet( inputdb, mean_file, batch_size, height, width, nchannels, net_type="train"):
    net = caffe.NetSpec()

    crop_size = -1
    if augment_data:
        crop_size = width

    train = False
    if net_type=="train":
        train = True

    data_layers,label = lt.data_layer_trimese( net, inputdb, mean_file, batch_size, net_type, height, width, nchannels, [1,2], crop_size=768 )

    # First conv  layer
    branch_ends = []
    for n,layer in enumerate(data_layers):
        conv1 = lt.convolution_layer( net, layer, "plane%d_conv1"%(n), "tri_conv1_plane%d"%(n), 64, 2, 5, 3, 0.05, addbatchnorm=True, train=train )
        pool1 = lt.pool_layer( net, conv1, "plane%d_pool1"%(n), 3, 1 )

        conv2 = lt.convolution_layer( net, pool1, "plane%d_conv2"%(n), "tri_conv2_plane%d"%(n), 64, 2, 3, 3, 0.05, addbatchnorm=True, train=train )
        
        conv3 = lt.convolution_layer( net, conv2, "plane%d_conv3"%(n), "tri_conv3_plane%d"%(n), 64, 2, 3, 3, 0.05, addbatchnorm=True, train=train )

        pool3 = lt.pool_layer( net, conv3, "plane%d_pool3"%(n), 3, 1 )

        branch_ends.append( pool3 )
        
    concat = lt.concat_layer( net, "mergeplanes", *branch_ends )


    resnet1  = lt.resnet_module( net, concat,  "resnet1", 64*3, 3, 1, 1,8,32, use_batch_norm, train)
    resnet2  = lt.resnet_module( net, resnet1, "resnet2", 32, 3, 1, 1,8,32, use_batch_norm, train)
    resnet3  = lt.resnet_module( net, resnet2, "resnet3", 32, 3, 1, 1,16,64, use_batch_norm, train)
    
    resnet4  = lt.resnet_module( net, resnet3, "resnet4", 64, 3, 1, 1,16,64, use_batch_norm, train)
    resnet5  = lt.resnet_module( net, resnet4, "resnet5", 64, 3, 1, 1,16,64, use_batch_norm, train)
    resnet6  = lt.resnet_module( net, resnet5, "resnet6", 64, 3, 1, 1,32,128, use_batch_norm, train)

    resnet7  = lt.resnet_module( net, resnet6, "resnet7", 128, 3, 1, 1, 32,128, use_batch_norm, train)
    resnet8  = lt.resnet_module( net, resnet7, "resnet8", 128, 3, 1, 1, 32,128, use_batch_norm, train)
    resnet9  = lt.resnet_module( net, resnet8, "resnet9", 128, 3, 1, 1, 64,256, use_batch_norm, train)
        
    net.lastpool = lt.pool_layer( net, resnet9, "lastpool", 5, 1, P.Pooling.AVE )
    lastpool_layer = net.lastpool
    
    if use_dropout:
        net.lastpool_dropout = L.Dropout(net.lastpool,
                                         in_place=True,
                                         dropout_param=dict(dropout_ratio=0.5))
        lastpool_layer = net.lastpool_dropout
    
    fc1 = lt.final_fully_connect( net, lastpool_layer, nclasses=512 )
    fc2 = lt.final_fully_connect( net, fc1, nclasses=4096 )
    fc3 = lt.final_fully_connect( net, fc2, nclasses=2 )
    
    if train:
        net.loss = L.SoftmaxWithLoss(fc3, net.label )
        net.acc = L.Accuracy(fc3,net.label)
    else:
        net.probt = L.Softmax( fc3 )
        net.acc = L.Accuracy(fc3,net.label)

    return net
Пример #9
0
def buildnet(inputdb,
             mean_file,
             batch_size,
             height,
             width,
             nchannels,
             net_type="train"):
    net = caffe.NetSpec()

    crop_size = -1
    if augment_data:
        crop_size = width

    train = False
    if net_type == "train":
        train = True

    data_layers, label = lt.data_layer_stacked(net,
                                               inputdb,
                                               mean_file,
                                               batch_size,
                                               net_type,
                                               height,
                                               width,
                                               nchannels,
                                               crop_size=crop_size)

    # First conv  layer
    conv1 = lt.convolution_layer(net,
                                 data_layers[0],
                                 "conv1",
                                 "conv1",
                                 16,
                                 2,
                                 7,
                                 3,
                                 0.05,
                                 addbatchnorm=True,
                                 train=train)
    pool1 = lt.pool_layer(net, conv1, "pool1", 5, 3)

    resnet2 = lt.resnet_module(net, pool1, "resnet2", 16, 3, 1, 1, 8, 16,
                               use_batch_norm, train)
    resnet3 = lt.resnet_module(net, resnet2, "resnet3", 16, 3, 1, 1, 8, 16,
                               use_batch_norm, train)
    resnet4 = lt.resnet_module(net, resnet3, "resnet4", 16, 3, 1, 1, 8, 32,
                               use_batch_norm, train)

    resnet5 = lt.resnet_module(net, resnet4, "resnet5", 32, 3, 1, 1, 8, 32,
                               use_batch_norm, train)
    resnet6 = lt.resnet_module(net, resnet5, "resnet6", 32, 3, 1, 1, 8, 32,
                               use_batch_norm, train)
    resnet7 = lt.resnet_module(net, resnet6, "resnet7", 32, 3, 1, 1, 16, 64,
                               use_batch_norm, train)

    resnet8 = lt.resnet_module(net, resnet7, "resnet8", 64, 3, 1, 1, 16, 64,
                               use_batch_norm, train)
    resnet9 = lt.resnet_module(net, resnet8, "resnet9", 64, 3, 1, 1, 16, 64,
                               use_batch_norm, train)
    resnet10 = lt.resnet_module(net, resnet9, "resnet10", 64, 3, 1, 1, 32, 128,
                                use_batch_norm, train)

    net.lastpool = lt.pool_layer(net, resnet10, "lastpool", 7, 1,
                                 P.Pooling.AVE)
    lastpool_layer = net.lastpool

    if use_dropout:
        net.lastpool_dropout = L.Dropout(net.lastpool,
                                         in_place=True,
                                         dropout_param=dict(dropout_ratio=0.5))
        lastpool_layer = net.lastpool_dropout

    fc2 = lt.final_fully_connect(net, lastpool_layer)

    if train:
        net.loss = L.SoftmaxWithLoss(fc2, net.label)
        net.acc = L.Accuracy(fc2, net.label)
    else:
        net.probt = L.Softmax(fc2)
        net.acc = L.Accuracy(fc2, net.label)

    return net
Пример #10
0
def buildnet(nclasses=3,
             cfgfile="filler.cfg",
             batch_size=2,
             base_numout=16,
             nchannels=1,
             net_type="",
             use_batch_norm=True):

    net = caffe.NetSpec()
    if net_type not in ["train", "test", "deploy"]:
        raise ValueError("Net type must be one of " + str(net_type))
        return
    if net_type == "train":
        train = True
    else:
        train = False

    data_layers, label = root_data_layer(net, batch_size, cfgfile, net_type)

    # several layers of ResNet, with maxpooling occuring

    # Contracting ends

    # resnet group 1
    in1 = 1
    out1 = base_numout
    bn1 = np.maximum(out1 / 4, 4)
    resnet1a = lt.resnet_module(net, data_layers[0], "resnet1a", in1, 3, 1, 1,
                                bn1, out1, True, train)  # always batch_norm
    resnet1b = lt.resnet_module(net, resnet1a, "resnet1b", out1, 3, 1, 1, bn1,
                                out1, True, train)  # always batch_norm
    maxpool1 = lt.pool_layer(net,
                             resnet1b,
                             "maxpool1",
                             3,
                             2,
                             P.Pooling.MAX,
                             pad_w=0,
                             pad_h=0)

    # resnet group 2
    in2 = out1
    out2 = in2 * 2
    bn2 = np.maximum(out2 / 4, 4)
    resnet2a = lt.resnet_module(net, maxpool1, "resnet2a", in2, 3, 1, 1, bn2,
                                out2, use_batch_norm, train)
    resnet2b = lt.resnet_module(net, resnet2a, "resnet2b", out2, 3, 1, 1, bn2,
                                out2, use_batch_norm, train)
    maxpool2 = lt.pool_layer(net,
                             resnet2b,
                             "maxpool2",
                             3,
                             2,
                             P.Pooling.MAX,
                             pad_w=0,
                             pad_h=0)

    # resnet group 3
    in3 = out2
    out3 = in3 * 2
    bn3 = np.maximum(out3 / 4, 4)
    resnet3a = lt.resnet_module(net, maxpool2, "resnet3a", in3, 3, 1, 1, bn3,
                                out3, use_batch_norm, train)
    resnet3b = lt.resnet_module(net, resnet3a, "resnet3b", out3, 3, 1, 1, bn3,
                                out3, use_batch_norm, train)
    maxpool3 = lt.pool_layer(net,
                             resnet3b,
                             "maxpool3",
                             3,
                             2,
                             P.Pooling.MAX,
                             pad_w=0,
                             pad_h=0)

    # resnet group 4
    in4 = out3
    out4 = in4 * 2
    bn4 = np.maximum(out4 / 4, 4)
    resnet4a = lt.resnet_module(net, maxpool3, "resnet4a", in4, 3, 1, 1, bn4,
                                out4, use_batch_norm, train)
    resnet4b = lt.resnet_module(net, resnet4a, "resnet4b", out4, 3, 1, 1, bn4,
                                out4, use_batch_norm, train)
    maxpool4 = lt.pool_layer(net,
                             resnet4b,
                             "maxpool4",
                             3,
                             2,
                             P.Pooling.MAX,
                             pad_w=0,
                             pad_h=0)

    # resnet group 5
    in5 = out4
    out5 = in5 * 2
    bn5 = np.maximum(out5 / 4, 4)
    resnet5a = lt.resnet_module(net, maxpool4, "resnet5a", in5, 3, 1, 1, bn5,
                                out5, use_batch_norm, train)
    resnet5b = lt.resnet_module(net, resnet5a, "resnet5b", out5, 3, 1, 1, bn5,
                                out5, use_batch_norm, train)

    # Expansive Part

    # resnet group 6
    unpool4 = lt.deconvolution_layer(net,
                                     resnet5b,
                                     "unpool4",
                                     4,
                                     2,
                                     1,
                                     out5,
                                     b_lr=0.0)
    input6 = [resnet4b, unpool4]  # skip connection
    merge5to6 = lt.concat_layer(net, "concat56", *input6)
    resnet6a = lt.resnet_module(net, merge5to6, "resnet6a", out4 + out5, 3, 1,
                                1, bn4, out4, use_batch_norm, train)
    resnet6b = lt.resnet_module(net, resnet6a, "resnet6b", out4, 3, 1, 1, bn4,
                                out4, use_batch_norm, train)

    # resnet group 7
    unpool3 = lt.deconvolution_layer(net,
                                     resnet6b,
                                     "unpool3",
                                     4,
                                     2,
                                     1,
                                     out4,
                                     b_lr=0.0)
    input7 = [resnet3b, unpool3]
    merge3to7 = lt.concat_layer(net, "concat37", *input7)
    resnet7a = lt.resnet_module(net, merge3to7, "resnet7a", out3 + out4, 3, 1,
                                1, bn3, out3, use_batch_norm, train)
    resnet7b = lt.resnet_module(net, resnet7a, "resnet7b", out3, 3, 1, 1, bn3,
                                out3, use_batch_norm, train)

    # resnet group 8
    unpool2 = lt.deconvolution_layer(net,
                                     resnet7b,
                                     "unpool2",
                                     4,
                                     2,
                                     1,
                                     out3,
                                     b_lr=0.0)
    input8 = [resnet2b, unpool2]
    merge2to8 = lt.concat_layer(net, "concat28", *input8)
    resnet8a = lt.resnet_module(net, merge2to8, "resnet8a", out2 + out3, 3, 1,
                                1, bn2, out2, use_batch_norm, train)
    resnet8b = lt.resnet_module(net, resnet8a, "resnet8b", out2, 3, 1, 1, bn2,
                                out2, use_batch_norm, train)

    # resnet group 9
    unpool1 = lt.deconvolution_layer(net,
                                     resnet8b,
                                     "unpool1",
                                     4,
                                     2,
                                     1,
                                     out2,
                                     b_lr=0.0)
    input9 = [resnet1b, unpool1]
    merge1to9 = lt.concat_layer(net, "concat19", *input9)
    resnet9a = lt.resnet_module(net, merge1to9, "resnet9a", out1 + out2, 3, 1,
                                1, bn1, out1, use_batch_norm, train)
    resnet9b = lt.resnet_module(net, resnet9a, "resnet9b", out1, 3, 1, 1, bn1,
                                out1, use_batch_norm, train)

    # 1 x 1
    score = lt.convolution_layer(net,
                                 resnet9b,
                                 "score",
                                 "score",
                                 nclasses + 1,
                                 1,
                                 1,
                                 0,
                                 0.0,
                                 addbatchnorm=False,
                                 train=train,
                                 add_relu=False,
                                 w_lr=1.0,
                                 b_lr=2.0,
                                 w_decay_mult=1.0,
                                 b_decay_mult=0.0)

    # crop
    offset = 0
    crop_score = L.Crop(score, label, crop_param=dict(axis=2, offset=offset))
    net.__setattr__("crop_score", crop_score)

    # softmax
    if net_type in ["train", "test"]:
        softmaxloss = L.SoftmaxWithLoss(
            crop_score,
            net.label,
            loss_param=dict(normalize=True,
                            class_loss_weights=[1, 1000, 1000, 1000]))
        net.__setattr__("softmaxloss", softmaxloss)

        acc = L.Accuracy(crop_score,
                         net.label,
                         accuracy_param=dict(top_k=1, ignore_label=0))
        net.__setattr__("accuracy", acc)

    elif net_type in ["deploy"]:
        softmax = L.Softmax(crop_score)
        net.__setattr__("softmax", softmax)

    # then deconv layers + skip connections

    return net