def main(_): # Load model configuration cu = CommonUtiler() config_path = os.path.join('./model_conf', FLAGS.model_name + '.py') config = cu.load_config(config_path) # Evaluate trained models on val decoder = mRNNDecoder(config, FLAGS.model_name, FLAGS.vocab_path, gpu_memory_fraction=FLAGS.gpu_memory_fraction) start, stop, step = [int(x) for x in FLAGS.eval_stat.split()] for i in range(start, stop, step): model_path = os.path.join(FLAGS.model_root, FLAGS.model_name, 'variables', 'model_%d.ckpt' % i) while not os.path.exists(model_path + ".meta"): logger.warning('Cannot load model file, sleep 1 hour to retry') time.sleep(3600) decoder.load_model(model_path) num_decode = 0 pred_sentences = [] for anno_file_path in FLAGS.anno_files_path.split(':'): annos = np.load(anno_file_path).tolist() for anno in annos: feat_path = os.path.join( FLAGS.vf_dir, anno['file_path'], anno['file_name'].split('.')[0] + '.txt') visual_features = np.loadtxt(feat_path) sentences = decoder.decode(visual_features, FLAGS.beam_size) sentence_coco = { 'image_id': anno['id'], 'caption': ' '.join(sentences[0]['words']) } pred_sentences.append(sentence_coco) num_decode += 1 if num_decode % 100 == 0: logger.info('%d images are decoded' % num_decode) pred_path = os.path.join(FLAGS.model_root, FLAGS.model_name, 'decode_val_result', 'generated_%d.json' % i) result_path = os.path.join(FLAGS.model_root, FLAGS.model_name, 'decode_val_result', 'result_%d.txt' % i) cu.create_dir_if_not_exists(os.path.dirname(pred_path)) with open(pred_path, 'w') as fout: json.dump(pred_sentences, fout) cu.coco_val_eval(pred_path, result_path)
logger = logging.getLogger('ExpMscoco') logging.basicConfig( format="[%(asctime)s - %(filename)s:line %(lineno)4s] %(message)s", datefmt='%d %b %H:%M:%S') logger.setLevel(logging.INFO) if __name__ == '__main__': # Hyperparameters min_count = 3 vocab_path = './cache/dictionary/mscoco_mc%d_vocab' % min_count mscoco_root = './datasets/ms_coco' anno_file_names = ['anno_list_mscoco_trainModelVal_m_RNN.npy'] # Preparations cu = CommonUtiler() cu.create_dir_if_not_exists(os.path.dirname(vocab_path)) # Scan the anno files vocab = {} for anno_file_name in anno_file_names: anno_path = os.path.join(mscoco_root, 'mscoco_anno_files', anno_file_name) annos = np.load(anno_path).tolist() for anno in annos: for sentence in anno['sentences']: for word in sentence: word = word.strip().lower() if word in vocab: vocab[word] += 1 else: vocab[word] = 1
if __name__ == '__main__': flag_ignore_exists = True # Path model_path = './external/tf_cnn_models/inception_v3.pb' mscoco_root = './datasets/ms_coco' anno_file_names = [ 'anno_list_mscoco_trainModelVal_m_RNN.npy', 'anno_list_mscoco_crVal_m_RNN.npy', 'anno_list_mscoco_test2014.npy' ] feat_dir = './cache/mscoco_image_features/inception_v3' # Preparations cu = CommonUtiler() ife = ImageFeatureExtractor(model_path) cu.create_dir_if_not_exists(os.path.join(feat_dir, 'train2014')) cu.create_dir_if_not_exists(os.path.join(feat_dir, 'test2014')) cu.create_dir_if_not_exists(os.path.join(feat_dir, 'val2014')) # Extract features for anno_file_name in anno_file_names: anno_path = os.path.join(mscoco_root, 'mscoco_anno_files', anno_file_name) annos = np.load(anno_path).tolist() for (ind_a, anno) in enumerate(annos): image_path = os.path.join(mscoco_root, 'images', anno['file_path'], anno['file_name']) feat_path = os.path.join(feat_dir, anno['file_path'], anno['file_name'].split('.')[0] + '.txt') if flag_ignore_exists and os.path.exists(feat_path):