Exemplo n.º 1
0
tr_X, tr_y, va_X, va_y, te_X, te_y = load_data()
tr_X, va_X, te_X = reshapeX(tr_X), reshapeX(va_X), reshapeX(te_X)

# 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
act = 'relu'
seq = Sequential()
seq.add(InputLayer(in_shape=(1, 28, 28)))
seq.add(Convolution2D(n_outfmaps=32, n_row=3, n_col=3, act='relu'))
seq.add(MaxPool2D(pool_size=(2, 2)))
seq.add(Convolution2D(n_outfmaps=32, n_row=3, n_col=3, act='relu'))
seq.add(MaxPool2D(pool_size=(2, 2)))
seq.add(Dropout(0.2))
seq.add(Flatten())
seq.add(Dense(n_hid, act='relu'))
seq.add(Dropout(0.5))
seq.add(Dense(n_hid, act='relu'))
seq.add(Dense(n_out, act='softmax'))
md = seq.combine()

# print summary info of model
md.summary()
Exemplo n.º 2
0
def train():
    # prepare data
    tr_X, tr_y, _, te_X, te_y, te_na_list = pp_dev_data.GetAllData( fe_fd, agg_num, hop, fold )
    [batch_num, n_time, n_freq] = tr_X.shape
    
    print tr_X.shape, tr_y.shape
    print te_X.shape, te_y.shape
    
    # build model
    seq = Sequential()
    seq.add( InputLayer( (n_time, n_freq) ) )
    seq.add( Flatten() )             # flatten to 2d: (n_time, n_freq) to 1d:(n_time*n_freq)
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_hid, act=act ) )
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_hid, act=act ) )
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_hid, act=act ) )
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_out, act='sigmoid' ) )
    md = seq.combine()
    md.summary()
    
    # optimizer
    optimizer = Adam(1e-4)
    
    # callbacks
    # tr_err, te_err are frame based. To get event based err, run recognize.py
    validation = Validation( tr_x=tr_X, tr_y=tr_y, va_x=None, va_y=None, te_x=te_X, te_y=te_y, batch_size=2000, 
                            metrics=['binary_crossentropy'], call_freq=1, dump_path=None )
                            
    # save model
    pp_dev_data.CreateFolder( cfg.dev_md_fd )
    save_model = SaveModel( dump_fd=cfg.dev_md_fd, call_freq=10 )
    
    # callbacks
    callbacks = [ validation, save_model ]
    
    # fit model
    md.fit( x=tr_X, y=tr_y, batch_size=2000, n_epochs=100, loss_func='binary_crossentropy', optimizer=optimizer, callbacks=callbacks, verbose=1 )
Exemplo n.º 3
0
hop = 5  # step_len
act = 'relu'
n_hid = 500
fold = 1
n_out = len(cfg.labels)

# prepare data
tr_X, tr_y, _ = pp_dev_data.GetAllData(cfg.dev_fe_mel_fd,
                                       agg_num,
                                       hop,
                                       fold=None)
[batch_num, n_time, n_freq] = tr_X.shape
print tr_X.shape, tr_y.shape

# build model
seq = Sequential()
seq.add(InputLayer((n_time, n_freq)))
seq.add(Flatten())  # flatten to 2d: (n_time, n_freq) to 1d:(n_time*n_freq)
seq.add(Dropout(0.1))
seq.add(Dense(n_hid, act=act))
seq.add(Dropout(0.1))
seq.add(Dense(n_hid, act=act))
seq.add(Dropout(0.1))
seq.add(Dense(n_hid, act=act))
seq.add(Dropout(0.1))
seq.add(Dense(n_out, act='sigmoid'))
md = seq.compile()
md.summary()

# validation
# tr_err, te_err are frame based. To get event based err, run recognize.py
Exemplo n.º 4
0

# hyper-params
tr_fe_fd = cfg.dev_tr_fe_mel_fd
max_len = 100
n_out = len( cfg.labels )

# prepare data
tr_X, tr_y = pp_dev_data.GetAllData( tr_fe_fd, max_len )
tr_y = sparse_to_categorical( tr_y, n_out )

print tr_X.shape, tr_y.shape
(_, n_time, n_freq) = tr_X.shape

