How to use optional_out method in pandera

Best Python code snippet using pandera_python

validation.py

Source:validation.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2import os3import numpy as np4from deep_image_prior import DeepImagePriorReconstructor5from copy import deepcopy6def val_sub_sub_path(i, i_sample):7 sub_sub_path_sample = os.path.join('rep_{:d}'.format(i),8 'sample_{:d}'.format(i_sample))9 return sub_sub_path_sample10def reconstruct(noisy_obs, fbp, gt, ray_trafo, save_val_sub_path, cfg, cfg_mdl_val):11 """12 Run a DIP validation reconstruction.13 Parameters14 ----------15 noisy_observation : :class:`torch.Tensor`16 Noisy observation.17 fbp : :class:`torch.Tensor`18 Input reconstruction (e.g. filtered backprojection).19 ground_truth : :class:`torch.Tensor`20 Ground truth image.21 ray_trafo : dict22 Dictionary with the following entries:23 `'ray_trafo_module'` : :class:`torch.nn.Module`24 Ray transform module.25 `'reco_space'` : :class:`odl.DiscretizedSpace`26 Image domain.27 `'observation_space'` : :class:`odl.DiscretizedSpace`28 Observation domain.29 save_val_sub_path : str30 Path to append to `cfg.save_histories_path` and31 `cfg.save_iterates_path` (if those are specified, respectively).32 This allows to store the results for multiple reconstructions within33 the validation run path.34 cfg : :class:`omegaconf.OmegaConf`35 Full configuration of the run. Note that ``cfg.mdl`` is ignored and36 instead `cfg_mdl_val` is used as the model configuration.37 cfg_mdl_val : :class:`omegaconf.OmegaConf`38 Configuration of the model.39 Returns40 -------41 reco : :class:`numpy.ndarray`42 The reconstruction.43 psnr_history : list of scalar values44 PSNR history.45 """46 reconstructor = DeepImagePriorReconstructor(**ray_trafo, cfg=cfg_mdl_val)47 reco, *optional_out = reconstructor.reconstruct(48 noisy_obs, fbp, gt,49 return_histories=True,50 return_iterates=cfg.save_iterates_path is not None)51 psnr_history = optional_out[0]['psnr']52 if cfg.save_histories_path is not None:53 histories = {k: np.array(v, dtype=np.float32)54 for k, v in optional_out[0].items()}55 save_histories_path = os.path.join(56 cfg.save_histories_path, save_val_sub_path)57 os.makedirs(save_histories_path, exist_ok=True)58 np.savez(os.path.join(save_histories_path, 'histories.npz'),59 **histories)60 if cfg.save_iterates_path is not None:61 iterates = optional_out[1]62 iterates_iters = optional_out[2]63 save_iterates_path = os.path.join(64 cfg.save_iterates_path, save_val_sub_path)65 os.makedirs(save_iterates_path, exist_ok=True)66 np.savez_compressed(67 os.path.join(save_iterates_path, 'iterates.npz'),68 iterates=np.asarray(iterates),69 iterates_iters=iterates_iters)70 return reco, psnr_history71def validate_model(val_dataset, ray_trafo, seed, val_sub_path_mdl, baseline_psnr_steady, log_path_base, cfg, cfg_mdl_val):72 """73 Validate a model on a validation dataset with repeated runs.74 Parameters75 ----------76 val_dataset : :class:`torch.utils.data.TensorDataset`77 Validation dataset, as returned by :func:`dataset.get_validation_data`.78 ray_trafo : dict79 Dictionary with the following entries:80 `'ray_trafo_module'` : :class:`torch.nn.Module`81 Ray transform module.82 `'reco_space'` : :class:`odl.DiscretizedSpace`83 Image domain.84 `'observation_space'` : :class:`odl.DiscretizedSpace`85 Observation domain.86 seed : int87 Initial seed for the model. In each repetition the seed is set by88 ``cfg_mdl_val.torch_manual_seed = seed + i``, where89 ``i in range(cfg.val.num_repeats)``.90 val_sub_path_mdl : str91 Path to append to `cfg.save_histories_path`,92 `cfg.save_iterates_path` (if those are specified, respectively) and93 `log_path_base` to identify the model. This allows to store the results94 for models within the validation run path. Inside the path specific to95 the model, the results of each individual run are saved in96 ``val_sub_sub_path(i, i_sample)``, where97 ``i in range(cfg.val.num_repeats)`` and98 ``i_sample in range(len(val_dataset))``.99 baseline_psnr_steady : float or `'own_PSNR_steady'`100 Steady PSNR of a baseline, by which ``info['rise_time_to_baseline']``101 is determined. If it is `'own_PSNR_steady'`, then102 ``info['PSNR_steady']`` is used. If it is `None`, also103 ``info['rise_time_to_baseline']`` is set to `None`.104 log_path_base : str105 Base path under which to save the tensorboard logs.106 cfg : :class:`omegaconf.OmegaConf`107 Full configuration of the run. Note that ``cfg.mdl`` is ignored and108 instead `cfg_mdl_val` is used as the model configuration.109 cfg_mdl_val : :class:`omegaconf.OmegaConf`110 Configuration of the model. This function will override111 ``cfg_mdl_val.torch_manual_seed`` and ``cfg_mdl_val.log_path`` (in a112 copy of the configuration).113 Returns114 -------115 psnr_histories : list of lists of lists of scalar values116 PSNR histories of all runs.117 The PSNR history of repetition `i` on validation sample `i_sample` is118 given by ``psnr_histories[i][i_sample]``.119 info : dict120 Validation info about the model. It is based on the median PSNR history121 that is the point-wise median w.r.t. all repetitions and validation122 samples, i.e. ``np.median(psnr_histories, axis=(0, 1))``.123 `'rise_time'` : int124 The first iteration where the median PSNR reaches125 ``info['PSNR_steady'] - cfg.val.rise_time_remaining_psnr``.126 `'rise_time_to_baseline'` : int or None127 The first iteration where the median PSNR reaches128 ``baseline_psnr_steady - cfg.val.rise_time_to_baseline_remaining_psnr``,129 or `None` if `baseline_psnr_steady` is `None`.130 `'PSNR_steady'` : scalar131 The steady PSNR, determined as the median of the median PSNR132 history in the configured interval, i.e.133 ``np.median(np.median(psnr_histories, axis=(0, 1))[cfg.val.psnr_steady_start:cfg.val.psnr_steady_stop])``.134 `'PSNR_0'`: scalar135 The initial median PSNR, i.e.136 ``np.median(psnr_histories, axis=(0, 1))[0]``.137 """138 cfg_mdl_val = deepcopy(cfg_mdl_val)139 psnr_histories = []140 for i in range(cfg.val.num_repeats):141 psnr_histories_i = []142 for i_sample, (noisy_obs, fbp, *gt) in enumerate(val_dataset):143 gt = gt[0] if gt else None144 if cfg.val.load_histories_from_run_path is not None:145 load_histories_path = os.path.join(146 cfg.val.load_histories_from_run_path,147 cfg.save_histories_path,148 val_sub_path_mdl,149 val_sub_sub_path(i=i, i_sample=i_sample))150 psnr_history = np.load(os.path.join(load_histories_path, 'histories.npz'))['psnr'].tolist()151 else:152 cfg_mdl_val.torch_manual_seed = seed + i153 cfg_mdl_val.log_path = os.path.join(154 log_path_base,155 val_sub_path_mdl,156 val_sub_sub_path(i=i, i_sample=i_sample))157 save_val_sub_path = os.path.join(158 val_sub_path_mdl,159 val_sub_sub_path(i=i, i_sample=i_sample))160 _, psnr_history = reconstruct(161 noisy_obs=noisy_obs.float().unsqueeze(dim=0),162 fbp=fbp.unsqueeze(dim=0), gt=gt.unsqueeze(dim=0),163 ray_trafo=ray_trafo,164 save_val_sub_path=save_val_sub_path,165 cfg=cfg, cfg_mdl_val=cfg_mdl_val)166 psnr_histories_i.append(psnr_history)167 psnr_histories.append(psnr_histories_i)168 median_psnr_output = np.median(psnr_histories, axis=(0, 1))169 psnr_steady = np.median(median_psnr_output[170 cfg.val.psnr_steady_start:cfg.val.psnr_steady_stop])171 rise_time = int(np.argwhere(172 median_psnr_output > psnr_steady - cfg.val.rise_time_remaining_psnr)[0][0])173 if baseline_psnr_steady == 'own_PSNR_steady':174 baseline_psnr_steady = psnr_steady175 if baseline_psnr_steady is None:176 rise_time_to_baseline = None177 else:178 argwhere_close_enough_to_baseline = np.argwhere(179 median_psnr_output > baseline_psnr_steady - cfg.val.rise_time_to_baseline_remaining_psnr)180 rise_time_to_baseline = (181 int(argwhere_close_enough_to_baseline[0][0])182 if len(argwhere_close_enough_to_baseline) >= 1 else None)183 info = {'rise_time': rise_time,184 'rise_time_to_baseline': rise_time_to_baseline,185 'PSNR_steady': psnr_steady,186 'PSNR_0': median_psnr_output[0]}...

