def val(opt): image_1_path = opt.image1_path image_2_path = opt.image2_path A_img = Image.open(image_1_path).convert('RGB') B_img = Image.open(image_2_path).convert('RGB') trans = transform() A = trans(A_img).unsqueeze(0) B = trans(B_img).unsqueeze(0) # 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 save_path = opt.results_dir mkdir(save_path) model.eval() data = {} data['A'] = A data['B'] = B data['A_paths'] = [image_1_path] model.set_input(data) # unpack data from data loader pred = model.test(val=False) # run inference return pred img_path = [image_1_path] # get image paths save_images(pred, save_path, img_path)
def main(): # step1: opt opt = TestOptions().parse() # step2: data data_loader = CreateDataLoader(opt) dataset = data_loader.load_data() # step3: model model = create_model(opt) model.setup(opt) # step4: web ,在test中不使用visdom,而是使用html web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch)) title = 'Experiment = {}, Phase = {}, Epoch = {}'.format( opt.name, opt.phase, opt.epoch) webpage = HTML(web_dir, title=title) # test with eval mode. This only affects layers like batchnorm and dropout. # pix2pix: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode. # CycleGAN: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout. model.eval() for i, data in enumerate(dataset): if i > opt.num_test: break model.set_input(data) model.test() visuals = model.get_current_visuals() img_path = model.get_images_paths() if i % 5 == 0: print('processing {:0>4d}-th image...{}'.format(i, img_path)) save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize) webpage.save()
def val(opt): # create a dataset given opt.dataset_mode and other options dataset = create_dataset(opt) # create a model given opt.model and other options model = create_model(opt) # regular setup: load and print networks; create schedulers model.setup(opt) save_path = opt.results_dir mkdir(save_path) model.eval() for i, data in enumerate(dataset): if i >= opt.num_test: # only apply our model to opt.num_test images. break model.set_input(data) # unpack data from data loader pred = model.test(val=False) # run inference return pred img_path = model.get_image_paths() # get image paths if i % 5 == 0: # save images to an HTML file print('processing (%04d)-th image... %s' % (i, img_path)) save_images(pred, save_path, img_path)
test_loss_iter = [] epoch_iter = 0 conf_mat = np.zeros( (dataset.dataset.num_labels, dataset.dataset.num_labels), dtype=np.float) with torch.no_grad(): for i, data in enumerate(dataset): model.set_input(data) model.forward() model.get_loss() epoch_iter += opt.batch_size gt = model.label.cpu().int().numpy() _, pred = torch.max(model.output.data.cpu(), 1) pred = pred.float().detach().int().numpy() save_images(save_dir, model.get_current_visuals(), model.get_image_names(), model.get_image_oriSize(), opt.prob_map) # Resize images to the original size for evaluation image_size = model.get_image_oriSize() oriSize = (image_size[0].item(), image_size[1].item()) gt = np.expand_dims(cv2.resize(np.squeeze(gt, axis=0), oriSize, interpolation=cv2.INTER_NEAREST), axis=0) pred = np.expand_dims(cv2.resize(np.squeeze(pred, axis=0), oriSize, interpolation=cv2.INTER_NEAREST), axis=0) conf_mat += confusion_matrix(gt, pred, dataset.dataset.num_labels)
test_loss_iter = [] epoch_iter = 0 conf_mat = np.zeros( (dataset.dataset.num_labels, dataset.dataset.num_labels), dtype=np.float) with torch.no_grad(): for i, data in enumerate(dataset): model.set_input(data) model.forward() model.get_loss() epoch_iter += opt.batch_size gt = model.label.cpu().int().numpy() _, pred = torch.max(model.output.data.cpu(), 1) pred = pred.float().detach().int().numpy() save_images(save_dir, model.get_current_visuals(), model.get_image_names(), model.get_image_oriSize()) # Resize images to the original size for evaluation image_size = model.get_image_oriSize() oriSize = (image_size[0].item(), image_size[1].item()) gt = np.expand_dims(cv2.resize(np.squeeze(gt, axis=0), oriSize, interpolation=cv2.INTER_NEAREST), axis=0) pred = np.expand_dims(cv2.resize(np.squeeze(pred, axis=0), oriSize, interpolation=cv2.INTER_NEAREST), axis=0) conf_mat += confusion_matrix(gt, pred, dataset.dataset.num_labels) test_loss_iter.append(model.loss_segmentation)
default=512, help='crop size of test images', type=int) parser.add_argument('--name', default='nature2painting', help='name of experiment, also where model is stored', type=str) args = parser.parse_args() dataroot = args.dataroot results_dir = args.result_dir load_size = args.load_size crop_size = args.crop_size name = args.name dataset = create_dataset(dataroot, batch_size=1, phase='test', load_size=load_size, crop_size=crop_size, serial_batches=True, input_nc=3, output_nc=3, no_flip = True) model = create_model(isTrain=False, name=name, model='test') model.setup() for i, data in enumerate(tqdm(dataset)): model.set_input(data) model.test() visuals = model.get_current_visuals() img_path = model.get_image_paths() save_images(visuals, results_dir, img_path, name=str(i))
import pdb # modify some of the arguments 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( ) # load_date actually returns the dataloader provided by pytorch model = create_model(opt) # test for i, data in enumerate(dataset): if i >= opt.ntest: break print 'Testing progress: {}/{}'.format(i + 1, len(dataset)) model.set_input(data) model.test( ) # same implementation as model.forward(), but without gradient backprop visuals = model.get_current_visuals() # returns real_A, fake_B, and real_B img_path = model.get_image_paths() save_path = os.path.join(opt.results_dir, opt.name) save_images(visuals, save_path, img_path)
from models import create_model from util.util import save_images 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 = 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.no_flip = True # no flip; comment this line if results on flipped images are needed. 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 # test with eval mode. This only affects layers like batchnorm and dropout. # For [pix2pix]: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode. img_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch)) if opt.eval: model.eval() for i, data in enumerate(dataset): if i >= opt.num_test: # only apply our model to opt.num_test images. break model.set_input(data) # unpack data from data loader model.test() # run inference visuals = model.get_current_visuals() # get image results img_path = model.get_image_paths() # get image paths save_images(visuals, img_path, img_dir)
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. dataset = create_dataset( opt) # create a dataset given opt.dataset_mode and other options model = CycleGANModel( opt) # create a model given opt.model and other options model.setup( opt) # regular setup: load and print networks; create schedulers # create results dir image_dir = create_results_dir(opt) # test with eval mode. This only affects layers like batchnorm and dropout. # For [CycleGAN]: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout. if opt.eval: model.eval() for i, data in enumerate(dataset): if i >= opt.num_test: # only apply our model to opt.num_test images. break model.set_input(data) # unpack data from data loader model.test() # run inference visuals = model.get_current_visuals() # get image results img_path = model.get_image_paths() # get image paths if i % 5 == 0: # save images to an HTML file print('processing (%04d)-th image... %s' % (i, img_path)) save_images(opt, image_dir, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)