Exemple #1
19
    def load_datasets(self):
        if self.mode == 'voc':
            self.num_classes = 21

            train_pascal_12 = tfds.load('voc/2012',
                                        data_dir=self.data_dir,
                                        split='train')
            valid_train_12 = tfds.load('voc/2012',
                                       data_dir=self.data_dir,
                                       split='validation')

            train_pascal_07 = tfds.load("voc",
                                        data_dir=self.data_dir,
                                        split='train')
            valid_train_07 = tfds.load("voc",
                                       data_dir=self.data_dir,
                                       split='validation')

            train_data = train_pascal_07.concatenate(valid_train_07).\
                concatenate(train_pascal_12).concatenate(valid_train_12)
            test_data = tfds.load("voc", data_dir=self.data_dir, split='test')

        else:
            self.num_classes = 81

            train_data = tfds.load('coco/2017',
                                   data_dir=self.data_dir,
                                   split='train')
            train_data = train_data.filter(lambda x: tf.reduce_all(
                tf.not_equal(tf.size(x['objects']['bbox']), 0)))
            train_data = train_data.filter(lambda x: tf.reduce_all(
                tf.not_equal(tf.size(x['objects']['label']), 0)))

            test_data = tfds.load('coco/2017',
                                  data_dir=self.data_dir,
                                  split='validation')
            test_data = test_data.filter(lambda x: tf.reduce_all(
                tf.not_equal(tf.size(x['objects']['bbox']), 0)))
            test_data = test_data.filter(lambda x: tf.reduce_all(
                tf.not_equal(tf.size(x['objects']['label']), 0)))

        self.number_train = train_data.reduce(0, lambda x, _: x + 1).numpy()
        self.number_test = test_data.reduce(0, lambda x, _: x + 1).numpy()

        self.training_dataset = prepare_dataset(train_data,
                                                self.image_size,
                                                self.batch_size,
                                                self.target,
                                                self.num_classes,
                                                train=True)
        self.validation_dataset = prepare_dataset(test_data,
                                                  self.image_size,
                                                  self.batch_size,
                                                  self.target,
                                                  self.num_classes,
                                                  train=False)
Exemple #2
0
def run_gru():
    train_file = 'Quora_question_pair_partition/train.tsv'
    dev_file = 'Quora_question_pair_partition/dev.tsv'
    test_file = 'Quora_question_pair_partition/test.tsv'

    # Prepare datasets
    q1_train, q2_train, y_train, emb_matrix = preprocessing.prepare_dataset(
        train_file, 1)
    q1_dev, q2_dev, y_dev = preprocessing.prepare_dataset(dev_file)
    q1_test, q2_test, y_test = preprocessing.prepare_dataset(test_file)

    net = create_model(emb_matrix)
    # Start training
    hist = net.fit(
        [q1_train, q2_train],
        y_train,
        validation_data=([q1_dev, q2_dev], y_dev),
        batch_size=4,
        nb_epoch=20,
        shuffle=True,
    )

    # compute final accuracy on training and test sets
    scores = net.evaluate([q1_test, q2_test], y_test)
    print("\n%s: %.2f%%" % (net.metrics_names[2], scores[2] * 100))
Exemple #3
0
def run_gru():
    train_file = 'Quora_question_pair_partition/train.tsv'
    dev_file = 'Quora_question_pair_partition/dev.tsv'
    test_file = 'Quora_question_pair_partition/test.tsv'

    # Prepare datasets
    q1_train, q2_train, y_train, emb_matrix = preprocessing.prepare_dataset(train_file, 1)
    q1_dev, q2_dev, y_dev = preprocessing.prepare_dataset(dev_file)
    q1_test, q2_test, y_test = preprocessing.prepare_dataset(test_file)

    net = create_model(emb_matrix)
    # Start training
    hist = net.fit([q1_train, q2_train], y_train,
                   validation_data=([q1_dev, q2_dev], y_dev),
                   batch_size=4, nb_epoch=20, shuffle=True, )

    # compute final accuracy on training and test sets
    scores = net.evaluate([q1_test, q2_test], y_test)
    print("\n%s: %.2f%%" % (net.metrics_names[2], scores[2] * 100))