# build model
seq = Sequential()
seq.add( InputLayer( (n_time, n_freq) ) )
seq.add( Flatten() )
seq.add( Dropout(0.1) )
seq.add( Dense(500, 'relu') )
seq.add( Dropout(0.1) )
seq.add( Dense(500, 'relu') )
seq.add( Dropout(0.1) )
seq.add( Dense(500, 'relu') )
seq.add( Dropout(0.1) )
seq.add( Dense(n_out, 'sigmoid') )
md = seq.combine()

md.summary()

# optimizer
Exemplo n.º 5
0
def train_cv_model():
    # init path
    if type == 'home':
        fe_fd = cfg.dev_fe_mel_home_fd
        labels = cfg.labels_home
        lb_to_id = cfg.lb_to_id_home
        tr_txt = cfg.dev_evaluation_fd + '/home_fold' + str(
            fold) + '_train.txt'
        te_txt = cfg.dev_evaluation_fd + '/home_fold' + str(
            fold) + '_evaluate.txt'
    if type == 'resi':
        fe_fd = cfg.dev_fe_mel_resi_fd
        labels = cfg.labels_resi
        lb_to_id = cfg.lb_to_id_resi
        tr_txt = cfg.dev_evaluation_fd + '/residential_area_fold' + str(
            fold) + '_train.txt'
        te_txt = cfg.dev_evaluation_fd + '/residential_area_fold' + str(
            fold) + '_evaluate.txt'

    n_out = len(labels)

    # load data to list
    tr_X, tr_y = pp_dev_data.LoadAllData(fe_fd, tr_txt, lb_to_id, agg_num, hop)
    tr_y = sparse_to_categorical(tr_y, n_out)

    print tr_X.shape
    print tr_y.shape
    n_freq = tr_X.shape[2]

    # build model
    seq = Sequential()
    seq.add(InputLayer((agg_num, n_freq)))
    seq.add(Flatten())
    seq.add(Dense(n_hid, act='relu'))
    seq.add(Dropout(0.1))
    seq.add(Dense(n_hid, act='relu'))
    seq.add(Dropout(0.1))
    seq.add(Dense(n_hid, act='relu'))
    seq.add(Dropout(0.1))
    seq.add(Dense(n_out, 'sigmoid'))
    md = seq.combine()

    # print summary info of model
    md.summary()

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

    # callbacks (optional)
    # save model every n epoch
    pp_dev_data.CreateFolder(cfg.dev_md_fd)
    save_model = SaveModel(dump_fd=cfg.dev_md_fd, call_freq=5)

    # validate model every n epoch
    validation = Validation(tr_x=tr_X,
                            tr_y=tr_y,
                            va_x=None,
                            va_y=None,
                            te_x=None,
                            te_y=None,
                            metrics=['binary_crossentropy'],
                            call_freq=1,
                            dump_path=None)

    # callbacks function
    callbacks = [validation, save_model]

    # train model
    md.fit(x=tr_X,
           y=tr_y,
           batch_size=20,
           n_epochs=100,
           loss_func='binary_crossentropy',
           optimizer=optimizer,
           callbacks=callbacks)
Exemplo n.º 6
0
# reshape X to shape: (n_pictures, n_fmaps=3, n_row=32, n_col=32)
tr_X = reshape_img_for_cnn(tr_X)
te_X = reshape_img_for_cnn(te_X)

# init params
n_out = 10

# sparse label to 1-of-K categorical label
tr_y = sparse_to_categorical(tr_y, n_out)
te_y = sparse_to_categorical(te_y, n_out)
print tr_X.shape
print tr_y.shape

### Build model
seq = Sequential()
seq.add(InputLayer(in_shape=(3, 32, 32)))

seq.add(
    Convolution2D(n_outfmaps=64,
                  n_row=3,
                  n_col=3,
                  act='linear',
                  border_mode=(1, 1)))
