Example #1
0
def main(unused_argv):
    
    vocab, pretrained_matrix = load_glove(vocab_size=100000, embedding_size=300)
    with tf.Graph().as_default():

        image_id, image_features, indicator, word_ids, pointer_ids = import_mscoco(
            mode="train", batch_size=FLAGS.batch_size, num_epochs=FLAGS.num_epochs, is_mini=FLAGS.is_mini)
        lengths = tf.reduce_sum(indicator, [1])
        show_and_tell_cell = ShowAndTellCell(300)
        best_first_image_captioner = BestFirstImageCaptioner(show_and_tell_cell, vocab, pretrained_matrix)
        word_logits, wids, pointer_logits, pids, ids, _lengths = best_first_image_captioner(
            mean_image_features=image_features,
            word_ids=word_ids, pointer_ids=pointer_ids, lengths=lengths)
        tf.losses.sparse_softmax_cross_entropy(pointer_ids, pointer_logits)
        tf.losses.sparse_softmax_cross_entropy(word_ids, word_logits)
        loss = tf.losses.get_total_loss()
        
        global_step = tf.train.get_or_create_global_step()
        optimizer = tf.train.AdamOptimizer()
        learning_step = optimizer.minimize(loss, var_list=best_first_image_captioner.variables, 
            global_step=global_step)

        captioner_saver = tf.train.Saver(var_list=best_first_image_captioner.variables + [global_step])
        captioner_ckpt, captioner_ckpt_name = get_best_first_checkpoint()
        with tf.Session() as sess:
            
            sess.run(tf.variables_initializer(optimizer.variables()))
            if captioner_ckpt is not None:
                captioner_saver.restore(sess, captioner_ckpt)
            else:
                sess.run(tf.variables_initializer(best_first_image_captioner.variables + [global_step]))
            captioner_saver.save(sess, captioner_ckpt_name, global_step=global_step)
            last_save = time.time()
            
            for i in itertools.count():
                
                time_start = time.time()
                try:
                    twids, tpids, _ids, _lengths, _loss, _learning_step = sess.run([
                        word_ids, pointer_ids, ids, lengths, loss, learning_step])
                except:
                    break
                    
                iteration = sess.run(global_step)
                
                insertion_sequence = insertion_sequence_to_array(twids, tpids, _lengths, vocab)
                    
                print(PRINT_STRING.format(
                    iteration, _loss, 
                    list_of_ids_to_string(insertion_sequence[0], vocab), 
                    list_of_ids_to_string(twids[0, :].tolist(), vocab), 
                    FLAGS.batch_size / (time.time() - time_start)))
                
                new_save = time.time()
                if new_save - last_save > 3600: # save the model every hour
                    captioner_saver.save(sess, captioner_ckpt_name, global_step=global_step)
                    last_save = new_save
                    
            captioner_saver.save(sess, captioner_ckpt_name, global_step=global_step)
            print("Finishing training.")
def main(unused_argv):
    
    vocab, pretrained_matrix = load_glove(vocab_size=100000, embedding_size=300)
    with tf.Graph().as_default():

        image_id, running_ids, indicator, previous_id, next_id, pointer, image_features = (
            import_mscoco(mode="train", batch_size=BATCH_SIZE, num_epochs=100, is_mini=True))
        best_first_module = BestFirstModule(pretrained_matrix)
        pointer_logits, word_logits = best_first_module(
            image_features, running_ids, previous_id, indicators=indicator, pointer_ids=pointer)
        tf.losses.sparse_softmax_cross_entropy(pointer, pointer_logits)
        tf.losses.sparse_softmax_cross_entropy(next_id, word_logits)
        loss = tf.losses.get_total_loss()
        
        ids = tf.argmax(word_logits, axis=-1, output_type=tf.int32)
        
        global_step = tf.train.get_or_create_global_step()
        learning_rate = LEARNING_RATE
        learning_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, 
            var_list=best_first_module.variables, global_step=global_step)

        captioner_saver = tf.train.Saver(var_list=best_first_module.variables + [global_step])
        captioner_ckpt, captioner_ckpt_name = get_best_first_checkpoint()
        with tf.Session() as sess:
            
            if captioner_ckpt is not None:
                captioner_saver.restore(sess, captioner_ckpt)
            else:
                sess.run(tf.variables_initializer(best_first_module.variables + [global_step]))
            captioner_saver.save(sess, captioner_ckpt_name, global_step=global_step)
            last_save = time.time()
            _ids, _loss, _learning_step = sess.run([ids, loss, learning_step])
            
            for i in itertools.count():
                
                time_start = time.time()
                try:
                    _ids, _loss, _learning_step = sess.run([ids, loss, learning_step])
                except:
                    break
                    
                iteration = sess.run(global_step)
                    
                print(PRINT_STRING.format(
                    iteration, _loss, list_of_ids_to_string(_ids.tolist(), vocab), 
                    BATCH_SIZE / (time.time() - time_start)))
                
                new_save = time.time()
                if new_save - last_save > 3600: # save the model every hour
                    captioner_saver.save(sess, captioner_ckpt_name, global_step=global_step)
                    last_save = new_save
                    
            captioner_saver.save(sess, captioner_ckpt_name, global_step=global_step)
            print("Finishing training.")
