Beispiel #1
0
def train():

    # load data
    tr_X2d_mix, tr_X3d_mix, tr_y2d_chn0, tr_y2d_chn1, tr_y3d_chn0, tr_y3d_chn1 = pp_data.LoadData(
        cfg.fe_fft_fd, n_time, hop, na_list=cfg.tr_list)

    # build model
    lay_in0 = InputLayer(in_shape=((n_time, n_freq)), name='in1')
    lay_a0 = Flatten()(lay_in0)
    lay_a1 = Dense(n_hid, act='relu', name='a1')(lay_a0)
    lay_a2 = Dropout(0.2, name='a2')(lay_a1)
    lay_a3 = Dense(n_hid, act='relu', name='a3')(lay_a2)
    lay_a4 = Dropout(0.2, name='a4')(lay_a3)
    lay_a5 = Dense(n_hid, act='relu', name='a5')(lay_a4)
    lay_a6 = Dropout(0.2, name='a6')(lay_a5)
    lay_b1 = Dense(n_freq, act='sigmoid',
                   name='a7')(lay_a6)  # mask_left, shape: (N, n_freq)
    lay_c1 = Dense(n_freq, act='sigmoid',
                   name='a8')(lay_a6)  # mask_right, shape: (N, n_freq)
    lay_out_b = Lambda(mul,
                       name='out_b')([lay_b1,
                                      lay_in0])  # out_left, shape: (N, n_freq)
    lay_out_c = Lambda(mul, name='out_c')([lay_c1, lay_in0
                                           ])  # out_right, shape: (N, n_freq)

    md = Model(in_layers=[lay_in0],
               out_layers=[lay_out_b, lay_out_c],
               any_layers=[lay_in0, lay_b1, lay_c1])
    md.summary()

    # validation
    validation = Validation(tr_x=[np.abs(tr_y3d_chn0) + np.abs(tr_y3d_chn1)],
                            tr_y=[np.abs(tr_y2d_chn0),
                                  np.abs(tr_y2d_chn1)],
                            batch_size=100,
                            metrics=[loss_func],
                            call_freq=1,
                            dump_path=None)

    # save model
    if not os.path.exists(cfg.md_fd): os.makedirs(cfg.md_fd)
    save_model = SaveModel(dump_fd=cfg.md_fd, call_freq=2)

    # callbacks
    callbacks = [validation, save_model]

    # optimizer
    optimizer = Adam(1e-3)

    # fit model
    md.fit( [np.abs(tr_y3d_chn0)+np.abs(tr_y3d_chn1)], [np.abs(tr_y2d_chn0), np.abs(tr_y2d_chn1)], \
        batch_size=100, n_epochs=100, loss_func='mse', optimizer=optimizer, callbacks=callbacks, verbose=1 )
Beispiel #2
0
def add_n_bottleneck_blocks(in_layer, n_outfmaps, n_repeat, is_first_layer):
    assert n_outfmaps % 4 == 0
    a0 = in_layer
    for i1 in xrange(n_repeat):
        if i1 == 0:
            if is_first_layer is True:
                strides = (1, 1)
                shortcut = a0
            else:
                strides = (2, 2)
                shortcut = Convolution2D(n_outfmaps=n_outfmaps,
                                         n_row=1,
                                         n_col=1,
                                         act='linear',
                                         border_mode='valid',
                                         strides=strides)(a0)
        else:
            strides = (1, 1)
            shortcut = a0

        a1 = BN(axes=(0, 2, 3))(a0)
        a2 = Activation('relu')(a1)
        a3 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=1,
                           n_col=1,
                           act='linear',
                           border_mode='valid',
                           strides=strides)(a2)
        a4 = BN(axes=(0, 2, 3))(a3)
        a5 = Activation('relu')(a4)
        a6 = Convolution2D(n_outfmaps=n_outfmaps / 4,
                           n_row=3,
                           n_col=3,
                           act='linear',
                           border_mode=(1, 1),
                           strides=(1, 1))(a5)
        a7 = BN(axes=(0, 2, 3))(a6)
        a8 = Activation('relu')(a7)
        a9 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=1,
                           n_col=1,
                           act='linear',
                           border_mode='valid',
                           strides=(1, 1))(a8)

        a10 = Lambda(add_layers)([shortcut, a9])

        a0 = a10

    return a0
