opt.texture_checkpoint, same_crop_load_size=True if opt.warp_checkpoint else False, texture_dir=opt.texture_dir, cloth_dir=cloth_dir, ) print(f"Texturing cloth segmentations in {cloth_dir}...") try: _run_test_loop(texture_model, texture_dataset, webpage) except KeyboardInterrupt: print("Ending texture early.") print(f"Textured results stored in {texture_out}") if __name__ == "__main__": config = TestOptions() config.parse() opt = config.opt # override checkpoint options if opt.checkpoint: if not opt.warp_checkpoint: opt.warp_checkpoint = os.path.join( opt.checkpoint, "warp", f"{opt.load_epoch}_net_generator.pth") print("Set warp_checkpoint to", opt.warp_checkpoint) if not opt.texture_checkpoint: opt.texture_checkpoint = os.path.join( opt.checkpoint, "texture", f"{opt.load_epoch}_net_generator.pth") print("Set texture_checkpoint to", opt.texture_checkpoint)
import torch.nn as nn import torchvision.transforms as transforms import onnx import onnxruntime from options.test_options import TestOptions import sys sys.path.insert(0, './image_segmentation') import network from optimizer import restore_snapshot from datasets import cityscapes from config import assert_and_infer_cfg TestOptions = TestOptions() opt = TestOptions.parse() opt.use_vae = True def remove_all_spectral_norm(item): if isinstance(item, nn.Module): try: nn.utils.remove_spectral_norm(item) except Exception: pass for child in item.children(): remove_all_spectral_norm(child) if isinstance(item, nn.ModuleList):
import os from collections import OrderedDict from torch.autograd import Variable from options.test_options import TestOptions from data.data_loader import CreateDataLoader from models.models import create_model import util.util as util from util.visualizer import Visualizer from util import html import torch opt = TestOptions().parse(save=False) opt.nThreads = 1 # test code only supports nThreads = 1 opt.serial_batches = True # no shuffle opt.no_flip = True # no flip data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() visualizer = Visualizer(opt) # create website web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch)) webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch)) # test if not opt.engine and not opt.onnx: model = create_model(opt) if opt.data_type == 16: model.half() elif opt.data_type == 8: model.type(torch.uint8)
y_true, y_pred = [], [] for img, label in tqdm.tqdm(data_loader, file=stdout): in_tens = img.cuda() y_pred.extend(model(in_tens).sigmoid().flatten().tolist()) y_true.extend(label.flatten().tolist()) y_true, y_pred = np.array(y_true), np.array(y_pred) r_acc = accuracy_score(y_true[y_true == 0], y_pred[y_true == 0] > 0.5) f_acc = accuracy_score(y_true[y_true == 1], y_pred[y_true == 1] > 0.5) acc = accuracy_score(y_true, y_pred > 0.5) ap = average_precision_score(y_true, y_pred) return acc, ap, r_acc, f_acc, y_true, y_pred if __name__ == '__main__': opt = TestOptions().parse(print_options=False) model = resnet50(num_classes=1) state_dict = torch.load(opt.model_path, map_location='cpu') model.load_state_dict(state_dict['model']) model.cuda() model.eval() acc, avg_precision, r_acc, f_acc, y_true, y_pred = validate(model, opt) print("accuracy:", acc) print("average precision:", avg_precision) print("accuracy of real images:", r_acc) print("accuracy of fake images:", f_acc)
def run(): test_opts = TestOptions().parse() out_path_results = os.path.join(test_opts.exp_dir, 'inference_results') os.makedirs(out_path_results, exist_ok=True) # load model used for initializing encoder bootstrapping ckpt = torch.load(test_opts.model_1_checkpoint_path, map_location='cpu') opts = ckpt['opts'] opts.update(vars(test_opts)) opts['checkpoint_path'] = test_opts.model_1_checkpoint_path opts = Namespace(**opts) if opts.encoder_type in ENCODER_TYPES['pSp']: net1 = pSp(opts) else: net1 = e4e(opts) net1.eval() net1.cuda() # load model used for translating input image after initialization ckpt = torch.load(test_opts.model_2_checkpoint_path, map_location='cpu') opts = ckpt['opts'] opts.update(vars(test_opts)) opts['checkpoint_path'] = test_opts.model_2_checkpoint_path opts = Namespace(**opts) if opts.encoder_type in ENCODER_TYPES['pSp']: net2 = pSp(opts) else: net2 = e4e(opts) net2.eval() net2.cuda() print('Loading dataset for {}'.format(opts.dataset_type)) dataset_args = data_configs.DATASETS[opts.dataset_type] transforms_dict = dataset_args['transforms'](opts).get_transforms() dataset = InferenceDataset( root=opts.data_path, transform=transforms_dict['transform_inference'], opts=opts) dataloader = DataLoader(dataset, batch_size=opts.test_batch_size, shuffle=False, num_workers=int(opts.test_workers), drop_last=False) if opts.n_images is None: opts.n_images = len(dataset) # get the image corresponding to the latent average avg_image = net1(net1.latent_avg.unsqueeze(0), input_code=True, randomize_noise=False, return_latents=False, average_code=True)[0] avg_image = avg_image.to('cuda').float().detach() resize_amount = (256, 256) if opts.resize_outputs else (opts.output_size, opts.output_size) global_i = 0 global_time = [] for input_batch in tqdm(dataloader): if global_i >= opts.n_images: break with torch.no_grad(): input_cuda = input_batch.cuda().float() tic = time.time() result_batch = run_on_batch(input_cuda, net1, net2, opts, avg_image) toc = time.time() global_time.append(toc - tic) for i in range(input_batch.shape[0]): results = [ tensor2im(result_batch[i][iter_idx]) for iter_idx in range(opts.n_iters_per_batch + 1) ] im_path = dataset.paths[global_i] input_im = tensor2im(input_batch[i]) # save step-by-step results side-by-side res = np.array(results[0].resize(resize_amount)) for idx, result in enumerate(results[1:]): res = np.concatenate( [res, np.array(result.resize(resize_amount))], axis=1) res = np.concatenate([res, input_im.resize(resize_amount)], axis=1) Image.fromarray(res).save( os.path.join(out_path_results, os.path.basename(im_path))) global_i += 1 stats_path = os.path.join(opts.exp_dir, 'stats.txt') result_str = 'Runtime {:.4f}+-{:.4f}'.format(np.mean(global_time), np.std(global_time)) print(result_str) with open(stats_path, 'w') as f: f.write(result_str)
def startModel(): logging.basicConfig(level=logging.INFO) logger = logging.getLogger('PRUEBAS DE MODELO') logger.info("Primera prueba") opt = TestOptions().parse() # get test options # hard-code some parameters for test opt.num_threads = 0 # test code only supports num_threads = 1 opt.batch_size = 1 # test code only supports batch_size = 1 opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed. opt.no_flip = True # no flip; comment this line if results on flipped images are needed. opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file. opt.eval = True # For experimentation opt.dataset_mode = 'single' opt.dataroot = './data/pottery/' logger.info("Segunda prueba") # Load 8 modelViews model0 = create_model( opt) # create a model given opt.model and other options model0.setup( opt, 'm0') # regular setup: load and print networks; create schedulers model1 = create_model( opt) # create a model given opt.model and other options model1.setup( opt, 'm1') # regular setup: load and print networks; create schedulers model2 = create_model( opt) # create a model given opt.model and other options model2.setup( opt, 'm2') # regular setup: load and print networks; create schedulers model3 = create_model( opt) # create a model given opt.model and other options model3.setup( opt, 'm3') # regular setup: load and print networks; create schedulers model4 = create_model( opt) # create a model given opt.model and other options model4.setup( opt, 'm4') # regular setup: load and print networks; create schedulers model5 = create_model( opt) # create a model given opt.model and other options model5.setup( opt, 'm5') # regular setup: load and print networks; create schedulers model6 = create_model( opt) # create a model given opt.model and other options model6.setup( opt, 'm6') # regular setup: load and print networks; create schedulers model7 = create_model( opt) # create a model given opt.model and other options model7.setup( opt, 'm7') # regular setup: load and print networks; create schedulers logger.info("Tercera prueba") if opt.eval: model0.eval() model1.eval() model2.eval() model3.eval() model4.eval() model5.eval() model6.eval() model7.eval() logger.info("Cuarta prueba") return model0, model1, model2, model3, model4, model5, model6, model7
start_time = time.time() # timer for validate a task matrixItems = [] for i, data in enumerate(val_dataset): # inner loop within one epoch model.set_data(PseudoData(opt, Bunch(**data["data"]))) model.test(visualizer) matrixItems.append(model.get_matrix_item(task_index)) res = my_sum(matrixItems) res = res / len(matrixItems) logging.info(f"Validation Time Taken: {time.time() - start_time} sec") return res if __name__ == '__main__': opt = TestOptions().parse() # get training options model = create_model( opt) # create a model given opt.model and other options model.setup() # regular setup: load and print networks; create schedulers visualizer = Visualizer(opt) visualizer.setup() # regular setup: # visualizer.add_graph(model,) TODO the model graph visualization test_datasets = create_task_dataset(opt, phase="test") nb_tasks = test_datasets.nb_tasks test_matrix = TestMatrix()
import time from options.test_options import TestOptions from data import create_dataset from models import * from util.util import * import os.path as osp import sys, os from models.base_visual import BaseVisual import torch.nn as nn if __name__ == '__main__': ################################## #Preparing the Logger and Backup ################################## opt, logger = TestOptions().parse() opt.batch_size = 1 os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu_ids ################################## #Preparing the Dataset ################################## test_loader = create_dataset(opt) ################################## #Initilizing the Model ################################## model = create_model(opt) # the_model = torch.load(osp.join(opt.backup_path, 'Best.pth')) # model.load_state_dict(the_model) print(model)
import time import os from options.test_options import TestOptions from options.train_options import TrainOptions from data.data_loader import CreateDataLoader from models.models import create_model from util.visualizer import Visualizer from util import util from PIL import Image from tqdm import tqdm opt = TestOptions().parse(stop_write=True) opt_def = TrainOptions().parse(stop_write=True) save_path = os.path.join('./checkpoints', opt.name) old_opt = util.load_opt(save_path, opt_def) old_opt.continue_train = True old_opt.test = True opt.fineSize = old_opt.fineSize opt.loadSize = old_opt.loadSize opt.nThreads = 1 # test code only supports nThreads = 1 opt.batchSize = 1 # test code only supports batchSize = 1 opt.serial_batches = True # no shuffle opt.no_flip = True # no flip #opt.dataroot = './datasets/cityscapes/' data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() #opt.name = 'final_city_skip-featonly_block-6-gp' old_opt.eval = False model = create_model(old_opt)
opt.fresdir =opt.results_dir+ '/' + opt.resdir evaldir = opt.fresdir+'/final/' if not os.path.exists(opt.fresdir): os.makedirs(opt.fresdir) print("%s: ; %d" %(dataset.name,len(dataset))) st = time.time() for i, data in enumerate(dataset): sd = self.model.get_prediction(data) resname = (data['imname']) if isinstance(sd,dict): evaldir = opt.fresdir+'/final/' for k in sd: if k == 'param': sdmkdir(os.path.join(opt.fresdir,k)) np.savetxt(os.path.join(opt.fresdir,k,resname+'.txt'), sd[k], delimiter=' ') else: sdmkdir(os.path.join(opt.fresdir,k)) im = Image.fromarray(sd[k]) im.save(os.path.join(opt.fresdir,k,resname)) else: evaldir = opt.fresdir sd = Image.fromarray(sd) sd.save(os.path.join(opt.fresdir,resname)) if __name__=='__main__': opt = TestOptions().parse() #args.parse() a= Predict(opt) a.ISTD_test()
from options.test_options import TestOptions from data.data_loader import CreateDataLoader from models.model_select import create_model from util.visualizer import Visualizer from util import html opt = TestOptions().GetOption() opt.nThreads = 1 # test code only supports nThreads = 1 opt.batchSize = 1 # test code only supports batchSize = 1 opt.serial_batches = True # no shuffle opt.no_flip = True # no flip data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() model = create_model(opt) visualizer = Visualizer(opt) # create website #web_dir = os.path.join(opt.output_dir, opt.name, '%s_%s' % (opt.phase, model.s_epoch)) #webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, model.s_epoch)) web_dir = opt.output_dir webpage = html.HTML( web_dir, 'Experiment, Phase = %s, Epoch = %s' % (opt.phase, model.s_epoch)) # test avgPSNR = 0.0 avgSSIM = 0.0 counter = 0 for i, data in enumerate(dataset): if i >= opt.how_many: break counter = i
from util import html from niidata import * import argparse from torch.autograd import Variable from torch.utils.data import DataLoader import SimpleITK as sitk import numpy as np import pandas as pd import random import eval_data as ed import time from motion_model import Net_3d_ALL from warp_layer import SpatialTransformer from skimage.measure import compare_ssim, compare_psnr, compare_mse, compare_nrmse args = TestOptions().parse() def dice_loss(pred, target): """pred: tensor with first dimension as batch target :tensor with first dimension as batch """ smooth = 1 iflat = pred.contiguous().view(-1) tflat = target.contiguous().view(-1) intersection = (iflat * tflat).sum() A_sum = torch.sum(tflat * iflat) B_sum = torch.sum(tflat * tflat) return ((2. * intersection+smooth)/(A_sum+B_sum+smooth))
import os from options.test_options import TestOptions from data.data_loader import CreateDataLoader from models.models import create_model from util.visualizer import Visualizer from util import html opt = TestOptions().parse() model = create_model(opt) t1, t2 = model.get_titles() TestOptions().printandsave(opt) data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() visualizer = Visualizer(opt) web_dir = os.path.join(opt.results_dir, opt.name, t1) webpage = html.HTML(web_dir, t2) for i, data in enumerate(dataset): model.set_input(data) model.test() visuals = model.get_current_visuals() img_path = model.get_image_paths() visualizer.save_images(webpage, visuals, img_path) webpage.save()
def run(): test_opts = TestOptions().parse() out_path_results = os.path.join(test_opts.exp_dir, 'inference_results') out_path_coupled = os.path.join(test_opts.exp_dir, 'inference_coupled') os.makedirs(out_path_results, exist_ok=True) os.makedirs(out_path_coupled, exist_ok=True) # update test options with options used during training ckpt = torch.load(test_opts.checkpoint_path, map_location='cpu') opts = ckpt['opts'] opts.update(vars(test_opts)) opts = Namespace(**opts) net = pSp(opts) net.eval() net.cuda() age_transformers = [ AgeTransformer(target_age=age) for age in opts.target_age.split(',') ] print(f'Loading dataset for {opts.dataset_type}') dataset_args = data_configs.DATASETS[opts.dataset_type] transforms_dict = dataset_args['transforms'](opts).get_transforms() dataset = InferenceDataset( root=opts.data_path, transform=transforms_dict['transform_inference'], opts=opts) dataloader = DataLoader(dataset, batch_size=opts.test_batch_size, shuffle=False, num_workers=int(opts.test_workers), drop_last=False) if opts.n_images is None: opts.n_images = len(dataset) global_time = [] for age_transformer in age_transformers: print(f"Running on target age: {age_transformer.target_age}") global_i = 0 for input_batch in tqdm(dataloader): if global_i >= opts.n_images: break with torch.no_grad(): input_age_batch = [ age_transformer(img.cpu()).to('cuda') for img in input_batch ] input_age_batch = torch.stack(input_age_batch) input_cuda = input_age_batch.cuda().float() tic = time.time() result_batch = run_on_batch(input_cuda, net, opts) toc = time.time() global_time.append(toc - tic) for i in range(len(input_batch)): result = tensor2im(result_batch[i]) im_path = dataset.paths[global_i] if opts.couple_outputs or global_i % 100 == 0: input_im = log_image(input_batch[i], opts) resize_amount = ( 256, 256) if opts.resize_outputs else (1024, 1024) res = np.concatenate([ np.array(input_im.resize(resize_amount)), np.array(result.resize(resize_amount)) ], axis=1) age_out_path_coupled = os.path.join( out_path_coupled, age_transformer.target_age) os.makedirs(age_out_path_coupled, exist_ok=True) Image.fromarray(res).save( os.path.join(age_out_path_coupled, os.path.basename(im_path))) age_out_path_results = os.path.join( out_path_results, age_transformer.target_age) os.makedirs(age_out_path_results, exist_ok=True) image_name = os.path.basename(im_path) im_save_path = os.path.join(age_out_path_results, image_name) Image.fromarray(np.array( result.resize(resize_amount))).save(im_save_path) global_i += 1 stats_path = os.path.join(opts.exp_dir, 'stats.txt') result_str = 'Runtime {:.4f}+-{:.4f}'.format(np.mean(global_time), np.std(global_time)) print(result_str) with open(stats_path, 'w') as f: f.write(result_str)
def main(): opt = TestOptions() args = opt.initialize() os.environ["CUDA_VISIBLE_DEVICES"] = args.GPU if not os.path.exists(args.save): os.makedirs(args.save) # 3 models are used because MBT method is used. args.restore_from = args.restore_opt1 model1 = CreateModel(args) model1.eval() model1.cuda() args.restore_from = args.restore_opt2 model2 = CreateModel(args) model2.eval() model2.cuda() args.restore_from = args.restore_opt3 model3 = CreateModel(args) model3.eval() model3.cuda() targetloader = CreateTrgDataLoader(args) # change the mean for different dataset other than CS IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32) IMG_MEAN = torch.reshape(torch.from_numpy(IMG_MEAN), (1, 3, 1, 1)) mean_img = torch.zeros(1, 1) # ------------------------------------------------- # # compute scores and save them with torch.no_grad(): for index, batch in enumerate(targetloader): if index % 100 == 0: print('%d processd' % index) image, _, name = batch # 1. get image # create mean image if mean_img.shape[-1] < 2: B, C, H, W = image.shape # 2. get mean image mean_img = IMG_MEAN.repeat(B, 1, H, W) image = image.clone() - mean_img # 3, image - mean_img image = Variable(image).cuda() # forward output1 = model1(image) output1 = nn.functional.softmax(output1, dim=1) output2 = model2(image) output2 = nn.functional.softmax(output2, dim=1) output3 = model3(image) output3 = nn.functional.softmax(output3, dim=1) a, b = 0.3333, 0.3333 output = a * output1 + b * output2 + (1.0 - a - b) * output3 output = nn.functional.interpolate( output, (1024, 2048), mode='bilinear', align_corners=True).cpu().data[0].numpy() #output = nn.functional.upsample( output, (1024, 2048), mode='bilinear', align_corners=True).cpu().data[0].numpy() output = output.transpose(1, 2, 0) output_nomask = np.asarray(np.argmax(output, axis=2), dtype=np.uint8) output_col = colorize_mask(output_nomask) output_nomask = Image.fromarray(output_nomask) name = name[0].split('/')[-1] output_nomask.save('%s/%s' % (args.save, name)) output_col.save('%s/%s_color.png' % (args.save, name.split('.')[0])) # scores computed and saved # ------------------------------------------------- # compute_mIoU(args.gt_dir, args.save, args.devkit_dir, args.restore_from)
def data_aug(): json_path = "./cityscapes/annotations/instancesonly_filtered_gtFine_train.json" bbox_list = {} with open(json_path) as json_file: data_city = json.load(json_file) for i in range(len(data_city['images'])): bbox_list[data_city['images'][i]['id']] = [] for i in range(len(data_city['annotations'])): img_id = data_city['annotations'][i]['image_id'] bbox = data_city['annotations'][i]['bbox'] bbox_list[img_id].append(bbox) num_img = len(data_city['images']) opt = TestOptions().parse() opt.nThreads = 1 # test code only supports nThreads = 1 opt.batchSize = 1 # test code only supports batchSize = 1 opt.serial_batches = True # no shuffle opt.no_flip = True # no flip model = create_model(opt) dis_path = './distribution_bboxes_human' f1 = open(dis_path, 'rb') data2 = pickle.load(f1) points = np.array(data2['center']) x = points[:, 0] y = points[:, 1] mu = np.mean((x, y), axis=1) con = np.cov(x, y) f1.close() nn = 0 with open(json_path) as json_file: js = json.load(json_file) # img_path=all_path_pickle('./data/cityscapes/leftImg8bit/train') # print(img_path) tt = len(js['annotations']) last_id = js['annotations'][tt - 1]['id'] # aug_img=all_path_pickle('./possion_blending/img') aug_img = all_path_pickle('./aug_img/') # aug_mask=all_path_pickle('./possion_blending/mask') num_img = len(aug_img) total = 1 for i in range(len(js['images'])): # city=str(data['images'][i]['file_name']).split['_'][0] file_name = js['images'][i]['file_name'] img_id = js['images'][i]['id'] city = file_name.split('_')[0] image_path = './cityscapes/leftImg8bit/train/' + js['images'][i][ 'file_name'] img = cv2.imread(image_path) added = 0 AB = [] bbox_dict = [] add_bbox = [] tryout = 0 while (added < 6): h, w, = 180, 90 sample = np.random.multivariate_normal(mean=mu, cov=con, size=1) random_x, random_y = sample[0] random_x = int(random_x) random_y = int(random_y) if random_x < 128 or random_x > 2048 - 128 or random_y < 128 or random_y > 1024 - 128: continue x1s = int(random_x - 128) x2s = int(random_x + 128) y1s = int(random_y - 128) y2s = int(random_y + 128) x1b = int(random_x - w / 2) x2b = int(random_x + w / 2) y1b = int(random_y - h / 2) y2b = int(random_y + h / 2) cover = 0 for j in range(len(bbox_list[img_id])): x1 = int(bbox_list[img_id][j][0]) y1 = int(bbox_list[img_id][j][1]) w = int(bbox_list[img_id][j][2]) h = int(bbox_list[img_id][j][3]) x2 = x1 + w y2 = y1 + h img_ppl = img[y1:y2, x1:x2] left_column_max = max(x1, x1s) right_column_min = min(x2, x2s) up_row_max = max(y1, y1s) down_row_min = min(y2, y2s) if left_column_max >= right_column_min or down_row_min <= up_row_max: cover = 0 else: cover = 1 break if cover == 1: tryout += 1 if tryout > 200: print("no space in this image!") break continue for j in range(len(add_bbox)): x1 = int(add_bbox[j][0]) y1 = int(add_bbox[j][1]) w = int(add_bbox[j][2]) h = int(add_bbox[j][3]) x2 = x1 + w y2 = y1 + h img_ppl = img[y1:y2, x1:x2] left_column_max = max(x1, x1s) right_column_min = min(x2, x2s) up_row_max = max(y1, y1s) down_row_min = min(y2, y2s) if left_column_max >= right_column_min or down_row_min <= up_row_max: cover = 0 else: cover = 1 break if cover == 1: tryout += 1 if tryout > 200: print("no space in this image!") break continue if cover == 0: tryout = 0 roi = img[y1s:y2s, x1s:x2s] bbox = img[y1b:y2b, x1b:x2b] bbox = cv2.cvtColor(bbox, cv2.COLOR_BGR2GRAY) bbox = sp_noise(bbox, 0.5) bbox = cv2.merge([bbox, bbox, bbox]) noise_img = roi.copy() noise_img[y1b - random_y + 128:128 + y2b - random_y, x1b - random_x + 128:128 - random_x + x2b] = bbox img_con = np.concatenate((roi, noise_img), axis=1) AB.append(img_con) dd = { 'x': x1b - random_x + 128, 'y': y1b - random_y + 128, 'w': 128 - random_x + x2b, 'h': 128 + y2b - random_y } bbox_dict.append(dd) add_bbox.append([x1b, y1b, 90, 180]) bbox_list[img_id].append([x1b, y1b, 90, 180]) added += 1 from data.aligned_dataset2 import AlignedDataset aa = AlignedDataset() aa.initialize(opt, AB, bbox_dict) data_loader = torch.utils.data.DataLoader( aa, batch_size=opt.batchSize, shuffle=not opt.serial_batches, num_workers=0) # data_loader = CreateDataLoader(opt) dataset = data_loader for ii, data in enumerate(dataset): model.set_input(data) model.test() x1s = add_bbox[ii][0] y1s = add_bbox[ii][1] ws = add_bbox[ii][2] hs = add_bbox[ii][3] x2s = x1s + ws y2s = y1s + hs visuals = model.get_current_visuals() result = cv2.resize(visuals['D2_fake'], (ws, hs), interpolation=cv2.INTER_CUBIC) img[y1s:y2s, x1s:x2s] = result new_d = { 'iscrowd': 0, 'category_id': 24, 'bbox': [x1s, y1s, ws, hs], 'area': ws * hs, 'segmentation': { 'size': [1024, 2048], 'counts': '' }, 'image_id': img_id, 'id': str(last_id + total) } js['annotations'].append(new_d) #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imwrite(image_path, img) #cv2.imwrite('./image_debug_city/tt.png', img) total += 1 #if total % 100 == 0: print(total) nn += len(add_bbox) print(nn) with open( './cityscapes/annotations/instancesonly_filtered_gtFine_train.json', 'w') as outfile: json.dump(js, outfile)
def runModel(filename, model0, model1, model2, model3, model4, model5, model6, model7): opt = TestOptions().parse() # get test options # hard-code some parameters for test opt.num_threads = 0 # test code only supports num_threads = 1 opt.batch_size = 1 # test code only supports batch_size = 1 opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed. opt.no_flip = True # no flip; comment this line if results on flipped images are needed. opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file. opt.eval = True # For experimentation opt.dataset_mode = 'single' opt.dataroot = './data/pottery/' + filename[:-4] dataset = create_dataset( opt) # create a dataset given opt.dataset_mode and other options for i, data in enumerate(dataset): model0.set_input(data, 0) model1.set_input(data, 1) model2.set_input(data, 2) model3.set_input(data, 3) model4.set_input(data, 4) model5.set_input(data, 5) model6.set_input(data, 6) model7.set_input(data, 7) # obtener shapememory en el model.shapememory f0 = model0.netG(model0.real) f1 = model1.netG(model1.real) f2 = model2.netG(model2.real) f3 = model3.netG(model3.real) f4 = model4.netG(model4.real) f5 = model5.netG(model5.real) f6 = model6.netG(model6.real) f7 = model7.netG(model7.real) shapeMemory = torch.cat((f0, f1, f2, f3, f4, f5, f6, f7), 1) model0.test(shapeMemory) model1.test(shapeMemory) model2.test(shapeMemory) model3.test(shapeMemory) model4.test(shapeMemory) model5.test(shapeMemory) model6.test(shapeMemory) model7.test(shapeMemory) visuals0 = model0.get_current_visuals() visuals1 = model1.get_current_visuals() visuals2 = model2.get_current_visuals() visuals3 = model3.get_current_visuals() visuals4 = model4.get_current_visuals() visuals5 = model5.get_current_visuals() visuals6 = model6.get_current_visuals() visuals7 = model7.get_current_visuals() img_fake0 = util.tensor2im(visuals0['fake']) img_fake1 = util.tensor2im(visuals1['fake']) img_fake2 = util.tensor2im(visuals2['fake']) img_fake3 = util.tensor2im(visuals3['fake']) img_fake4 = util.tensor2im(visuals4['fake']) img_fake5 = util.tensor2im(visuals5['fake']) img_fake6 = util.tensor2im(visuals6['fake']) img_fake7 = util.tensor2im(visuals7['fake']) img_fake = util.get_concat_v(img_fake0, img_fake1, img_fake2, img_fake3, img_fake4, img_fake5, img_fake6, img_fake7) img_fake.save('./data/pottery/send/' + filename) return
def crop_image(image, shapes): h, w = image.shape[:2] if h >= shapes[0] and w >= shapes[1]: h_start = (h - shapes[0]) // 2 w_start = (w - shapes[1]) // 2 image = image[h_start:h_start + shapes[0], w_start:w_start + shapes[1], :] else: t = min(h, w) image = image[(h - t) // 2:(h - t) // 2 + t, (w - t) // 2:(w - t) // 2 + t, :] image = cv2.resize(image, (shapes[1], shapes[0])) return image config = TestOptions().parse() print(config.dataset_path) if os.path.isfile(config.dataset_path): pathfile = open(config.dataset_path, 'rt').read().splitlines() elif os.path.isdir(config.dataset_path): pathfile = glob.glob(os.path.join(config.dataset_path, '*.png')) t = [] for x in pathfile: if '001.png' in x or '002.png' in x or '003.png' in x or '004.png' in x: t.append(x) pathfile = t else: print('Invalid testing data file/folder path.') exit(1) np.random.shuffle(pathfile) total_number = len(pathfile)
def main(): opt = TestOptions().parse() dataloader = data.create_dataloader(opt) model = Pix2PixModel(opt) model.eval() visualizer = Visualizer(opt) # create a webpage that summarizes the all results web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch)) webpage = html.HTML( web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch)) # test ssim = [] acc = [] for i, data_i in enumerate(dataloader): if i * opt.batchSize >= opt.how_many: break generated, masked_image, semantics = model(data_i, mode='inference') if masked_image.shape[1] != 3: masked_image = masked_image[:, :3] img_path = data_i['path'] if opt.bbox: img_path = data_i['image_path'] try: ran = generated.shape[0] except: ran = generated[0].shape[0] for b in range(ran): # print('process image... %s' % img_path[b]) label = data_i['label'][b] if opt.segmentation_mask and not opt.phase == "test": label = semantics[b].unsqueeze(0).max(dim=1)[1] mask = semantics[b][-1] visuals = OrderedDict([('input_label', label), ('synthesized_image', generated[b]), ('real_label', data_i['label'][b]), ('real_image', data_i['image'][b]), ('masked_image', masked_image[b])]) if not opt.no_instance: instance = data_i['instance'][b] if opt.segmentation_mask: instance = semantics[b, 35].unsqueeze(0) visuals['instance'] = instance if opt.tf_log: visualizer.display_current_results(visuals, 200, b, True) else: visualizer.save_images(webpage, visuals, img_path[b:b + 1], i) # Compute SSIM on edited areas pred_img = generated[0].detach().cpu().numpy().transpose(1, 2, 0) gt_img = data_i['image'].float()[0].numpy().transpose(1, 2, 0) pred_img = rgb2gray(pred_img) gt_img = rgb2gray(gt_img) ssim_pic = compare_ssim(gt_img, pred_img, multichannel=False, full=True)[1] mask = data_i['mask_in'][0] ssim.append( np.ma.masked_where(1 - mask.cpu().numpy().squeeze(), ssim_pic).mean()) webpage.save() print(np.mean(ssim))
def main(): #load test arguments opt = TestOptions().parse() opt.device = torch.device("cuda") # network builders builder = ModelBuilder() net_visual = builder.build_visual(weights=opt.weights_visual) net_audio = builder.build_audio(ngf=opt.unet_ngf, input_nc=opt.unet_input_nc, output_nc=opt.unet_output_nc, weights=opt.weights_audio) nets = (net_visual, net_audio) # construct our audio-visual model model = AudioVisualModel(nets, opt) model = torch.nn.DataParallel(model, device_ids=opt.gpu_ids) model.to(opt.device) model.eval() #load the audio to perform separation audio, audio_rate = librosa.load(opt.input_audio_path, sr=opt.audio_sampling_rate, mono=False) audio_channel1 = audio[0, :] audio_channel2 = audio[1, :] #define the transformation to perform on visual frames vision_transform_list = [ transforms.Resize((224, 448)), transforms.ToTensor() ] vision_transform_list.append( transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])) vision_transform = transforms.Compose(vision_transform_list) #perform spatialization over the whole audio using a sliding window approach overlap_count = np.zeros( (audio.shape)) #count the number of times a data point is calculated binaural_audio = np.zeros((audio.shape)) #perform spatialization over the whole spectrogram in a siliding-window fashion sliding_window_start = 0 data = {} samples_per_window = int(opt.audio_length * opt.audio_sampling_rate) while sliding_window_start + samples_per_window < audio.shape[-1]: sliding_window_end = sliding_window_start + samples_per_window normalizer, audio_segment = audio_normalize( audio[:, sliding_window_start:sliding_window_end]) audio_segment_channel1 = audio_segment[0, :] audio_segment_channel2 = audio_segment[1, :] audio_segment_mix = audio_segment_channel1 + audio_segment_channel2 data['audio_diff_spec'] = torch.FloatTensor( generate_spectrogram(audio_segment_channel1 - audio_segment_channel2)).unsqueeze( 0) #unsqueeze to add a batch dimension data['audio_mix_spec'] = torch.FloatTensor( generate_spectrogram(audio_segment_channel1 + audio_segment_channel2)).unsqueeze( 0) #unsqueeze to add a batch dimension #get the frame index for current window frame_index = int( round((((sliding_window_start + samples_per_window / 2.0) / audio.shape[-1]) * opt.input_audio_length + 0.05) * 10)) image = Image.open( os.path.join(opt.video_frame_path, str(frame_index).zfill(6) + '.png')).convert('RGB') #image = image.transpose(Image.FLIP_LEFT_RIGHT) frame = vision_transform(image).unsqueeze( 0) #unsqueeze to add a batch dimension data['frame'] = frame output = model.forward(data) predicted_spectrogram = output['binaural_spectrogram'][ 0, :, :, :].data[:].cpu().numpy() #ISTFT to convert back to audio reconstructed_stft_diff = predicted_spectrogram[0, :, :] + ( 1j * predicted_spectrogram[1, :, :]) reconstructed_signal_diff = librosa.istft(reconstructed_stft_diff, hop_length=160, win_length=400, center=True, length=samples_per_window) reconstructed_signal_left = (audio_segment_mix + reconstructed_signal_diff) / 2 reconstructed_signal_right = (audio_segment_mix - reconstructed_signal_diff) / 2 reconstructed_binaural = np.concatenate( (np.expand_dims(reconstructed_signal_left, axis=0), np.expand_dims(reconstructed_signal_right, axis=0)), axis=0) * normalizer binaural_audio[:, sliding_window_start: sliding_window_end] = binaural_audio[:, sliding_window_start: sliding_window_end] + reconstructed_binaural overlap_count[:, sliding_window_start: sliding_window_end] = overlap_count[:, sliding_window_start: sliding_window_end] + 1 sliding_window_start = sliding_window_start + int( opt.hop_size * opt.audio_sampling_rate) #deal with the last segment normalizer, audio_segment = audio_normalize(audio[:, -samples_per_window:]) audio_segment_channel1 = audio_segment[0, :] audio_segment_channel2 = audio_segment[1, :] data['audio_diff_spec'] = torch.FloatTensor( generate_spectrogram(audio_segment_channel1 - audio_segment_channel2)).unsqueeze( 0) #unsqueeze to add a batch dimension data['audio_mix_spec'] = torch.FloatTensor( generate_spectrogram(audio_segment_channel1 + audio_segment_channel2)).unsqueeze( 0) #unsqueeze to add a batch dimension #get the frame index for last window frame_index = int( round(((opt.input_audio_length - opt.audio_length / 2.0) + 0.05) * 10)) image = Image.open( os.path.join(opt.video_frame_path, str(frame_index).zfill(6) + '.png')).convert('RGB') #image = image.transpose(Image.FLIP_LEFT_RIGHT) frame = vision_transform(image).unsqueeze( 0) #unsqueeze to add a batch dimension data['frame'] = frame output = model.forward(data) predicted_spectrogram = output['binaural_spectrogram'][ 0, :, :, :].data[:].cpu().numpy() #ISTFT to convert back to audio reconstructed_stft_diff = predicted_spectrogram[0, :, :] + ( 1j * predicted_spectrogram[1, :, :]) reconstructed_signal_diff = librosa.istft(reconstructed_stft_diff, hop_length=160, win_length=400, center=True, length=samples_per_window) reconstructed_signal_left = (audio_segment_mix + reconstructed_signal_diff) / 2 reconstructed_signal_right = (audio_segment_mix - reconstructed_signal_diff) / 2 reconstructed_binaural = np.concatenate( (np.expand_dims(reconstructed_signal_left, axis=0), np.expand_dims(reconstructed_signal_right, axis=0)), axis=0) * normalizer #add the spatialized audio to reconstructed_binaural binaural_audio[:, -samples_per_window:] = binaural_audio[:, -samples_per_window:] + reconstructed_binaural overlap_count[:, -samples_per_window:] = overlap_count[:, -samples_per_window:] + 1 #divide aggregated predicted audio by their corresponding counts predicted_binaural_audio = np.divide(binaural_audio, overlap_count) #check output directory if not os.path.isdir(opt.output_dir_root): os.mkdir(opt.output_dir_root) mixed_mono = (audio_channel1 + audio_channel2) / 2 librosa.output.write_wav( os.path.join(opt.output_dir_root, 'predicted_binaural.wav'), predicted_binaural_audio, opt.audio_sampling_rate) librosa.output.write_wav( os.path.join(opt.output_dir_root, 'mixed_mono.wav'), mixed_mono, opt.audio_sampling_rate) librosa.output.write_wav( os.path.join(opt.output_dir_root, 'input_binaural.wav'), audio, opt.audio_sampling_rate)
def main(): global mousePressed, mousePressedLocation, mousePressedOffset, lastMouseLocation, brushSize, selectedSemantic, semantics # Create a figure to draw to and connect the mouse functions fig = plt.figure() fig.canvas.mpl_connect('button_press_event', onpress) fig.canvas.mpl_connect('button_release_event', onrelease) fig.canvas.mpl_connect('motion_notify_event', mousemove) fig.canvas.mpl_connect('scroll_event', scrollevent) fig.canvas.mpl_connect('key_press_event', onkey) # Create the image that is drawn to the figure imgData=np.zeros((256, 256)) img = plt.imshow(imgData); plt.pause(0.05) # First, load the model opt = TestOptions().parse() dataloader = data.create_dataloader(opt) model = Pix2PixModel(opt) model.eval() visualizer = Visualizer(opt) # Create a webpage that summarizes the all results web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch)) webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch)) # Get the format of the input data inData = None for i, data_i in enumerate(dataloader): inData = data_i break # Initially, Set the input to zero inData['image'][:,:,:,:] = 0.0; # initially, set the labels to zero inData['label'][0,0,:,:] = 0 tmpImg = np.zeros((1,1,256,256), dtype=np.uint8); # While the program is running, generate imagery from input while True: # Update the mouse location if mousePressed: # Doesn't work: # Get current location # cursorPos = win32gui.GetCursorPos() # mouseLocation = [cursorPos[0] - mousePressedLocation[0] + mousePressedOffset[0], cursorPos[1] - mousePressedLocation[1] + mousePressedOffset[1]] # Print the last mouse location # print(lastMouseLocation) # Draw any mouse movements to the input image # inData['label'][0,0] = 0 cv2.circle(tmpImg[0,0], (int(lastMouseLocation[0]),int(lastMouseLocation[1])), brushSize, (semantics[selectedSemantic][0]-1), -1) inData['label'] = torch.tensor(tmpImg) # data_i['label'] # Run a forward pass through the model to "infer" the output image generated = model(inData, mode='inference') img_path = inData['path'] for b in range(generated.shape[0]): # Extract the visuals # print('process image... %s' % img_path[b]) visuals = OrderedDict([('input_label', data_i['label'][b]),('synthesized_image', generated[b])]) # Draw the image imgData = visualizer.convert_visuals_to_numpy(visuals)['synthesized_image']; # Draw the cursor location and size cv2.circle(imgData, (int(lastMouseLocation[0]),int(lastMouseLocation[1])), brushSize, (255, 0, 0), 2) img.set_data(imgData); # img.set_data(tmpImg[0,0]) plt.pause(0.05)
def run(): test_opts = TestOptions().parse() if test_opts.resize_factors is not None: assert len( test_opts.resize_factors.split(',') ) == 1, "When running inference, provide a single downsampling factor!" out_path_results = os.path.join( test_opts.exp_dir, 'inference_results', 'downsampling_{}'.format(test_opts.resize_factors)) else: out_path_results = os.path.join(test_opts.exp_dir, 'inference_results') os.makedirs(out_path_results, exist_ok=True) out_path_coupled = None if test_opts.couple_outputs: if test_opts.resize_factors is not None: out_path_coupled = os.path.join( test_opts.exp_dir, 'inference_coupled', 'downsampling_{}'.format(test_opts.resize_factors)) else: out_path_coupled = os.path.join(test_opts.exp_dir, 'inference_coupled') os.makedirs(out_path_coupled, exist_ok=True) # update test options with options used during training ckpt = torch.load(test_opts.checkpoint_path, map_location='cpu') opts = ckpt['opts'] opts.update(vars(test_opts)) if 'learn_in_w' not in opts: opts['learn_in_w'] = False opts = Namespace(**opts) net = pSp(opts) net.eval() net.cuda() print('Loading dataset for {}'.format(opts.dataset_type)) dataset_args = data_configs.DATASETS[opts.dataset_type] transforms_dict = dataset_args['transforms'](opts).get_transforms() dataset = InferenceDataset( root=opts.data_path, transform=transforms_dict['transform_inference'], opts=opts) dataloader = DataLoader(dataset, batch_size=opts.test_batch_size, shuffle=False, num_workers=int(opts.test_workers), drop_last=True) global_i = 0 global_time = [] for input_batch in tqdm(dataloader): with torch.no_grad(): input_cuda = input_batch.cuda().float() tic = time.time() result_batch = run_on_batch(input_cuda, net, opts) toc = time.time() global_time.append(toc - tic) for i in range(opts.test_batch_size): result = tensor2im(result_batch[i]) im_path = dataset.paths[global_i] if opts.couple_outputs or global_i % 100 == 0: input_resized = log_input_image(input_batch[i], opts) if opts.resize_factors is not None: # for super resolution, save the original, down-sampled, and output source = Image.open(im_path) res = np.concatenate([ np.array(source.resize((256, 256))), np.array( input_resized.resize( (256, 256), resample=Image.NEAREST)), np.array(result.resize((256, 256))) ], axis=1) else: # otherwise, save the original and output res = np.concatenate([ np.array(input_resized.resize((256, 256))), np.array(result.resize((256, 256))) ], axis=1) Image.fromarray(res).save( os.path.join(out_path_coupled, os.path.basename(im_path))) im_save_path = os.path.join(out_path_results, os.path.basename(im_path)) Image.fromarray(np.array(result.resize( (256, 256)))).save(im_save_path) global_i += 1 stats_path = os.path.join(opts.exp_dir, 'stats.txt') result_str = 'Runtime {:.4f}+-{:.4f}'.format(np.mean(global_time), np.std(global_time)) print(result_str) with open(stats_path, 'w') as f: f.write(result_str)
If the upsampling module isn't trained (train.py is used with '--n_epochs_upsample 0'), remove --do_upsampling. Use '--results_dir <directory_path_to_save_result>' to specify the results directory. See options/base_options.py and options/test_options.py for more test options. """ import os from options.test_options import TestOptions from third_party.data import create_dataset from third_party.models import create_model from third_party.util.visualizer import save_images, save_videos from third_party.util import html import torch if __name__ == '__main__': testopt = TestOptions() testopt.parse() opt = testopt.parse_dataset_meta() # hard-code some parameters for test opt.num_threads = 0 # test code only supports num_threads = 0 opt.batch_size = 1 # test code only supports batch_size = 1 opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed. opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file. dataset = create_dataset( opt) # create a dataset given opt.dataset_mode and other options model = create_model( opt) # create a model given opt.model and other options model.setup( opt) # regular setup: load and print networks; create schedulers # create a website web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(
# coding=utf-8 import random import torch import torch.nn as nn from tqdm import tqdm import torch.nn.parallel import torch.backends.cudnn as cudnn from datasets import ImageFiles from models import GANModel from options.test_options import TestOptions opt = TestOptions().parse() # set CUDA_VISIBLE_DEVICES before import torch opt.manualSeed = random.randint(1, 10000) print("Random Seed: ", opt.manualSeed) random.seed(opt.manualSeed) torch.manual_seed(opt.manualSeed) cudnn.benchmark = True opt.mean = [0.5, 0.5, 0.5] opt.std = [0.5, 0.5, 0.5] ngpu = len(opt.gpu_ids) nz = 100 ngf = 64 ndf = 64 model = GANModel(opt, nz, ngf, ndf) model.load(99000)
from options.test_options import TestOptions from data import create_dataset from models import create_model from util.visualizer import save_segment_result from util.metrics import RunningScore import time import numpy as np if __name__ == '__main__': opt_val = TestOptions().parse() dataset_val = create_dataset(opt_val) dataset_val_size = len(dataset_val) print('The number of valling images = %d' % dataset_val_size) model_val = create_model(opt_val) model_val.eval() metrics = RunningScore(opt_val.num_classes) model_val.opt.epoch = 114 model_val.setup(model_val.opt) for i, data in enumerate(dataset_val): model_val.set_input(data) model_val.forward() gt = np.squeeze(data["label"].numpy(), axis=1) # [N, W, H] pre = model_val.pre pre = pre.data.max(1)[1].cpu().numpy() # [N, W, H] metrics.update(gt, pre)
def main(): opt = TestOptions().parse() if opt.data == "KTH": lims_ssim = [1, opt.T, 0.6, 1] lims_psnr = [1, opt.T, 20, 34] else: raise ValueError('Dataset [%s] not recognized.' % opt.data) data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() dataset_size = len(data_loader) print('# testing videos = %d' % dataset_size) model = create_model(opt) psnr_err = np.zeros((0, opt.T)) ssim_err = np.zeros((0, opt.T)) for i, datas in enumerate(dataset): if opt.pick_mode == 'First': datas = [datas] for data in datas: model.set_inputs(data) model.forward() if len(opt.gpu_ids) > 0: seq_batch = data['targets'].cpu().numpy().transpose(0, 2, 3, 4, 1) pred = [a.data.cpu().numpy().transpose((0, 2, 3, 1)) for a in model.pred] else: seq_batch = data['targets'].numpy().transpose(0, 2, 3, 4, 1) pred = [a.data.numpy().transpose((0, 2, 3, 1)) for a in model.pred] pred_data = np.stack(pred, axis=3) true_data = seq_batch[:,:,:,opt.K:opt.K + opt.T,:].copy() pred_data = np.concatenate((seq_batch[:, :, :, :opt.K, :], pred_data), axis=3) true_data = np.concatenate((seq_batch[:, :, :, :opt.K, :], true_data), axis=3) cpsnr = np.zeros((opt.K + opt.T,)) cssim = np.zeros((opt.K + opt.T,)) for t in range(opt.K + opt.T): pred = (inverse_transform(pred_data[0, :, :, t]) * 255).astype("uint8") target = (inverse_transform(true_data[0, :, :, t]) * 255).astype("uint8") if opt.c_dim == 1: pred = np.squeeze(pred, axis=-1) target = np.squeeze(target, axis=-1) cpsnr[t] = measure.compare_psnr(pred, target) # pdb.set_trace() cssim[t] = ssim(target, pred) # cssim[t] = ssim(Image.fromarray(target),Image.fromarray(pred)) if opt.c_dim == 1: pred = np.expand_dims(pred, axis=-1) target = np.expand_dims(target, axis=-1) pred = draw_frame(pred, t < opt.K) target = draw_frame(target, t < opt.K) savedir = os.path.join(opt.save_dir, data['video_name'][0].split('.')[0] + '_' + data['start-end'][0]) makedir(savedir) cv2.imwrite(savedir + "/pred_" + "{0:04d}".format(t) + ".png", pred) cv2.imwrite(savedir + "/gt_" + "{0:04d}".format(t) + ".png", target) cmd1 = "rm " + savedir + "/pred.gif" cmd2 = ("ffmpeg -f image2 -framerate 3 -i " + savedir + "/pred_%04d.png " + savedir + "/pred.gif") cmd3 = "rm " + savedir + "/pred*.png" # Comment out "system(cmd3)" if you want to keep the output images # Otherwise only the gifs will be kept system(cmd1); system(cmd2) system(cmd3) cmd1 = "rm " + savedir + "/gt.gif" cmd2 = ("ffmpeg -f image2 -framerate 3 -i " + savedir + "/gt_%04d.png " + savedir + "/gt.gif") cmd3 = "rm " + savedir + "/gt*.png" # Comment out "system(cmd3)" if you want to keep the output images # Otherwise only the gifs will be kept system(cmd1); system(cmd2) system(cmd3) psnr_err = np.concatenate((psnr_err, cpsnr[None, opt.K:]), axis=0) ssim_err = np.concatenate((ssim_err, cssim[None, opt.K:]), axis=0) save_path = os.path.join(opt.quant_dir, 'results_model=' + opt.name + '_' + opt.which_epoch + '.npz') np.savez(save_path, psnr=psnr_err, ssim=ssim_err) psnr_plot = os.path.join(opt.quant_dir, 'results_model=' + opt.name + '_' + opt.which_epoch + 'psnr.png') draw_err_plot(psnr_err, 'Peak Signal to Noise Ratio', path=psnr_plot, lims=lims_psnr, type="Test") ssim_plot = os.path.join(opt.quant_dir, 'results_model=' + opt.name + '_' + opt.which_epoch + 'ssim.png') draw_err_plot(psnr_err, 'Structural Similarity', path=ssim_plot, lims=lims_ssim, type="Test") print("Results saved to " + save_path) print("Done.")
import matplotlib matplotlib.use('Agg') import os from options.test_options import TestOptions from data.data_loader import CreateDataLoader from models.models import create_model from util.visualizer import Visualizer from util import html opt = TestOptions().parse() opt.nThreads = 1 # test code only supports nThreads = 1 opt.batchSize = 1 # test code only supports batchSize = 1 opt.serial_batches = True # no shuffle opt.no_flip = True # no flip data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() model = create_model(opt) visualizer = Visualizer(opt) # create website web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch)) webpage = html.HTML( web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch)) # test for i, data in enumerate(dataset): if i >= opt.how_many: break model.set_input(data)
def test(number): # Variables for Options RESULTS_DIR = './results/edges2handbags_2' CLASS = 'edges2handbags' DIRECTION = 'AtoB' # from domain A to domain B LOAD_SIZE = 256 # scale images to this size CROP_SIZE = 256 # then crop to this size INPUT_NC = 1 # number of channels in the input image # misc GPU_ID = -1 # gpu id NUM_TEST = 1 # number of input images duirng test NUM_SAMPLES = 20 # number of samples per input images # options opt = TestOptions().parse() opt.num_threads = 1 # test code only supports num_threads=1 opt.batch_size = 1 # test code only supports batch_size=1 opt.serial_batches = True # no shuffle # Options Added opt.dataroot = './datasets/edges2handbags' opt.results_dir = RESULTS_DIR opt.checkpoints_dir = './pretrained_models/' opt.name = CLASS opt.direction = DIRECTION opt.load_size = LOAD_SIZE opt.crop_size = CROP_SIZE opt.input_nc = INPUT_NC opt.num_test = NUM_TEST opt.n_samples = NUM_SAMPLES # create dataset dataset = create_dataset(opt) model = create_model(opt) model.setup(opt) model.eval() print('Loading model %s' % opt.model) print(dataset) # create website web_dir = os.path.join(opt.results_dir, opt.phase + '_sync' if opt.sync else opt.phase) webpage = html.HTML( web_dir, 'Training = %s, Phase = %s, Class =%s' % (opt.name, opt.phase, opt.name)) # sample random z if opt.sync: z_samples = model.get_z_random(opt.n_samples + 1, opt.nz) # test stage #for i, data in enumerate(islice(dataset, opt.num_test)): for i, data in enumerate(islice(dataset, number, number + opt.num_test)): print(data) model.set_input(data) print('process input image %3.3d/%3.3d' % (i + 1, opt.num_test)) if not opt.sync: z_samples = model.get_z_random(opt.n_samples + 1, opt.nz) for nn in range(opt.n_samples + 1): encode = nn == 0 and not opt.no_encode real_A, fake_B, real_B = model.test(z_samples[[nn]], encode=encode) if nn == 0: images = [real_A, real_B, fake_B] #names = ['input' + str(number), 'ground truth' + str(number), 'encoded'] names = [str(99999), str(9999), '999'] else: images.append(fake_B) #names.append('random_sample%2.2d' % nn) names.append(nn) img_path = str(number + 101) #img_path = 'input_%3.3d' % (number + 101) #img_path = '' print(img_path) save_images(webpage, images, names, img_path, aspect_ratio=opt.aspect_ratio, width=opt.crop_size) webpage.save()
file.create_dataset(f"labels/{img_path[0]}_{index}", data=gt) file.create_dataset(f"labels_with_skull/{img_path[0]}_{index}", data=label.cpu().numpy()) file.create_dataset( f"reference_real_image_please_dont_use/{img_path[0]}_{index}", data=real_img.cpu().numpy()) # misc.imsave(f"imgs/test/{img_path[0]}_{index}_img.png", # np.moveaxis(syn_img.cpu().squeeze().numpy(), 0, -1)) # misc.imsave(f"imgs/test/{img_path[0]}_{index}_seg.png",gt) # misc.imsave(f"imgs/test/{img_path[0]}_{index}_label.png", # np.moveaxis(label.cpu().squeeze().numpy(), 0, -1)) if __name__ == '__main__': opt = TestOptions().parse() # get test options # hard-code some parameters for test opt.num_threads = 0 # test code only supports num_threads = 1 # opt.batch_size = 1 # test code only supports batch_size = 1 opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed. opt.no_flip = True # no flip; comment this line if results on flipped images are needed. opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file. dataset = create_dataset( opt) # create a dataset given opt.dataset_mode and other options model = create_model( opt) # create a model given opt.model and other options model.setup( opt) # regular setup: load and print networks; create schedulers # create a website web_dir = os.path.join( opt.results_dir, opt.name,
def run(): test_opts = TestOptions().parse() if test_opts.resize_factors is not None: factors = test_opts.resize_factors.split(',') assert len(factors) == 1, "When running inference, please provide a single downsampling factor!" mixed_path_results = os.path.join(test_opts.exp_dir, 'style_mixing', 'downsampling_{}'.format(test_opts.resize_factors)) else: mixed_path_results = os.path.join(test_opts.exp_dir, 'style_mixing') os.makedirs(mixed_path_results, exist_ok=True) # update test options with options used during training ckpt = torch.load(test_opts.checkpoint_path, map_location='cpu') opts = ckpt['opts'] opts.update(vars(test_opts)) if 'learn_in_w' not in opts: opts['learn_in_w'] = False if 'output_size' not in opts: opts['output_size'] = 1024 opts = Namespace(**opts) net = pSp(opts) net.eval() net.cuda() print('Loading dataset for {}'.format(opts.dataset_type)) dataset_args = data_configs.DATASETS[opts.dataset_type] transforms_dict = dataset_args['transforms'](opts).get_transforms() dataset = InferenceDataset(root=opts.data_path, transform=transforms_dict['transform_inference'], opts=opts) dataloader = DataLoader(dataset, batch_size=opts.test_batch_size, shuffle=False, num_workers=int(opts.test_workers), drop_last=True) latent_mask = [int(l) for l in opts.latent_mask.split(",")] if opts.n_images is None: opts.n_images = len(dataset) global_i = 0 for input_batch in tqdm(dataloader): if global_i >= opts.n_images: break with torch.no_grad(): input_batch = input_batch.cuda() for image_idx, input_image in enumerate(input_batch): # generate random vectors to inject into input image vecs_to_inject = np.random.randn(opts.n_outputs_to_generate, 512).astype('float32') multi_modal_outputs = [] for vec_to_inject in vecs_to_inject: cur_vec = torch.from_numpy(vec_to_inject).unsqueeze(0).to("cuda") # get latent vector to inject into our input image _, latent_to_inject = net(cur_vec, input_code=True, return_latents=True) # get output image with injected style vector res = net(input_image.unsqueeze(0).to("cuda").float(), latent_mask=latent_mask, inject_latent=latent_to_inject, alpha=opts.mix_alpha, resize=opts.resize_outputs) multi_modal_outputs.append(res[0]) # visualize multi modal outputs input_im_path = dataset.paths[global_i] image = input_batch[image_idx] input_image = log_input_image(image, opts) resize_amount = (256, 256) if opts.resize_outputs else (opts.output_size, opts.output_size) res = np.array(input_image.resize(resize_amount)) os.makedirs('projection_animation', exist_ok=True) i=0 for output in multi_modal_outputs: output = tensor2im(output) i +=1 res = np.concatenate([res, np.array(output.resize(resize_amount))], axis=1) res2 = output.resize(resize_amount) res2.save(f'projection_animation/{i+1}.jpg') Image.fromarray(res).save(os.path.join(mixed_path_results, os.path.basename(input_im_path))) global_i += 1