示例#1
0
from data import Vocab, EmojiVocab, Corpus
from sklearn.metrics import classification_report, confusion_matrix, precision_score, recall_score
import numpy as np

model = load_model(os.path.join('weight', args.model))

# load corpus and vocab
vocab = Vocab(20000)  # 20k
emoji_vocab = EmojiVocab(40)
corpus = Corpus(vocab, emoji_vocab, debug=False, eval=True)

encoded_test = corpus.encoded_test

# evaluation
y_pred = model.predict_generator(
    data_generator(encoded_test, args.batch_size, args.step_size,
                   len(emoji_vocab)),
    len(encoded_test[0]) // (args.batch_size * args.step_size),
    verbose=1)

target_names = [emoji_vocab.decode(x) for x in range(len(emoji_vocab))]
y_true = list(
    np.array(
        generator_y_true(encoded_test, args.batch_size, args.step_size,
                         len(emoji_vocab))).reshape(-1))

y_pred = list(y_pred.reshape(-1, len(emoji_vocab)).argmax(axis=1))

assert len(y_true) == len(y_pred)

# print('Confusion Matrix')
# print(confusion_matrix(y_true, y_pred))
示例#2
0
print("starting Training....Finger Crossed....")
if resume_trng =='yes':
    print("if part")

else:
    # define the model
    model,modelFileName = models.define_model_BILSTM1L_withAttentionSVM(vocab_size, max_query_length, max_para_length,num_classes,embedding_matrix,emb_dim)
    modelFileName = modelFileName + "Vcab_"+str(vocab_size) + "Emb_"+ str(emb_dim)
    for i in range(n_repeats):
        # define checkpoint callback
        filepath = 'trained_model/'+modelFileName+'best.h5'#-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
        checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1)
        earlystop=EarlyStopping(monitor="val_loss",patience=2)
        # fit model
        history =model.fit_generator(
            util.data_generator(queryTrain,paraTrain,labelTrain, tokenizer, max_query_length, max_para_length,
                           n_queries_per_update),
            validation_data= util.data_generator(queryV, paraV, labelV, tokenizer, max_query_length,
                           max_para_length, n_queries_per_update),
            validation_steps=validationSteps,
            steps_per_epoch=n_batches_per_epoch,
            epochs=n_epochs,
            verbose=verbose,
            callbacks=[checkpoint,earlystop])


        hist = pd.DataFrame(history.history)
        dump(hist, open(modelFileName + '-history.pkl', 'wb'))

        del queryTrain, queryV,paraTrain,paraV

        import testing
