Commit 1937676a authored by Rudolf Chrispens's avatar Rudolf Chrispens
Browse files

validation fix

parent bd63579f
Loading
Loading
Loading
Loading
+41 −1
Original line number Diff line number Diff line
@@ -170,7 +170,8 @@ class coco_model(object):
                                            scale=True,
                                            is_training=(mode=='train'),
                                            updates_collections=None,
                                            scope=(name+'batch_norm'))
                                            scope=(name+'batch_norm'),
                                            reuse=tf.AUTO_REUSE) # changed this NOTE Rudi!

    def build_model(self):
        features = self.features
@@ -261,3 +262,42 @@ class coco_model(object):
        betas = tf.transpose(tf.squeeze(beta_list), (1, 0))    # (N, T)
        sampled_captions = tf.transpose(tf.stack(sampled_word_list), (1, 0))     # (N, max_len)
        return alphas, betas, sampled_captions

    def build_sampler_test(self, features_to_test, max_len=20):
        features = self.features

        # batch normalize feature vectors
        features = self._batch_norm(features, mode='test', name='conv_features')

        c, h = self._get_initial_lstm(features=features)
        features_proj = self._project_features(features=features)

        sampled_word_list = []
        alpha_list = []
        beta_list = []
        lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=self.H)

        for t in range(max_len):
            if t == 0:
                x = self._word_embedding(inputs=tf.fill([tf.shape(features)[0]], self._start))
            else:
                x = self._word_embedding(inputs=sampled_word, reuse=True)

            context, alpha = self._attention_layer(features, features_proj, h, reuse=(t!=0))
            alpha_list.append(alpha)

            if self.selector:
                context, beta = self._selector(context, h, reuse=(t!=0))
                beta_list.append(beta)

            with tf.variable_scope('lstm', reuse=(t!=0)):
                _, (c, h) = lstm_cell(inputs=tf.concat( [x, context],1), state=[c, h])

            logits = self._decode_lstm(x, h, context, reuse=(t!=0))
            sampled_word = tf.argmax(logits, 1)
            sampled_word_list.append(sampled_word)

        alphas = tf.transpose(tf.stack(alpha_list), (1, 0, 2))     # (N, T, L)
        betas = tf.transpose(tf.squeeze(beta_list), (1, 0))    # (N, T)
        sampled_captions = tf.transpose(tf.stack(sampled_word_list), (1, 0))     # (N, max_len)
        return alphas, betas, sampled_captions
+0 −2
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ from .helper import get_input_data
from .helper import get_vocabulary
from .helper import get_sequences
from .helper import check_folders
from .basic_masa_reader import basic_masa_reader
from .coco_reader import coco_reader