Example #3
0
def main(unused_argv):

    vocab, pretrained_matrix = load_glove(vocab_size=100000,
                                          embedding_size=300)
    with tf.Graph().as_default():

        image_id, spatial_features, input_seq, target_seq, indicator = (
            import_mscoco(mode="train",
                          batch_size=BATCH_SIZE,
                          num_epochs=100,
                          is_mini=True))
        visual_sentinel_cell = VisualSentinelCell(300)
        image_captioner = ImageCaptioner(visual_sentinel_cell, vocab,
                                         pretrained_matrix)
        logits, ids = image_captioner(lengths=tf.reduce_sum(indicator, axis=1),
                                      spatial_image_features=spatial_features,
                                      seq_inputs=input_seq)
        tf.losses.sparse_softmax_cross_entropy(target_seq,
                                               logits,
                                               weights=indicator)
        loss = tf.losses.get_total_loss()
        learning_step = tf.train.GradientDescentOptimizer(1.0).minimize(
            loss, var_list=image_captioner.variables)

        captioner_saver = tf.train.Saver(var_list=image_captioner.variables)
        captioner_ckpt, captioner_ckpt_name = get_visual_sentinel_checkpoint()
        with tf.Session() as sess:
            sess.run(tf.variables_initializer(image_captioner.variables))
            if captioner_ckpt is not None:
                captioner_saver.restore(sess, captioner_ckpt)
            captioner_saver.save(sess, captioner_ckpt_name)
            last_save = time.time()
            for i in itertools.count():
                time_start = time.time()
                try:
                    _ids, _loss, _learning_step = sess.run(
                        [ids, loss, learning_step])
                except:
                    break
                print(
                    PRINT_STRING.format(
                        i, _loss,
                        list_of_ids_to_string(_ids[0, :].tolist(), vocab),
                        BATCH_SIZE / (time.time() - time_start)))
                new_save = time.time()
                if new_save - last_save > 3600:  # save the model every hour
                    captioner_saver.save(sess, captioner_ckpt_name)
                    last_save = new_save

            captioner_saver.save(sess, captioner_ckpt_name)
            print("Finishing training.")