示例#3
0
def main():
    global config
    feature_dim = config.feature_dim
    n_classes = config.n_classes
    hidden_dim = config.hidden_dim
    n_train = config.n_train
    n_test = config.n_test
    n_epochs = config.n_epochs
    batch_size = config.batch_size
    lr = config.lr
    #
    if config.GPUs=='':
        num_threads = config.num_threads
    else:
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = config.GPUs
    #
    train_files = glob.glob(config.data_path_train)
    test_files = glob.glob(config.data_path_test)
    #
    x_idxs = tf.placeholder(tf.int64, shape=[None,2])
    x_vals = tf.placeholder(tf.float32, shape=[None])
    x = tf.SparseTensor(x_idxs, x_vals, [batch_size,feature_dim])
    y = tf.placeholder(tf.float32, shape=[None,n_classes],name="y")
    #
    W1 = tf.Variable(tf.truncated_normal([feature_dim,hidden_dim], stddev=2.0/math.sqrt(feature_dim+hidden_dim)))
    b1 = tf.Variable(tf.truncated_normal([hidden_dim], stddev=2.0/math.sqrt(feature_dim+hidden_dim)))
    layer_1 = tf.nn.relu(tf.sparse_tensor_dense_matmul(x,W1)+b1)
    #
    W2 = tf.Variable(tf.truncated_normal([hidden_dim,n_classes], stddev=2.0/math.sqrt(hidden_dim+n_classes)))
    b2 = tf.Variable(tf.truncated_normal([n_classes], stddev=2.0/math.sqrt(n_classes+hidden_dim)))
    logits = tf.matmul(layer_1,W2)+b2
    #
    k=1
    if k==1:
        top_idxs = tf.argmax(logits, axis=1)
    else:
        top_idxs = tf.nn.top_k(logits, k=k, sorted=False)[1]
    #
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
    #
    train_step = tf.train.AdamOptimizer(lr).minimize(loss)
    #
    if config.GPUs=='':
        Config = tf.ConfigProto(inter_op_parallelism_threads=num_threads, intra_op_parallelism_threads=num_threads)
    else:
        Config = tf.ConfigProto()
        Config.gpu_options.allow_growth = True
    #
    sess = tf.Session(config=Config)
    sess.run(tf.global_variables_initializer())
    #
    training_data_generator = data_generator(train_files, batch_size, n_classes)
    steps_per_epoch = n_train//batch_size
    n_steps = n_epochs*steps_per_epoch
    n_check = 50
    #
    begin_time = time.time()
    total_time = 0
    counter = 0
    #
    with open(config.log_file, 'a') as out:
        for i in range(n_steps):
            if i%n_check==0:
                total_time+=time.time()-begin_time
                print('Finished ',i,' steps. Time elapsed for last',n_check,'batches = ',time.time()-begin_time)
                n_steps_val = n_test//batch_size
                test_data_generator = data_generator_tst(test_files, batch_size)
                tmp_k = 0
                for h in range(20):  # a few test batches to check the precision 
                    idxs_batch, vals_batch, labels_batch = next(test_data_generator)
                    top_k_classes = sess.run(top_idxs, feed_dict={x_idxs:idxs_batch, x_vals:vals_batch})
                    tmp_k += np.mean([len(np.intersect1d(top_k_classes[j],labels_batch[j]))/min(k,len(labels_batch[j])) for j in range(len(top_k_classes))])
                print('test_acc: ',tmp_k/20)
                print('#######################')
                print(i,int(total_time),tmp_k/20 , file=out)
                begin_time = time.time()
            idxs_batch, vals_batch, labels_batch = next(training_data_generator)
            sess.run(train_step, feed_dict={x_idxs:idxs_batch, x_vals:vals_batch, y:labels_batch})
            if i%steps_per_epoch==steps_per_epoch-1:
                total_time+=time.time()-begin_time
                print('Finished ',i,' steps. Time elapsed for last 100 batches = ',time.time()-begin_time)
                n_steps_val = n_test//batch_size
                test_data_generator = data_generator_tst(test_files, batch_size)
                num_batches = 0
                p_at_k = 0
                for l in range(n_steps_val): # precision on entire test data
                    idxs_batch, vals_batch, labels_batch = next(test_data_generator)
                    top_k_classes = sess.run(top_idxs, feed_dict={x_idxs:idxs_batch, x_vals:vals_batch})
                    p_at_k += np.mean([len(np.intersect1d(top_k_classes[j],labels_batch[j]))/min(k,len(labels_batch[j])) for j in range(len(top_k_classes))])
                    num_batches += 1
                #
                print('Overall p_at_1 after ',num_batches,'batches = ', p_at_k/num_batches)
                print(i, int(total_time), p_at_k/num_batches, file=out)
                #
                begin_time = time.time()

    writer = tf.summary.FileWriter('logs', sess.graph)
    writer.close()

    # Export the model in Tensorflow v1 SavedModel format
    print('Exporting trained model to ./models')
    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder('./models')

    # Creates the TensorInfo protobuf objects that encapsulates the input/output tensors
    tensor_info_input = tf.compat.v1.saved_model.utils.build_tensor_info(x)

    # output tensor info
    tensor_info_output = tf.compat.v1.saved_model.utils.build_tensor_info(y)

    # Defines the signature, uses the TF Classify API
    amazon_signature = (
        tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
             inputs={'feature_ids_values': tensor_info_input},
             outputs={'classification': tensor_info_output},
             method_name=tf.saved_model.CLASSIFY_METHOD_NAME))

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.SERVING],
        signature_def_map={
             'slide_amazon':
             amazon_signature,
        })

    # Note: the optimizer is lost... hopefully the function and weights come through!
    builder.save()

    # Save a frozen graphdef too..
#    frozen_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(
#         sess,
#         sess.graph_def,
#         ['y']  
#    )

#    output_graph="amazon-frozen-model.pb"
#    with tf.gfile.GFile(output_graph, "wb") as f:
#       f.write(frozen_graph_def.SerializeToString())

    # Try conversion here... 
#    g = tf2onnx.tfonnx.process_tf_graph(sess.graph_def, opset=11, input_names=tensor_info_input, output_names=tensor_info_output)
#    onnx_graph = tf2onnx.optimizer.optimize_graph(g)
#    model_proto = onnx_graph.make_model("converted from {}".format("/tmp/test.onnx"))

    sess.close()
import tensorflow as tf
import sys
import config
import util

size = 128
batch_size = 16

trian_img_paths, train_labels = util.process_annotation(
    config.TRAIN_ANNOTATION_FILE, config.TRAIN_DIR)
train_data_gen = util.data_generator(trian_img_paths, train_labels, batch_size)
num_batch = len(train_labels) // batch_size

