def _change_backend(backend): if backend not in image_utils.get_available_backends(): pytest.skip("{} is not installed".format(backend)) # change backend arbitrary image_utils.set_backend(backend) assert image_utils.get_backend() == backend
def train(args): # create real images reals = create_real_images(args) # save real images for i, real in enumerate(reals): image_path = os.path.join(args.logdir, 'real_%d.png' % i) imwrite(denormalize(np.transpose(real, [0, 2, 3, 1])[0]), image_path) # nnabla monitor monitor = Monitor(args.logdir) # use cv2 backend at MonitorImage set_backend('cv2') prev_models = [] Zs = [] noise_amps = [] for scale_num in range(len(reals)): fs = min(args.fs_init * (2**(scale_num // 4)), 128) min_fs = min(args.min_fs_init * (2**(scale_num // 4)), 128) model = Model(real=reals[scale_num], num_layer=args.num_layer, fs=fs, min_fs=min_fs, kernel=args.kernel, pad=args.pad, lam_grad=args.lam_grad, alpha_recon=args.alpha_recon, d_lr=args.d_lr, g_lr=args.g_lr, beta1=args.beta1, gamma=args.gamma, lr_milestone=args.lr_milestone, scope=str(scale_num)) z_curr = train_single_scale(args, scale_num, model, reals, prev_models, Zs, noise_amps, monitor) prev_models.append(model) Zs.append(z_curr) noise_amps.append(args.noise_amp) # save data nn.save_parameters(os.path.join(args.logdir, 'models.h5')) save_pkl(Zs, os.path.join(args.logdir, 'Zs.pkl')) save_pkl(reals, os.path.join(args.logdir, 'reals.pkl')) save_pkl(noise_amps, os.path.join(args.logdir, 'noise_amps.pkl')) return Zs, reals, noise_amps
def load_image_dcm(file, shape=None, normalize=False): if normalize: max_range = 1.0 else: max_range = -1 img = None try: if 'dicom' not in image_utils.get_available_backends(): raise ValueError("Please ensure cv2 and dicom is installed.") current_backend = image_utils.get_backend() image_utils.set_backend('dicom') img = load_image_imread(file, shape, max_range) return img finally: image_utils.set_backend(current_backend) return img
def test_image_utils_backend_manager(): with pytest.raises(ValueError): image_utils.set_backend("not_installed_module")