def thread_lmdb_worker(output_lmdb): lmdb = lmdb_data.lmdb_data(output_lmdb, int(1e11)) lmdb.open_for_write() while True: data_fc = queue_fc.get() is_end = data_fc['is_end'] if is_end == 1: break batch_num = data_fc['batch_num'] ids = data_fc['ids'] fc_feats = data_fc['fc_feats'] np_fc_feats = fc_feats.cpu().numpy() for j in range(batch_num): np_fc_feat = np_fc_feats[j] output = io.BytesIO() # np.savez(output, x=I) # 921788 np.savez_compressed(output, x=np_fc_feat) # 726509 content = output.getvalue() lmdb.insert(str(ids[j]), content) lmdb.commit() lmdb.close() print('lmdb end')
def __init__(self, opt): self.opt = opt self.folder_path = opt.get('folder_path', '') self.batch_size = opt.get('batch_size', 1) self.start = opt.get('start', 0) self.num = opt.get('num', 7500) self.seq_per_img = 1 self.use_bu_att = opt.get('use_bu_att', False) self.input_bu = opt.get('input_bu', False) self.bu_size = opt.get('bu_size', False) self.bu_feat_size = opt.get('bu_feat_size', False) if self.use_bu_att: self.lmdb_bu = lmdb_data.lmdb_data(self.input_bu) self.lmdb_bu.open_for_read() self.files = [] self.ids = [] def isImage(f): supportExt = [ '.jpg', '.JPG', '.JPEG', '.png', '.PNG', '.ppm', '.PPM' ] for ext in supportExt: start_idx = f.rfind(ext) if start_idx >= 0 and start_idx + len(ext) == len(f): return True return False print('start: ', self.start, 'num: ', self.num) n = 0 for root, dirs, files in os.walk(self.folder_path, topdown=False): for file in files: fullpath = os.path.join(self.folder_path, file) if isImage(fullpath): if self.num == -1 or (n >= self.start and n < self.start + self.num): self.files.append(fullpath) self.ids.append(file.split('.')[0]) n = n + 1 self.N = len(self.files) print('DataLoaderRaw found', self.N, ' images') self.iterator = 0
def __init__(self, opt, is_only_test = False): self.opt = opt self.batch_size = opt.batch_size self.seq_per_img = opt.seq_per_img self.images_root = opt.images_root self.image_size = opt.image_size self.img_padding_max = opt.img_padding_max self.input_bu = opt.input_bu self.bu_size = opt.bu_size self.bu_feat_size = opt.bu_feat_size self.use_image = opt.use_image if opt.use_pre_feat: self.lmdb = lmdb_data.lmdb_data(opt.path_lmdb) self.lmdb.open_for_read() print('DataLoader loading json file: ', opt.input_json) self.info = json.load(open(self.opt.input_json)) self.ix_to_word = self.info['ix_to_word'] self.vocab_size = len(self.ix_to_word) print('vocab size is', self.vocab_size) print('DataLoader laoding h5 file: ',opt.input_h5) self.input_h5 = h5py.File(self.opt.input_h5, 'r') print('DataLoader laoding bu file: ',opt.input_bu) self.lmdb_bu = lmdb_data.lmdb_data(opt.input_bu) self.lmdb_bu.open_for_read() # if models.is_inception(opt.cnn_model): # img_mean = [0.5, 0.5, 0.5] # img_std = [0.5, 0.5, 0.5] # else: # img_mean = [0.485, 0.456, 0.406] # img_std = [0.229, 0.224, 0.225] # # vgg_mean = torch.FloatTensor(img_mean).cuda().view(1, 3, 1, 1) # self.img_mean = vgg_mean.expand(self.batch_size, 3, self.image_size, self.image_size) # # vgg_std = torch.FloatTensor(img_std).cuda().view(1, 3, 1, 1) # self.img_std = vgg_std.expand(self.batch_size, 3, self.image_size, self.image_size) if models.is_inception(opt.cnn_model): self.preprocess = trn.Compose([trn.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]) else: self.preprocess = trn.Compose([trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) # image_size = self.input_h5['images'].shape # self.num_images = image_size[0] # print('image size', image_size) # print('read %d image'%(self.num_images)) # load in the sequence data seq_size = self.input_h5['labels'].shape self.seq_length = seq_size[1] print('max sequence length in data is', self.seq_length) self.label_start_ix = self.input_h5['label_start_ix'][:] self.label_end_ix = self.input_h5['label_end_ix'][:] # separate out indexes for each of the provided splits self.split_ix = {'train':[],'val':[],'test':[]} for ix in range(len(self.info['images'])): img = self.info['images'][ix] if img['split'] == 'train': self.split_ix['train'].append(ix) elif img['split'] == 'val': self.split_ix['val'].append(ix) elif img['split'] == 'test': self.split_ix['test'].append(ix) elif opt.train_only == 0: self.split_ix['train'].append(ix) random.shuffle(self.split_ix['train']) random.shuffle(self.split_ix['val']) random.shuffle(self.split_ix['test']) print('assigned %d images to split train'%len(self.split_ix['train'])) print('assigned %d images to split val'%len(self.split_ix['val'])) print('assigned %d images to split test'%len(self.split_ix['test'])) self.iterators = {'train':0, 'val':0 ,'test':0} self.queues = {} self.flags = {} self.threads = {} if not is_only_test: for split in ['train']: self.queues[split] = Queue.Queue(maxsize=1) self.flags[split] = True thread = threading.Thread(target=get_batch_worker_thread, args=(split, self, opt.batch_size)) thread.setDaemon(True) thread.start() self.threads[split] = thread
import io import numpy as np import db.lmdb_data as lmdb_data import cv2 from scipy.misc import imread, imresize import Dic2Obj path_lmdb = '/media/amds/disk/code_cap/bottom-up-attention/output/aic_val_resnet101_fasterRcnn_lmdb' lmdb_bu = lmdb_data.lmdb_data(path_lmdb) lmdb_bu.open_for_read() image_path = '/media/amds/disk/dataset/aic/ai_challenger_caption_validation_20170910/caption_validation_images_20170910/0003a0755539c426ecfc7ed79bc74aeea6be740b.jpg' image_id = '0003a0755539c426ecfc7ed79bc74aeea6be740b.jpg' value = lmdb_bu.get(image_id) data = np.load(io.BytesIO(value)) data_x = data['x'].tolist() boxes = data_x['boxes'] np_boxes = np.array(boxes) img = imread(image_path) # print(img.shape[0]) img_h = float(img.shape[0]) img_w = float(img.shape[1]) print(img_h, img_w) box_size = np.array([img_w, img_h, img_w, img_h]) np_nboxes = np_boxes / box_size