x = tf.placeholder(tf.float32, [None, size, size, 3])
y_ = tf.placeholder(tf.float32, [None, 61])

keep_prob_5 = tf.placeholder(tf.float32)
keep_prob_75 = tf.placeholder(tf.float32)


#权重
def weightVariable(shape):
    init = tf.random_normal(shape, stddev=0.01)
    return tf.Variable(init)


#偏置
def biasVariable(shape):
    init = tf.random_normal(shape)
    return tf.Variable(init)
示例#5
0
from sklearn.metrics import classification_report, confusion_matrix, precision_score, recall_score
import numpy as np

model = load_model(os.path.join('weight', args.model))

# load corpus and vocab
vocab = CharVocab(100000)  # 100k
corpus = Corpus(vocab, debug=False)
encoded_test = corpus.encoded_test
# encoded_test = ([10]*2000, [0] * 2000)

output_punc = {0: vocab.decode(0), 1: vocab.decode(1), 2: vocab.decode(2)}

# evaluation
y_pred = model.predict_generator(
    data_generator(encoded_test, args.batch_size, args.step_size,
                   len(output_punc)),
    len(encoded_test[0]) // (args.batch_size * args.step_size),
    verbose=1)

target_names = ['Blank', 'Comma', 'Period']
y_true = list(
    np.array(
        generator_y_true(encoded_test, args.batch_size, args.step_size,
                         len(output_punc))).reshape(-1))

if args.merge_punc:
    print('merge punctuation')
    target_names = ['Blank', 'Punctuation']
    y_true = [x if x == 0 else 1 for x in y_true]
    y_pred = [0 if x[0] > x[1] + x[2] else 1 for x in y_pred.reshape(-1, 3)]
else:
示例#6
0
def main():
    global config
    feature_dim = config.feature_dim
    n_classes = config.n_classes
    hidden_dim = config.hidden_dim
    n_train = config.n_train
    n_test = config.n_test
    n_epochs = config.n_epochs
    batch_size = config.batch_size
    lr = config.lr
    #
    if config.GPUs == '':
        num_threads = config.num_threads
    else:
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = config.GPUs
    #
    train_files = glob.glob(config.data_path_train)
    test_files = glob.glob(config.data_path_test)
    #
    x_idxs = tf.placeholder(tf.int64, shape=[None, 2])
    x_vals = tf.placeholder(tf.float32, shape=[None])
    x = tf.SparseTensor(x_idxs, x_vals, [batch_size, feature_dim])
    y = tf.placeholder(tf.float32, shape=[None, n_classes])
    #
    W1 = tf.Variable(
        tf.truncated_normal([feature_dim, hidden_dim],
                            stddev=2.0 / math.sqrt(feature_dim + hidden_dim)))
    b1 = tf.Variable(
        tf.truncated_normal([hidden_dim],
                            stddev=2.0 / math.sqrt(feature_dim + hidden_dim)))
    layer_1 = tf.nn.relu(tf.sparse_tensor_dense_matmul(x, W1) + b1)
    #
    W2 = tf.Variable(
        tf.truncated_normal([hidden_dim, n_classes],
                            stddev=2.0 / math.sqrt(hidden_dim + n_classes)))
    b2 = tf.Variable(
        tf.truncated_normal([n_classes],
                            stddev=2.0 / math.sqrt(n_classes + hidden_dim)))
    logits = tf.matmul(layer_1, W2) + b2
    #
    k = 1
    if k == 1:
        top_idxs = tf.argmax(logits, axis=1)
    else:
        top_idxs = tf.nn.top_k(logits, k=k, sorted=False)[1]
    #
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
    #
    train_step = tf.train.AdamOptimizer(lr).minimize(loss)
    #
    if config.GPUs == '':
        Config = tf.ConfigProto(inter_op_parallelism_threads=num_threads,
                                intra_op_parallelism_threads=num_threads)
    else:
        Config = tf.ConfigProto()
        Config.gpu_options.allow_growth = True
    #
    sess = tf.Session(config=Config)
    sess.run(tf.global_variables_initializer())
    #
    training_data_generator = data_generator(train_files, batch_size,
                                             n_classes)
    steps_per_epoch = n_train // batch_size
    n_steps = n_epochs * steps_per_epoch
    n_check = 50
    #
    begin_time = time.time()
    total_time = 0
    counter = 0
    #
    with open(config.log_file, 'a') as out:
        for i in range(n_steps):
            if i % n_check == 0:
                total_time += time.time() - begin_time
                print('Finished ', i, ' steps. Time elapsed for last', n_check,
                      'batches = ',
                      time.time() - begin_time)
                n_steps_val = n_test // batch_size
                test_data_generator = data_generator_tst(
                    test_files, batch_size)
                tmp_k = 0
                for h in range(
                        20):  # a few test batches to check the precision
                    idxs_batch, vals_batch, labels_batch = next(
                        test_data_generator)
                    top_k_classes = sess.run(top_idxs,
                                             feed_dict={
                                                 x_idxs: idxs_batch,
                                                 x_vals: vals_batch
                                             })
                    tmp_k += np.mean([
                        len(np.intersect1d(top_k_classes[j], labels_batch[j]))
                        / min(k, len(labels_batch[j]))
                        for j in range(len(top_k_classes))
                    ])
                print('test_acc: ', tmp_k / 20)
                print('#######################')
                print(i, int(total_time), tmp_k / 20, file=out)
                begin_time = time.time()
            idxs_batch, vals_batch, labels_batch = next(
                training_data_generator)
            sess.run(train_step,
                     feed_dict={
                         x_idxs: idxs_batch,
                         x_vals: vals_batch,
                         y: labels_batch
                     })
            if i % steps_per_epoch == steps_per_epoch - 1:
                total_time += time.time() - begin_time
                print('Finished ', i,
                      ' steps. Time elapsed for last 100 batches = ',
                      time.time() - begin_time)
                n_steps_val = n_test // batch_size
                test_data_generator = data_generator_tst(
                    test_files, batch_size)
                num_batches = 0
                p_at_k = 0
                for l in range(n_steps_val):  # precision on entire test data
                    idxs_batch, vals_batch, labels_batch = next(
                        test_data_generator)
                    top_k_classes = sess.run(top_idxs,
                                             feed_dict={
                                                 x_idxs: idxs_batch,
                                                 x_vals: vals_batch
                                             })
                    p_at_k += np.mean([
                        len(np.intersect1d(top_k_classes[j], labels_batch[j]))
                        / min(k, len(labels_batch[j]))
                        for j in range(len(top_k_classes))
                    ])
                    num_batches += 1
                #
                print('Overall p_at_1 after ', num_batches, 'batches = ',
                      p_at_k / num_batches)
                print(i, int(total_time), p_at_k / num_batches, file=out)
                #
                begin_time = time.time()
