def get_feats(): train_caption_file = 'D:/download/art_desc/train/ann.csv' eval_caption_file = 'D:/download/art_desc/val/ann.csv' train_features_dir = 'D:/download/art_desc/train/images_vgg/' eval_features_dir = 'D:/download/art_desc/val/images_vgg/' image_loader = ImageLoader('./utils/ilsvrc_2012_mean.npy') net = VGG19(weights='imagenet') model = Model(input=net.input, output=net.get_layer('fc2').output) with open(eval_caption_file, 'r') as f: reader = csv.reader(f) for id, file_name, caption in reader: try: img = image_loader.load_image(file_name) fc2 = model.predict(img) reshaped = np.reshape(fc2, (4096)) np.save(eval_features_dir + 'art_desc' + id, reshaped) except Exception: print("cannot identify image file:" + file_name) pass
def get_feats(dir=False): train_caption_file = 'D:/download/art_desc/train/ann.csv' train_features_dir = 'D:/download/art_desc/train/images_vgg_redo/' eval_caption_file = 'D:/download/art_desc/val/ann.csv' #eval_image_dir = 'D:/download/art_desc/val/test_images/' eval_image_dir = 'D:/download/art_desc/val/images_redo/' eval_features_dir = 'D:/download/art_desc/val/images_vgg_redo/' #eval_features_dir = 'D:/download/art_desc/val/test_vgg/' image_loader = ImageLoader('./utils/ilsvrc_2012_mean.npy') ignore_file = 'D:/download/art_desc/val/ignore.csv' net = VGG19(weights='imagenet') model = Model(input=net.input, output=net.get_layer('fc2').output) bad_ids = [] prev_id = 0 prev_bad = False if dir: with open(eval_caption_file, 'r') as f: #caption file reader = csv.reader(f) for id, file_name, caption in reader: try: img = image_loader.load_image(file_name) ''' fc2 = model.predict(img) reshaped = np.reshape(fc2, (4096)) np.save(eval_features_dir + 'art_desc'+ str(id), reshaped) ''' prev_bad = False except Exception: if id != prev_id or prev_bad is False: print("cannot identify image file:" + file_name) bad_ids.append(id) prev_bad = True prev_id = id else: with open(train_caption_file, 'r') as f: #caption file reader = csv.reader(f) for id, file_name, caption in reader: try: img = image_loader.load_image(file_name) ''' fc2 = model.predict(img) reshaped = np.reshape(fc2, (4096)) np.save(train_features_dir + 'art_desc'+ id, reshaped) #feature dir ''' prev_bad = False except Exception: if id != prev_id or prev_bad is False: print("cannot identify image file:" + file_name) bad_ids.append(id) prev_bad = True prev_id = id print("Total bad image files:%d" % len(bad_ids)) data = pd.DataFrame({'index': bad_ids}) data.to_csv(ignore_file)