def pre_val_to_train(self, train_vect_in_path, val_in_path): """ :param train_vect_in_path: :param val_in_path: :return: """ train_vect = utils.load_pickle(train_vect_in_path) val_vect = utils.load_pickle(val_in_path) n = 1280 train_vect = train_vect + val_vect[n:] val_vect = val_vect[:n] utils.save_pickle(train_vect, train_vect_in_path) utils.save_pickle(val_vect, val_in_path)
def fetch_val_batch(self, caption_data_path, image_data_path, batch_size): """ :param caption_data_path: :param image_data_path: :param batch_size: :return: """ captions = utils.load_pickle(caption_data_path) val_data_count = len(captions) if TrainingArg.check_test: val_data_count = batch_size print('WARNING: CHECK TEST MODEL NOW') start = 0 end = batch_size for item in range(0, val_data_count, batch_size): temp = captions[start:end] caption_batch = [] image_batch = [] for x in temp: caption_batch.append(x['caption']) image_batch.append(x['image_id']) try: image_batch = self.pre_data_with_vgg19(image_data_path, image_batch) except Exception as e: print('load image failed: {}-{}, cause {}'.format( start, end, e)) start += batch_size end += batch_size continue start += batch_size end += batch_size yield caption_batch, image_batch
def __init__(self): self.word_to_idx = utils.load_pickle(Const.vocab_path) self.model = CaptionGenerator(self.word_to_idx, dim_feature=[196, 512], dim_embed=512, dim_hidden=1024, n_time_step=33, prev2out=True, ctx2out=True, alpha_c=1.0, selector=True, dropout=True) self.n_epochs = TrainingArg.n_epochs self.batch_size = TrainingArg.batch_size self.update_rule = TrainingArg.update_rule self.learning_rate = TrainingArg.learning_rate self.print_bleu = TrainingArg.print_bleu self.print_every = TrainingArg.print_every self.save_every = TrainingArg.save_every self.log_path = TrainingArg.log_path self.model_path = TrainingArg.model_path self.pretrained_model = TrainingArg.pretrained_model self.test_model = TrainingArg.test_model self.max_words_len = 35 self.pre_mgr = PreData(vgg19_path=TrainingArg.vgg19_path) # 数据管理
def __init__(self): self.word_to_idx = utils.load_pickle(Const.vocab_path) self.model = CaptionGenerator(self.word_to_idx, dim_feature=[196, 512], dim_embed=512, dim_hidden=1024, n_time_step=33, prev2out=True, ctx2out=True, alpha_c=1.0, selector=True, dropout=True) self.n_epochs = TrainingArg.n_epochs self.batch_size = TrainingArg.batch_size self.update_rule = TrainingArg.update_rule self.learning_rate = TrainingArg.learning_rate self.print_bleu = TrainingArg.print_bleu self.print_every = TrainingArg.print_every self.save_every = TrainingArg.save_every self.log_path = TrainingArg.log_path # FLAGS.log_dir self.model_path = TrainingArg.model_path # FLAGS.output_dir # TrainingArg.model_path self.data_dir = Const.resize_train_out_path # FLAGS.data_dir self.pretrained_model = TrainingArg.pretrained_model self.test_model = TrainingArg.test_model self.max_words_len = 35 self.pre_mgr = PreData(vgg19_path=TrainingArg.vgg19_path) # 数据管理 # set an optimizer by update rule if self.update_rule == 'adam': self.optimizer = tf.train.AdamOptimizer elif self.update_rule == 'momentum': self.optimizer = tf.train.MomentumOptimizer elif self.update_rule == 'rmsprop': self.optimizer = tf.train.RMSPropOptimizer if not os.path.exists(self.model_path): os.makedirs(self.model_path) if not os.path.exists(self.log_path): os.makedirs(self.log_path) self.org_decoded = {} self.val_data_flag = False
def fetch_batch(self, caption_data_path, image_data_path, batch_size, epochs): """ :param caption_data_path: :param image_data_path: :param batch_size: :param epochs: :return: """ captions = utils.load_pickle(caption_data_path) self.train_data_count = len(captions) for epoch in range(4, epochs): start = 0 end = batch_size random.shuffle(captions) for item in range(0, self.train_data_count, batch_size): temp = captions[start:end] random.shuffle(temp) if epoch < 5: cap_index = epoch % 5 else: cap_index = random.randint(0, 4) caption_batch = [] image_batch = [] for x in temp: caption_batch.append(x['caption'][cap_index]) image_batch.append(x['image_id']) try: image_batch = self.pre_data_with_vgg19( image_data_path, image_batch) except Exception as e: print('load image failed: {}-{}, cause {}'.format( start, end, e)) start += batch_size end += batch_size continue start += batch_size end += batch_size yield caption_batch, image_batch, epoch
def load_vocab(self, vocab_path): """ :return: """ return utils.load_pickle(vocab_path)