def main(unused_argv):

    vocab, pretrained_matrix = load_glove(vocab_size=100000,
                                          embedding_size=300)
    attribute_map = get_visual_attributes()
    attribute_to_word_lookup_table = vocab.word_to_id(
        attribute_map.reverse_vocab)

    with tf.Graph().as_default():

        (image_id, image_features, object_features, input_seq, target_seq,
         indicator, attributes) = import_mscoco(mode="train",
                                                batch_size=FLAGS.batch_size,
                                                num_epochs=FLAGS.num_epochs,
                                                is_mini=FLAGS.is_mini)

        attribute_detector = AttributeDetector(1000)
        _, image_attributes, object_attributes = attribute_detector(
            image_features, object_features)

        grounded_attribute_cell = GroundedAttributeCell(1024)
        attribute_captioner = AttributeCaptioner(
            grounded_attribute_cell, vocab, pretrained_matrix,
            attribute_to_word_lookup_table)
        logits, ids = attribute_captioner(lengths=tf.reduce_sum(indicator,
                                                                axis=1),
                                          mean_image_features=image_features,
                                          mean_object_features=object_features,
                                          seq_inputs=input_seq,
                                          image_attributes=image_attributes,
                                          object_attributes=object_attributes)

        tf.losses.sparse_softmax_cross_entropy(target_seq,
                                               logits,
                                               weights=indicator)
        loss = tf.losses.get_total_loss()

        global_step = tf.train.get_or_create_global_step()
        learning_rate = tf.train.exponential_decay(5e-4,
                                                   global_step,
                                                   3 * 586363 //
                                                   FLAGS.batch_size,
                                                   0.8,
                                                   staircase=True)
        optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
        learning_step = optimizer.minimize(
            loss,
            var_list=attribute_captioner.variables,
            global_step=global_step)

        detector_saver = tf.train.Saver(var_list=attribute_detector.variables +
                                        [global_step])
        detector_ckpt, detector_ckpt_name = get_attribute_detector_checkpoint()

        captioner_saver = tf.train.Saver(
            var_list=attribute_captioner.variables + [global_step])
        captioner_ckpt, captioner_ckpt_name = get_grounded_attribute_checkpoint(
        )

        with tf.Session() as sess:

            sess.run(tf.variables_initializer(optimizer.variables()))

            if detector_ckpt is not None:
                detector_saver.restore(sess, detector_ckpt)
            else:
                sess.run(
                    tf.variables_initializer(attribute_detector.variables +
                                             [global_step]))

            if captioner_ckpt is not None:
                captioner_saver.restore(sess, captioner_ckpt)
            else:
                sess.run(
                    tf.variables_initializer(attribute_captioner.variables +
                                             [global_step]))

            captioner_saver.save(sess,
                                 captioner_ckpt_name,
                                 global_step=global_step)
            last_save = time.time()

            for i in itertools.count():

                time_start = time.time()
                try:
                    _target, _ids, _loss, _learning_step = sess.run(
                        [target_seq, ids, loss, learning_step])
                except:
                    break

                iteration = sess.run(global_step)

                print(
                    PRINT_STRING.format(
                        iteration, _loss,
                        list_of_ids_to_string(_ids[0, :].tolist(), vocab),
                        list_of_ids_to_string(_target[0, :].tolist(), vocab),
                        FLAGS.batch_size / (time.time() - time_start)))

                new_save = time.time()
                if new_save - last_save > 3600:  # save the model every hour
                    captioner_saver.save(sess,
                                         captioner_ckpt_name,
                                         global_step=global_step)
                    last_save = new_save

            captioner_saver.save(sess,
                                 captioner_ckpt_name,
                                 global_step=global_step)
            print("Finishing training.")
Example #5
0
def main(unused_argv):

    vocab, pretrained_matrix = load_glove(vocab_size=100000,
                                          embedding_size=300)
    with tf.Graph().as_default():

        image_id, spatial_features, input_seq, target_seq, indicator = import_mscoco(
            mode="train",
            batch_size=FLAGS.batch_size,
            num_epochs=FLAGS.num_epochs,
            is_mini=FLAGS.is_mini)
        image_captioner = ImageCaptioner(SpatialAttentionCell(300), vocab,
                                         pretrained_matrix)
        logits, ids = image_captioner(lengths=tf.reduce_sum(indicator, axis=1),
                                      spatial_image_features=spatial_features,
                                      seq_inputs=input_seq)
        tf.losses.sparse_softmax_cross_entropy(target_seq,
                                               logits,
                                               weights=indicator)
        loss = tf.losses.get_total_loss()

        global_step = tf.train.get_or_create_global_step()
        optimizer = tf.train.AdamOptimizer()
        learning_step = optimizer.minimize(loss,
                                           var_list=image_captioner.variables,
                                           global_step=global_step)

        captioner_saver = tf.train.Saver(var_list=image_captioner.variables +
                                         [global_step])
        captioner_ckpt, captioner_ckpt_name = get_spatial_attention_checkpoint(
        )
        with tf.Session() as sess:

            sess.run(tf.variables_initializer(optimizer.variables()))
            if captioner_ckpt is not None:
                captioner_saver.restore(sess, captioner_ckpt)
            else:
                sess.run(
                    tf.variables_initializer(image_captioner.variables +
                                             [global_step]))
            captioner_saver.save(sess,
                                 captioner_ckpt_name,
                                 global_step=global_step)
            last_save = time.time()

            for i in itertools.count():

                time_start = time.time()
                try:
                    _target, _ids, _loss, _learning_step = sess.run(
                        [target_seq, ids, loss, learning_step])
                except:
                    break

                iteration = sess.run(global_step)

                print(
                    PRINT_STRING.format(
                        iteration, _loss,
                        list_of_ids_to_string(_ids[0, :].tolist(), vocab),
                        list_of_ids_to_string(_target[0, :].tolist(), vocab),
                        FLAGS.batch_size / (time.time() - time_start)))

                new_save = time.time()

                if new_save - last_save > 3600:
                    captioner_saver.save(sess,
                                         captioner_ckpt_name,
                                         global_step=global_step)
                    last_save = new_save

            captioner_saver.save(sess,
                                 captioner_ckpt_name,
                                 global_step=global_step)
            print("Finishing training.")