Spaces:
Sleeping
Sleeping
File size: 4,150 Bytes
7e2a2a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import os
from utils.util import check_path_is_img
from utils.data_utils import Transforms, check_create_shuffled_order, check_equal_length
from utils.augmentation import NumpyToTensor
def add_numpy_paired_data(data, transforms, config, paired_data_order):
A_paths = []
B_paths = []
if config['dataset']['paired_' + config['common']['phase'] + '_filelist'] != '':
paired_data_file = open(config['dataset']['paired_' + config['common']['phase'] + '_filelist'], 'r')
Lines = paired_data_file.readlines()
paired_data_order = check_create_shuffled_order(Lines, paired_data_order)
check_equal_length(Lines, paired_data_order, data)
for i in paired_data_order:
line = Lines[i]
if not config['dataset']['use_absolute_datafile']:
file1 = os.path.join(config['dataset']['dataroot'], line.split(" ")[0]).strip()
file2 = os.path.join(config['dataset']['dataroot'], line.split(" ")[1]).strip()
else:
file1 = line.split(" ")[0].strip()
file2 = line.split(" ")[1].strip()
if os.path.exists(file1) and os.path.exists(file2):
A_paths.append(file1)
B_paths.append(file2)
paired_data_file.close()
elif config['dataset']['paired_' + config['common']['phase'] + 'A_folder'] != '' and \
config['dataset']['paired_' + config['common']['phase'] + 'B_folder'] != '':
dir_A = config['dataset']['paired_' + config['common']['phase'] + 'A_folder']
dir_B = config['dataset']['paired_' + config['common']['phase'] + 'B_folder']
filenames = os.listdir(dir_A)
paired_data_order = check_create_shuffled_order(filenames, paired_data_order)
check_equal_length(filenames, paired_data_order, data)
for i in paired_data_order:
filename = filenames[i]
if not check_path_is_img(filename):
continue
A_path = os.path.join(dir_A, filename)
B_path = os.path.join(dir_B, filename)
if os.path.exists(A_path) and os.path.exists(B_path):
A_paths.append(A_path)
B_paths.append(B_path)
else:
dir_A = os.path.join(config['dataset']['dataroot'], config['common']['phase'] + 'numpypairedA')
dir_B = os.path.join(config['dataset']['dataroot'], config['common']['phase'] + 'numpypairedB')
if os.path.exists(dir_A) and os.path.exists(dir_B):
filenames = os.listdir(dir_A)
paired_data_order = check_create_shuffled_order(filenames, paired_data_order)
check_equal_length(filenames, paired_data_order, data)
for i in paired_data_order:
filename = filenames[i]
if not check_path_is_img(filename):
continue
A_path = os.path.join(dir_A, filename)
B_path = os.path.join(dir_B, filename)
if os.path.exists(A_path) and os.path.exists(B_path):
A_paths.append(A_path)
B_paths.append(B_path)
btoA = config['dataset']['direction'] == 'BtoA'
# get the number of channels of input image
input_nc = config['model']['output_nc'] if btoA else config['model']['input_nc']
output_nc = config['model']['input_nc'] if btoA else config['model']['output_nc']
transform = Transforms(config, input_grayscale_flag=(input_nc == 1), output_grayscale_flag=(output_nc == 1))
transform.transform_list.append(NumpyToTensor())
transform = transform.compose_transforms()
data['paired_A_path'] = A_paths
data['paired_B_path'] = B_paths
transforms['paired'] = transform
return paired_data_order
def apply_numpy_paired_transforms(index, data, transforms, return_dict):
if len(data['paired_A_path']) > 0:
return_dict['paired_A'], return_dict['paired_B'] = transforms['paired'] \
(data['paired_A_path'][index], data['paired_B_path'][index])
return_dict['paired_A_path'] = data['paired_A_path'][index]
return_dict['paired_B_path'] = data['paired_B_path'][index]
|