def hack_image_features(image_features): try: if len(image_features[0]) == IMAGE_FEATURE_LEN and len( image_features[1]) == IMAGE_FEATURE_LEN: return image_features else: return np.array( [gezi.nppad(x, TEXT_MAX_WORDS) for x in image_features]) except Exception: return np.array( [gezi.nppad(x, TEXT_MAX_WORDS) for x in image_features])
def init(): #for evaluation without train will also use evaluator so only set log path in train.py #logging.set_logging_path(FLAGS.model_dir) test_dir = FLAGS.valid_resource_dir global all_distinct_texts, all_distinct_text_strs global vocab, vocab_size if all_distinct_texts is None: print('loading valid resorce from:', test_dir) vocabulary.init() vocab = vocabulary.vocab vocab_size = vocabulary.vocab_size if os.path.exists(test_dir + '/distinct_texts.npy'): all_distinct_texts = np.load(test_dir + '/distinct_texts.npy') else: all_distinct_texts = [] #to avoid outof gpu mem all_distinct_texts = all_distinct_texts[:FLAGS.max_texts] print('all_distinct_texts len:', len(all_distinct_texts), file=sys.stderr) #--padd it as test data set might be smaller in shape[1] all_distinct_texts = np.array( [gezi.nppad(text, TEXT_MAX_WORDS) for text in all_distinct_texts]) if FLAGS.feed_dict: all_distinct_texts = texts2ids(evaluator.all_distinct_text_strs) if os.path.exists(test_dir + '/distinct_text_strs.npy'): all_distinct_text_strs = np.load(test_dir + '/distinct_text_strs.npy') else: all_distinct_text_strs = []
def run(): m = {} files = glob.glob(FLAGS.image_feature_pattern) for file in files: for line in open(file): l = line.strip().split('\t') m[l[0]] = l[-1] for i, line in enumerate(open(FLAGS.image_file)): image = line.strip() if image not in m: print('image not find in ', FLAGS.image_feature_pattern) exit(0) image_feature = m[image].split('\x01') image_feature = [float(x) for x in image_feature] timer = gezi.Timer() word_ids_list = np.load(FLAGS.all_texts) word_ids_list = np.array( [gezi.nppad(text, TEXT_MAX_WORDS) for text in word_ids_list]) all_text_strs = np.load(FLAGS.all_text_strs) scores = predicts([image_feature], word_ids_list) print(img_html.format(image)) topn = 50 indexes = (-scores).argsort()[:topn] for i, index in enumerate(indexes): print(i, all_text_strs[index], scores[index]) print('<br>') print(i, image, timer.elapsed(), file=sys.stderr)
def init(image_model_=None): global inited if inited: return test_dir = FLAGS.valid_resource_dir global all_distinct_texts, all_distinct_text_strs global vocab, vocab_size if all_distinct_texts is None: print('loading valid resource from:', test_dir) #vocabulary.init() text2ids.init() vocab = vocabulary.vocab vocab_size = vocabulary.vocab_size if os.path.exists(test_dir + '/distinct_texts.npy'): all_distinct_texts = np.load(test_dir + '/distinct_texts.npy') else: all_distinct_texts = [] #to avoid outof gpu mem #all_distinct_texts = all_distinct_texts[:FLAGS.max_texts] print('all_distinct_texts len:', len(all_distinct_texts), file=sys.stderr) #--padd it as test data set might be smaller in shape[1] all_distinct_texts = np.array([gezi.nppad(text, TEXT_MAX_WORDS) for text in all_distinct_texts]) if FLAGS.feed_dict: all_distinct_texts = texts2ids(evaluator.all_distinct_text_strs) if os.path.exists(test_dir + '/distinct_text_strs.npy'): all_distinct_text_strs = np.load(test_dir + '/distinct_text_strs.npy') else: all_distinct_text_strs = [] init_labels() #for evaluation without train will also use evaluator so only set log path in train.py #logging.set_logging_path(FLAGS.model_dir) if FLAGS.assistant_model_dir: global assistant_predictor #use another session different from main graph, otherwise variable will be destroy/re initailized in melt.flow #by default now Predictor using tf.Session already, here for safe, if use same session then not work #NOTICE must use seperate sess!! if is_raw_image(image_features) and not melt.varname_in_checkpoint(FLAGS.image_model_name, FLAGS.assistant_model_dir): print('assist predictor use deepiu.util.sim_predictor.SimPredictor as is raw image as input') global image_model if image_model_ is not None: image_model = image_model_ else: image_model = melt.image.ImageModel(FLAGS.image_checkpoint_file, FLAGS.image_model_name, feature_name=None) assistant_predictor = deepiu.util.sim_predictor.SimPredictor(FLAGS.assistant_model_dir, image_model=image_model) print('assistant predictor init ok') else: assistant_predictor = melt.SimPredictor(FLAGS.assistant_model_dir) print('assistant_predictor', assistant_predictor) inited = True
def hack_image_features(image_features): """ the hack is for textsim use ltext as image(similar), so hack for it """ #first for real image but not dump feature, use original encoded image since big, we assume #pre save binary pics and can refer to pic in disk by pic name and pic dir assert len(image_features) > 0 if isinstance(image_features[0], np.string_): #return np.array([melt.read_image(pic_path) for pic_path in image_features]) return image_features try: if len(image_features[0]) == IMAGE_FEATURE_LEN and len(image_features[1]) == IMAGE_FEATURE_LEN: return image_features else: return np.array([gezi.nppad(x, TEXT_MAX_WORDS) for x in image_features]) except Exception: return np.array([gezi.nppad(x, TEXT_MAX_WORDS) for x in image_features])