__all__ = [
@@ -10,6 +9,5 @@ __all__ = [
    'get_vocabulary',
    'get_sequences',
    'check_folders',
    'basic_masa_reader',
    'coco_reader'
]
+0 −84
Original line number Diff line number Diff line
#!/usr/bin/env python3

from lstm.configuration import current as config
import collections
import tensorflow as tf
from lstm.Readers import helper
import pickle

"""To run this code, you'll need to first download and extract the text dataset
    from here: http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz. Change the
    data_path variable below to your local exraction path"""

# data_path = "C:\\Users\Andy\Documents\simple-examples\data"

# parser = argparse.ArgumentParser()
# parser.add_argument('run_opt', type=int, default=1, help='An integer: 1 to train, 2 to test')
# parser.add_argument('--data_path', type=str, default=data_path, help='The full path of the training data')
# args = parser.parse_args()

"""
with open('interface_train_30pics_per_category.picle', 'rb') as handle:
    data = picle.load(handle)
"""

class basic_masa_reader:
    def read_input(self, filename: str):
        with tf.gfile.GFile(helper.inputPath + filename, "r") as f:
            if(config.reader.verbose is True):
                print("")
            return f.read().replace("\n", "<eos>").split()


    def create_vocabulary(self, filename: str):
        data = self.read_input(filename + config.reader.input_data_type)

        counter = collections.Counter(data)
        count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))

        words, _ = list(zip(*count_pairs))
        word_to_id = dict(zip(words, range(len(words))))

        return word_to_id


    def file_to_word_ids(self, filename: str, word_to_id):
        data = self.read_input(filename + config.reader.input_data_type)
        return [word_to_id[word] for word in data if word in word_to_id]


    def load_data(self):
        # build the complete vocabulary, then convert text data to list of integers
        word_to_id = self.create_vocabulary(config.path.input_train)
        train_data = self.file_to_word_ids(config.path.input_train, word_to_id)
        valid_data = self.file_to_word_ids(config.path.input_validate, word_to_id)
        test_data = self.file_to_word_ids(config.path.input_test, word_to_id)
        config.trainer.vocab_size = len(word_to_id)
        reversed_dictionary = dict(zip(word_to_id.values(), word_to_id.keys()))

        if(config.reader.verbose):
            print("\n###### READER ######")
            print("Reader: <train_data>\n", train_data[:20])
            print("Reader: <word_to_id\n", {k: word_to_id[k] for k in list(word_to_id)[:20]})
            print("Reader: <vocab_size>\n", config.trainer.vocab_size)
            print("Reader: <reversed_dictionary>\n", {k: reversed_dictionary[k] for k in list(reversed_dictionary)[:20]})
        return train_data, valid_data, test_data, config.trainer.vocab_size, reversed_dictionary

    def batch_producer(self, raw_data, batch_size, num_steps):
        raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)

        data_len = tf.size(raw_data)
        batch_len = data_len // batch_size
        data = tf.reshape(raw_data[0: batch_size * batch_len],
                        [batch_size, batch_len])

        epoch_size = (batch_len - 1) // num_steps

        i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
        x = data[:, i * num_steps:(i + 1) * num_steps]
        x.set_shape([batch_size, num_steps])

        y = data[:, i * num_steps + 1: (i + 1) * num_steps + 1]
        y.set_shape([batch_size, num_steps])

        return x, y
+8 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ class coco_reader:
        features = []
        captions = []
        image_idxs = []
        file_names = []

        with open(data_path + filename + '.pickle', 'rb') as handle:
            data = pickle.load(handle)
@@ -26,6 +27,7 @@ class coco_reader:
                #5 times
                #Image Append
                features.append(data_from_image['vector'])
                file_names.append(data_from_image['file_name'])
                #Caption Append
                startword = ['<START>']
                words = re.findall(r"\w+|[^\w\s]", image_captions_dict['caption'])
@@ -51,6 +53,7 @@ class coco_reader:
        formatted_data['captions'] = np.array(captions)
        formatted_data['image_idxs'] = np.array(image_idxs)
        formatted_data['features'] = np.array(features)
        formatted_data['file_names'] = np.array(file_names)

        if(_config.reader.verbose):
            print("#> Data shape:")
@@ -132,14 +135,17 @@ class coco_reader:

        return np.array(decoded)

    def decode_captions_2(self, encoded_captions, id_to_word):
    def decode_captions_2(self, encoded_captions, id_to_word, abbort_at_end=False):
        decoded = []
        vocab = id_to_word

        for i, single_caption in enumerate(encoded_captions):
            decoded.append([])
            for word in single_caption:
                decoded[i].append(vocab[word])
                if int(word) > 0:
                    decoded[i].append(vocab[int(word)])
                    if decoded[i][-1] == '<END>' and abbort_at_end is True:
                        break

        decoded = np.array(decoded)
        return decoded
@@ -195,7 +201,6 @@ class coco_reader:
    def create_vocab_from_data(self, data_train, data_valid, data_test, save_filename_prefix):
        counter: int = 0
        vocab_id_word = {}
        print("TODO add testset!")
        tr = data_train['captions']
        val = data_valid['captions']
        data_combined = np.concatenate((tr, val), axis=0)
+0 −3
Original line number Diff line number Diff line
# reader init
from .masa_trainer import masa_trainer
from .coco_trainer import coco_trainer

__all__ = [
    'masa_trainer',
    'coco_trainer'
]
Loading