Exemplo n.º 1
0
    def load_models(self, c_c, r_c, a_c):
        # Load the models on separate sessions
        print("Loading the models")
        c_g = tf.Graph()
        with c_g.as_default():
            cae = CAEModel(c_c)

        c_s = tf.Session(graph=c_g)
        with c_s.as_default():
            with c_g.as_default():
                cae.load(c_s)

        r_g = tf.Graph()
        with r_g.as_default():
            rnn = RNNModel(r_c)

        r_s = tf.Session(graph=r_g)
        with r_s.as_default():
            with r_g.as_default():
                rnn.load(r_s)

        a_g = tf.Graph()
        with a_g.as_default():
            actions = ActionsModel(a_c)

        a_s = tf.Session(graph=a_g)
        with a_s.as_default():
            with a_g.as_default():
                actions.load(a_s)

        return cae, c_s, rnn, r_s, actions, a_s
Exemplo n.º 2
0
def main(wikitext2_path,
         save_dir,
         num_gpus=1,
         epochs=5,
         bptt=100,
         max_vocab_size=100000,
         batch_size=64,
         mode='train'):
    lines = open(os.path.join(wikitext2_path, 'wiki.train.tokens'),
                 'r').readlines()
    x_texts = [line.lower() for line in lines if len(line) > 40]
    #text_batches = create_data(lines, bptt=bptt)
    # Modelling part
    vocab_path = os.path.join(save_dir, 'vocab.p')
    if os.path.exists(save_dir):
        vocab = pickle.load(open(vocab_path, 'rb'))
        model = RNNModel(2, vocab)
        model.load(save_dir)
    else:
        vocab = Vocab(wikitext2_path, max_vocab_size=max_vocab_size).vocab
        model = RNNModel(2, vocab)

    if mode == 'train':
        try:
            model.representation_learning(
                x_texts,
                verbose=True,
                epochs=epochs,
                num_gpus=num_gpus,
                batch_size=batch_size,
                bptt=bptt,
                on_epoch_done=lambda m: save_model_and_vocab(
                    m, vocab, vocab_path, save_dir))

            save_model_and_vocab(model, vocab, vocab_path, save_dir)
        except KeyboardInterrupt:
            save_model_and_vocab(model, vocab, vocab_path, save_dir)
    else:
        model.representation_learning(text_batches, evaluate=True)
        print("Evaluation is not implemented yet.")
Exemplo n.º 3
0
def train_deep_models():
    from models.rnn_model import RNNModel
    from models.textcnn_model import TextCNNModel
    from models.dpcnn_model import DpcnnModel
    train_x, test_x, train_y, vocab = read_onehot_feature_label()
    models = [
        TextCNNModel(max_len=300,
                     num_folds=1,
                     name='textcnn',
                     filter_sizes='3,4,5',
                     embedding_dim=300,
                     hidden_dim=128,
                     num_filters=512,
                     num_classes=2,
                     batch_size=32,
                     vocabulary_size=len(vocab),
                     dropout=0.5,
                     num_epochs=4),
        RNNModel(max_len=300,
                 num_folds=1,
                 name='rnn',
                 embedding_dim=300,
                 hidden_dim=128,
                 num_classes=2,
                 batch_size=32,
                 vocabulary_size=len(vocab),
                 num_epochs=10),
        DpcnnModel(max_len=300,
                   num_folds=1,
                   name='dpcnn',
                   embedding_dim=300,
                   hidden_dim=256,
                   num_classes=2,
                   batch_size=32,
                   vocabulary_size=len(vocab),
                   num_epochs=10)
    ]
    for m in models:
        predict_path = config.output_dir + "%s.csv" % m.name
        submit_path = config.output_dir + "%s_submit.csv" % m.name
        score = m.train_predict(train_x, train_y, test_x, predict_path)
        generate_submit_result(config.data_file_path,
                               predict_path,
                               submit_path=submit_path)
        print(m.name, score)
Exemplo n.º 4
0
    def build(self):
        if 'custom' in self.config and self.config['custom']:
            return self.build_custom_model()

        #if self.dataset.data_type == Dataset.TEXT_TYPE:
        #    lm = self.build_language_model()

        # Right now there is an assumption that
        if self.dataset.data_type == Dataset.IMAGE_TYPE and self.label.label_type == Label.BINARY:
            return CNNModel(2, input_shape=(128, 128))

        if self.dataset.data_type == Dataset.IMAGE_TYPE and self.label.label_type == Label.CLASSIFICATION:
            return CNNModel(len(self.label.classes), input_shape=(128, 128))

        word_list = pickle.load(
            open("vendor/keras_language_model/vocab.p", "rb"))

        if self.dataset.data_type == Dataset.TEXT_TYPE and self.label.label_type == Label.BINARY:
            rnn_model = RNNModel(2, word_list)
            rnn_model.load_lm('vendor/keras_language_model')
            return rnn_model

        if self.dataset.data_type == Dataset.TEXT_TYPE and self.label.label_type == Label.CLASSIFICATION:
            rnn_model = RNNModel(len(self.label.classes), word_list)
            rnn_model.load_lm('vendor/keras_language_model')
            return rnn_model
            #return LM_TextClassifier(lm, len(self.label.classes))

        if self.dataset.data_type == Dataset.TEXT_TYPE and self.label.label_type == Label.SEQUENCE:
            return SequenceModel(
                valid_outputs=self.label.valid_tokens,
                seq2seq=False,
                character_mode=False,
            )

        if self.dataset.data_type == Dataset.OBJECT_DETECTION_TYPE and self.label.label_type == Label.OBJECT_DETECTION:
            from models.lightnet_model import LightnetModel
            return LightnetModel()

        return StubModel()
