def __init__(self, vocab_size=10, rnn_unit='LSTM'): embed = embed_id.EmbedID(vocab_size, 10) if rnn_unit == 'LSTM': rnns = link.ChainList(lstm.LSTM(10, 20), lstm.LSTM(20, 20)) elif rnn_unit == 'GRU': rnns = link.ChainList(gru.StatefulGRU(20, 10), gru.StatefulGRU(20, 20)) else: raise ValueError('Invalid RNN unit:{}'.format(rnn_unit)) linears = link.ChainList(linear.Linear(20, 10), linear.Linear(10, vocab_size)) super(BigLSTM, self).__init__(embed=embed, rnns=rnns, linears=linears)
def __init__(self, label_dim): super(Classifier, self).__init__( ASPP=link.ChainList( ConvBN(2048, 512, 1, 1, 0, init_weights=None, pool=None), ConvBN(2048, 512, 1, 1, 0, init_weights=None, pool=None), DilatedConvBN(2048, 256, 3, 1, 3, 3, init_weights=None, pool=None), DilatedConvBN(2048, 256, 3, 1, 6, 6, init_weights=None, pool=None), DilatedConvBN(2048, 256, 3, 1, 9, 9, init_weights=None, pool=None), DilatedConvBN(2048, 256, 3, 1, 12, 12, init_weights=None, pool=None), ConvBN(2048, 1024, 1, 1, 0, init_weights=None, pool=None)), classifier=link.ChainList( Conv(1024, label_dim, 1, 1, 0, init_weights=None, pool=None, nobias=False)), )
def __init__(self, predictor, label_dim, args): super(ReIDClassifier, self).__init__(predictor=predictor, classifiers=link.ChainList( Conv(2048 * 3, label_dim, 1, 1, 0, init_weights=None, pool=None, nobias=False))) self.args = args
def __init__(self, label_dim): super(Classifier, self).__init__( ASPP=link.ChainList( ConvBN(2048, 512, 1, 1, 0, init_weights=None, pool=None), ConvBN(2048, 384, 1, 1, 0, init_weights=None, pool=None), DilatedConvBN(2048, 384, 3, 1, 4, 4, init_weights=None, pool=None), DilatedConvBN(2048, 384, 3, 1, 8, 8, init_weights=None, pool=None), DilatedConvBN(2048, 384, 3, 1, 16, 16, init_weights=None, pool=None), ConvBN(2048, 2048, 1, 1, 0, init_weights=None, pool=None)), classifier=link.ChainList( Conv(2048, label_dim, 1, 1, 0, init_weights=None, pool=None, nobias=False)), )
def __init__(self, channel_dim=32, hidden_dim=1760, out_dim=29, rnn_unit='Linear', use_cudnn=True): c1 = ConvBN(1, channel_dim, (5, 20), 2, use_cudnn=use_cudnn) c2 = ConvBN(channel_dim, channel_dim, (5, 10), (1, 2), use_cudnn=use_cudnn) convolution = Sequential(c1, c2) brnn1 = BRNN(31 * channel_dim, hidden_dim, rnn_unit=rnn_unit) brnn2 = BRNN(hidden_dim, hidden_dim, rnn_unit=rnn_unit) brnn3 = BRNN(hidden_dim, hidden_dim, rnn_unit=rnn_unit) brnn4 = BRNN(hidden_dim, hidden_dim, rnn_unit=rnn_unit) brnn5 = BRNN(hidden_dim, hidden_dim, rnn_unit=rnn_unit) brnn6 = BRNN(hidden_dim, hidden_dim, rnn_unit=rnn_unit) brnn7 = BRNN(hidden_dim, hidden_dim, rnn_unit=rnn_unit) recurrent = Sequential(brnn1, brnn2, brnn3, brnn4, brnn5, brnn6, brnn7) fc1 = LinearBN(hidden_dim, hidden_dim) fc2 = L.Linear(hidden_dim, out_dim) linear = link.ChainList(fc1, fc2) super(DeepSpeech2, self).__init__(convolution=convolution, recurrent=recurrent, linear=linear)
def __init__(self, predictor, classifiers, args): super(InceptionV3Classifier, self).__init__(predictor=predictor, classifiers=link.ChainList(*classifiers)) self.args = args
def __init__(self, args): convolution = link.ChainList(ConvBN(3, 32, 3, 2, 0, 'conv'), ConvBN(32, 32, 3, 1, 0, 'conv_1'), ConvBN(32, 64, 3, 1, 1, 'conv_2'), ConvBN(64, 80, 1, 1, 0, 'conv_3'), ConvBN(80, 192, 3, 1, 0, 'conv_4')) def inception_35(ich, pch, name): # 1x1 s1 = ConvBN(ich, 64, 1, 1, 0, name['1x1'][0]) # 5x5 s21 = ConvBN(ich, 48, 1, 1, 0, name['5x5'][0]) s22 = ConvBN(48, 64, 5, 1, 2, name['5x5'][1]) s2 = Sequential(s21, s22) # double 3x3 s31 = ConvBN(ich, 64, 1, 1, 0, name['3x3'][0]) s32 = ConvBN(64, 96, 3, 1, 1, name['3x3'][1]) s33 = ConvBN(96, 96, 3, 1, 1, name['3x3'][2]) s3 = Sequential(s31, s32, s33) # pool s4 = ConvBN(ich, pch, 1, 1, 0, name['pool'][1], pool=A.AveragePooling2D(3, 1, 1)) return Inception(s1, s2, s3, s4) inception35_names = ({ '1x1': ['mixed_conv'], '5x5': ['mixed_tower_conv', 'mixed_tower_conv_1'], '3x3': [ 'mixed_tower_1_conv', 'mixed_tower_1_conv_1', 'mixed_tower_1_conv_2' ], 'pool': ['mixed_tower_2_pool', 'mixed_tower_2_conv'] }, { '1x1': ['mixed_1_conv'], '5x5': ['mixed_1_tower_conv', 'mixed_1_tower_conv_1'], '3x3': [ 'mixed_1_tower_1_conv', 'mixed_1_tower_1_conv_1', 'mixed_1_tower_1_conv_2' ], 'pool': ['mixed_1_tower_2_pool', 'mixed_1_tower_2_conv'] }, { '1x1': ['mixed_2_conv'], '5x5': ['mixed_2_tower_conv', 'mixed_2_tower_conv_1'], '3x3': [ 'mixed_2_tower_1_conv', 'mixed_2_tower_1_conv_1', 'mixed_2_tower_1_conv_2' ], 'pool': ['mixed_2_tower_2_pool', 'mixed_2_tower_2_conv'] }) inception35 = Sequential(*[ inception_35(ich, pch, name) for ich, pch, name in zip( [192, 256, 288], [32, 64, 64], inception35_names) ]) reduction35 = Inception( # strided 3x3 ConvBN(288, 384, 3, 2, 0, 'mixed_3_conv'), # originally stride-pad: 2-0 # double 3x3 Sequential( ConvBN(288, 64, 1, 1, 0, 'mixed_3_tower_conv'), ConvBN(64, 96, 3, 1, 1, 'mixed_3_tower_conv_1'), ConvBN(96, 96, 3, 2, 0, 'mixed_3_tower_conv_2') # originally stride-pad: 2-0 ), # pool pool=M.MaxPooling2D(3, 2, 0, cover_all=False)) # originally stride-pad: 2-0 def inception_17(hidden_channel, name): # 1x1 s1 = ConvBN(768, 192, 1, 1, 0, name['1x1'][0]) # 7x7 s21 = ConvBN(768, hidden_channel, 1, 1, 0, name['7x7'][0]) s22 = ConvBN(hidden_channel, hidden_channel, (1, 7), (1, 1), (0, 3), name['7x7'][1]) s23 = ConvBN(hidden_channel, 192, (7, 1), (1, 1), (3, 0), name['7x7'][2]) s2 = Sequential(s21, s22, s23) # double 7x7 s31 = ConvBN(768, hidden_channel, 1, 1, 0, name['double7x7'][0]) s32 = ConvBN(hidden_channel, hidden_channel, (7, 1), (1, 1), (3, 0), name['double7x7'][1]) s33 = ConvBN(hidden_channel, hidden_channel, (1, 7), (1, 1), (0, 3), name['double7x7'][2]) s34 = ConvBN(hidden_channel, hidden_channel, (7, 1), (1, 1), (3, 0), name['double7x7'][3]) s35 = ConvBN(hidden_channel, 192, (1, 7), (1, 1), (0, 3), name['double7x7'][4]) s3 = Sequential(s31, s32, s33, s34, s35) # pool s4 = ConvBN(768, 192, 1, 1, 0, name['pool'][1], pool=A.AveragePooling2D(3, 1, 1)) return Inception(s1, s2, s3, s4) inception17_names = ({ '1x1': ['mixed_4_conv'], '7x7': [ 'mixed_4_tower_conv', 'mixed_4_tower_conv_1', 'mixed_4_tower_conv_2' ], 'double7x7': [ 'mixed_4_tower_1_conv', 'mixed_4_tower_1_conv_1', 'mixed_4_tower_1_conv_2', 'mixed_4_tower_1_conv_3', 'mixed_4_tower_1_conv_4' ], 'pool': ['mixed_4_tower_2_pool', 'mixed_4_tower_2_conv'] }, { '1x1': ['mixed_5_conv'], '7x7': [ 'mixed_5_tower_conv', 'mixed_5_tower_conv_1', 'mixed_5_tower_conv_2' ], 'double7x7': [ 'mixed_5_tower_1_conv', 'mixed_5_tower_1_conv_1', 'mixed_5_tower_1_conv_2', 'mixed_5_tower_1_conv_3', 'mixed_5_tower_1_conv_4' ], 'pool': ['mixed_5_tower_2_pool', 'mixed_5_tower_2_conv'] }, { '1x1': ['mixed_6_conv'], '7x7': [ 'mixed_6_tower_conv', 'mixed_6_tower_conv_1', 'mixed_6_tower_conv_2' ], 'double7x7': [ 'mixed_6_tower_1_conv', 'mixed_6_tower_1_conv_1', 'mixed_6_tower_1_conv_2', 'mixed_6_tower_1_conv_3', 'mixed_6_tower_1_conv_4' ], 'pool': ['mixed_6_tower_2_pool', 'mixed_6_tower_2_conv'] }, { '1x1': ['mixed_7_conv'], '7x7': [ 'mixed_7_tower_conv', 'mixed_7_tower_conv_1', 'mixed_7_tower_conv_2' ], 'double7x7': [ 'mixed_7_tower_1_conv', 'mixed_7_tower_1_conv_1', 'mixed_7_tower_1_conv_2', 'mixed_7_tower_1_conv_3', 'mixed_7_tower_1_conv_4' ], 'pool': ['mixed_7_tower_2_pool', 'mixed_7_tower_2_conv'] }) inception17 = Sequential(*[ inception_17(c, name) for c, name in zip([128, 160, 160, 192], inception17_names) ]) # Reduction 17 to 8 reduction17 = Inception( # strided 3x3 Sequential( ConvBN(768, 192, 1, 1, 0, 'mixed_8_tower_conv'), ConvBN(192, 320, 3, 1, 1, 'mixed_8_tower_conv_1')), # originally stride-pad: 2-0 # 7x7 and 3x3 Sequential( ConvBN(768, 192, 1, 1, 0, 'mixed_8_tower_1_conv'), ConvBN(192, 192, (1, 7), (1, 1), (0, 3), 'mixed_8_tower_1_conv_1'), ConvBN(192, 192, (7, 1), (1, 1), (3, 0), 'mixed_8_tower_1_conv_2'), ConvBN( 192, 192, 3, 1, 1, 'mixed_8_tower_1_conv_3')), # originally stride-pad: 2-0 # pool pool=M.MaxPooling2D(3, 1, 1, cover_all=False)) # originally stride-pad: 2-0 def inception_8(input_channel, name): # 1x1 s1 = ConvBN(input_channel, 320, 1, 1, 0, name['1x1'][0]) # 3x3 s21 = ConvBN(input_channel, 384, 1, 1, 0, name['3x3'][0]) s22 = Inception( ConvBN(384, 384, (1, 3), (1, 1), (0, 1), name['3x3'][1]), ConvBN(384, 384, (3, 1), (1, 1), (1, 0), name['3x3'][2])) s2 = Sequential(s21, s22) # double 3x3 s31 = ConvBN(input_channel, 448, 1, 1, 0, name['double3x3'][0]) s32 = ConvBN(448, 384, 3, 1, 1, name['double3x3'][1]) s331 = ConvBN(384, 384, (1, 3), (1, 1), (0, 1), name['double3x3'][2]) s332 = ConvBN(384, 384, (3, 1), (1, 1), (1, 0), name['double3x3'][3]) s33 = Inception(s331, s332) s3 = Sequential(s31, s32, s33) # pool s4 = ConvBN(input_channel, 192, 1, 1, 0, name['pool'][1], pool=A.AveragePooling2D(3, 1, 1)) return Inception(s1, s2, s3, s4) inception8_names = ({ '1x1': ['mixed_9_conv'], '3x3': [ 'mixed_9_tower_conv', 'mixed_9_tower_mixed_conv', 'mixed_9_tower_mixed_conv_1' ], 'double3x3': [ 'mixed_9_tower_1_conv', 'mixed_9_tower_1_conv_1', 'mixed_9_tower_1_mixed_conv', 'mixed_9_tower_1_mixed_conv_1' ], 'pool': ['mixed_9_tower_2_pool', 'mixed_9_tower_2_conv'] }, { '1x1': ['mixed_10_conv'], '3x3': [ 'mixed_10_tower_conv', 'mixed_10_tower_mixed_conv', 'mixed_10_tower_mixed_conv_1' ], 'double3x3': [ 'mixed_10_tower_1_conv', 'mixed_10_tower_1_conv_1', 'mixed_10_tower_1_mixed_conv', 'mixed_10_tower_1_mixed_conv_1' ], 'pool': ['mixed_10_tower_2_pool', 'mixed_10_tower_2_conv'] }) inception8 = Sequential(*[ inception_8(input_channel, name) for input_channel, name in zip([1280, 2048], inception8_names) ]) super(InceptionV3, self).__init__( convolution=convolution, inception=link.ChainList(inception35, inception17, inception8), grid_reduction=link.ChainList(reduction35, reduction17), )
def __init__(self, use_cudnn=True): convolution = link.ChainList( AuxConv(C.Convolution2D(3, 32, 3, 2, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(32, 32, 3, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(32, 64, 3, 1, 1, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(64, 80, 3, 1, 1, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(80, 192, 3, use_cudnn=use_cudnn))) def inception_0(input_channel, pool_channel): # 1x1 s1 = AuxConv( C.Convolution2D(input_channel, 64, 1, use_cudnn=use_cudnn)) # 5x5 s21 = AuxConv( C.Convolution2D(input_channel, 48, 1, use_cudnn=use_cudnn)) s22 = AuxConv( C.Convolution2D(48, 64, 5, pad=2, use_cudnn=use_cudnn)) s2 = Sequential(s21, s22) # double 3x3 s31 = AuxConv( C.Convolution2D(input_channel, 64, 1, use_cudnn=use_cudnn)) s32 = AuxConv( C.Convolution2D(64, 96, 3, pad=1, use_cudnn=use_cudnn)) s33 = AuxConv( C.Convolution2D(96, 96, 3, pad=1, use_cudnn=use_cudnn)) s3 = Sequential(s31, s32, s33) # pool s4 = AuxConv(C.Convolution2D(input_channel, pool_channel, 3, pad=1, use_cudnn=use_cudnn), pool=M.MaxPooling2D(3, 1, 1, use_cudnn=use_cudnn)) return Inception(s1, s2, s3, s4) inception0 = Sequential(*[ inception_0(input_channel, pool_channel) for input_channel, pool_channel in zip([192, 256, 288], [32, 64, 64]) ]) grid_reduction0 = Inception( # strided 3x3 AuxConv(C.Convolution2D(288, 384, 3, 2, use_cudnn=use_cudnn)), # double 3x3 Sequential( AuxConv(C.Convolution2D(288, 64, 1, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(64, 96, 3, pad=1, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(96, 96, 3, 2, use_cudnn=use_cudnn))), # pool pool=M.MaxPooling2D(3, 2)) def inception_1(hidden_channel): # 1x1 s1 = AuxConv(C.Convolution2D(768, 192, 1, use_cudnn=use_cudnn)) # 7x7 s21 = AuxConv( C.Convolution2D(768, hidden_channel, 1, use_cudnn=use_cudnn)) s22 = AuxConv( C.Convolution2D(hidden_channel, hidden_channel, (1, 7), pad=(0, 3), use_cudnn=use_cudnn)) s23 = AuxConv( C.Convolution2D(hidden_channel, 192, (7, 1), pad=(3, 0), use_cudnn=use_cudnn)) s2 = Sequential(s21, s22, s23) # double 7x7 s31 = AuxConv( C.Convolution2D(768, hidden_channel, 1, use_cudnn=use_cudnn)) s32 = AuxConv( C.Convolution2D(hidden_channel, hidden_channel, (1, 7), pad=(0, 3), use_cudnn=use_cudnn)) s33 = AuxConv( C.Convolution2D(hidden_channel, hidden_channel, (7, 1), pad=(3, 0), use_cudnn=use_cudnn)) s34 = AuxConv( C.Convolution2D(hidden_channel, hidden_channel, (1, 7), pad=(0, 3), use_cudnn=use_cudnn)) s35 = AuxConv( C.Convolution2D(hidden_channel, 192, (7, 1), pad=(3, 0), use_cudnn=use_cudnn)) s3 = Sequential(s31, s32, s33, s34, s35) # pool s4 = AuxConv(C.Convolution2D(768, 192, 3, pad=1, use_cudnn=use_cudnn), pool=A.AveragePooling2D(3, 1, 1, use_cudnn=use_cudnn)) return Inception(s1, s2, s3, s4) inception1 = Sequential( *[inception_1(c) for c in [128, 160, 160, 192]]) grid_reduction1 = Inception( # strided 3x3 Sequential( AuxConv(C.Convolution2D(768, 192, 1, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(192, 320, 3, 2, use_cudnn=use_cudnn))), # 7x7 and 3x3 Sequential( AuxConv(C.Convolution2D(768, 192, 1, use_cudnn=use_cudnn)), AuxConv( C.Convolution2D(192, 192, (1, 7), pad=(0, 3), use_cudnn=use_cudnn)), AuxConv( C.Convolution2D(192, 192, (7, 1), pad=(3, 0), use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(192, 192, 3, 2, use_cudnn=use_cudnn))), # pool pool=M.MaxPooling2D(3, 2, use_cudnn=use_cudnn)) def inception_2(input_channel): # 1x1 s1 = AuxConv( C.Convolution2D(input_channel, 320, 1, use_cudnn=use_cudnn)) # 3x3 s21 = AuxConv( C.Convolution2D(input_channel, 384, 1, use_cudnn=use_cudnn)) s22 = Inception( AuxConv( C.Convolution2D(384, 384, (1, 3), pad=(0, 1), use_cudnn=use_cudnn)), AuxConv( C.Convolution2D(384, 384, (3, 1), pad=(1, 0), use_cudnn=use_cudnn))) s2 = Sequential(s21, s22) # double 3x3 s31 = AuxConv( C.Convolution2D(input_channel, 448, 1, use_cudnn=use_cudnn)) s32 = AuxConv( C.Convolution2D(448, 384, 3, pad=1, use_cudnn=use_cudnn)) s331 = AuxConv( C.Convolution2D(384, 384, (1, 3), pad=(0, 1), use_cudnn=use_cudnn)) s332 = AuxConv( C.Convolution2D(384, 384, (3, 1), pad=(1, 0), use_cudnn=use_cudnn)) s33 = Inception(s331, s332) s3 = Sequential(s31, s32, s33) # pool s4 = AuxConv(C.Convolution2D(input_channel, 192, 3, pad=1, use_cudnn=use_cudnn), pool=A.AveragePooling2D(3, 1, 1, use_cudnn=use_cudnn)) return Inception(s1, s2, s3, s4) inception2 = Sequential( *[inception_2(input_channel) for input_channel in [1280, 2048]]) auxiliary_convolution = Sequential( AuxConv(C.Convolution2D(768, 128, 1, use_cudnn=use_cudnn), pool=A.AveragePooling2D(5, 3, use_cudnn=use_cudnn)), AuxConv(C.Convolution2D(128, 768, 5, use_cudnn=use_cudnn))) super(InceptionV3, self).__init__( convolution=convolution, inception=link.ChainList(inception0, inception1, inception2), grid_reduction=link.ChainList(grid_reduction0, grid_reduction1), auxiliary_convolution=auxiliary_convolution, auxiliary_linear=linear.Linear(768, 1000), linear=linear.Linear(2048, 1000))
print vars(args) print 'Initialize Model' predictor = Models.InceptionV3(args, dilated=False) model = Models.ReIDClassifier(predictor, args.label_dim[0], args) with model.init_scope(): model.segmentation = Models.InceptionV3Classifier( Models.InceptionV3(args, dilated=True), [Models.Classifier(20)], args) if len(args.model_path_for_ft) > 0: model = ResumeFromCheckpoint(args.model_path_for_ft, model) model.classifiers = link.ChainList( Models.Conv(2048 * 3, args.label_dim_ft, 1, 1, 0, init_weights=None, pool=None, nobias=False)) print 'Setup optimizer' opt = SetupOptimizer(model) # Use lower learning rate for pretrained parts for name, param in opt.target.namedparams(): if name.startswith('/predictor/'): param.update_rule.hyperparam.lr = args.optimizer['lr_pretrained'] opt.add_hook(WeightDecay(0.0005)) # opt.add_hook(GradientClipping(2.0))