def CreateFeature(model, phase, outputPath='.'): """ Create h5py dataset for feature extraction. ARGS: outputPath : h5py output path model : used model labelList : list of corresponding groundtruth texts """ featurenet = feature_net(model) if use_gpu: featurenet.cuda() feature_map = torch.FloatTensor() label_map = torch.LongTensor() for data in tqdm(dataloader[phase]): img, label = data if use_gpu: img = Variable(img, volatile=True).cuda() else: img = Variable(img, volatile=True) out = featurenet(img) feature_map = torch.cat((feature_map, out.cpu().data), 0) label_map = torch.cat((label_map, label), 0) feature_map = feature_map.numpy() label_map = label_map.numpy() file_name = '_feature_{}.hd5f'.format(model) h5_path = os.path.join(outputPath, phase) + file_name with h5py.File(h5_path, 'w') as h: h.create_dataset('data', data=feature_map) h.create_dataset('label', data=label_map)