x = x[:, :, :, 0] x = x[0, :, :] skimage.io.imsave(str(filepath), (x * 255).astype("uint8")) def get_image(path): image = skimage.io.imread(path) original_shape = image.shape if len(original_shape) == 2: image = image[:, :, np.newaxis] image = (image.astype(np.float32)) / 255 image = image[np.newaxis, ] return image results_path = config.testing_path() / "affine_generator/" results_path.mkdir(parents=True, exist_ok=True) samples = { "cifar1": Path("testing/samples/cifar1.png"), "cifar2": Path("testing/samples/cifar2.png"), "mnist": Path("testing/samples/mnist.png"), } for sample_id, sample_path in samples.items(): image = get_image(sample_path) transformations_sets = config.common_transformations() for transformation_set in transformations_sets: n, h, w, c = image.shape transformation_set.set_input_shape((h, w, c)) transformation_set.set_pytorch(False)
import skimage.io import transformational_measures as tm import numpy as np import matplotlib.pyplot as plt import os import config results_path = config.testing_path() / "affine_transformation/" results_path.mkdir(parents=True, exist_ok=True) use_cuda = True source_path = "testing/samples/mnist.png" input_shape = np.array((28, 28, 1)) def apply_transformation_numpy(t, image_name): x = skimage.io.imread(source_path) x = x[np.newaxis, :, :, np.newaxis] x = t(x) filepath = results_path / f"{image_name}_numpy.png" skimage.io.imsave(filepath, x) def apply_transformation_pytorch(t, image_name): adapter = tm.NumpyPytorchImageTransformationAdapter(use_cuda=use_cuda) x = skimage.io.imread(source_path) / 255.0 x = x[np.newaxis, :, :, np.newaxis].astype(np.float32) x = adapter.pre_adapt(x) x = t(x)
model = model_config.make_model(dataset.input_shape, dataset.num_classes, use_cuda) p= profiler.Profiler() p.event("start") #transformations=tm.SimpleAffineTransformationGenerator(r=360, s=4, t=3) transformation.set_input_shape(dataset.input_shape) transformation.set_pytorch(True) transformation.set_cuda(use_cuda) iterator = iterator_class(model, image_dataset, transformation, batch_size=batch_size, num_workers=0, adapter=None,use_cuda=use_cuda) adapter = tm.PytorchNumpyImageTransformationAdapter(use_cuda=use_cuda) folderpath = config.testing_path() / f"{iterator.__class__.__name__}" folderpath.mkdir(exist_ok=True,parents=True) i=0 for original_x,activations_iterator in iterator.samples_first(): for v in activations_iterator: if iterator_class == tm.BothPytorchActivationsIterator: x_transformed, pre_transformed_activations, post_transformed_activations = v layers=np.concatenate([pre_transformed_activations[0], post_transformed_activations[0]], axis=0) else: x_transformed, activations = v layers=activations[0] layers= layers.transpose((0, 2, 3, 1)) # grab only the first feature map
p.event("start") #transformations=tm.SimpleAffineTransformationGenerator(r=360, s=4, t=3) transformations = tm.SimpleAffineTransformationGenerator(r=360, n_rotations=4) transformations.set_input_shape(dataset.input_shape) transformations.set_pytorch(True) transformations.set_cuda(use_cuda) iterator = tm.NormalPytorchActivationsIterator(model, image_dataset, transformations, batch_size=64, num_workers=0, use_cuda=use_cuda) adapter = tm.PytorchNumpyImageTransformationAdapter(use_cuda=use_cuda) folderpath = config.testing_path() / f"activations_iterator" folderpath.mkdir(exist_ok=True, parents=True) batch_size = 64 i = 0 for original_x, activations_iterator in iterator.samples_first(): for x_transformed, activations in activations_iterator: x = adapter.pre_adapt(x_transformed) filepath = folderpath / f"{dataset}_samples_first.png" util.plot_image_grid(x, x.shape[0], show=False, save=filepath) i = i + 1 if i == 10: break break p.event("end")
from testing.util import plot_image_grid import datasets import config from pytorch.pytorch_image_dataset import ImageClassificationDataset from pytorch.numpy_dataset import NumpyDataset import itertools folderpath=config.testing_path()/ "numpy_iterator" folderpath.mkdir(parents=True,exist_ok=True) images= 32 preprocessing=True print(f"Using datasets: {datasets.names}") for dataset_name,preprocessing,normalize in itertools.product(datasets.names,[True,False],[True,False]): print(dataset_name,preprocessing,normalize) dataset = datasets.get_classification(dataset_name) # print(dataset.summary()) pre_str = 'preprocessing_' if preprocessing else "" normalize_str = 'normalized_' if preprocessing else "" filepath = folderpath / f"{pre_str}{normalize_str}{dataset_name}.png" numpy_dataset=NumpyDataset(dataset.x_test,dataset.y_test) if preprocessing: numpy_dataset= ImageClassificationDataset(numpy_dataset) x,y= numpy_dataset.get_batch(list(range(images))) if not preprocessing: x = x.float()/255 # print(x.shape,y.shape) # permute to NHWC order