Full Screen

Full Screen

blas_test.py

Source:blas_test.py Github

copy

Full Screen

1#!/usr/bin/env python32# Copyright (c) 2020 Graphcore Ltd. All rights reserved.3import pytest4import torch5import poptorch6import helpers7def blas_op(op, input1, input2, out, trace_model, atol=1e-04, rtol=1e-04):8 class Model(torch.nn.Module):9 def __init__(self, op):10 super(Model, self).__init__()11 self.op = op12 def forward(self, x, y, out=None):13 return self.op(x, y, out=out)14 model = Model(op)15 args = [input1, input2]16 if out is not None:17 args.append(out)18 # Run on CPU.19 native_out = model(*args)20 # Run on IPU.21 options = poptorch.Options()22 options.Jit.traceModel(trace_model)23 poptorch_model = poptorch.inferenceModel(model, options)24 poptorch_out = poptorch_model(*args)25 helpers.assert_allclose(expected=native_out,26 actual=poptorch_out,27 atol=atol,28 rtol=rtol,29 equal_nan=True)30 if out is not None:31 helpers.assert_allclose(expected=native_out,32 actual=out,33 atol=atol,34 rtol=rtol,35 equal_nan=True)36@pytest.mark.parametrize("optional_out", [True, False])37@pytest.mark.parametrize("trace_model", [True, False])38def test_matmul(optional_out, trace_model):39 torch.manual_seed(42)40 input1 = torch.randn([10, 200])41 input2 = torch.randn([200, 45])42 out = torch.randn([10, 45]) if optional_out else None43 blas_op(torch.matmul, input1, input2, out, trace_model)44@pytest.mark.parametrize("trace_model", [True, False])45@pytest.mark.parametrize("mode",46 (poptorch.MatMulSerializationMode.InputChannels,47 poptorch.MatMulSerializationMode.ReducingDim,48 poptorch.MatMulSerializationMode.OutputChannels,49 poptorch.MatMulSerializationMode.Disabled))50@pytest.mark.parametrize("factor", (2, 5, 10))51@pytest.mark.parametrize("keep_precision", [True, False])52def test_serializedMatMul(trace_model, mode, factor, keep_precision):53 torch.manual_seed(42)54 input1 = torch.rand(1, 10, 200)55 input2_dim = 4556 if mode == poptorch.MatMulSerializationMode.OutputChannels:57 # Ensure the value is a multiple of factor58 input2_dim = input2_dim // factor * factor59 input2 = torch.rand(200, input2_dim)60 def serialise_matmal_op(input, other, out):61 assert out is None62 return poptorch.serializedMatMul(input, other, mode, factor,63 keep_precision)64 if keep_precision:65 input1 = input1.half()66 input2 = input2.half()67 blas_op(serialise_matmal_op,68 input1,69 input2,70 None,71 trace_model,72 rtol=0.01,73 atol=0.05)74 else:75 blas_op(serialise_matmal_op, input1, input2, None, trace_model)76@pytest.mark.parametrize("optional_out", [True, False])77@pytest.mark.parametrize("trace_model", [True, False])78def test_bmm(optional_out, trace_model):79 input1 = torch.randn([12, 10, 200])80 input2 = torch.randn([12, 200, 33])81 out = torch.randn([12, 10, 33]) if optional_out else None82 blas_op(torch.bmm, input1, input2, out, trace_model)83@pytest.mark.parametrize("bias", [True, False])84@pytest.mark.parametrize("trace_model", [True, False])85def test_matmul_training(bias, trace_model):86 N, M, K, C = 100, 9, 7, 587 class Net(torch.nn.Module):88 def __init__(self):89 super(Net, self).__init__()90 torch.manual_seed(42)91 self.linear = torch.nn.Linear(K, K, bias=bias)92 self.softmax = torch.nn.LogSoftmax(dim=1)93 self.loss = torch.nn.L1Loss(reduction="mean")94 def forward(self, x, y, target):95 x = self.linear(x)96 x = torch.matmul(x, y)97 return x, self.loss(x, target)98 torch.manual_seed(42)99 model = Net()100 opts = poptorch.Options()101 opts.Jit.traceModel(trace_model)102 optimizer = torch.optim.SGD(model.parameters(), lr=0.01)103 torch.manual_seed(42)104 poptorch_model = poptorch.trainingModel(model, opts, optimizer)105 x = torch.randn(N, M, K)106 y = torch.randn(K, K)107 target = torch.empty(N, M, K, dtype=torch.long).random_(0, C)108 for _ in range(0, 400):109 optimizer.zero_grad()110 poptorch_out, poptorch_loss = poptorch_model(x, y, target)111 native_out, native_loss = model(x, y, target)112 native_loss.backward(retain_graph=True)113 optimizer.step()114 helpers.assert_allclose(actual=poptorch_out,115 expected=native_out,116 rtol=1e-02,117 atol=1e-02)118 helpers.assert_allclose(actual=poptorch_loss,119 expected=native_loss,120 rtol=1e-03,...