Beispiel #3
0
def add_n_blocks(in_layer, n_outfmaps, n_repeat, is_first_layer):
    a0 = in_layer
    for i1 in range(n_repeat):
        if i1 == 0:
            if is_first_layer is True:
                strides = (1, 1)
                shortcut = a0
            else:
                strides = (2, 2)
                shortcut = Convolution2D(n_outfmaps=n_outfmaps,
                                         n_row=1,
                                         n_col=1,
                                         act='linear',
                                         border_mode='valid',
                                         strides=strides)(a0)
        else:
            strides = (1, 1)
            shortcut = a0

        a1 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=3,
                           n_col=3,
                           act='linear',
                           border_mode=(1, 1),
                           strides=strides)(a0)
        a2 = BN(axes=(0, 2, 3))(a1)
        a3 = Activation('relu')(a2)
        a4 = Convolution2D(n_outfmaps=n_outfmaps,
                           n_row=3,
                           n_col=3,
                           act='linear',
                           border_mode=(1, 1),
                           strides=(1, 1))(a3)
        a5 = BN(axes=(0, 2, 3))(a4)
        a6 = Activation('relu')(a5)

        a7 = Lambda(add_layers)([shortcut, a6])

        a0 = a7

    return a0
Beispiel #4
0
    return a0


x0 = InputLayer(in_shape=(3, 32, 32))
x1 = Convolution2D(n_outfmaps=64,
                   n_row=3,
                   n_col=3,
                   act='relu',
                   border_mode=(1, 1))(x0)
x2 = add_n_blocks(x1, n_outfmaps=64, n_repeat=3, is_first_layer=True)
x3 = add_n_blocks(x2, n_outfmaps=128, n_repeat=4, is_first_layer=False)
x4 = add_n_blocks(x3, n_outfmaps=256, n_repeat=6, is_first_layer=False)
x5 = add_n_blocks(x4, n_outfmaps=512, n_repeat=3, is_first_layer=False)

y1 = Lambda(mean_pool)(x5)
y2 = Flatten()(y1)
y3 = Dense(n_out, act='softmax')(y2)
md = Model([x0], [y3])

# print summary info of model
md.summary()

### optimization method
optimizer = Adam(1e-3)

### callbacks (optional)
# save model every n epoch (optional)
if not os.path.exists('Md'): os.makedirs('Md')  # create folder
save_model = SaveModel(dump_fd='Md', call_freq=1)
# init params
n_in = 784
n_hid = 500
n_out = 10

# sparse label to 1-of-K categorical label
tr_y = sparse_to_categorical(tr_y, n_out)
va_y = sparse_to_categorical(va_y, n_out)
te_y = sparse_to_categorical(te_y, n_out)

### Build model
lay_in1 = InputLayer(in_shape=(n_in, ))
lay_in2 = InputLayer(in_shape=(n_in, ))
a1 = Dense(n_out=n_hid, act='relu')(lay_in1)
a2 = Dense(n_out=n_hid, act='relu')(lay_in2)
b = Lambda(merge)([a1, a2])
b = Dense(n_out=n_hid, act='relu')(b)
lay_out1 = Dense(n_out=n_out, act='softmax')(b)
lay_out2 = Dense(n_out=n_out, act='softmax')(b)

md = Model(in_layers=[lay_in1, lay_in2], out_layers=[lay_out1, lay_out2])
md.compile()
md.summary()

# validate model every n epoch (optional)
validation = Validation(tr_x=[tr_x, tr_x],
                        tr_y=[tr_y, tr_y],
                        va_x=None,
                        va_y=None,
                        te_x=[te_x, te_x],
                        te_y=[te_y, te_y],
def train():
    _loss_func = _jdc_loss_func0
    
    # load data
    t1 = time.time()
    dict = cPickle.load( open( cfg.scrap_fd+'/denoise_enhance_pool_fft_all0.p', 'rb' ) )
    tr_X, tr_mask, tr_y, tr_na_list, te_X, te_mask, te_y, te_na_list = dict['tr_X'], dict['tr_mask'], dict['tr_y'], dict['tr_na_list'], dict['te_X'], dict['te_mask'], dict['te_y'], dict['te_na_list']
    t2 = time.time()
    
    tr_X = pp_data.wipe_click( tr_X, tr_na_list )
    te_X = pp_data.wipe_click( te_X, te_na_list )
    
    # balance data
    tr_X, tr_mask, tr_y = pp_data.BalanceData2( tr_X, tr_mask, tr_y )
    te_X, te_mask, te_y = pp_data.BalanceData2( te_X, te_mask, te_y )
    
    
    print tr_X.shape, tr_y.shape, te_X.shape, te_y.shape
    [n_songs, n_chunks, n_freq] = te_X.shape
    
    tr_y = tr_y.reshape( (len(tr_y), 1) )
    te_y = te_y.reshape( (len(te_y), 1) )
    
    
    # jdc model
    # classifier
    lay_z0 = InputLayer( (n_chunks,) )          # shape:(n_songs, n_chunks) keep the length of songs
    
    lay_in0 = InputLayer( (n_chunks, n_freq), name='in0' )   # shape: (n_songs, n_chunk, n_freq)
    lay_a1 = lay_in0
    # lay_a1 = Lambda( _conv2d )( lay_a1 )
    
    lay_a1 = Lambda( _reshape_3d_to_4d )( lay_a1 )
    lay_a1 = Convolution2D( 32, 3, 3, act='relu', init_type='glorot_uniform', border_mode=(1,1), strides=(1,1), name='a11' )( lay_a1 )
    lay_a1 = Dropout( 0.2 )( lay_a1 )
    lay_a1 = MaxPool2D( pool_size=(1,2) )( lay_a1 )
    
    lay_a1 = Convolution2D( 64, 3, 3, act='relu', init_type='glorot_uniform', border_mode=(1,1), strides=(1,1), name='a12' )( lay_a1 )
    lay_a1 = Dropout( 0.2 )( lay_a1 )
    lay_a1 = MaxPool2D( pool_size=(1,2) )( lay_a1 )
    lay_a1 = Lambda( _reshape_4d_to_3d )( lay_a1 )
    
    lay_a1 = Dense( n_hid, act='relu', name='a2' )( lay_a1 )       # shape: (n_songs, n_chunk, n_hid)
    lay_a1 = Dropout( 0.2 )( lay_a1 )
    lay_a1 = Dense( n_hid, act='relu', name='a4' )( lay_a1 )
    lay_a1 = Dropout( 0.2 )( lay_a1 )
    lay_a1 = Dense( n_hid, act='relu', name='a6' )( lay_a1 )
    lay_a1 = Dropout( 0.2 )( lay_a1 )
    lay_a8 = Dense( n_out, act='sigmoid', init_type='zeros', b_init=0, name='a8' )( lay_a1 )     # shape: (n_songs, n_chunk, n_out)
    
    # detector
    lay_b1 = lay_in0     # shape: (n_songs, n_chunk, n_freq)
    lay_b2 = Lambda( _conv2d )( lay_b1 )    # shape: (n_songs, n_chunk, n_freq)
    lay_b2 = Lambda( _reshape_3d_to_4d )( lay_b1 )
    lay_b2 = MaxPool2D( pool_size=(1,2) )( lay_b2 )
    lay_b2 = Lambda( _reshape_4d_to_3d )( lay_b2 )
    lay_b8 = Dense( n_out, act='hard_sigmoid', init_type='zeros', b_init=-2.3, name='b8' )( lay_b2 )
    md = Model( in_layers=[lay_in0, lay_z0], out_layers=[lay_a8, lay_b8], any_layers=[] )
    
      
    # print summary info of model
    md.summary()

    # callbacks (optional)
    # save model every n epoch (optional)
    pp_data.CreateFolder( cfg.wbl_dev_md_fd )
    pp_data.CreateFolder( cfg.wbl_dev_md_fd+'/cnn_fft' )
    save_model = SaveModel( dump_fd=cfg.wbl_dev_md_fd+'/cnn_fft', call_freq=20, type='iter' )
    validation = Validation( tr_x=None, tr_y=None, va_x=None, va_y=None, te_x=[te_X, te_mask], te_y=te_y, batch_size=100, metrics=[_loss_func], call_freq=20, dump_path=None, type='iter' )
    
    # callbacks function
    callbacks = [save_model, validation]

    
    # EM training
    md.set_gt_nodes( tr_y )
    md.find_layer('a11').set_trainable_params( ['W','b'] )
    md.find_layer('a12').set_trainable_params( ['W','b'] )
    md.find_layer('a2').set_trainable_params( ['W','b'] )
    md.find_layer('a4').set_trainable_params( ['W','b'] )
    md.find_layer('a6').set_trainable_params( ['W','b'] )
    md.find_layer('a8').set_trainable_params( ['W','b'] )
    md.find_layer('b8').set_trainable_params( [] )
    opt_classifier = Adam( 1e-3 )
    f_classify = md.get_optimization_func( loss_func=_loss_func, optimizer=opt_classifier, clip=None )
    
    
    md.find_layer('a11').set_trainable_params( [] )
    md.find_layer('a12').set_trainable_params( [] )
    md.find_layer('a2').set_trainable_params( [] )
    md.find_layer('a4').set_trainable_params( [] )
    md.find_layer('a6').set_trainable_params( [] )
    md.find_layer('a8').set_trainable_params( [] )
    md.find_layer('b8').set_trainable_params( ['W','b'] )
    opt_detector = Adam( 1e-3 )
    f_detector = md.get_optimization_func( loss_func=_loss_func, optimizer=opt_detector, clip=None )
    
    
    _x, _y = md.preprocess_data( [tr_X, tr_mask], tr_y, shuffle=True )
    
    for i1 in xrange(500):
        print '-----------------------'
        opt_classifier.reset()
        md.do_optimization_func_iter_wise( f_classify, _x, _y, batch_size=100, n_iters=80, callbacks=callbacks, verbose=1 )
        print '-----------------------'
        opt_detector.reset()
        md.do_optimization_func_iter_wise( f_detector, _x, _y, batch_size=100, n_iters=20, callbacks=callbacks, verbose=1 )
Beispiel #7
0
                          strides=strides))
        seq.add(BN(axes=(0, 2, 3)))
        seq.add(Activation('relu'))

    return seq


