Commit 15c1882c authored by Rudolf Chrispens's avatar Rudolf Chrispens
Browse files

fixed vocab creation

parent f2825fe6
Loading
Loading
Loading
Loading
+58 −12
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ import json as jsonlib
import re

class coco_reader:
    def load_data(self, data_path=helper.inputPath, filename=_config.path.input_train):
    def load_data_encoded(self, data_path=helper.inputPath, filename=_config.path.input_train):
        start_t = time.time()
        data = {}
        formatted_data = {}
@@ -62,6 +62,56 @@ class coco_reader:
        print("#>\tLoad data elapsed time: %.2f" % (end_t - start_t))
        return formatted_data

    def load_data_decoded(self, data_path=helper.inputPath, filename=_config.path.input_train):
        start_t = time.time()
        data = {}
        formatted_data = {}

        features = []
        captions = []
        image_idxs = []

        with open(data_path + filename + '.pickle', 'rb') as handle:
            data = pickle.load(handle)

        for i, data_from_image in enumerate(data):
            for image_captions_dict in data_from_image['captions']:
                #5 times
                #Image Append
                features.append(data_from_image['vector'])
                #Caption Append
                startword = ['<START>']
                words = re.findall(r"\w+|[^\w\s]", image_captions_dict['caption'])
                words.append('<END>')
                words = self.add_padding(startword + words)
                captions.append(np.array(words))
                #Image ID Append
                image_idxs.append(np.array(data_from_image['id']))

            if(_config.reader.verbose and i < 3):
                print('\n#> Reader printing ' + filename + ' data:')
                print('#>\tcaptions:', data_from_image['captions'][0]['caption'])
                print('\tcaptions:', data_from_image['captions'][1]['caption'])
                print('\tcaptions:', data_from_image['captions'][2]['caption'])
                print('\tcaptions:', data_from_image['captions'][3]['caption'])
                print('\tcaptions:', data_from_image['captions'][4]['caption'])
                print('#>\timage_id:', data_from_image['id'])
                print('#>\tvector:', data_from_image['vector'])

        formatted_data['captions'] = np.array(captions)
        formatted_data['image_idxs'] = np.array(image_idxs)
        formatted_data['features'] = np.array(features)

        if(_config.reader.verbose):
            print("#> Data shape:")
            print("#>\tfeature set count complete:", formatted_data['features'].shape)
            print("#>\tcaption set count complete:", formatted_data['captions'].shape)
            print("#>\timageId set count complete:", formatted_data['image_idxs'].shape)

        end_t = time.time()
        print("#>\tLoad data elapsed time: %.2f" % (end_t - start_t))
        return formatted_data

    def add_padding(self, sequence):
        new_seq = sequence
        seq_length = len(sequence)
@@ -136,16 +186,17 @@ class coco_reader:
            print("Elapse time: %.2f" % (end_t - start_t))
        return data

    """
    def create_vocab_from_filesystem(self, data_path: str=helper.inputPath, filename: str=_config.path.input_train):
        data = self.load_data_masa(data_path, filename)
        return self._create_vocab_from_data(data, filename)
        return self.create_vocab_from_data(data, filename)
    """

    def _create_vocab_from_data(self, data, save_filename_prefix):
    def create_vocab_from_data(self, data, save_filename_prefix):
        counter: int = 0
        vocab_id_word = {}
        for x in data:
            for i in x['captions']:
                for c in re.findall(r"\w+|[^\w\s]", i['caption']):
        for i in data['captions']:
            for c in i:
                if c not in vocab_id_word.values():
                    vocab_id_word[counter] = c
                    counter = counter + 1
@@ -154,12 +205,7 @@ class coco_reader:
                        vocab_id_word[counter] = '<UNKN>'
                        counter = counter + 1
                        break
        vocab_id_word[counter] = '<END>'
        counter = counter + 1
        vocab_id_word[counter] = '<START>'
        counter = counter + 1
        vocab_id_word[counter] = '<NULL>'
        counter = counter + 1

        print('#>\tfinal vocab size: ', len(vocab_id_word))

        vocab_word_id = {v: k for k, v in vocab_id_word.items()}
+4 −3
Original line number Diff line number Diff line
@@ -29,15 +29,16 @@ def main(self, parameter_list):

    # load train dataset
    current_reader = _reader.coco_reader()
    train_data = current_reader.load_data()
    word_to_idx = current_reader.create_vocab_from_filesystem(filename=_config.path.input_train)
    train_data_decoded = current_reader.load_data_decoded()
    word_to_idx = current_reader.create_vocab_from_data(data=train_data_decoded, save_filename_prefix=_config.path.input_train)
    train_data = current_reader.load_data_encoded()

    #test1 = current_reader.get_stored_vocab_id_word(filename_prefix=_config.path.read_train)
    #test2 = current_reader.get_stored_vocab_word_id(filename_prefix=_config.path.read_train)


    # load val dataset to print out bleu scores every epoch
    val_data = current_reader.load_data(filename=_config.path.input_validate)
    val_data = current_reader.load_data_encoded(filename=_config.path.input_validate)

    model = _model.coco_model(  word_to_idx,
                                current_reader,