def __init__(self, generator, adversary, x_dim, z_dim, y_dim, adv_type, optim=None, optim_kwargs=None, feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder="./veganModels/cAbstractGAN1v1", secure=True): adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) gen_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) if secure: AbstractConditionalGenerativeModel._check_conditional_network_input( generator, in_dim=z_dim, y_dim=y_dim, name="Generator") AbstractConditionalGenerativeModel._check_conditional_network_input( adversary, in_dim=x_dim, y_dim=y_dim, name="Adversary") AbstractGAN1v1.__init__(self, generator=generator, adversary=adversary, x_dim=adv_in_dim, z_dim=gen_in_dim, adv_type=adv_type, optim=optim, optim_kwargs=optim_kwargs, fixed_noise_size=fixed_noise_size, device=device, folder=folder, ngpu=0, secure=secure, _called_from_conditional=True) AbstractConditionalGenerativeModel.__init__( self, x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, optim=optim, optim_kwargs=optim_kwargs, feature_layer=feature_layer, fixed_noise_size=fixed_noise_size, device=device, folder=folder, ngpu=ngpu, secure=secure) if self.secure: assert (self.generator.output_size == self.x_dim), ( "Generator output shape must be equal to x_dim. {} vs. {}.". format(self.generator.output_size, self.x_dim))
def load_example_autoencoder(x_dim, y_dim=None): """ Load some example architecture for the auto-encoder. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for autoencoder. """ if y_dim is not None: adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) else: adv_in_dim = x_dim if len(adv_in_dim) == 3 and np.prod(adv_in_dim) > 1024: first_layer = nn.Conv2d(in_channels=adv_in_dim[0], out_channels=3, kernel_size=5, stride=2) out_pixels_x = int((adv_in_dim[1] - (5 - 1) - 1) / 2 + 1) out_pixels_y = int((adv_in_dim[2] - (5 - 1) - 1) / 2 + 1) adv_in_dim = (3, out_pixels_x, out_pixels_y) else: first_layer = nn.Identity() return MyAutoEncoder(adv_in_dim=adv_in_dim, x_dim=x_dim, first_layer=first_layer)
def load_mnist_adversary(x_dim=(1, 32, 32), y_dim=None, adv_type="Critic"): """ Load some mnist architecture for the adversary. Parameters ---------- y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for adversary. """ possible_types = ["Discriminator", "Critic"] if adv_type == "Critic": last_layer = nn.Identity elif adv_type == "Discriminator": last_layer = nn.Sigmoid else: raise ValueError( "'adv_type' must be one of: {}.".format(possible_types)) adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if y_dim is not None else x_dim return MyAdversary(adv_in_dim=adv_in_dim, last_layer=last_layer)
def load_mnist_generator(x_dim, z_dim, y_dim=None): """ Load some mnist architecture for the generator. Parameters ---------- z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : None, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for generator,. """ z_dim = [z_dim] if isinstance(z_dim, int) else z_dim y_dim = tuple([y_dim]) if isinstance(y_dim, int) else y_dim if len(z_dim) > 1: assert (z_dim[1] <= 16) and ( z_dim[1] % 2 == 0 ), "z_dim[1] must be smaller 16 and divisible by 2. Given: {}.".format( z_dim[1]) assert z_dim[1] == z_dim[ 2], "z_dim[1] must be equal to z_dim[2]. Given: {} and {}.".format( z_dim[1], z_dim[2]) gen_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) if y_dim is not None else z_dim return MyGenerator(x_dim=x_dim, gen_in_dim=gen_in_dim)
def load_example_decoder(x_dim, z_dim, y_dim=None): """ Load some example architecture for the decoder. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : None, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for decoder. """ x_dim = [x_dim] if isinstance(x_dim, int) else x_dim if y_dim is not None: dec_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) else: dec_in_dim = z_dim return MyDecoder(x_dim=x_dim, dec_in_dim=dec_in_dim)
def __init__(self, x_dim, z_dim, y_dim, optim, optim_kwargs, feature_layer, fixed_noise_size, device, ngpu, folder, secure): self.adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) self.gen_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) AbstractGenerativeModel.__init__(self, x_dim=x_dim, z_dim=z_dim, optim=optim, optim_kwargs=optim_kwargs, feature_layer=feature_layer, fixed_noise_size=fixed_noise_size, device=device, folder=folder, ngpu=ngpu, secure=secure) self.y_dim = tuple([y_dim]) if isinstance(y_dim, int) else y_dim self.hyperparameters["y_dim"] = self.y_dim self.eval()
def __init__( self, generator, adversary, encoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, adv_type="Discriminator", feature_layer=None, fixed_noise_size=32, device=None, folder="./veganModels/cAbstractConditionalGANGAE", ngpu=0, secure=True, _called_from_conditional=False): enc_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) gen_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if secure: AbstractConditionalGenerativeModel._check_conditional_network_input(encoder, in_dim=x_dim, y_dim=y_dim, name="Encoder") AbstractConditionalGenerativeModel._check_conditional_network_input(generator, in_dim=z_dim, y_dim=y_dim, name="Generator") AbstractConditionalGenerativeModel._check_conditional_network_input(adversary, in_dim=x_dim, y_dim=y_dim, name="Adversary") self.adv_type = adv_type self.encoder = Encoder(encoder, input_size=enc_in_dim, device=device, ngpu=ngpu, secure=secure) self.generator = Generator(generator, input_size=gen_in_dim, device=device, ngpu=ngpu, secure=secure) self.adversary = Adversary(adversary, input_size=adv_in_dim, adv_type=adv_type, device=device, ngpu=ngpu, secure=secure) self.neural_nets = { "Generator": self.generator, "Adversary": self.adversary, "Encoder": self.encoder } super().__init__( x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, optim=optim, optim_kwargs=optim_kwargs, feature_layer=feature_layer, fixed_noise_size=fixed_noise_size, device=device, ngpu=ngpu, folder=folder, secure=secure ) self.hyperparameters["adv_type"] = adv_type if not _called_from_conditional and self.secure: assert self.generator.output_size == self.x_dim, ( "Generator output shape must be equal to x_dim. {} vs. {}.".format(self.generator.output_size, self.x_dim) )
def _check_conditional_network_input(network, in_dim, y_dim, name): architecture = NeuralNetwork._get_iterative_layers(network, input_type="Object") has_error = False for i, layer in enumerate(architecture): if "in_features" in layer.__dict__: inpt_dim = int(layer.__dict__["in_features"]) if inpt_dim == in_dim: has_error = True elif np.prod(inpt_dim) == np.prod(in_dim): has_error = True break elif "in_channels" in layer.__dict__: inpt_dim = int(layer.__dict__["in_channels"]) if not isinstance(in_dim, int) and inpt_dim == in_dim[0]: has_error = True if not isinstance(y_dim, int) and inpt_dim == y_dim[0]: has_error = True break else: raise ValueError( "No layer with `in_features`, `in_channels` or `num_features` found." ) if has_error: required_dim = get_input_dim(in_dim, y_dim) if len(required_dim) == 1: first_layer = "torch.nn.Linear(in_features={}, out_features=...)".format( required_dim[0]) else: first_layer = ( "torch.nn.Conv2d(in_channels={}, out_channels=...)\n". format(required_dim[0]) + "\t\t\t\t\t\t\t\t\ttorch.nn.ConvTranspose2d(in_channels={}, out_channels=...)\n" .format(required_dim[0]) + "\t\t\t\t\t\t\t\t\ttorch.nn.Linear(in_features={}, out_features=...) if torch.nn.Flatten() is used before" .format(np.prod(required_dim))) raise AssertionError( "\n\n**{}** is a conditional network.".format(name) + "The y_dim (label) will be concatenated to the input of this network.\n\n" + "The first layer will receive input shape: {} due to y_dim={}. " .format(required_dim, y_dim) + "Given: {}.(Reshape & Flatten not considered)\n".format( str(layer)) + "First layer should be of the form: {}.\n\n".format( first_layer) + "Please use vegans.utils.get_input_dim(in_dim, y_dim) to get the correct input dimensions.\n" + "Check on github for notebooks of conditional GANs.\n\n")
def _check_unconditional_network_input(network, in_dim, y_dim, name): architecture = NeuralNetwork._get_iterative_layers(network, input_type="Object") has_error = False concat_input = get_input_dim(in_dim, y_dim) for i, layer in enumerate(architecture): if "in_features" in layer.__dict__: inpt_dim = int(layer.__dict__["in_features"]) if inpt_dim == concat_input: has_error = True elif np.prod(inpt_dim) == np.prod(concat_input): has_error = True break elif "in_channels" in layer.__dict__: inpt_dim = int(layer.__dict__["in_channels"]) if inpt_dim == concat_input[0]: has_error = True break else: raise TypeError( "No input layer found. No Linear or Conv2d layers?") if has_error: if len(in_dim) == 1: first_layer = "torch.nn.Linear(in_features={}, out_features=...)".format( in_dim[0]) else: first_layer = ( "torch.nn.Conv2d(in_channels={}, out_channels=...)\n". format(in_dim[0]) + "\t\t\t\t\t\t\t\t\ttorch.nn.ConvTranspose2d(in_channels={}, out_channels=...)\n" .format(in_dim[0]) + "\t\t\t\t\t\t\t\t\ttorch.nn.Linear(in_features={}, out_features=...) if torch.nn.Flatten() is used before" .format(np.prod(in_dim))) raise AssertionError( "\n\n**{}** is **not** a conditional network. The y_dim (label) will **not** be concatenated to the input of this network.\n\n" .format(name) + "The first layer will receive input shape: {} (same as x_dim). Given: {}.(Reshape & Flatten not considered)\n" .format(in_dim, str(layer)) + "First layer should be of the form: {}.\n\n".format( first_layer) + "Please use vegans.utils.get_input_dim(in_dim, y_dim) to get the correct input dimensions.\n" + "Check on github for notebooks of conditional GANs.\n\n")
def load_mnist_autoencoder(x_dim=(1, 32, 32), y_dim=None): """ Load some mnist architecture for the auto-encoder. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for autoencoder. """ ae_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if y_dim is not None else x_dim return MyAutoEncoder(ae_in_dim=ae_in_dim)
def load_mnist_decoder(x_dim, z_dim, y_dim=None): """ Load some mnist architecture for the decoder. Parameters ---------- z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for decoder. """ dec_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) if y_dim is not None else z_dim return MyDecoder(x_dim=x_dim, dec_in_dim=dec_in_dim)
def load_example_adversary(x_dim, y_dim=None, adv_type="Critic"): """ Load some example architecture for the adversary. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for adversary. """ possible_types = ["Discriminator", "Critic"] if adv_type == "Critic": last_layer = nn.Identity elif adv_type == "Discriminator": last_layer = nn.Sigmoid else: raise ValueError( "'adv_type' must be one of: {}.".format(possible_types)) x_dim = [x_dim] if isinstance(x_dim, int) else x_dim if y_dim is not None: adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) else: adv_in_dim = x_dim if len(adv_in_dim) == 3 and np.prod(adv_in_dim) > 1024: first_layer = nn.Conv2d(in_channels=adv_in_dim[0], out_channels=3, kernel_size=5, stride=2) out_pixels_x = int((adv_in_dim[1] - (5 - 1) - 1) / 2 + 1) out_pixels_y = int((adv_in_dim[2] - (5 - 1) - 1) / 2 + 1) adv_in_dim = (3, out_pixels_x, out_pixels_y) else: first_layer = nn.Identity() return MyAdversary(adv_in_dim=adv_in_dim, first_layer=first_layer, last_layer=last_layer)
def load_celeba_encoder(x_dim, z_dim, y_dim=None): """ Load some celeba architecture for the encoder. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for encoder. """ enc_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if y_dim is not None else x_dim return MyEncoder(enc_in_dim=enc_in_dim, z_dim=z_dim)
def test_fit_image_feature_loss(gan, adv_dim, enc_dim): z_dim = 10 x_dim = [3, 16, 16] y_dim = 5 X_train = np.zeros(shape=[100, *x_dim]) y_train = np.zeros(shape=[100, y_dim]) X_test = np.zeros(shape=[100, *x_dim]) y_test = np.zeros(shape=[100, y_dim]) gen = generate_net(in_dim=z_dim + y_dim, last_layer=torch.nn.Sigmoid, out_dim=x_dim) enc = generate_net(in_dim=get_input_dim(x_dim, y_dim), last_layer=torch.nn.Identity, out_dim=enc_dim) disc = generate_net(in_dim=adv_dim, last_layer=torch.nn.Sigmoid, out_dim=1) fit_kwargs = { "epochs": 1, "batch_size": 4, "steps": None, "print_every": None, "save_model_every": None, "save_images_every": None, "save_losses_every": "1e", "enable_tensorboard": False } testgan = gan(generator=gen, adversary=disc, encoder=enc, x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, folder=None, feature_layer=disc.hidden_part) testgan.fit(X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test, **fit_kwargs) assert hasattr(testgan, "logged_losses")
def load_celeba_decoder(x_dim, z_dim, y_dim=None): """ Load some mnist architecture for the decoder. Parameters ---------- z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for decoder. """ assert x_dim[1] % 2 == 0, "`x_dim[1]` must be divisible by 2. Given: {}.".format(x_dim[1]) assert x_dim[1] % 8 == 0, "`x_dim[1]` must be divisible by 8. Given: {}.".format(x_dim[1]) assert (x_dim[1] / 8) % 2 == 0, "`x_dim[1]/8` must be divisible by 2. Given: {}.".format(x_dim[1]) dec_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) if y_dim is not None else z_dim return MyDecoder(x_dim=x_dim, dec_in_dim=dec_in_dim)
def load_example_encoder(x_dim, z_dim, y_dim=None): """ Load some example architecture for the encoder. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : None, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for encoder. """ z_dim = [z_dim] if isinstance(z_dim, int) else z_dim if y_dim is not None: enc_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) else: enc_in_dim = x_dim if len(enc_in_dim) == 3 and np.prod(enc_in_dim) > 1024: first_layer = nn.Conv2d(in_channels=enc_in_dim[0], out_channels=3, kernel_size=5, stride=2) out_pixels_x = int((enc_in_dim[1] - (5 - 1) - 1) / 2 + 1) out_pixels_y = int((enc_in_dim[2] - (5 - 1) - 1) / 2 + 1) enc_in_dim = (3, out_pixels_x, out_pixels_y) else: first_layer = nn.Identity() return MyEncoder(enc_in_dim=enc_in_dim, z_dim=z_dim, first_layer=first_layer)
def load_example_generator(x_dim, z_dim, y_dim=None): """ Load some example architecture for the generator. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for generator,. """ if y_dim is not None: gen_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) else: gen_in_dim = z_dim return MyGenerator(gen_in_dim=gen_in_dim, x_dim=x_dim)
def test_fit_image(gan, last_layer): z_dim = 10 y_dim = 5 x_dim = [3, 16, 16] X_train = np.zeros(shape=[100, *x_dim]) y_train = np.zeros(shape=[100, y_dim]) X_test = np.zeros(shape=[100, *x_dim]) y_test = np.zeros(shape=[100, y_dim]) gen = generate_net(in_dim=15, last_layer=torch.nn.Sigmoid, out_dim=x_dim) adv = generate_net(in_dim=get_input_dim(x_dim, y_dim), last_layer=last_layer, out_dim=1) fit_kwargs = { "epochs": 1, "batch_size": 4, "steps": None, "print_every": None, "save_model_every": None, "save_images_every": None, "save_losses_every": "1e", "enable_tensorboard": False } testgan = gan(generator=gen, adversary=adv, x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, folder=None) testgan.fit(X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test, **fit_kwargs) assert hasattr(testgan, "logged_losses")
def load_mnist_encoder(x_dim, z_dim, y_dim=None): """ Load some mnist architecture for the encoder. Parameters ---------- x_dim : integer, list Indicating the number of dimensions for the real data. z_dim : integer, list Indicating the number of dimensions for the latent space. y_dim : integer, list, optional Indicating the number of dimensions for the labels. Returns ------- torch.nn.Module Architectures for encoder. """ z_dim = [z_dim] if isinstance(z_dim, int) else z_dim assert len(z_dim) == 1, "z_dim must be of length one. Given: {}.".format( z_dim) enc_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if y_dim is not None else x_dim return MyEncoder(enc_in_dim=enc_in_dim, z_dim=z_dim)
def __init__(self, generator, adversary, encoder, x_dim, z_dim, y_dim, c_dim_discrete, c_dim_continuous, optim=None, optim_kwargs=None, lambda_z=10, feature_layer=None, fixed_noise_size=32, device=None, ngpu=0, folder="./veganModels/cInfoGAN", secure=True): c_dim_discrete = [c_dim_discrete] if isinstance( c_dim_discrete, int) else c_dim_discrete assert c_dim_discrete == [0] or 0 not in c_dim_discrete, ( "`c_dim_discrete` has multiple elements. Zero not allowed. Given: {}." .format(c_dim_discrete)) assert isinstance( c_dim_continuous, int), ("`c_dim_continuous` must be of type int. Given: {}.".format( type(c_dim_continuous))) self.c_dim_discrete = tuple([i for i in list(c_dim_discrete)]) self.c_dim_continuous = tuple([c_dim_continuous]) self.c_dim = tuple( [sum(self.c_dim_discrete) + sum(self.c_dim_continuous)]) if len(y_dim) == 3: intermediate_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) gen_in_dim = get_input_dim(dim1=intermediate_in_dim, dim2=self.c_dim) else: gen_in_dim = get_input_dim(dim1=z_dim, dim2=sum(self.c_dim) + sum(y_dim)) adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if secure: AbstractConditionalGenerativeModel._check_conditional_network_input( generator, in_dim=z_dim, y_dim=self.c_dim, name="Generator") self.generator = Generator(generator, input_size=gen_in_dim, device=device, ngpu=ngpu, secure=secure) self.adversary = Adversary(adversary, input_size=adv_in_dim, adv_type="Discriminator", device=device, ngpu=ngpu, secure=secure) self.encoder = Encoder(encoder, input_size=adv_in_dim, device=device, ngpu=ngpu, secure=secure) self.neural_nets = { "Generator": self.generator, "Adversary": self.adversary, "Encoder": self.encoder } super().__init__(x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, optim=optim, optim_kwargs=optim_kwargs, feature_layer=feature_layer, fixed_noise_size=fixed_noise_size, device=device, folder=folder, ngpu=ngpu, secure=secure) if self.c_dim_discrete != (0, ): self.multinomial = nn.Sequential( nn.Flatten(), nn.Linear(np.prod(self.encoder.output_size), np.sum(self.c_dim_discrete)), nn.Sigmoid()).to(self.device) if self.c_dim_continuous != (0, ): self.mu = nn.Sequential( nn.Flatten(), nn.Linear(np.prod(self.encoder.output_size), np.sum(self.c_dim_continuous)), LayerReshape(self.c_dim_continuous)).to(self.device) self.log_variance = nn.Sequential( nn.Flatten(), nn.Linear(np.prod(self.encoder.output_size), np.sum(self.c_dim_continuous)), nn.ReLU(), LayerReshape(self.c_dim_continuous)).to(self.device) self.lambda_z = lambda_z self.hyperparameters["lambda_z"] = lambda_z if self.secure: assert (self.generator.output_size == self.x_dim), ( "Generator output shape must be equal to x_dim. {} vs. {}.". format(self.generator.output_size, self.x_dim))
def __init__(self, generatorX_Y, adversaryX_Y, generatorY_X, adversaryY_X, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_x=10, adv_type="Discriminator", fixed_noise_size=32, device=None, ngpu=0, folder="./veganModels/cCycleGAN", secure=True): gen_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) adv_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) if secure: assert x_dim == y_dim, ( "`x_dim` and `y_dim` must be equal in the current implementation of CycleGAN. Given: {} and {}." .format(x_dim, y_dim)) AbstractConditionalGenerativeModel._check_conditional_network_input( generatorX_Y, in_dim=z_dim, y_dim=x_dim, name="GeneratorX_Y") AbstractConditionalGenerativeModel._check_conditional_network_input( adversaryX_Y, in_dim=y_dim, y_dim=x_dim, name="AdversaryX_Y") AbstractConditionalGenerativeModel._check_conditional_network_input( generatorY_X, in_dim=z_dim, y_dim=y_dim, name="GeneratorY_X") AbstractConditionalGenerativeModel._check_conditional_network_input( adversaryY_X, in_dim=x_dim, y_dim=y_dim, name="AdversaryY_X") self.adv_type = adv_type self.generatorX_Y = Generator(generatorX_Y, input_size=gen_in_dim, device=device, ngpu=ngpu, secure=secure) self.adversaryX_Y = Adversary(adversaryX_Y, input_size=adv_in_dim, adv_type=adv_type, device=device, ngpu=ngpu, secure=secure) self.generatorY_X = Generator(generatorY_X, input_size=gen_in_dim, device=device, ngpu=ngpu, secure=secure) self.adversaryY_X = Adversary(adversaryY_X, input_size=adv_in_dim, adv_type=adv_type, device=device, ngpu=ngpu, secure=secure) self.autoencoder = Autoencoder(encoder=self.generatorX_Y, decoder=self.generatorY_X) self.neural_nets = { "Autoencoder": self.autoencoder, "AdversaryX_Y": self.adversaryX_Y, "AdversaryY_X": self.adversaryY_X } self.generator = self.generatorX_Y self.adversary = self.adversaryX_Y super().__init__(x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, optim=optim, optim_kwargs=optim_kwargs, feature_layer=None, fixed_noise_size=fixed_noise_size, device=device, folder=folder, ngpu=ngpu, secure=secure) self.lambda_x = lambda_x self.hyperparameters["lambda_x"] = lambda_x if self.secure: assert (self.generatorX_Y.output_size == self.x_dim), ( "GeneratorX_Y output shape must be equal to x_dim. {} vs. {}.". format(self.generatorX_Y.output_size, self.x_dim)) assert (self.generatorY_X.output_size == self.x_dim), ( "GeneratorY_X output shape must be equal to x_dim. {} vs. {}.". format(self.generatorY_X.output_size, self.x_dim))
def test_get_input_dim(): dim1 = 12 dim2 = 34 assert utils.get_input_dim(dim1, dim2) == tuple([46]) dim1 = [12] dim2 = [34] assert utils.get_input_dim(dim1, dim2) == tuple([46]) dim1 = [1, 3, 4] dim2 = [5] assert utils.get_input_dim(dim1, dim2) == tuple([6, 3, 4]) dim1 = 5 dim2 = [1, 3, 4] assert utils.get_input_dim(dim1, dim2) == tuple([6, 3, 4]) dim1 = [3, 4, 6] dim2 = [7, 4, 6] assert utils.get_input_dim(dim1, dim2) == tuple([10, 4, 6]) with pytest.raises(AssertionError): dim1 = [3, 4] utils.get_input_dim(dim1, dim2) with pytest.raises(AssertionError): dim1 = [20, 1, 4, 4] utils.get_input_dim(dim1, dim2) with pytest.raises(AssertionError): dim1 = [3, 4, 4] dim2 = [3, 4, 5] utils.get_input_dim(dim1, dim2)
def test_fit_image_error(gan, adv_dim, enc_dim): z_dim = 10 x_dim = [3, 16, 16] y_dim = 5 X_train = np.zeros(shape=[100, *x_dim]) X_train_wrong_shape1 = np.zeros(shape=[100]) X_train_wrong_shape2 = np.zeros(shape=[100, 17]) X_train_wrong_shape3 = np.zeros(shape=[100, 17, 14]) X_train_wrong_shape4 = np.zeros(shape=[100, 4, 16, 16]) X_test_wrong_shape = np.zeros(shape=[100, 3, 17, 17]) y_train = np.zeros(shape=[100, y_dim]) y_test = np.zeros(shape=[100, y_dim]) gen = generate_net(in_dim=z_dim + y_dim, last_layer=torch.nn.Sigmoid, out_dim=x_dim) enc = generate_net(in_dim=get_input_dim(x_dim, y_dim), last_layer=torch.nn.Identity, out_dim=enc_dim) disc = generate_net(in_dim=adv_dim, last_layer=torch.nn.Sigmoid, out_dim=1) fit_kwargs = { "epochs": 1, "batch_size": 4, "steps": None, "print_every": None, "save_model_every": None, "save_images_every": None, "save_losses_every": "1e", "enable_tensorboard": False } testgan = gan(generator=gen, adversary=disc, encoder=enc, x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, folder=None, feature_layer=disc.hidden_part) with pytest.raises(AssertionError): testgan.fit(X_train=X_train_wrong_shape1, y_train=y_train, **fit_kwargs) with pytest.raises(AssertionError): testgan.fit(X_train=X_train_wrong_shape2, y_train=y_train, **fit_kwargs) with pytest.raises(AssertionError): testgan.fit(X_train=X_train_wrong_shape3, y_train=y_train, **fit_kwargs) with pytest.raises(AssertionError): testgan.fit(X_train=X_train_wrong_shape4, y_train=y_train, **fit_kwargs) with pytest.raises(AssertionError): testgan.fit(X_train=X_train, y_train=y_train, X_test=X_test_wrong_shape, y_test=y_test, **fit_kwargs) with pytest.raises(AssertionError): testgan = gan(generator=gen, adversary=disc, encoder=enc, x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, folder=None, feature_layer="hidden_part")
def __init__(self, encoder, decoder, x_dim, z_dim, y_dim, optim=None, optim_kwargs=None, lambda_KL=10, fixed_noise_size=32, device=None, ngpu=0, folder="./veganModels/cVanillaVAE", secure=True): enc_in_dim = get_input_dim(dim1=x_dim, dim2=y_dim) dec_in_dim = get_input_dim(dim1=z_dim, dim2=y_dim) if secure: AbstractConditionalGenerativeModel._check_conditional_network_input( encoder, in_dim=x_dim, y_dim=y_dim, name="Encoder") AbstractConditionalGenerativeModel._check_conditional_network_input( decoder, in_dim=z_dim, y_dim=y_dim, name="Decoder") self.decoder = Decoder(decoder, input_size=dec_in_dim, device=device, ngpu=ngpu, secure=secure) self.encoder = Encoder(encoder, input_size=enc_in_dim, device=device, ngpu=ngpu, secure=secure) self.autoencoder = Autoencoder(self.encoder, self.decoder) self.neural_nets = {"Autoencoder": self.autoencoder} super().__init__(x_dim=x_dim, z_dim=z_dim, y_dim=y_dim, optim=optim, optim_kwargs=optim_kwargs, feature_layer=None, fixed_noise_size=fixed_noise_size, device=device, folder=folder, ngpu=ngpu, secure=secure) self.mu = nn.Sequential( nn.Flatten(), nn.Linear(np.prod(self.encoder.output_size), np.prod(z_dim)), LayerReshape(shape=z_dim)).to(self.device) self.log_variance = nn.Sequential( nn.Flatten(), nn.Linear(np.prod(self.encoder.output_size), np.prod(z_dim)), LayerReshape(shape=z_dim)).to(self.device) self.lambda_KL = lambda_KL self.hyperparameters["lambda_KL"] = lambda_KL if self.secure: # if self.encoder.output_size == self.z_dim: # raise ValueError( # "Encoder output size is equal to z_dim, but for VAE algorithms the encoder last layers for mu and sigma " + # "are constructed by the algorithm itself.\nSpecify up to the second last layer for this particular encoder." # ) assert (self.decoder.output_size == self.x_dim), ( "Decoder output shape must be equal to x_dim. {} vs. {}.". format(self.decoder.output_size, self.x_dim))