Full Screen

Full Screen

coordinator.py

Source:coordinator.py Github

copy

Full Screen

1import hydra2import os3import h5py4import numpy as np5from omegaconf import DictConfig6from dataset import get_standard_dataset, get_test_data, get_validation_data7import torch8from torch.utils.data import DataLoader9from deep_image_prior import DeepImagePriorReconstructor10from pre_training import Trainer11from copy import deepcopy12@hydra.main(config_path='cfgs', config_name='config')13def coordinator(cfg : DictConfig) -> None:14 dataset, ray_trafos = get_standard_dataset(cfg.data.name, cfg.data)15 obs_shape = dataset.space[0].shape16 im_shape = dataset.space[1].shape17 if cfg.validation_run:18 if cfg.data.validation_data:19 dataset_test = get_validation_data(cfg.data.name, cfg.data)20 else:21 dataset_test = dataset.create_torch_dataset(22 fold='validation', reshape=((1,) + obs_shape,23 (1,) + im_shape,24 (1,) + im_shape))25 else:26 if cfg.data.test_data:27 dataset_test = get_test_data(cfg.data.name, cfg.data)28 else:29 dataset_test = dataset.create_torch_dataset(30 fold='test', reshape=((1,) + obs_shape,31 (1,) + im_shape,32 (1,) + im_shape))33 ray_trafo = {'ray_trafo_module': ray_trafos['ray_trafo_module'],34 'reco_space': dataset.space[1],35 'observation_space': dataset.space[0]36 }37 if cfg.torch_manual_seed_pretrain_init_model:38 torch.random.manual_seed(cfg.torch_manual_seed_pretrain_init_model)39 reconstructor = DeepImagePriorReconstructor(**ray_trafo, cfg=cfg.mdl)40 model = deepcopy(reconstructor.model)41 if cfg.pretraining:42 trn_ray_trafos = ({'smooth_pinv_ray_trafo_module':43 ray_trafos['smooth_pinv_ray_trafo_module']}44 if cfg.trn.use_adversarial_attacks else {})45 Trainer(model=model,46 ray_trafos=trn_ray_trafos,47 cfg=cfg.trn).train(dataset)48 os.makedirs(cfg.save_reconstruction_path, exist_ok=True)49 if cfg.save_histories_path is not None:50 os.makedirs(cfg.save_histories_path, exist_ok=True)51 if cfg.save_iterates_path is not None:52 os.makedirs(cfg.save_iterates_path, exist_ok=True)53 if cfg.save_iterates_params_path is not None:54 os.makedirs(cfg.save_iterates_params_path, exist_ok=True)55 filename = os.path.join(cfg.save_reconstruction_path,'recos.hdf5')56 file = h5py.File(filename, 'w')57 recos_dataset = file.create_dataset('recos',58 shape=(1,) + im_shape, maxshape=(1,) + im_shape, dtype=np.float32,59 chunks=True)60 dataloader = DataLoader(dataset_test, batch_size=1, num_workers=0,61 shuffle=True, pin_memory=True)62 for i, (noisy_obs, fbp, *gt) in enumerate(dataloader):63 gt = gt[0] if gt else None64 reco, *optional_out = reconstructor.reconstruct(65 noisy_obs.float(), fbp, gt,66 return_histories=cfg.save_histories_path is not None,67 return_iterates=cfg.save_iterates_path is not None,68 return_iterates_params=cfg.save_iterates_params_path is not None)69 recos_dataset[i] = reco70 if cfg.save_histories_path is not None:71 histories = optional_out.pop(0)72 histories = {k: np.array(v, dtype=np.float32)73 for k, v in histories.items()}74 np.savez(os.path.join(cfg.save_histories_path, 'histories.npz'),75 **histories)76 if cfg.save_iterates_path is not None:77 iterates = optional_out.pop(0)78 iterates_iters = optional_out.pop(0)79 np.savez_compressed(80 os.path.join(cfg.save_iterates_path, 'iterates.npz'),81 iterates=np.asarray(iterates),82 iterates_iters=iterates_iters)83 if cfg.save_iterates_params_path is not None:84 iterates_params = optional_out.pop(0)85 iterates_params_iters = optional_out.pop(0)86 for params, iters in zip(iterates_params, iterates_params_iters):87 torch.save(params,88 os.path.join(cfg.save_iterates_params_path,89 'params_iters{:d}.pt'.format(iters)))90if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful