def __init__(self, opt): self.opt = opt # path to data self.path_A = os.path.expanduser(self.opt.data_A) self.path_B = os.path.expanduser(self.opt.data_B) # get image path sets self.image_paths_A = sorted(utils.get_image_paths(self.path_A)) self.image_paths_B = sorted(utils.get_image_paths(self.path_B)) # get the size of the dataset self.size_A = len(self.image_paths_A) self.size_B = len(self.image_paths_B) # set input and output channels properly based on direction of mapping print("direction - " + self.opt.direction) AtoB = self.opt.direction == 'AtoB' if AtoB: print("going A to B") else: print("going B to A") self.in_channels = self.opt.in_channels if AtoB else self.opt.out_channels self.out_channels = self.opt.out_channels if AtoB else self.opt.in_channels self.step = 0
def __init__(self, data_path, batch_size=16): self.img_w = img_w self.img_h = img_h self.batch_size = batch_size self.image_paths = get_image_paths(data_path) np.random.shuffle(self.image_paths)
def main(): # A. McKay # initialize model image_paths1 = get_image_paths('../data/raw') # image_paths2 = get_image_paths('../data/raw2') model = VGG16(weights='imagenet', include_top=True) layer_name, style_feature_model = get_embedding_model(model, 1) # content_feature_model = get_embedding_model(model, CONTENT_LAYERS[0]) # generate embeddings valid_image_paths, style_embeddings = generate_embeddings(style_feature_model, image_paths1) # valid_image_paths, content_embeddings = generate_embeddings(content_feature_model, image_paths1) query_image_idx = int(len(valid_image_paths) * random.random()) style_embeddings print(f'style_embeddings: {len(style_embeddings)}') # print(f'content_embeddings: {len(content_embeddings)}') # generate features style_features = pca(style_embeddings, None) #yhl # content_features = pca(content_embeddings, None) #yhl closest_image_indices_style = get_closest_indices(query_image_idx, style_features) # closest_image_indices_content = get_closest_indices(query_image_idx, content_features) results_image_paths_style = [valid_image_paths[i] for i in closest_image_indices_style] # results_image_paths_content = [valid_image_paths[i] for i in closest_image_indices_content] save_image(results_image_paths_style)
def main(): crnn = load_model(MODEL_PATH) root = DIR_NAME images = get_image_paths(root) random.shuffle(images) rec = [] ts_score = 0 crnn_score = 0 for n, image in enumerate(images, 1): result = eval_image(image, crnn) ts_score += similarity_score(result['true_text'], result['tesseract']) crnn_score += similarity_score(result['true_text'], result['crnn']) rec.append({'image': image, 'true_text': result['true_text'], 'tesseract': result['tesseract'], 'crnn': result['crnn']}) print(n, image) print(result) print(ts_score / n, crnn_score / n) print() # time.sleep(2) rec_df = pd.DataFrame(rec) rec_df.to_csv('records_10.csv')
def sr_from_folder(model, lr_dir, save_dir, ext): if lr_dir is not None: if not os.path.exists(lr_dir): raise Exception('Not found folder: ' + lr_dir) lr_paths = utils.get_image_paths(lr_dir, ext) for lr_path in lr_paths: sr_from_path(model, lr_path, save_dir)
def main(argv): del argv threshold = 4 OCR = OCRModel(FLAGS.target, FLAGS.target_height, target_threshold=threshold) ad_logo_paths = get_image_paths(FLAGS.glob_path) print("found {} files to match".format(len(ad_logo_paths))) scores = [] t1 = timer() for ad_logo_path in ad_logo_paths: ad_logo = cv2.imread(ad_logo_path, -1) assert ad_logo is not None ad_logo_name = os.path.basename(ad_logo_path) _, score = OCR.match(ad_logo, verbose=True, img_name=ad_logo_name) scores.append(score) t2 = timer() print("evaluated {} images in {} seconds".format(len(ad_logo_paths), t2 - t1)) scores = np.array(scores) ad_logo_paths = np.array(ad_logo_paths) print(np.sum(scores <= threshold)) print(ad_logo_paths[scores <= threshold]) topk = scores.argsort()[:10] print(list(zip(ad_logo_paths[topk], scores[topk])))
def convert_images(pickle_save_dir, image_dir): image_paths = get_image_paths(image_dir) for path in tqdm(image_paths): fname = '{}.pkl'.format(path.split('/')[-1].replace('.jpg', '')) try: img = load_image(path) except Exception: pass with open(os.path.join(os.path.join(pickle_save_dir, fname)), 'wb') as f: pickle.dump(img, f)
def __init__(self, opt, is_training): self.opt = opt self.is_training = is_training # path to data self.path_A = os.path.expanduser(self.opt.data_A) self.path_B = os.path.expanduser(self.opt.data_B) # get image path sets self.image_paths_A = sorted(utils.get_image_paths(self.path_A)) self.image_paths_B = sorted(utils.get_image_paths(self.path_B)) # get the size of the dataset self.size_A = len(self.image_paths_A) self.size_B = len(self.image_paths_B) # set input and output channels properly based on direction of mapping AtoB = self.opt.direction == 'AtoB' self.in_channels = self.opt.in_channels if AtoB else self.opt.out_channels self.out_channels = self.opt.out_channels if AtoB else self.opt.in_channels self.step = 0
def get_best_model(checkpoints_folder: Path) -> str: """ Finds the model with the best validation accuracy. Args: checkpoints_folder: Folder containing the models to test. Returns: The location of the model with the best validation accuracy. """ return max({ model: parse_val_acc(model_path=model) for model in get_image_paths(folder=checkpoints_folder) }.items(), key=operator.itemgetter(1))[0]
def main(args): output_dir = Path(args.output_dir) encoder.load_weights(str(output_dir / "models" / "encoder.h5")) #decoder_A.load_weights( str(output_dir/ "models" / "decoder_A.h5" )) decoder_B.load_weights(str(output_dir / "models" / "decoder_B.h5")) input_dir = output_dir / "old_faces" output_dir = output_dir / "new_faces" output_dir.mkdir(parents=True, exist_ok=True) images_A = get_image_paths(str(input_dir)) for fn in images_A: print(Path(fn).name) image = cv2.imread(fn) new_image = convert_one_image(autoencoder_B, image) output_file = output_dir / Path(fn).name cv2.imwrite(str(output_file), new_image)
def get_folder_size_and_num_images(folder: Path) -> Tuple[float, int]: """ Finds the number and size of images in a folder path. Used to decide how much to slide windows. Args: folder: Folder containing images. Returns: A tuple containing the total size of the images and the number of images. """ image_paths = get_image_paths(folder=folder) file_size = 0 for image_path in image_paths: file_size += image_path.stat().st_size file_size_mb = file_size / 1e6 return file_size_mb, len(image_paths)
def train_batches(config_path, save_dir, data_dir): config = load_config(config_path) batch_size = config['batch_size'] dcgan_path, generator_path, discriminator_path = get_gan_paths(save_dir) dcgan, discriminator, generator = load_or_create_model( config_path, dcgan_path, discriminator_path, generator_path) epochs = config['epochs'] paths = get_image_paths(data_dir) num_batches = int(len(paths) / batch_size) print("-------------------") print("Total epoch:", config['epochs'], "Number of batches:", num_batches) print("-------------------") z_pred = np.array([np.random.normal(0, 0.5, 100) for _ in range(100)]) y_g = [1] * batch_size y_d_true = [1] * batch_size y_d_gen = [0] * batch_size for epoch in range(epochs): start = time() for index in tqdm(range(num_batches)): X_train = load_image_batch(paths, batch_size, index) d_loss_fake, d_loss_real, g_loss = train_batch( X_train, batch_size, dcgan, discriminator, generator, index, y_d_gen, y_d_true, y_g) end = time() - start # save generated images print('D-loss-real: {}, D-loss-fake: {}, ' 'G-loss: {}, epoch: {}, time: {}'.format(d_loss_real, d_loss_fake, g_loss, epoch, end)) if epoch % 10 == 0: generator.save(generator_path) discriminator.save(discriminator_path) dcgan.save(dcgan_path) images = generator.predict(z_pred) save_images(images, 'dcgan_keras_epoch_{}.png'.format(epoch))
def get_coxs2v_samples(still_dir, video_dir, video_list, subject_list, max_samples_per_subject=10, video_only=False): image_path_list = [] labels_list = [] for video_view in video_list: for subject in subject_list: subject_video_path = os.path.join(video_dir, video_view, subject) video_image_paths = utils.get_image_paths(subject_video_path) if len(video_image_paths) > max_samples_per_subject: video_image_paths = video_image_paths[ 0:max_samples_per_subject] label = subject + '_' + video_view labels_list += [label] * len(video_image_paths) if not video_only: video_image_paths += os.path.join(still_dir, subject + '_0000.JPG') labels_list += [subject + '_still'] image_path_list += video_image_paths label_name_list = utils.unique(labels_list) label_to_target_dict = { key: tgt for (tgt, key) in enumerate(label_name_list) } target_list = [] for label in labels_list: target_list += label_to_target_dict[label] return image_path_list, target_list, label_name_list
def build(cls, image_dir, model, layer_range=None, pca_dim=None, vector_buffer_size=2000, index_buffer_size=5000, max_files=2000): inst = cls() inst.lib_name = None inst.vector_buffer_size = vector_buffer_size inst.index_buffer_size = index_buffer_size inst.pca_dim = pca_dim image_paths = get_image_paths(image_dir) random.shuffle(image_paths) image_paths = image_paths[:max_files] if isinstance(model, str): model_cls = cls.models[model] model = model_cls(weights='imagenet', include_top=False) inst.model = model inst._build_image_embedder(layer_range) inst._embedding_gen = inst._gen_lib_embeddings(image_paths) inst._build_index() return inst
def balance_classes(training_folder: Path) -> None: """ Balancing class distribution so that training isn't skewed. Args: training_folder: Folder containing the subfolders to be balanced. """ subfolders = get_subfolder_paths(folder=training_folder) subfolder_to_images = { subfolder: get_image_paths(folder=subfolder) for subfolder in subfolders } # Find the class with the most images. biggest_size = max({ subfolder: len(subfolder_to_images[subfolder]) for subfolder in subfolders }.values()) for subfolder in subfolder_to_images: duplicate_until_n(image_paths=subfolder_to_images[subfolder], n=biggest_size) print(f"balanced all training classes to have {biggest_size} images\n")
from model import encoder, decoder_A, decoder_B try: encoder .load_weights( "/input/data/models/encoder.h5" ) decoder_A.load_weights( "/input/data/models/decoder_A.h5" ) decoder_B.load_weights( "/input/data/models/decoder_B.h5" ) except: pass def save_model_weights(): encoder .save_weights( "/input/data/models/encoder.h5" ) decoder_A.save_weights( "/input/data/models/decoder_A.h5" ) decoder_B.save_weights( "/input/data/models/decoder_B.h5" ) print( "save model weights" ) images_A = get_image_paths( "/input/data/data/trump" ) images_B = get_image_paths( "/input/data/data/cage" ) images_A = load_images( images_A ) / 255.0 images_B = load_images( images_B ) / 255.0 images_A += images_B.mean( axis=(0,1,2) ) - images_A.mean( axis=(0,1,2) ) print( "press 'q' to stop training and save model" ) for epoch in range(1000000): batch_size = 64 warped_A, target_A = get_training_data( images_A, batch_size ) warped_B, target_B = get_training_data( images_B, batch_size ) loss_A = autoencoder_A.train_on_batch( warped_A, target_A ) loss_B = autoencoder_B.train_on_batch( warped_B, target_B )
if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('template_path', type=str) parser.add_argument('glob_path', type=str) parser.add_argument('--hash_type', choices=['avg', 'dct'], default='avg') args = parser.parse_args() threshold = 0.8 PHash = PHashModel(args.template_path, args.hash_type, match_threshold=threshold) ad_logo_paths = get_image_paths(args.glob_path) print("found {} files to match".format(len(ad_logo_paths))) scores = [] t1 = timer() for ad_logo_path in ad_logo_paths: ad_logo = cv2.imread(ad_logo_path, -1) assert ad_logo is not None ad_logo_name = os.path.basename(ad_logo_path) _, score = PHash.match(ad_logo, verbose=True, img_name=ad_logo_name) scores.append(score)
path = "../../../data/faceswap/face/" modelpath = "../../../data/faceswap/" try: encoder .load_weights(modelpath + "models/encoder.h5" ) decoder_A.load_weights(modelpath + "models/decoder_A.h5" ) decoder_B.load_weights(modelpath + "models/decoder_B.h5" ) except: pass def save_model_weights(): encoder .save_weights(modelpath + "models/encoder.h5" ) decoder_A.save_weights(modelpath + "models/decoder_A.h5" ) decoder_B.save_weights(modelpath + "models/decoder_B.h5" ) print( "save model weights" ) images_A = get_image_paths(path + "a" ) images_B = get_image_paths(path + "b" ) images_A = load_images( images_A ) / 255.0 images_B = load_images( images_B ) / 255.0 images_A += images_B.mean( axis=(0,1,2) ) - images_A.mean( axis=(0,1,2) ) print( "press 'q' to stop training and save model" ) for epoch in range(1000000): batch_size = 64 warped_A, target_A = get_training_data( images_A, batch_size ) warped_B, target_B = get_training_data( images_B, batch_size ) loss_A = autoencoder_A.train_on_batch( warped_A, target_A ) loss_B = autoencoder_B.train_on_batch( warped_B, target_B )
saver.restore(sess,ckpt_path) return input_low_images,main_out_clip,main_hf,main_lf,sess if __name__ == '__main__': os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.run_gpu input_image,main_output,hf_output,lf_output,sess = load_model(FLAGS.model_path) ####################################### #Where is your test low-resolution images ######################################## test_image_path = './Test/image/path' output_path = '' test_image_list = utils.get_image_paths(test_image_path) temp_time = 0 for test_idx,test_image in enumerate(test_image_list): loaded_image = cv2.imread(test_image) feed_dict ={input_image:[loaded_image[:,:,::-1]]} main_out,hf_out,lf_out = sess.run([main_output,hf_output,lf_output]feed_dict=feed_dict) main_image = main_out[0,:,:,:] hf_image = hf_out[0,:,:,:] lf_image = lf_out[0,:,:,:] image_name = os.path.basename(test_image).split('.')[0] ################################ #Where to Save the output images ################################
import numpy from utils import get_image_paths, load_images, stack_images from training_data import get_training_data import glob import random from scipy.stats import linregress from tqdm import tqdm if __name__ == '__main__': print('running') images_A = get_image_paths( "data/A" ) images_B = get_image_paths( "data/B" ) minImages = 2000#min(len(images_A),len(images_B))*20 random.shuffle(images_A) random.shuffle(images_B) images_A,landmarks_A = load_images( images_A[:minImages] ) images_B,landmarks_B = load_images( images_B[:minImages] ) print('Images A', images_A.shape) print('Images B', images_B.shape) images_A = images_A/255.0 images_B = images_B/255.0
save_descriptions(descriptions, 'descriptions.txt') ################################################################## # load training dataset (6K) filename = os.path.join(text_file_base_dir, 'Flickr_8k.trainImages.txt') train = load_image_names(filename) print('Num of data samples: %d' % len(train)) # Below path contains all the images images_dir = os.path.join(base_dir, 'data/captioning/Flicker8k_Dataset/') # Below file conatains the names of images to be used in train data train_image_txt_file = os.path.join(text_file_base_dir, 'Flickr_8k.trainImages.txt') train_image_paths = get_image_paths(images_dir, train_image_txt_file) test_image_txt_file = os.path.join(text_file_base_dir, 'Flickr_8k.testImages.txt') test_image_paths = get_image_paths(images_dir, test_image_txt_file) # descriptions train_descriptions = load_clean_descriptions('descriptions.txt', train) print('Descriptions: train=%d' % len(train_descriptions)) # Load the inception v3 model model = InceptionV3(weights='imagenet', include_top=True) model_new = Model(model.input, model.layers[-2].output) # model_new = InceptionV3(weights='imagenet', include_top=False) # Create a new model, by removing the last layer (output layer) from the inception v3
from keras.models import Model from keras.layers import Input, Dense, Flatten, Reshape, concatenate, Add, add, Dropout from keras.layers.advanced_activations import LeakyReLU from keras.layers.convolutional import Conv2D from keras.initializers import RandomNormal from keras.optimizers import Adam from keras.utils import conv_utils from keras.engine.topology import Layer import keras.backend as K import tensorflow as tf import time from keras_contrib.losses import DSSIMObjective images_A = get_image_paths("data/A") images_B = get_image_paths("data/B") images_C = get_image_paths("data/C") images_D = get_image_paths("data/D") minImages = max(len(images_A), len(images_B), len(images_C), len(images_D)) random.shuffle(images_A) random.shuffle(images_B) random.shuffle(images_C) random.shuffle(images_D) images_A, landmarks_A = load_images(images_A[:minImages]) images_B, landmarks_B = load_images(images_B[:minImages]) images_C, landmarks_C = load_images(images_C[:minImages]) images_D, landmarks_D = load_images(images_D[:minImages])
token = "PJYAHHJQGOINNXZCWYYJIIZYBGVFGTBWYERAMUUQDXFVFOEE" print("WARNING /n Using REAL token for the SigOpt API") # CUDA for PyTorch if torch.cuda.is_available(): device = torch.cuda.device("cuda:0") torch.backends.cudnn.benchmark = True print("CUDA is available") else: device = torch.device("cpu") all_img_folder = ut.get_image_paths( conf.SOMITE_COUNTS, opt.data_dir ) train_folder, val_folder = ut.test_train_split( opt.data_dir, all_img_folder, 0.2, conf.N_CLASSES ) # Datasets train_data = SomiteStageDataset( img_folder=train_folder ) val_data = SomiteStageDataset(
from pathlib import Path from utils import get_image_paths from model import autoencoder_A from model import autoencoder_B from model import encoder, decoder_A, decoder_B videopath = "../../../data/faceswap/video/" modelpath = "../../../data/faceswap/" encoder.load_weights(modelpath + "models/encoder.h5") decoder_A.load_weights(modelpath + "models/decoder_A.h5") decoder_B.load_weights(modelpath + "models/decoder_B.h5") images_A = get_image_paths("../../../data/faceswap/original3/a") images_B = get_image_paths("../../../data/faceswap/original3/b") def convert_one_image(autoencoder, image): assert image.shape == (256, 256, 3) crop = slice(48, 208) face = image[crop, crop] face = cv2.resize(face, (64, 64)) face = numpy.expand_dims(face, 0) new_face = autoencoder.predict(face / 255.0)[0] new_face = numpy.clip(new_face * 255, 0, 255).astype(image.dtype) new_face = cv2.resize(new_face, (160, 160)) new_image = image.copy() new_image[crop, crop] = new_face return new_image
size[min_arg] = int(min_len) im = cv2.resize(im, (size[1], size[0])) return im, ratio if __name__ == '__main__': # set your gpus usage os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.run_gpu # get pre-trained generator model input_image, generated_image, sess = load_model(FLAGS.model_path) # get test_image_list test_image_list = utils.get_image_paths(FLAGS.image_path) # make save_folder if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) # do test for test_idx, test_image in enumerate(test_image_list): loaded_image = cv2.imread(test_image) processed_image, tmp_ratio = init_resize_image(loaded_image) feed_dict = {input_image: [processed_image[:, :, ::-1]]} output_image = sess.run(generated_image, feed_dict=feed_dict)
from model import encoder, decoder_A, decoder_B try: encoder .load_weights( "models/encoder.h5" ) decoder_A.load_weights( "models/decoder_A.h5" ) decoder_B.load_weights( "models/decoder_B.h5" ) except: pass def save_model_weights(): encoder .save_weights( "models/encoder.h5" ) decoder_A.save_weights( "models/decoder_A.h5" ) decoder_B.save_weights( "models/decoder_B.h5" ) print( "save model weights" ) images_A = get_image_paths( "data/trump" ) images_B = get_image_paths( "data/cage" ) images_A = load_images( images_A ) / 255.0 images_B = load_images( images_B ) / 255.0 images_A += images_B.mean( axis=(0,1,2) ) - images_A.mean( axis=(0,1,2) ) print( "press 'q' to stop training and save model" ) for epoch in range(1000000): batch_size = 64 warped_A, target_A = get_training_data( images_A, batch_size ) warped_B, target_B = get_training_data( images_B, batch_size ) loss_A = autoencoder_A.train_on_batch( warped_A, target_A ) loss_B = autoencoder_B.train_on_batch( warped_B, target_B )
DEFAULT_MAX_WIDTH = 100 if __name__ == "__main__": # argument parser parser = argparse.ArgumentParser() parser.add_argument('--image-dir', help='image directory') parser.add_argument('--output-dir', help='output directory') args = parser.parse_args() # prepare gabor kernels gabor_kernels = feat.prepare_gabor_kernels(gabor_freqs=[0.2]) # get image paths image_paths = util.get_image_paths(args.image_dir) all_features = [] for i, image_path in enumerate(image_paths): print "%i from %i" % (i + 1, len(image_paths)) # load images image = io.imread(image_path['crop']) # compute scale factor h, w, d = image.shape scale = float(DEFAULT_MAX_WIDTH) / w # rescale image image = tran.rescale(image, scale=(scale, scale)) image_gray = color.rgb2gray(image)
def get_predictions(patches_eval_folder: Path, output_folder: Path, checkpoints_folder: Path, auto_select: bool, eval_model: Path, device: torch.device, classes: List[str], num_classes: int, path_mean: List[float], path_std: List[float], num_layers: int, pretrain: bool, batch_size: int, num_workers: int) -> None: """ Main function for running the model on all of the generated patches. Args: patches_eval_folder: Folder containing patches to evaluate on. output_folder: Folder to save the model results to. checkpoints_folder: Directory to save model checkpoints to. auto_select: Automatically select the model with the highest validation accuracy, eval_model: Path to the model with the highest validation accuracy. device: Device to use for running model. classes: Names of the classes in the dataset. num_classes: Number of classes in the dataset. path_mean: Means of the WSIs for each dimension. path_std: Standard deviations of the WSIs for each dimension. num_layers: Number of layers to use in the ResNet model from [18, 34, 50, 101, 152]. pretrain: Use pretrained ResNet weights. batch_size: Mini-batch size to use for training. num_workers: Number of workers to use for IO. """ # Initialize the model. model_path = get_best_model( checkpoints_folder=checkpoints_folder) if auto_select else eval_model model = create_model(num_classes=num_classes, num_layers=num_layers, pretrain=pretrain) ckpt = torch.load(f=model_path) model.load_state_dict(state_dict=ckpt["model_state_dict"]) model = model.to(device=device) model.train(mode=False) print(f"model loaded from {model_path}") # For outputting the predictions. class_num_to_class = {i: classes[i] for i in range(num_classes)} start = time.time() # Load the data for each folder. image_folders = get_subfolder_paths(folder=patches_eval_folder) # Where we want to write out the predictions. # Confirm the output directory exists. output_folder.mkdir(parents=True, exist_ok=True) # For each WSI. for image_folder in image_folders: # Temporary fix. Need not to make folders with no crops. try: # Load the image dataset. dataloader = torch.utils.data.DataLoader( dataset=datasets.ImageFolder( root=str(image_folder), transform=transforms.Compose(transforms=[ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=path_mean, std=path_std) ])), batch_size=batch_size, shuffle=False, num_workers=num_workers) except RuntimeError: print( "WARNING: One of the image directories is empty. Skipping this directory." ) continue num_test_image_windows = len(dataloader) * batch_size # Load the image names so we know the coordinates of the patches we are predicting. image_folder = image_folder.joinpath(image_folder.name) window_names = get_image_paths(folder=image_folder) print(f"testing on {num_test_image_windows} crops from {image_folder}") test_label_to_class = {0:"0", 1:"180", 2:"270", 3:"90"} with output_folder.joinpath(f"{image_folder.name}.csv").open( mode="w") as writer: writer.write("image_name,ground_truth,prediction,confidence\n") # Loop through all of the patches. for batch_num, (test_inputs, test_labels) in enumerate(dataloader): batch_window_names = window_names[batch_num * batch_size:batch_num * batch_size + batch_size] confidences, test_preds = torch.max(nn.Softmax(dim=1)(model( test_inputs.to(device=device))), dim=1) for i in range(test_preds.shape[0]): # Find coordinates and predicted class. image_name = batch_window_names[i].name # xy = batch_window_names[i].name.split(".")[0].split(";") writer.write( f"{','.join([image_name, image_folder.name, f'{class_num_to_class[test_preds[i].data.item()]}', f'{confidences[i].data.item():.5f}'])}\n" ) print(f"time for {patches_eval_folder}: {time.time() - start:.2f} seconds")
def split(all_wsi, train_folder, val_folder, test_folder, val_split, test_split, keep_orig_copy, labels_train, labels_val, labels_test): head = 'cp' if keep_orig_copy else 'mv' # based on whether we want to move or keep the files # create folders for folder in [train_folder, val_folder, test_folder]: subfolders = [join(folder, _class) for _class in config.classes] for subfolder in subfolders: confirm_output_folder(subfolder) train_img_to_label = {} val_img_to_label = {} test_img_to_label = {} def move_set(folder, image_files, ops): """ Return: a dictionary where key is (str)image_file_name and value is (str)image_class """ def remove_topdir(filepath): """filepath should be a relative path ex) a/b/c.jpg -> b/c.jpg """ first_delimiter_idx = filepath.find('/') return filepath[first_delimiter_idx + 1:] img_to_label = {} for image_file in image_files: output_path = join(folder, remove_topdir(image_file)) os.system(f'{ops} {image_file} {output_path}') img_name = basename(image_file) img_class = basename(dirname(image_file)) img_to_label[img_name] = img_class return img_to_label # sort the images and move/copy them appropriately subfolder_paths = get_subfolder_paths(all_wsi) for subfolder in subfolder_paths: image_paths = get_image_paths(subfolder) assert len(image_paths) > val_split + test_split # make sure we have enough slides in each class # assign training, test, and val images test_idx = len(image_paths) - test_split val_idx = test_idx - val_split train_images = image_paths[:val_idx] val_images = image_paths[val_idx:test_idx] test_images = image_paths[test_idx:] print('class {}:'.format(basename(subfolder)), '#train={}'.format(len(train_images)), '#val={} '.format(len(val_images)), '#test={}'.format(len(test_images))) # move train tmp_train_img_to_label = move_set(folder=train_folder, image_files=train_images, ops=head) train_img_to_label.update(tmp_train_img_to_label) # move val tmp_val_img_to_label = move_set(folder=val_folder, image_files=val_images, ops=head) val_img_to_label.update(tmp_train_img_to_label) # move test tmp_test_img_to_label = move_set(folder=test_folder, image_files=test_images, ops=head) # for making the csv files def write_to_csv(dest_filename, image_lable_dict): with open(dest_filename, 'w') as writer: writer.write('img,gt\n') for img in sorted(image_lable_dict.keys()): writer.write(img + ',' + image_lable_dict[img] + '\n') write_to_csv(dest_filename=labels_train, image_lable_dict=train_img_to_label) write_to_csv(dest_filename=labels_val, image_lable_dict=val_img_to_label) write_to_csv(dest_filename=labels_test, image_lable_dict=test_img_to_label)
def convert_one_images(autoencoder, image): assert image.shape == (256, 256, 3) crop = slice(48, 208) face = image[crop, crop] face = cv2.resize(face, (64, 64)) face = numpy.expand_dims(face, 0) new_face = autoencoder.predict(face / 255.0)[0] new_face = numpy.clip(new_face * 255, 0, 255).astype(image.dtype) new_face = cv2.resize(new_face, (160, 160)) new_image = image.copy() new_image[crop, crop] = new_face return new_image if __name__ == '__main__': output_dir = Path('output') output_dir.mkdir(parents=True, exist_ok=True) encoder.load_weights("models/encoder.h5") decoder_A.load_weights("models/decoder_A.h5") decoder_B.load_weights("models/decoder_B.h5") images_A = get_image_paths("data/trump") images_B = get_image_paths("data/cage") for fn in images_A: image = cv2.imread(fn) new_image = convert_one_image(autoencoder_B, image) output_file = output_dir / Path(fn).name cv2.imwrite(str(output_file), new_image)
help="Name of a Person to swap the face with (e.g. Taylor Swift)") parser.add_argument("--epochs", default=100000, type=int, help="Number of Epochs to train") args = parser.parse_args() # check out directory directory and create if necessary if not os.path.exists("./out/"): os.makedirs("./out/") # empty directory for f in glob.glob(os.path.join("./out/", "*.jpg")): os.remove(f) # load dataset images images_A = get_image_paths("data/faces/{}".format(args.src.lower().replace( " ", "_"))) images_B = get_image_paths("data/faces/{}".format(args.dst.lower().replace( " ", "_"))) # map between 0 and 1 and normalize images images_A = load_images(images_A) / 255.0 images_B = load_images(images_B) / 255.0 images_A += images_B.mean(axis=(0, 1, 2)) - images_A.mean(axis=(0, 1, 2)) # create 100 preview images during training (to not spam your disk) print_rate = args.epochs // 100 if print_rate < 1: print_rate = 1 # iterate epochs and train for epoch in tqdm(range(args.epochs)): # get next training batch