Exemple #4
0
    dense = Dense(dense_dim, activation=activation)(dense)
    dense = Dropout(dense_dropout)(dense)
    # out_ = Dense(3, activation='sigmoid')(dense)
    out_ = Dense(1, activation='sigmoid')(dense)

    model = Model(inputs=model_input, outputs=out_)
    model.compile(optimizer=Adam(lr=lr), loss='binary_crossentropy',
                  metrics=['binary_crossentropy', 'accuracy', model_utils.f1])

    return model


if __name__ == "__main__":
    train_file = 'Quora_question_pair_partition/train.tsv'
    dev_file = 'Quora_question_pair_partition/dev.tsv'
    test_file = 'Quora_question_pair_partition/test.tsv'
    # Prepare datasets
    q1_train, q2_train, y_train, word_embedding_matrix = preprocessing.prepare_dataset(train_file, 1)
    q1_dev, q2_dev, y_dev = preprocessing.prepare_dataset(dev_file)
    q1_test, q2_test, y_test = preprocessing.prepare_dataset(test_file)

    net = create_model(word_embedding_matrix)
    # Start training
    hist = net.fit([q1_train, q2_train], y_train,
                   validation_data=([q1_dev, q2_dev], y_dev),
                   batch_size=4, nb_epoch=20, shuffle=True, )

    # compute final accuracy on training and test sets
    scores = net.evaluate([q1_test, q2_test], y_test)
    print("\n%s: %.2f%%" % (net.metrics_names[2], scores[2] * 100))
Exemple #5
0
specs = set_priorBox(MODEL_NAME)
priors = create_priors_boxes(specs, IMAGE_SIZE[0])
target_transform = MatchingPriors(priors, center_variance, size_variance,
                                  iou_threshold)

if TRAIN_MODE == 'voc':
    test_data = tfds.load('voc', data_dir=DATASET_DIR, split='test')
    number_test = test_data.reduce(0, lambda x, _: x + 1).numpy()
    print("테스트 데이터 개수:", number_test)
    CLASSES_NUM = 21
    with open('./pascal_labels.txt') as f:
        CLASSES = f.read().splitlines()

    validation_dataset = prepare_dataset(test_data,
                                         IMAGE_SIZE,
                                         BATCH_SIZE,
                                         target_transform,
                                         CLASSES_NUM,
                                         train=False)

else:
    if EVAL_TESTDEV:
        test_data, test_info = tfds.load('coco/2017',
                                         data_dir=DATASET_DIR,
                                         split='validation',
                                         with_info=True)
        # test_data = test_data.filter(lambda x: tf.reduce_all(tf.not_equal(tf.size(x['objects']['bbox']), 0)))
        # test_data = test_data.filter(lambda x: tf.reduce_all(tf.not_equal(tf.size(x['objects']['label']), 0)))

    else:
        test_dev_idList = []
        with open('datasets/image_info_test-dev2017.json', 'r') as f:
Exemple #6
0
def run(FLAGS):
    # quora_full_dataset = FLAGS.full_dataset
    train_file = FLAGS.train_path
    dev_file = FLAGS.dev_path
    test_file = FLAGS.test_path
    embeddings = FLAGS.embeddings
    model = FLAGS.model
    mode = FLAGS.mode
    maxlen = FLAGS.max_sent_length
    max_nb_words = FLAGS.max_nb_words
    experiment = FLAGS.experiment
    dataset = FLAGS.task
    sent_embed = FLAGS.sent_embed

    if embeddings == 'elmo':
        init_embeddings = 0
    else:
        init_embeddings = 1

    word_index = vocab.prepare_vocab(train_file, embeddings)

    # Prepare datasets
    if init_embeddings == 1:
        q1_train, q2_train, y_train, qid_train, raw1_train, raw2_train, word_embedding_matrix = preprocessing.prepare_dataset(
            train_file, maxlen, max_nb_words, experiment, dataset, embeddings,
            init_embeddings)
    else:
        q1_train, q2_train, y_train, qid_train, raw1_train, raw2_train = preprocessing.prepare_dataset(
            train_file, maxlen, max_nb_words, experiment, dataset, embeddings,
            init_embeddings)
        word_embedding_matrix = np.zeros(1)

    q1_dev, q2_dev, y_dev, qid_dev, raw1_dev, raw2_dev = preprocessing.prepare_dataset(
        dev_file, maxlen, max_nb_words, experiment, dataset, embeddings)
    q1_test, q2_test, y_test, qid_test, raw1_test, raw2_test = preprocessing.prepare_dataset(
        test_file, maxlen, max_nb_words, experiment, dataset, embeddings)

    if dataset == 'snli':
        y_train = to_categorical(y_train, num_classes=None)
        y_dev = to_categorical(y_dev, num_classes=None)
        y_test = to_categorical(y_test, num_classes=None)

    net = create_model(word_embedding_matrix)
    net.summary()

    filepath = "models/weights.best.%s.%s.%s.%s.%s.hdf5" % (
        FLAGS.task, model, experiment, embeddings, sent_embed)
    if mode == "ensemble":
        print("Create ensemble of models")
    elif mode == "fine-tuning":
        model_file = "models/weights.best.quora.dec_att.training_full.glove.no_univ_sent.hdf5"
        print("Loading pre-trained model from %s:" % model_file)
        net.load_weights(model_file)
        net = freeze_layers(net)
        net.compile(
            optimizer=Adam(lr=1e-4),
            loss='binary_crossentropy',
            metrics=['binary_crossentropy', 'accuracy', model_utils.f1])
        t0 = time.time()
        callbacks = get_callbacks(filepath)
        history = net.fit(
            [q1_train, q2_train, raw1_train, raw2_train],
            y_train,
            validation_data=([q1_dev, q2_dev, raw1_dev, raw2_dev], y_dev),
            batch_size=FLAGS.batch_size,
            nb_epoch=FLAGS.max_epochs,
            shuffle=True,
            callbacks=callbacks)

        pickle_file = "saved_history/history.%s.%s.%s.%s.%s.pickle" % (
            FLAGS.task, model, experiment, embeddings, sent_embed)
        with open(pickle_file, 'wb') as handle:
            pickle.dump(history.history,
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

        t1 = time.time()
        print("Training ended at", datetime.datetime.now())
        print("Minutes elapsed: %f" % ((t1 - t0) / 60.))
    elif mode == "transfer_learning":
        model_file = "models/weights.best.snli.esim.hdf5"
        print("Loading pre-trained model from %s:" % model_file)
        net.load_weights(model_file)
        net = replace_layer(net)
        net.compile(
            optimizer=Adam(lr=1e-4),
            loss='binary_crossentropy',
            metrics=['binary_crossentropy', 'accuracy', model_utils.f1])
        t0 = time.time()
        callbacks = get_callbacks(filepath)
        history = net.fit(
            [q1_train, q2_train, raw1_train, raw2_train],
            y_train,
            validation_data=([q1_dev, q2_dev, raw1_dev, raw2_dev], y_dev),
            batch_size=FLAGS.batch_size,
            nb_epoch=FLAGS.max_epochs,
            shuffle=True,
            callbacks=callbacks)

        pickle_file = "saved_history/history.%s.%s.%s.%s.%s.pickle" % (
            FLAGS.task, model, experiment, embeddings, sent_embed)
        with open(pickle_file, 'wb') as handle:
            pickle.dump(history.history,
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

        t1 = time.time()
        print("Training ended at", datetime.datetime.now())
        print("Minutes elapsed: %f" % ((t1 - t0) / 60.))
    elif mode == "load":
        print("Loading weights from %s" % filepath)
        net.load_weights(filepath)
        net.compile(
            optimizer=Adam(lr=1e-3),
            loss='binary_crossentropy',
            metrics=['binary_crossentropy', 'accuracy', model_utils.f1])
    elif mode == "training":
        # Start training
        print("Starting training at", datetime.datetime.now())
        t0 = time.time()
        callbacks = get_callbacks(filepath)
        history = net.fit(
            [q1_train, q2_train, raw1_train, raw2_train],
            y_train,
            validation_data=([q1_dev, q2_dev, raw1_dev, raw2_dev], y_dev),
            batch_size=FLAGS.batch_size,
            nb_epoch=FLAGS.max_epochs,
            shuffle=True,
            callbacks=callbacks)

        pickle_file = "saved_history/history.%s.%s.%s.%s.%s.pickle" % (
            FLAGS.task, model, experiment, embeddings, sent_embed)
        with open(pickle_file, 'wb') as handle:
            pickle.dump(history.history,
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

        t1 = time.time()
        print("Training ended at", datetime.datetime.now())
        print("Minutes elapsed: %f" % ((t1 - t0) / 60.))

        max_val_acc, idx = get_best(history)
        print('Maximum accuracy at epoch', '{:d}'.format(idx + 1), '=',
              '{:.4f}'.format(max_val_acc))
    else:
        print("------------Unknown mode------------")

    test_f1 = get_confusion_matrix(net, q1_test, q2_test, y_test, raw1_test,
                                   raw2_test)

    predictions = find_prediction_probability(net, q1_test, q2_test, y_test,
                                              qid_test, raw1_test, raw2_test)
    write_predictions(predictions)

    # cvscores, loss_scores = evaluate_model(word_embedding_matrix, q1_train, q2_train, y_train
    #                                        q1_test, q2_test, y_test)
    print("Finished running %s model on %s with %s and %s" %
          (model, experiment, embeddings, sent_embed))
    # print_crossval(cvscores)
    # print("Crossvalidation accuracy result: %.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))
    # print("Crossvalidation lostt result: %.2f (+/- %.2f)" % (np.mean(loss_scores), np.std(loss_scores)))

    if mode != "ensemble":
        test_loss, test_acc = evaluate_best_model(net, q1_test, q2_test,
                                                  y_test, raw1_test, raw2_test,
                                                  filepath)

    else:
        test_loss = evaluate_error(net, q1_test, q2_test, raw1_test, raw2_test,
                                   y_test)
        test_acc = evaluate_accuracy(net, q1_test, q2_test, raw1_test,
                                     raw2_test, y_test)
    print(
        'Evaluation without crossval: loss = {0:.4f}, accuracy = {1:.4f} F1-score = {2:.4f}'
        .format(test_loss, test_acc * 100, test_f1 * 100))

    with open("results.txt", "a") as myfile:
        myfile.write(
            "Finished running %s model on %s with %s and %s in %s mode" %
            (model, experiment, embeddings, sent_embed, mode))
        myfile.write(
            'Evaluation without crossval: loss = {0:.4f}, accuracy = {1:.4f} F1-score = {2:.4f}'
            .format(test_loss, test_acc * 100, test_f1 * 100))
        myfile.write('\n')
Exemple #7
0
dev_sentences = load_sentences(data_path, lower, "dev")
test_train_sentences = load_sentences(data_path, lower, "train_test")

dico_words_train = word_mapping(train_sentences, lower)[0]

dico_words, word_to_id, id_to_word = augment_with_pretrained(
    dico_words_train.copy(), parameters['pre_emb'],
    list(
        itertools.chain.from_iterable([[w[0] for w in s]
                                       for s in dev_sentences]))
    if not parameters['all_emb'] else None)

dico_chars, char_to_id, id_to_char = char_mapping(train_sentences)
dico_tags, tag_to_id, id_to_tag = tag_mapping(data_path, "train")

train_data = prepare_dataset(train_sentences, word_to_id, char_to_id,
                             tag_to_id, data_path, "train", lower)
dev_data = prepare_dataset(dev_sentences, word_to_id, char_to_id, tag_to_id,
                           data_path, "dev", lower)
test_train_data = prepare_dataset(test_train_sentences, word_to_id, char_to_id,
                                  tag_to_id, data_path, "train_test", lower)

print("%i / %i sentences in train / dev." % (len(train_data), len(dev_data)))

all_word_embeds = {}
for i, line in enumerate(codecs.open(opts.pre_emb, 'r', 'utf-8')):
    s = line.strip().split()
    if len(s) == parameters['word_dim'] + 1:
        all_word_embeds[s[0]] = np.array([float(i) for i in s[1:]])

print('Loaded %i pretrained embeddings.' % len(all_word_embeds))
Exemple #8
0
# compute vocab size
params['label_size'] = len(mappings['id_to_tag'])
params['word_vocab_size'] = len(mappings['id_to_word'])
params['bi_word_vocab_size'] = len(mappings['bi_id_to_word'])
params['char_vocab_size'] = len(mappings['id_to_char'])
params['feat_vocab_size'] = [len(item) for item in mappings['id_to_feat_list']]

print("word vocab size: ", params['word_vocab_size'])
print("bi word vocab size: ", params['bi_word_vocab_size'])

# Index data
dataset = dict()
dataset['train'] = prepare_dataset(train_sentences,
                                   mappings,
                                   zeros,
                                   lower,
                                   is_train=True)
dataset['bi_train'] = prepare_dataset_bi(bi_train_sentences,
                                         mappings,
                                         zeros,
                                         lower,
                                         is_train=True)
dataset['dev'] = prepare_dataset(dev_sentences,
                                 mappings,
                                 zeros,
                                 lower,
                                 is_train=True)
dataset['test'] = prepare_dataset(test_sentences,
                                  mappings,
                                  zeros,
Exemple #9
0
specs = [
                SSDSpec(38, 8, SSDBoxSizes(30, 60), [2]),
                SSDSpec(19, 16, SSDBoxSizes(60, 111), [2, 3]),
                SSDSpec(10, 32, SSDBoxSizes(111, 162), [2, 3]),
                SSDSpec(5, 64, SSDBoxSizes(162, 213), [2, 3]),
                SSDSpec(3, 100, SSDBoxSizes(213, 264), [2]),
                SSDSpec(1, 300, SSDBoxSizes(264, 315), [2])
        ]


priors = generate_ssd_priors(specs, IMAGE_SIZE[0])
target_transform = MatchPrior(priors, center_variance, size_variance, iou_threshold)

# instantiate the datasets
training_dataset = prepare_dataset(train_data, IMAGE_SIZE, BATCH_SIZE, target_transform, train=True)
validation_dataset = prepare_dataset(test_data, IMAGE_SIZE, BATCH_SIZE, target_transform, train=False)

print("Building SSD Model with EfficientNet{0} backbone..".format(MODEL_NAME))
model = ssd(MODEL_NAME)
steps_per_epoch = number_train // BATCH_SIZE
validation_steps = number_test // BATCH_SIZE
print("Number of Train Batches:", steps_per_epoch)
print("Number of Test Batches:", validation_steps)


reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=1, min_lr=1e-5, verbose=1)
checkpoint = ModelCheckpoint(checkpoint_filepath, monitor='val_loss', save_best_only=True, save_weights_only=True, verbose=1)

model.compile(
    optimizer = tf.keras.optimizers.Adam(learning_rate=base_lr),