### Build model
seq = Sequential()
seq.add(InputLayer(in_shape=(3, 32, 32)))
seq = add_n_blocks(seq, n_outfmaps=64, n_repeat=6, is_first_layer=True)
seq = add_n_blocks(seq, n_outfmaps=128, n_repeat=8, is_first_layer=False)
seq = add_n_blocks(seq, n_outfmaps=256, n_repeat=12, is_first_layer=True)
seq = add_n_blocks(seq, n_outfmaps=512, n_repeat=6, is_first_layer=True)

seq.add(Lambda(mean_pool))
seq.add(Flatten())

seq.add(Dense(n_out, act='softmax'))
md = seq.combine()

# print summary info of model
md.summary()

### optimization method
optimizer = Adam(1e-3)

### callbacks (optional)
# save model every n epoch (optional)
if not os.path.exists('Md'): os.makedirs('Md')  # create folder
save_model = SaveModel(dump_fd='Md', call_freq=1)
Beispiel #8
0
def train():

    # create empty folders in workspace
    create_folders()

    # get dev & eva data
    tr_X, tr_y, _, _, _, _ = pp_dev_data.GetSegData(dev_fe_fd,
                                                    agg_num,
                                                    hop,
                                                    fold=None)
    te_X, te_na_list = pp_eva_data.GetEvaSegData(eva_fe_fd, agg_num, hop)

    [n_songs, n_chunks, _, n_in] = tr_X.shape
    print tr_X.shape, tr_y.shape
    print te_X.shape

    # model
    # classifier
    lay_in0 = InputLayer(
        (n_chunks, agg_num, n_in),
        name='in0')  # shape: (n_songs, n_chunk, agg_num, n_in)
    lay_a1 = Flatten(3, name='a1')(
        lay_in0)  # shape: (n_songs, n_chunk, agg_num*n_in)
    lay_a2 = Dense(n_hid,
                   act='relu')(lay_a1)  # shape: (n_songs, n_chunk, n_hid)
    lay_a3 = Dropout(0.2)(lay_a2)
    lay_a4 = Dense(n_hid, act='relu')(lay_a3)
    lay_a5 = Dropout(0.2)(lay_a4)
    lay_a6 = Dense(n_hid, act='relu')(lay_a5)
    lay_a7 = Dropout(0.2)(lay_a6)
    lay_a8 = Dense(n_out, act='sigmoid', b_init=-1,
                   name='a8')(lay_a7)  # shape: (n_songs, n_chunk, n_out)

    # detector
    lay_b1 = Lambda(mean_pool)(lay_in0)  # shape: (n_songs, n_chunk, n_out)
    lay_b8 = Dense(n_out, act='sigmoid',
                   name='b4')(lay_b1)  # shape: (n_songs, n_chunk, n_out)

    md = Model(in_layers=[lay_in0], out_layers=[lay_a8, lay_b8], any_layers=[])
    md.compile()
    md.summary()

    # callback, write out dection scores to .txt each epoch
    dump_fd = cfg.scrap_fd + '/Results_eva/jdc_eer'
    print_scores = cb_eer.PrintScoresBagOfBlocks(te_X,
                                                 te_na_list,
                                                 dump_fd,
                                                 call_freq=1)

    # callback, print loss each epoch
    validation = Validation(tr_x=tr_X,
                            tr_y=tr_y,
                            va_x=None,
                            va_y=None,
                            te_x=None,
                            te_y=None,
                            metrics=[loss_func],
                            call_freq=1,
                            dump_path=None)

    # callback, save model every N epochs
    save_model = SaveModel(dump_fd=cfg.scrap_fd + '/Md_eva_jdc', call_freq=10)

    # combine all callbacks
    callbacks = [validation, save_model, print_scores]

    # optimizer
    optimizer = Adam(2e-4)

    # fit model
    md.fit(x=tr_X,
           y=tr_y,
           batch_size=10,
           n_epochs=1001,
           loss_func=loss_func,
           optimizer=optimizer,
           callbacks=callbacks)