seq.add(BN(axes=(0, 2, 3)))
seq.add(Activation('relu'))
seq.add(
    Convolution2D(n_outfmaps=64,
                  n_row=3,
                  n_col=3,
                  act='linear',
Exemplo n.º 7
0
def train():
    # prepare data
    tr_X, tr_y, _, te_X, te_y, te_na_list = pp_dev_data.GetAllData(
        fe_fd, agg_num, hop, fold)
    [batch_num, n_time, n_freq] = tr_X.shape

    print tr_X.shape, tr_y.shape
    print te_X.shape, te_y.shape

    # build model
    seq = Sequential()
    seq.add(InputLayer((n_time, n_freq)))
    seq.add(Flatten())  # flatten to 2d: (n_time, n_freq) to 1d:(n_time*n_freq)
    seq.add(Dropout(0.1))
    seq.add(Dense(n_hid, act=act))
    seq.add(Dropout(0.1))
    seq.add(Dense(n_hid, act=act))
    seq.add(Dropout(0.1))
    seq.add(Dense(n_hid, act=act))
    seq.add(Dropout(0.1))
    seq.add(Dense(n_out, act='sigmoid'))
    md = seq.compile()
    md.summary()

    # optimizer
    optimizer = Adam(1e-4)

    # callbacks
    # tr_err, te_err are frame based. To get event based err, run recognize.py
    validation = Validation(tr_x=tr_X,
                            tr_y=tr_y,
                            va_x=None,
                            va_y=None,
                            te_x=te_X,
                            te_y=te_y,
                            batch_size=2000,
                            metrics=['binary_crossentropy'],
                            call_freq=1,
                            dump_path=None)

    # save model
    pp_dev_data.CreateFolder(cfg.dev_md_fd)
    save_model = SaveModel(dump_fd=cfg.dev_md_fd, call_freq=10)

    # callbacks
    callbacks = [validation, save_model]

    # fit model
    md.fit(x=tr_X,
           y=tr_y,
           batch_size=2000,
           n_epochs=100,
           loss_func='binary_crossentropy',
           optimizer=optimizer,
           callbacks=callbacks,
           verbose=1)
Exemplo n.º 8
0
### load & prepare data
tr_X, tr_y, va_X, va_y, te_X, te_y = load_data()

# 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
seq = Sequential()
seq.add(InputLayer(n_in))
seq.add(Dense(n_hid, act='relu'))
seq.add(Dropout(p_drop=0.2))
seq.add(Dense(n_hid, act='relu'))
seq.add(Dropout(p_drop=0.2))
seq.add(Dense(n_out, act='softmax'))
md = seq.combine()

# print summary info of model
md.summary()
# md.plot_connection()

### optimization method
optimizer = Adam(lr=0.001)  # Try SGD, Adagrad, Rmsprop, etc. instead
Exemplo n.º 9
0
            strides = (1, 1)
        seq.add(
            Convolution2D(n_outfmaps=n_outfmaps,
                          n_row=3,
                          n_col=3,
                          act='linear',
                          border_mode=(1, 1),
                          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()
Exemplo n.º 10
0
def train_cv_model():
    # init path
    if type=='home':
        fe_fd = cfg.dev_fe_mel_home_fd
        labels = cfg.labels_home
        lb_to_id = cfg.lb_to_id_home
        tr_txt = cfg.dev_evaluation_fd + '/home_fold' + str(fold) + '_train.txt'
        te_txt = cfg.dev_evaluation_fd + '/home_fold' + str(fold) + '_evaluate.txt'
    if type=='resi':
        fe_fd = cfg.dev_fe_mel_resi_fd
        labels = cfg.labels_resi
        lb_to_id = cfg.lb_to_id_resi
        tr_txt = cfg.dev_evaluation_fd + '/residential_area_fold' + str(fold) + '_train.txt'
        te_txt = cfg.dev_evaluation_fd + '/residential_area_fold' + str(fold) + '_evaluate.txt'
        
    n_out = len( labels )
    
    # load data to list
    tr_X, tr_y = pp_dev_data.LoadAllData( fe_fd, tr_txt, lb_to_id, agg_num, hop )
    tr_y = sparse_to_categorical( tr_y, n_out )
    
    print tr_X.shape
    print tr_y.shape
    n_freq = tr_X.shape[2]
    
    # build model
    seq = Sequential()
    seq.add( InputLayer( (agg_num, n_freq) ) )
    seq.add( Flatten() )
    seq.add( Dense( n_hid, act='relu' ) )
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_hid, act='relu' ) )
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_hid, act='relu' ) )
    seq.add( Dropout( 0.1 ) )
    seq.add( Dense( n_out, 'sigmoid' ) )
    md = seq.combine()
    
    # print summary info of model
    md.summary()
    
    # optimization method
    optimizer = Adam(1e-3)
    
    # callbacks (optional)
    # save model every n epoch
    pp_dev_data.CreateFolder( cfg.dev_md_fd )
    save_model = SaveModel( dump_fd=cfg.dev_md_fd, call_freq=5 )
    
    # validate model every n epoch
    validation = Validation( tr_x=tr_X, tr_y=tr_y, va_x=None, va_y=None, te_x=None, te_y=None, metrics=['binary_crossentropy'], call_freq=1, dump_path=None )
    
    # callbacks function
    callbacks = [validation, save_model]
    
    # train model
    md.fit( x=tr_X, y=tr_y, batch_size=20, n_epochs=100, loss_func='binary_crossentropy', optimizer=optimizer, callbacks=callbacks )