示例#7
0
from util import data_generator, data_len, sample_data_batch
import config_dat as cf
from keras.optimizers import Adagrad, Adam
model = DRVO().model
print(model.summary())

optimizer = Adam(lr=0.0005)
model.compile(loss="mean_squared_error",
              optimizer=optimizer,
              metrics=['accuracy'])

if cf.weight_path is not None:
    model.load_weights(cf.weight_path)
    print("weight is loaded")

test_generator = data_generator(cf.label_path, cf.label_name)
x_true, y_true, image_name = sample_data_batch(cf.label_path, cf.label_name,
                                               cf.image_batch)

###### Prediction with multiple batch ######

# result_list = []
# result = (model.predict_generator(test_generator, steps=data_len() / cf.image_batch))

###### Prediction with single batch #####
print(x_true.shape)
result = model.evaluate(x_true, y_true, batch_size=cf.image_batch)
prediction = model.predict(x_true, batch_size=cf.image_batch)
print(result)
print(prediction)
print(y_true)
示例#8
0
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

# load corpus and vocab
vocab = Vocab(20000)  # 20k
emoji_vocab = EmojiVocab(29)
corpus = Corpus(vocab, emoji_vocab, debug=False)

# train with keras
checkpoint = ModelCheckpoint('weight/model_%s_{epoch:02d}_{val_loss:02f}.h5' % args.model,
                             verbose=1, save_best_only=True, mode='auto')

earlystop = EarlyStopping(patience=1)

print('%s model is used' % args.model)
model = models[args.model](len(vocab), args.embedding_size, args.hidden_size, len(emoji_vocab), args.step_size)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])

model.fit_generator(
    generator=data_generator(corpus.encoded_train, args.batch_size, args.step_size, len(emoji_vocab)),
    validation_data=data_generator(corpus.encoded_dev, args.batch_size, args.step_size, len(emoji_vocab)),
    steps_per_epoch=len(corpus.encoded_train[0]) // (args.batch_size * args.step_size),
    validation_steps=len(corpus.encoded_dev[0]) // (args.batch_size * args.step_size),
    epochs=2,
    class_weight=[0.1] + [1] * (len(emoji_vocab) - 1),  # give blank less weight
    callbacks=[checkpoint, earlystop],
    shuffle=False)  # We will use stateful LSTM, don't shuffle. Also, data is already shuffled before.

model.evaluate_generator(data_generator(corpus.encoded_test, args.batch_size, args.step_size, len(emoji_vocab)),
                         steps=len(corpus.encoded_test[0]) // (args.batch_size * args.step_size))