def __read_file(self, data_dir, dataset): img_list = list() mlabel_list = list() img_dict = dict() all_img_list = [] with open(self.configer.get('data.{}_label_path'.format(dataset)), 'r') as file_stream: all_img_list += file_stream.readlines() if dataset == 'train' and self.configer.get('data.include_val', default=False): with open(self.configer.get('data.val_label_path'), 'r') as file_stream: all_img_list += file_stream.readlines() for line_cnt in range(len(all_img_list)): line_items = all_img_list[line_cnt].strip().split() if len(line_items) == 0: continue path = line_items[0] if not os.path.exists(os.path.join( data_dir, path)) or not ImageHelper.is_img(path): Log.warn('Invalid Image Path: {}'.format( os.path.join(data_dir, path))) continue img_list.append(os.path.join(data_dir, path)) mlabel_list.append([int(item) for item in line_items[1:]]) assert len(img_list) > 0 Log.info('Length of {} imgs is {}...'.format(dataset, len(img_list))) return img_list, mlabel_list
def __init__(self, test_dir=None, aug_transform=None, img_transform=None, configer=None): super(TestDefaultDataset, self).__init__() self.configer = configer self.aug_transform=aug_transform self.img_transform = img_transform self.item_list = [(os.path.abspath(os.path.join(test_dir, filename)), filename) for filename in FileHelper.list_dir(test_dir) if ImageHelper.is_img(filename)]
def __read_list(self, data_dir, list_path): item_list = [] with open(list_path, 'r') as fr: for line in fr.readlines(): filename = line.strip().split()[0] label = None if len(line.strip().split()) == 1 else line.strip().split()[1] img_path = os.path.join(data_dir, filename) if not os.path.exists(img_path) or not ImageHelper.is_img(img_path): Log.error('Image Path: {} is Invalid.'.format(img_path)) exit(1) item_list.append((img_path, filename, label)) Log.info('There are {} images..'.format(len(item_list))) return item_list
def __read_file(self, root_dir, dataset, label_path): img_list = list() mlabel_list = list() with open(label_path, 'r') as file_stream: for line in file_stream.readlines(): line_items = line.rstrip().split() path = line_items[0] if not os.path.exists(os.path.join(root_dir, path)) or not ImageHelper.is_img(path): Log.warn('Invalid Image Path: {}'.format(os.path.join(root_dir, path))) continue img_list.append(os.path.join(root_dir, path)) mlabel_list.append([int(item) for item in line_items[1:]]) assert len(img_list) > 0 Log.info('Length of {} imgs is {}...'.format(dataset, len(img_list))) return img_list, mlabel_list
def __read_and_split_file(self, root_dir, dataset, label_path): img_list = list() mlabel_list = list() select_interval = int(1 / self.configer.get('data', 'val_ratio')) img_dict = dict() with open(label_path, 'r') as file_stream: for line in file_stream.readlines(): label = line.strip().split()[1] if int(label) in img_dict: img_dict[int(label)].append(line) else: img_dict[int(label)] = [line] all_img_list = [] for i in sorted(img_dict.keys()): all_img_list += img_dict[i] for line_cnt in range(len(all_img_list)): if line_cnt % select_interval == 0 and dataset == 'train' and not self.configer.get('data', 'include_val'): continue if line_cnt % select_interval != 0 and dataset == 'val': continue line_items = all_img_list[line_cnt].strip().split() path = line_items[0] if not os.path.exists(os.path.join(root_dir, path)) or not ImageHelper.is_img(path): Log.warn('Invalid Image Path: {}'.format(os.path.join(root_dir, path))) continue img_list.append(os.path.join(root_dir, path)) mlabel_list.append([int(item) for item in line_items[1:]]) assert len(img_list) > 0 Log.info('Length of {} imgs is {} after split trainval...'.format(dataset, len(img_list))) return img_list, mlabel_list