Exemplo n.º 11
0
import config as cfg

# hyper-params
tr_fe_fd = cfg.dev_tr_fe_mel_fd
max_len = 100
n_out = len(cfg.labels)

# prepare data
tr_X, tr_y = pp_dev_data.GetAllData(tr_fe_fd, max_len)
tr_y = sparse_to_categorical(tr_y, n_out)

print tr_X.shape, tr_y.shape
(_, n_time, n_freq) = tr_X.shape

# build model
seq = Sequential()
seq.add(InputLayer((n_time, n_freq)))
seq.add(Flatten())
seq.add(Dropout(0.1))
seq.add(Dense(500, 'relu'))
seq.add(Dropout(0.1))
seq.add(Dense(500, 'relu'))
seq.add(Dropout(0.1))
seq.add(Dense(500, 'relu'))
seq.add(Dropout(0.1))
seq.add(Dense(n_out, 'sigmoid'))
md = seq.combine()

md.summary()

# optimizer
Exemplo n.º 12
0
def train(tr_fe_fd, tr_csv_file, te_fe_fd, te_csv_file, n_concat, hop, scaler,
          out_md_fd):
    # Prepare data
    tr_x, tr_y = pp_data.get_matrix_format_data(fe_fd=tr_fe_fd,
                                                csv_file=tr_csv_file,
                                                n_concat=n_concat,
                                                hop=hop,
                                                scaler=scaler)

    te_x, te_y = pp_data.get_matrix_format_data(fe_fd=te_fe_fd,
                                                csv_file=te_csv_file,
                                                n_concat=n_concat,
                                                hop=hop,
                                                scaler=scaler)

    n_freq = tr_x.shape[2]
    print 'tr_x.shape:', tr_x.shape  # (n_samples, n_concat, n_freq)
    print 'tr_y.shape:', tr_y.shape  # (n_samples, n_labels)

    # Build model
    n_out = len(cfg.labels)
    seq = Sequential()
    seq.add(InputLayer((n_concat, n_freq)))
    seq.add(Flatten())
    seq.add(Dropout(0.2))
    seq.add(Dense(200, act='relu'))
    seq.add(Dropout(0.2))
    seq.add(Dense(200, act='relu'))
    seq.add(Dropout(0.2))
    seq.add(Dense(n_out, act='softmax'))
    md = seq.compile()
    md.summary()

    # Validation.
    # tr_err, te_err are frame based. To get event based err, run recognize.py
    validation = Validation(tr_x=tr_x,
                            tr_y=tr_y,
                            va_x=None,
                            va_y=None,
                            te_x=te_x,
                            te_y=te_y,
                            batch_size=500,
                            call_freq=1,
                            dump_path=None)

    # Save model
    pp_data.create_folder(out_md_fd)
    save_model = SaveModel(out_md_fd, call_freq=2)

    # Callbacks
    callbacks = [validation, save_model]

    # Optimizer
    optimizer = Adam(1e-3)

    # fit model
    md.fit(x=tr_x,
           y=tr_y,
           batch_size=100,
           n_epochs=101,
           loss_func='categorical_crossentropy',
           optimizer=optimizer,
           callbacks=callbacks)
Exemplo n.º 13
0
# pad & truncate sequences
tr_X = pad_trunc_seqs(tr_X, max_len, pad_type='pre')
te_X = pad_trunc_seqs(te_X, max_len, pad_type='pre')