Exemplo n.º 5
0
def main() -> None:

    args = parse_args()
    raw_dataset = RawDataset(subsample=args.subsample)
    print(f'Raw dataset has {len(raw_dataset.raw_df)} records.')

    processed_dataset = ProcessedDataset(raw_dataset)
    print(
        f'Processed dataset has {len(processed_dataset.processed_df)} records.'
    )

    featurized_dataset = FeaturizedDataset(processed_dataset)
    print(
        f'Featurized dataset has {len(featurized_dataset.featurized_df)} records.'
    )

    model = RNNModel(featurized_dataset)
    handler = ModelHandler(model, use_wandb=True)
    print(f'Comensing training on {handler.device}')
    handler.fit(args.max_epochs)
    print('Done.')
Exemplo n.º 6
0
def running(is_train=True):
    if not os.path.exists(FLAGS.checkpoints_dir):
        os.makedirs(FLAGS.checkpoints_dir)

    # shakespeare = Shakespeare(batch_size=FLAGS.batch_size, n_steps=FLAGS.n_steps)
    # with open('./datasets/Shakespeare/shakespeare.pkl', 'wb') as f:
    #     pickle.dump(shakespeare, f)
    with open('./datasets/Shakespeare/shakespeare.pkl', 'rb') as f:
        shakespeare = pickle.load(f)
    vocab_size = shakespeare.vocab_size

    if is_train:
        inputs = tf.placeholder(tf.int32, [FLAGS.batch_size, FLAGS.n_steps])
        labels = tf.placeholder(tf.int32, [FLAGS.batch_size])
    else:
        inputs = tf.placeholder(tf.int32, [1, FLAGS.n_steps])
        labels = None
    model = RNNModel(inputs,
                     labels,
                     n_units=FLAGS.n_units,
                     n_layers=FLAGS.n_layers,
                     lr=FLAGS.learning_rate,
                     vocab_size=vocab_size)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    saver = tf.train.Saver(max_to_keep=2)
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())

        start_epoch = 0
        checkpoint = tf.train.latest_checkpoint(FLAGS.checkpoints_dir)
        if FLAGS.is_restore:
            if checkpoint:
                saver.restore(sess, checkpoint)
                logging.info(
                    "restore from the checkpoint {0}".format(checkpoint))
                start_epoch += int(checkpoint.split('-')[-1])
        if is_train:
            logging.info('training start...')
            epoch = 0
            try:
                for epoch in range(start_epoch, FLAGS.epochs):
                    shakespeare.batch_index = 0
                    for batch in range(shakespeare.n_chunks):
                        batch_x, batch_y = shakespeare.next_batch()
                        loss, _ = sess.run([model.loss, model.train_op],
                                           feed_dict={
                                               inputs: batch_x,
                                               labels: batch_y
                                           })
                        logging.info('epoch: %s,  batch: %s, loss: %s' %
                                     (epoch, batch, loss))
                    if epoch % 60 == 0:
                        saver.save(sess,
                                   os.path.join(FLAGS.checkpoints_dir,
                                                FLAGS.model_prefix),
                                   global_step=epoch)
                logging.info(
                    'optimization done! enjoy your Shakespeare composer!')
            except KeyboardInterrupt:
                logging.info(
                    'interrupt manually, try saving checkpoint for now...')
                saver.save(sess,
                           os.path.join(FLAGS.checkpoints_dir,
                                        FLAGS.model_prefix),
                           global_step=epoch)
                logging.info(
                    'last epoch were saved, next time will start from epoch {}.'
                    .format(epoch))
        else:
            logging.info('I am thinking to compose Shakespeare drama...')
            if checkpoint:
                saver.restore(sess, checkpoint)
                logging.info(
                    "restore from the checkpoint {0}".format(checkpoint))
                start_epoch += int(checkpoint.split('-')[-1])

            vocab_int_map = shakespeare.vocab_int_map
            start_sentence = input('please input a sentence in Chinese: ')
            sent = process_sent(start_sentence, vocab_int_map, FLAGS.n_steps)
            outputs, last_state = sess.run([model.outputs, model.states],
                                           feed_dict={inputs: sent})
            sent = np.append(sent, np.argmax(outputs))
            sent = np.array([sent[1:]])

            next_words = []
            for i in range(2000):
                outputs, last_state = sess.run([model.outputs, model.states],
                                               feed_dict={
                                                   inputs: sent,
                                                   model.states: last_state
                                               })
                next_word_index = np.argmax(outputs)
                next_words.append(next_word_index)
                sent = np.append(sent, next_word_index)
                sent = np.array([sent[1:]])
            drama_text = [{v: k
                           for k, v in vocab_int_map.items()}[i]
                          for i in next_words]
            drama_text.insert(0, start_sentence)
            pretty_print(drama_text)