# sparse target to categorical target
tr_y = sparse_to_categorical(tr_y, n_out)
te_y = sparse_to_categorical(te_y, n_out)


### build model
def mean_pool(input):
    return K.mean(input, axis=1)


seq = Sequential()
seq.add(InputLayer(max_len))
seq.add(Embedding(n_words, n_proj))
seq.add(LSTM(n_hid, 'tanh', return_sequence=True))  # Try RNN, GRU instead
seq.add(Lambda(mean_pool))
seq.add(Dense(n_out, 'softmax'))
md = seq.combine()

# print summary info of model
md.summary()

### optimization method
optimizer = Rmsprop(0.001)

### callbacks (optional)
# save model every n epoch (optional)
Exemplo n.º 14
0
def train(tr_fe_fd, tr_csv_file, te_fe_fd, te_csv_file, 
          n_concat, hop, scaler, out_md_fd):
    # Prepare data 
    tr_x, tr_y = pp_data.get_matrix_format_data(
                     fe_fd=tr_fe_fd, 
                     csv_file=tr_csv_file, 
                     n_concat=n_concat, hop=hop, scaler=scaler)
                     
    te_x, te_y = pp_data.get_matrix_format_data(
                     fe_fd=te_fe_fd, 
                     csv_file=te_csv_file, 
                     n_concat=n_concat, hop=hop, scaler=scaler)
    
    n_freq = tr_x.shape[2]
    print 'tr_x.shape:', tr_x.shape     # (n_samples, n_concat, n_freq)
    print 'tr_y.shape:', tr_y.shape     # (n_samples, n_labels)
    
    
    # Build model
    n_out = len(cfg.labels)
    seq = Sequential()
    seq.add(InputLayer((n_concat, n_freq)))
    seq.add(Flatten())
    seq.add(Dropout(0.2))
    seq.add(Dense(200, act='relu'))
    seq.add(Dropout(0.2))
    seq.add(Dense(200, act='relu'))
    seq.add(Dropout(0.2))
    seq.add(Dense(n_out, act='softmax'))
    md = seq.compile()
    md.summary()
    
    # Validation. 
    # tr_err, te_err are frame based. To get event based err, run recognize.py
    validation = Validation(tr_x=tr_x, tr_y=tr_y, 
                            va_x=None, va_y=None, 
                            te_x=te_x, te_y=te_y, 
                            batch_size=500, call_freq=1, dump_path=None)
    
    # Save model
    pp_data.create_folder(out_md_fd)
    save_model = SaveModel(out_md_fd, call_freq=2)
    
    # Callbacks
    callbacks = [validation, save_model]
    
    # Optimizer
    optimizer = Adam(1e-3)
    
    # fit model
    md.fit(x=tr_x, y=tr_y, 
           batch_size=100, 
           n_epochs=101, 
           loss_func='categorical_crossentropy', 
           optimizer=optimizer, 
           callbacks=callbacks)
Exemplo n.º 15
0
# hyper-params
agg_num = 11        # concatenate frames
hop = 5            # step_len
act = 'relu'
n_hid = 500
fold = 1
n_out = len( cfg.labels )

# prepare data
tr_X, tr_y, _ = pp_dev_data.GetAllData( cfg.dev_fe_mel_fd, agg_num, hop, fold=None )
[batch_num, n_time, n_freq] = tr_X.shape
print tr_X.shape, tr_y.shape

# build model
seq = Sequential()
seq.add( InputLayer( (n_time, n_freq) ) )
seq.add( Flatten() )             # flatten to 2d: (n_time, n_freq) to 1d:(n_time*n_freq)
seq.add( Dropout( 0.1 ) )
seq.add( Dense( n_hid, act=act ) )
seq.add( Dropout( 0.1 ) )
seq.add( Dense( n_hid, act=act ) )
seq.add( Dropout( 0.1 ) )
seq.add( Dense( n_hid, act=act ) )
seq.add( Dropout( 0.1 ) )
seq.add( Dense( n_out, act='sigmoid' ) )
md = seq.combine()
md.summary()

# validation
# tr_err, te_err are frame based. To get event based err, run recognize.py