Exemplo n.º 1
0
    def create_discriminator_and_generator(self):
        print('Creating Discriminator and Generator ...')
        # Discriminator
        D_A = self.Discriminator()
        D_B = self.Discriminator()
        loss_weights_D = [0.5]
        image_A = Input(shape=self.data_shape)
        image_B = Input(shape=self.data_shape)
        guess_A = D_A(image_A)
        guess_B = D_B(image_B)
        self.D_A = Model(inputs=image_A, outputs=guess_A, name='D_A')
        self.D_B = Model(inputs=image_B, outputs=guess_B, name='D_B')
        self.D_A.compile(optimizer=self.opt_D,
                         loss=self.lse,
                         loss_weights=loss_weights_D)
        self.D_B.compile(optimizer=self.opt_D,
                         loss=self.lse,
                         loss_weights=loss_weights_D)
        # Use containers to avoid falsy keras error about weight descripancies
        self.D_A_static = Network(inputs=image_A,
                                  outputs=guess_A,
                                  name='D_A_static')
        self.D_B_static = Network(inputs=image_B,
                                  outputs=guess_B,
                                  name='D_B_static')
        # Do note update discriminator weights during generator training
        self.D_A_static.trainable = False
        self.D_B_static.trainable = False

        # Generators
        self.G_A2B = self.Generator(name='G_A2B')
        self.G_B2A = self.Generator(name='G_B2A')
        real_A = Input(shape=self.data_shape, name='real_A')
        real_B = Input(shape=self.data_shape, name='real_B')
        synthetic_B = self.G_A2B(real_A)
        synthetic_A = self.G_B2A(real_B)
        dA_guess_synthetic = self.D_A_static(synthetic_A)
        dB_guess_synthetic = self.D_B_static(synthetic_B)
        reconstructed_A = self.G_B2A(synthetic_B)
        reconstructed_B = self.G_A2B(synthetic_A)
        model_outputs = [reconstructed_A, reconstructed_B]
        compile_losses = [self.cycle_loss, self.cycle_loss, self.lse, self.lse]
        compile_weights = [
            self.lambda_1, self.lambda_2, self.lambda_D, self.lambda_D
        ]
        model_outputs.append(dA_guess_synthetic)
        model_outputs.append(dB_guess_synthetic)
        if self.use_supervised_learning:
            model_outputs.append(synthetic_A)
            model_outputs.append(synthetic_B)
            compile_losses.append('MAE')
            compile_losses.append('MAE')
            compile_weights.append(self.supervised_weight)
            compile_weights.append(self.supervised_weight)
        self.G_model = Model(inputs=[real_A, real_B],
                             outputs=model_outputs,
                             name='G_model')
        self.G_model.compile(optimizer=self.opt_G,
                             loss=compile_losses,
                             loss_weights=compile_weights)
Exemplo n.º 2
0
    def __init__(self,
                 config: GARMConfig):
        self._config = config

        self._rep_net = self.build_representation()
        self._reg_net = self.build_regression()

        self._dis_net = self.build_discriminator()
        self._dis_net.compile(RMSprop(0.001),
                              loss=['binary_crossentropy'],
                              metrics=['acc'])  # 训练判别器的,输入为x, t, y

        x_input = Input(shape=(self._config.n_input, ))
        t_input = Input(shape=(self._config.n_treat, ))
        x_rep = self._rep_net(x_input)
        y_pred = self._reg_net([x_rep, t_input])
        self._generator = Model(inputs=(x_input, t_input), outputs=y_pred)  # 训练生成器,输入为x, t
        if self._config.categorical == 0:
            self._generator.compile(RMSprop(0.001),
                                    loss=['mean_squared_error'])
        else:
            self._generator.compile(RMSprop(0.001),
                                    loss=['binary_crossentropy'],
                                    metrics=['acc'])

        self._dis_net_fixed = Network(inputs=self._dis_net.input, outputs=self._dis_net.output)
        self._dis_net_fixed.trainable = False
        logits = self._dis_net_fixed([x_rep, t_input, y_pred])
        self._gan = Model(inputs=(x_input, t_input), outputs=logits)  # 训练生成器用的,只需要x和t的输入
        self._gan.compile(RMSprop(0.001),
                          loss=['binary_crossentropy'],
                          metrics=['acc'])
Exemplo n.º 3
0
    def __init__(self):
        #Input shape
        self.coordinates = 2
        self.annotations = 18
        self.flames = 32
        self.pose_movie_shape = (self.flames, self.annotations,
                                 self.coordinates)
        self.latent_dim = 100
        self.msgan_parameter = 0.1

        optimizer = Adam(0.0002, 0.5)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='binary_crossentropy',
                                   optimizer=optimizer,
                                   metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        self.generator2 = Network(inputs=self.generator.inputs,
                                  outputs=self.generator.outputs)

        # The generator takes noise as input and generates imgs
        z1 = Input(shape=(self.latent_dim, ))
        z2 = Input(shape=(self.latent_dim, ))
        #gen1 = self.generator(z[0])
        poses1 = self.generator(z1)
        poses2 = self.generator2(z2)

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator takes generated images as input and determines validity
        valid = self.discriminator(poses1)

        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator

        loss = Lambda(self.msgan_loss, output_shape=(1, ),
                      name="msgan_loss")([z1, z2, poses1, poses2, valid])
        self.combined = Model([z1, z2], loss)

        #self.combined.compile(loss={"msgan_loss" : lambda y_true, y_pred : y_pred,
        self.combined.compile(loss='binary_crossentropy',
                              optimizer=optimizer,
                              metrics=['accuracy'])
Exemplo n.º 4
0
guess_B = D_B(image_B)
model['D_A'] = Model(inputs=image_A, outputs=guess_A, name='D_A_model')
model['D_B'] = Model(inputs=image_B, outputs=guess_B, name='D_B_model')

# Compile discriminator models
loss_weights_D = [0.5]  # 0.5 since we train on real and synthetic images
model['D_A'].compile(optimizer=model['opt_D'],
                     loss=mse,
                     loss_weights=loss_weights_D)
model['D_B'].compile(optimizer=model['opt_D'],
                     loss=mse,
                     loss_weights=loss_weights_D)

# Use containers to make a static copy of discriminators, used when training the generators
model['D_A_static'] = Network(inputs=image_A,
                              outputs=guess_A,
                              name='D_A_static_model')
model['D_B_static'] = Network(inputs=image_B,
                              outputs=guess_B,
                              name='D_B_static_model')

# Do not update discriminator weights during generator training
model['D_A_static'].trainable = False
model['D_B_static'].trainable = False

# Build generators
model['G_A2B'] = build_generator(model, opt, name='G_A2B_model')
model['G_B2A'] = build_generator(model, opt, name='G_B2A_model')

# Define full CycleGAN model, used for training the generators
real_A = Input(shape=opt['img_shape'], name='real_A')
Exemplo n.º 5
0
    def __init__(self,
                 lr_D=2e-4,
                 lr_G=2e-4,
                 image_shape=(256 * 1, 256 * 1, 3),
                 date_time_string_addition='_test',
                 image_folder='Upload'):
        self.img_shape = image_shape
        self.channels = self.img_shape[-1]
        self.normalization = InstanceNormalization
        # Hyper parameters
        self.lambda_1 = 10.0  # Cyclic loss weight A_2_B
        self.lambda_2 = 10.0  # Cyclic loss weight B_2_A
        self.lambda_D = 1.0  # Weight for loss from discriminator guess on synthetic images
        self.learning_rate_D = lr_D
        self.learning_rate_G = lr_G
        self.generator_iterations = 1  # Number of generator training iterations in each training loop
        self.discriminator_iterations = 1  # Number of generator training iterations in each training loop
        self.beta_1 = 0.5
        self.beta_2 = 0.999
        self.batch_size = 1
        self.epochs = 200  # choose multiples of 25 since the models are save each 25th epoch
        self.save_interval = 1
        self.synthetic_pool_size = 50

        # Linear decay of learning rate, for both discriminators and generators
        self.use_linear_decay = False
        self.decay_epoch = 101  # The epoch where the linear decay of the learning rates start

        # Identity loss - sometimes send images from B to G_A2B (and the opposite) to teach identity mappings
        self.use_identity_learning = False
        self.identity_mapping_modulus = 10  # Identity mapping will be done each time the iteration number is divisable with this number

        # PatchGAN - if false the discriminator learning rate should be decreased
        self.use_patchgan = True

        # Multi scale discriminator - if True the generator have an extra encoding/decoding step to match discriminator information access
        self.use_multiscale_discriminator = False

        # Resize convolution - instead of transpose convolution in deconvolution layers (uk) - can reduce checkerboard artifacts but the blurring might affect the cycle-consistency
        self.use_resize_convolution = False

        # Supervised learning part - for MR images - comparison
        self.use_supervised_learning = False
        self.supervised_weight = 10.0

        # Fetch data during training instead of pre caching all images - might be necessary for large datasets
        self.use_data_generator = False

        # Tweaks
        self.REAL_LABEL = 1.0  # Use e.g. 0.9 to avoid training the discriminators to zero loss

        # Used as storage folder name
        self.date_time = time.strftime(
            '%Y%m%d-%H%M%S', time.localtime()) + date_time_string_addition

        # optimizer
        self.opt_D = Adam(self.learning_rate_D, self.beta_1, self.beta_2)
        self.opt_G = Adam(self.learning_rate_G, self.beta_1, self.beta_2)

        # ======= Discriminator model ==========
        if self.use_multiscale_discriminator:
            D_A = self.modelMultiScaleDiscriminator()
            D_B = self.modelMultiScaleDiscriminator()
            loss_weights_D = [
                0.5, 0.5
            ]  # 0.5 since we train on real and synthetic images
        else:
            D_A = self.modelDiscriminator()
            D_B = self.modelDiscriminator()
            loss_weights_D = [
                0.5
            ]  # 0.5 since we train on real and synthetic images
        # D_A.summary()

        # Discriminator builds
        image_A = Input(shape=self.img_shape)
        image_B = Input(shape=self.img_shape)
        guess_A = D_A(image_A)
        guess_B = D_B(image_B)
        self.D_A = Model(inputs=image_A, outputs=guess_A, name='D_A_model')
        self.D_B = Model(inputs=image_B, outputs=guess_B, name='D_B_model')

        # self.D_A.summary()
        # self.D_B.summary()
        self.D_A.compile(optimizer=self.opt_D,
                         loss=self.lse,
                         loss_weights=loss_weights_D)
        self.D_B.compile(optimizer=self.opt_D,
                         loss=self.lse,
                         loss_weights=loss_weights_D)

        # Use Networks to avoid falsy keras error about weight descripancies
        self.D_A_static = Network(inputs=image_A,
                                  outputs=guess_A,
                                  name='D_A_static_model')
        self.D_B_static = Network(inputs=image_B,
                                  outputs=guess_B,
                                  name='D_B_static_model')

        # ======= Generator model ==========
        # Do note update discriminator weights during generator training
        self.D_A_static.trainable = False
        self.D_B_static.trainable = False

        # Generators
        self.G_A2B = self.modelGenerator(name='G_A2B_model')
        self.G_B2A = self.modelGenerator(name='G_B2A_model')
        # self.G_A2B.summary()

        if self.use_identity_learning:
            self.G_A2B.compile(optimizer=self.opt_G, loss='MAE')
            self.G_B2A.compile(optimizer=self.opt_G, loss='MAE')

        # Generator builds
        real_A = Input(shape=self.img_shape, name='real_A')
        real_B = Input(shape=self.img_shape, name='real_B')
        synthetic_B = self.G_A2B(real_A)
        synthetic_A = self.G_B2A(real_B)
        dA_guess_synthetic = self.D_A_static(synthetic_A)
        dB_guess_synthetic = self.D_B_static(synthetic_B)
        reconstructed_A = self.G_B2A(synthetic_B)
        reconstructed_B = self.G_A2B(synthetic_A)

        model_outputs = [reconstructed_A, reconstructed_B]
        compile_losses = [self.cycle_loss, self.cycle_loss, self.lse, self.lse]
        compile_weights = [
            self.lambda_1, self.lambda_2, self.lambda_D, self.lambda_D
        ]

        if self.use_multiscale_discriminator:
            for _ in range(2):
                compile_losses.append(self.lse)
                compile_weights.append(
                    self.lambda_D
                )  # * 1e-3)  # Lower weight to regularize the model
            for i in range(2):
                model_outputs.append(dA_guess_synthetic[i])
                model_outputs.append(dB_guess_synthetic[i])
        else:
            model_outputs.append(dA_guess_synthetic)
            model_outputs.append(dB_guess_synthetic)

        if self.use_supervised_learning:
            model_outputs.append(synthetic_A)
            model_outputs.append(synthetic_B)
            compile_losses.append('MAE')
            compile_losses.append('MAE')
            compile_weights.append(self.supervised_weight)
            compile_weights.append(self.supervised_weight)

        self.G_model = Model(inputs=[real_A, real_B],
                             outputs=model_outputs,
                             name='G_model')

        self.G_model.compile(optimizer=self.opt_G,
                             loss=compile_losses,
                             loss_weights=compile_weights)
        # self.G_A2B.summary()

        # ======= Data ==========
        # Use 'None' to fetch all available images
        nr_A_train_imgs = None
        nr_B_train_imgs = None
        nr_A_test_imgs = None
        nr_B_test_imgs = None

        if self.use_data_generator:
            print('--- Using dataloader during training ---')
        else:
            print('--- Caching data ---')
        sys.stdout.flush()

        if self.use_data_generator:
            self.data_generator = load_data.load_data(
                nr_of_channels=self.channels,
                batch_size=self.batch_size,
                generator=True,
                subfolder=image_folder)

            # Only store test images
            nr_A_train_imgs = 0
            nr_B_train_imgs = 0

        data = load_data.load_data(nr_of_channels=self.channels,
                                   batch_size=self.batch_size,
                                   nr_A_train_imgs=nr_A_train_imgs,
                                   nr_B_train_imgs=nr_B_train_imgs,
                                   nr_A_test_imgs=nr_A_test_imgs,
                                   nr_B_test_imgs=nr_B_test_imgs,
                                   subfolder=image_folder)

        self.A_train = data["trainA_images"]
        self.B_train = data["trainB_images"]
        self.A_test = data["testA_images"]
        self.B_test = data["testB_images"]
        self.testA_image_names = data["testA_image_names"]
        self.testB_image_names = data["testB_image_names"]
        if not self.use_data_generator:
            print('Data has been loaded')

        # ======= Create designated run folder and store meta data ==========
        directory = os.path.join('images', self.date_time)
        if not os.path.exists(directory):
            os.makedirs(directory)
        self.writeMetaDataToJSON()

        # ======= Avoid pre-allocating GPU memory ==========
        # TensorFlow wizardry
        config = tf.ConfigProto()

        # Don't pre-allocate memory; allocate as-needed
        config.gpu_options.allow_growth = True

        # Create a session with the above options specified.
        K.tensorflow_backend.set_session(tf.Session(config=config))

        # ===== Tests ======
        # Simple Model
        #         self.G_A2B = self.modelSimple('simple_T1_2_T2_model')
        #         self.G_B2A = self.modelSimple('simple_T2_2_T1_model')
        #         self.G_A2B.compile(optimizer=Adam(), loss='MAE')
        #         self.G_B2A.compile(optimizer=Adam(), loss='MAE')
        #         # self.trainSimpleModel()
        #         self.load_model_and_generate_synthetic_images()

        # ======= Initialize training ==========
        sys.stdout.flush()
        #plot_model(self.G_A2B, to_file='GA2B_expanded_model_new.png', show_shapes=True)
        #self.train(epochs=self.epochs, batch_size=self.batch_size, save_interval=self.save_interval)
        self.load_model_and_generate_synthetic_images()
Exemplo n.º 6
0
    def __init__(self):
        # Rescale -1 to 1
        self.X_train = X_train / 127.5 - 1.
        self.Y_train = Y_train / 127.5 - 1.
        # X_test = X_test / 127.5 - 1.
        # Y_test = Y_test / 127.5 - 1.

        # Prameters
        self.height = self.X_train.shape[1]
        self.width = self.X_train.shape[2]
        self.channels = self.X_train.shape[3]
        self.latent_dimension = self.width

        self.discriminator_optimizer = Adam(lr=2e-4, beta_1=0.5, beta_2=0.999)
        self.generator_optimizer = Adam(lr=2e-4, beta_1=0.5, beta_2=0.999)

        self.batch = int(self.X_train.shape[0] / batch_size)

        self.n_show_image = 1  # Number of images to show
        self.history = []
        self.number = 0

        # Build and compile the discriminator
        discriminator_A = self.build_discriminator()
        discriminator_B = self.build_discriminator()

        image_A = Input(shape=(self.height, self.width, self.channels))
        image_B = Input(shape=(self.height, self.width, self.channels))

        guess_A = discriminator_A(image_A)
        guess_B = discriminator_B(image_B)

        self.discriminator_A = Model(inputs=image_A, outputs=guess_A)
        self.discriminator_B = Model(inputs=image_B, outputs=guess_B)

        self.discriminator_A.compile(optimizer=self.discriminator_optimizer,
                                     loss=self.least_squares_error,
                                     loss_weights=[0.5])
        self.discriminator_B.compile(optimizer=self.discriminator_optimizer,
                                     loss=self.least_squares_error,
                                     loss_weights=[0.5])

        # Use Networks to avoid falsy keras error about weight descripancies
        self.discriminator_A_static = Network(inputs=image_A, outputs=guess_A)
        self.discriminator_B_static = Network(inputs=image_B, outputs=guess_B)

        # Do note update discriminator weights during generator training
        self.discriminator_A_static.trainable = False
        self.discriminator_B_static.trainable = False

        # Build and compile the generator
        self.generator_A_to_B = self.build_generator()
        self.generator_B_to_A = self.build_generator()

        # If use identity learning
        # self.generator_A_to_B.compile(optimizer = self.generator_optimizer, loss = 'mae')
        # self.generator_B_to_A.compile(optimizer = self.generator_optimzier, loss = 'mae')

        # Generator builds
        real_A = Input(shape=(self.height, self.width, self.channels))
        real_B = Input(shape=(self.height, self.width, self.channels))

        synthetic_A = self.generator_B_to_A(real_B)
        synthetic_B = self.generator_A_to_B(real_A)

        discriminator_A_guess_synthetic = self.discriminator_A_static(
            synthetic_A)
        discriminator_B_guess_synthetic = self.discriminator_B_static(
            synthetic_B)

        reconstructed_A = self.generator_B_to_A(synthetic_B)
        reconstructed_B = self.generator_A_to_B(synthetic_A)

        model_output = [reconstructed_A, reconstructed_B]
        compile_loss = [
            self.cycle_loss, self.cycle_loss, self.least_squares_error,
            self.least_squares_error
        ]
        compile_weights = [10.0, 10.0, 1.0, 1.0]

        model_output.append(discriminator_A_guess_synthetic)
        model_output.append(discriminator_B_guess_synthetic)

        self.generator = Model(inputs=[real_A, real_B], outputs=model_output)
        self.generator.compile(optimizer=self.generator_optimizer,
                               loss=compile_loss,
                               loss_weights=compile_weights)
Exemplo n.º 7
0
    def __init__(self, args):

        # Parse input arguments
        os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)  # Select GPU device
        self.volume_folder = os.path.split(args.dataset.rstrip('/'))[-1]
        batch_size = args.batch
        self.fixedsize = args.fixedsize

        # ======= Data ==========
        print('--- Caching data ---')

        data = load_data_3D(subfolder=self.volume_folder)

        self.channels_A = data["nr_of_channels_A"]
        self.vol_shape_A = data["volume_size_A"] + (self.channels_A, )

        self.channels_B = data["nr_of_channels_B"]
        self.vol_shape_B = data["volume_size_B"] + (self.channels_B, )

        print('volume A shape: ', self.vol_shape_A)
        print('volume B shape: ', self.vol_shape_B)

        if self.fixedsize:
            self.input_shape_A = self.vol_shape_A
            self.input_shape_B = self.vol_shape_B
        else:
            self.input_shape_A = (None, None, None) + (self.channels_A, )
            self.input_shape_B = (None, None, None) + (self.channels_B, )
            print('Using unspecified input size')

        self.A_train = data["trainA_volumes"]
        self.B_train = data["trainB_volumes"]
        self.A_test = data["testA_volumes"]
        self.B_test = data["testB_volumes"]

        self.paired_data = True

        # ===== Model parameters ======
        # Training parameters
        self.lambda_ABA = 10.0  # Cyclic loss weight A_2_B
        self.lambda_BAB = 10.0  # Cyclic loss weight B_2_A
        self.lambda_adversarial = 1.0  # Weight for loss from discriminator guess on synthetic volumes
        self.learning_rate_D = 2e-4
        self.learning_rate_G = 2e-4
        self.generator_iterations = 1  # Number of generator training iterations in each training loop
        self.discriminator_iterations = 1  # Number of generator training iterations in each training loop
        self.synthetic_pool_size = 50  # Size of volume pools used for training the discriminators
        self.beta_1 = 0.5  # Adam parameter
        self.beta_2 = 0.999  # Adam parameter
        self.batch_size = batch_size  # Number of volumes per batch
        self.epochs = 200  # choose multiples of 20 since the models are saved each 20th epoch

        self.save_models = True  # Save or not the generator and discriminator models
        self.save_models_interval = 5  # Number of epoch between saves of generator and discriminator models
        self.save_training_vol = True  # Save or not example training results or only tmp.png
        self.save_training_vol_interval = 1  # Number of epoch between saves of intermediate training results
        self.tmp_vol_update_frequency = 3  # Number of batches between updates of tmp.png
        self.tmp_img_z_A = self.vol_shape_A[2] // 2
        self.tmp_img_z_B = self.vol_shape_B[2] // 2

        # Architecture parameters
        self.use_instance_normalization = True  # Use instance normalization or batch normalization
        self.use_dropout = False  # Dropout in residual blocks
        self.use_bias = True  # Use bias
        self.use_linear_decay = True  # Linear decay of learning rate, for both discriminators and generators
        self.decay_epoch = 101  # The epoch where the linear decay of the learning rates start
        self.use_patchgan = True  # PatchGAN - if false the discriminator learning rate should be decreased
        self.use_resize_convolution = False  # Resize convolution - instead of transpose convolution in deconvolution layers (uk) - can reduce checkerboard artifacts but the blurring might affect the cycle-consistency
        self.discriminator_sigmoid = True
        self.generator_residual_blocks = args.resBlocks
        self.discriminator_layers = args.discLayers
        self.stride_2_layers = args.nStride2
        self.base_discirminator_filters = args.baseDiscFilts
        self.base_generator_filters = args.baseGenFilts

        # Tweaks
        self.REAL_LABEL = 1.0  # Use e.g. 0.9 to avoid training the discriminators to zero loss

        # ===== Architecture =====
        # Normalization
        if self.use_instance_normalization:
            self.normalization = InstanceNormalization
        else:
            self.normalization = BatchNormalization

        # Optimizers
        self.opt_D = Adam(self.learning_rate_D, self.beta_1, self.beta_2)
        self.opt_G = Adam(self.learning_rate_G, self.beta_1, self.beta_2)

        # Build discriminators
        D_A = self.build_discriminator(self.input_shape_A)
        D_B = self.build_discriminator(self.input_shape_B)

        # Define discriminator models
        volume_A = Input(shape=self.input_shape_A)
        volume_B = Input(shape=self.input_shape_B)
        guess_A = D_A(volume_A)
        guess_B = D_B(volume_B)
        self.D_A = Model(inputs=volume_A, outputs=guess_A, name='D_A_model')
        self.D_B = Model(inputs=volume_B, outputs=guess_B, name='D_B_model')

        # Compile discriminator models
        loss_weights_D = [0.5
                          ]  # 0.5 since we train on real and synthetic volumes
        self.D_A.compile(optimizer=self.opt_D,
                         loss=self.lse,
                         loss_weights=loss_weights_D)
        self.D_B.compile(optimizer=self.opt_D,
                         loss=self.lse,
                         loss_weights=loss_weights_D)

        # Use containers to make a static copy of discriminators, used when training the generators
        self.D_A_static = Network(inputs=volume_A,
                                  outputs=guess_A,
                                  name='D_A_static_model')
        self.D_B_static = Network(inputs=volume_B,
                                  outputs=guess_B,
                                  name='D_B_static_model')

        # Do note update discriminator weights during generator training
        self.D_A_static.trainable = False
        self.D_B_static.trainable = False

        # Build generators
        self.G_A2B = self.build_generator(self.input_shape_A,
                                          self.input_shape_B,
                                          name='G_A2B_model')
        self.G_B2A = self.build_generator(self.input_shape_B,
                                          self.input_shape_A,
                                          name='G_B2A_model')

        # Define full CycleGAN model, used for training the generators
        real_A = Input(shape=self.input_shape_A, name='real_A')
        real_B = Input(shape=self.input_shape_B, name='real_B')
        synthetic_B = self.G_A2B(real_A)
        synthetic_A = self.G_B2A(real_B)
        dB_guess_synthetic = self.D_B_static(synthetic_B)
        dA_guess_synthetic = self.D_A_static(synthetic_A)
        reconstructed_A = self.G_B2A(synthetic_B)
        reconstructed_B = self.G_A2B(synthetic_A)

        # Compile full CycleGAN model
        model_outputs = [
            reconstructed_A, reconstructed_B, dB_guess_synthetic,
            dA_guess_synthetic
        ]
        compile_losses = [self.cycle_loss, self.cycle_loss, self.lse, self.lse]
        compile_weights = [
            self.lambda_ABA, self.lambda_BAB, self.lambda_adversarial,
            self.lambda_adversarial
        ]

        self.G_model = Model(inputs=[real_A, real_B],
                             outputs=model_outputs,
                             name='G_model')

        self.G_model.compile(optimizer=self.opt_G,
                             loss=compile_losses,
                             loss_weights=compile_weights)

        # ===== Folders and configuration =====
        if args.tag:
            # Calculate discriminator receptive field
            nDiscFiltsStride2 = np.clip(self.stride_2_layers, 0,
                                        self.discriminator_layers - 1)
            nDiscFiltsStride1 = self.discriminator_layers - nDiscFiltsStride2

            receptField = int((1 + 3 * nDiscFiltsStride1) *
                              2**nDiscFiltsStride2 +
                              2**(nDiscFiltsStride2 + 1) - 2)

            # Generate tag
            self.tag = '_LR_{}_RL_{}_DF_{}_GF_{}_RF_{}'.format(
                self.learning_rate_D, self.generator_residual_blocks,
                self.base_discirminator_filters, self.base_generator_filters,
                receptField)
        else:
            self.tag = ''

        if args.extra_tag:
            self.tag = self.tag + '_' + args.extra_tag

        self.date_time = time.strftime(
            '%Y%m%d-%H%M%S',
            time.localtime()) + '-' + self.volume_folder + self.tag

        # Output folder for run data and volumes
        self.out_dir = os.path.join('runs', self.date_time)
        if not os.path.exists(self.out_dir):
            os.makedirs(self.out_dir)

        if self.save_training_vol:
            self.out_dir_volumes = os.path.join(self.out_dir,
                                                'training_volumes')
            if not os.path.exists(self.out_dir_volumes):
                os.makedirs(self.out_dir_volumes)

        # Output folder for saved models
        if self.save_models:
            self.out_dir_models = os.path.join(self.out_dir, 'models')
            if not os.path.exists(self.out_dir_models):
                os.makedirs(self.out_dir_models)

        self.write_metadata_to_JSON()

        # Don't pre-allocate GPU memory; allocate as needed
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        K.tensorflow_backend.set_session(tf.Session(config=config))

        # ======= Initialize training ==========
        sys.stdout.flush()
        # plot_model(self.G_A2B, to_file='GA2B_expanded_model_new.png', show_shapes=True)
        self.train(epochs=self.epochs, batch_size=self.batch_size)
Exemplo n.º 8
0
def model_create_new(dataset):
    #Function to create model and return
    emb_size, hidden_size = cfg.TEXT.EMBEDDING_DIM, cfg.TEXT.HIDDEN_DIM // 2
    emb_size_dec, hidden_size_dec = cfg.TEXT.EMBEDDING_DIM_DEC, cfg.TEXT.HIDDEN_DIM_DEC
    vocab_size = dataset.n_words

    #DECODER I2T
    CR_model = CNN_ENCODER_RNN_DECODER(emb_size_dec, hidden_size_dec,
                                       vocab_size)
    if not cfg.TRAIN.RNN_DEC == '':
        CR_model.load_weights(cfg.TRAIN.RNN_DEC)
    CR_model.trainable = False
    # T2I
    RNN_model, words_emb, sent_emb, c_code = \
        RNN_ENCODER(emb_size, hidden_size,vocab_size, rec_unit=cfg.RNN_TYPE)
    netGs_out, out_image_block, attm, atts, z_code_input, mask_input = \
        G_DCGAN(sent_emb, words_emb, c_code)

    cap_input, eps_input = RNN_model.get_input_at(0)

    if cfg.TREE.BRANCH_NUM == 1:
        D_h_logits, D_hc_logits, D_pic_input = D_NET64(sent_emb)
    if cfg.TREE.BRANCH_NUM == 2:
        D_h_logits, D_hc_logits, D_pic_input = D_NET128(sent_emb)
    if cfg.TREE.BRANCH_NUM == 3:
        D_h_logits, D_hc_logits, D_pic_input = D_NET256(sent_emb)
    #For learning D
    D_model = Model([D_pic_input, cap_input], [D_h_logits, D_hc_logits],
                    name="Discriminator")
    #D weight load
    if not cfg.TRAIN.NET_D == "":
        print("\nload D_model from" + cfg.TRAIN.NET_D)
        D_model.load_weights(cfg.TRAIN.NET_D)
    D_model.compile(loss={
        'D_h_out_layre': 'binary_crossentropy',
        'D_hc_out_layre': 'binary_crossentropy'
    },
                    loss_weights=[0.2, 0.8],
                    optimizer=Adam(lr=cfg.TRAIN.D_LR,
                                   beta_1=cfg.TRAIN.D_BETA1),
                    metrics=['accuracy'])

    n_D_trainable = len(D_model.trainable_weights)

    D_model_freeze = Network([D_pic_input, cap_input],
                             [D_h_logits, D_hc_logits],
                             name="Discriminator")

    #G (for D learning output)
    if cfg.TREE.BRANCH_NUM > 0:
        init_G_model = Model([cap_input, eps_input, z_code_input],
                             netGs_out[0],
                             name="init_G")
        G_output = init_G_model.output
        if not cfg.TRAIN.INIT_NET_G == "":
            init_G_model.load_weights(cfg.TRAIN.INIT_NET_G, by_name=True)

    if cfg.TREE.BRANCH_NUM > 1:
        next_G_model128 = Model(
            [cap_input, eps_input, z_code_input, mask_input],
            netGs_out[1],
            name="next_G128")
        G_output = next_G_model128.output
        if not cfg.TRAIN.NEXT128_NET_G == "":
            next_G_model128.load_weights(cfg.TRAIN.NEXT128_NET_G, by_name=True)

    if cfg.TREE.BRANCH_NUM > 2:
        next_G_model256 = Model(
            [cap_input, eps_input, z_code_input, mask_input],
            netGs_out[2],
            name="next_G256")
        G_output = next_G_model256.output
        if not cfg.TRAIN.NEXT256_NET_G == "":
            next_G_model256.load_weights(cfg.TRAIN.NEXT256_NET_G, by_name=True)
    #Coupling with output layer and weight load
    out_img = out_image_block(G_output)
    if cfg.TREE.BRANCH_NUM == 1:
        G_model = Model(init_G_model.get_input_at(0),
                        out_img,
                        name="Generator")
        if not cfg.TRAIN.INIT_NET_G == "":
            print("\nload G_model from" + cfg.TRAIN.INIT_NET_G)
            G_model.load_weights(cfg.TRAIN.INIT_NET_G)
    else:  #2 or 3
        G_model = Model(init_G_model.get_input_at(0) + [mask_input],
                        out_img,
                        name="Generator")
        if (not cfg.TRAIN.NEXT128_NET_G == "") and (cfg.TREE.BRANCH_NUM == 2):
            G_model.load_weights(cfg.TRAIN.NEXT128_NET_G)
        if (not cfg.TRAIN.NEXT256_NET_G == "") and (cfg.TREE.BRANCH_NUM == 3):
            G_model.load_weights(cfg.TRAIN.NEXT256_NET_G)

    n_G_trainable = len(G_model.trainable_weights)

    #GRD (For learning G)
    D_model_freeze.trainable = False
    DG_h_logits, DG_hc_logits = D_model_freeze([out_img, sent_emb])
    cr_cap_input = CR_model.get_input_at(0)[1]
    cr_logith = CR_model([out_img, cr_cap_input])  #pic_input #cap_input
    GRD_model = Model(G_model.get_input_at(0) + [cr_cap_input],
                      [DG_h_logits, DG_hc_logits, cr_logith],
                      name="GRD_model")
    GRD_model.compile(loss={
        'Discriminator': binary_crossentropy,
        'CNN_RNN_DEC': categorical_crossentropy
    },
                      loss_weights=[0.5, 0.5, cfg.TRAIN.RNN_DEC_LOSS_W],
                      optimizer=Adam(lr=cfg.TRAIN.G_LR,
                                     beta_1=cfg.TRAIN.G_BETA1),
                      metrics=['accuracy'])

    if not len(D_model._collected_trainable_weights) == n_D_trainable:
        print(
            "\n D collected trainable weights {:,} does not equals trainable weights {:,}!"
            .format(len(D_model._collected_trainable_weights), n_D_trainable))
    if not len(GRD_model._collected_trainable_weights) == n_G_trainable:
        print(
            "\n G collected trainable weights {:,} does not equals trainable weights {:,}!"
            .format(len(GRD_model._collected_trainable_weights),
                    n_G_trainable))

    # SIM for computing cosine similarity between real captions and generated captions
    image_input = CR_model.get_input_at(0)[0]
    caption_input = CR_model.get_input_at(0)[1]
    logit = CR_model([image_input, caption_input])
    cap_label_input = Input(shape=(cfg.TEXT.WORDS_NUM + 1, vocab_size),
                            name="cap_label_input")
    re_cap_label_input = Reshape(
        (1, (cfg.TEXT.WORDS_NUM + 1) * vocab_size))(cap_label_input)
    re_cr_logith = Reshape((1, (cfg.TEXT.WORDS_NUM + 1) * vocab_size))(logit)
    cosin = Dot(axes=2, normalize=True)([re_cap_label_input, re_cr_logith])
    SIM_model = Model(CR_model.get_input_at(0) + [cap_label_input], [cosin],
                      name="SIM_model")

    return G_model, D_model, GRD_model, CR_model, RNN_model, SIM_model
Exemplo n.º 9
0
def make_decoder(input_size, fixed=False):
    # Reveal network
    reveal_input = Input(shape=(input_size))

    # Adding Gaussian noise with 0.01 standard deviation.
    input_with_noise = GaussianNoise(0.01, name='output_C_noise')(reveal_input)

    x3 = Conv2D(50, (3, 3),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev0_3x3')(input_with_noise)
    x4 = Conv2D(10, (4, 4),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev0_4x4')(input_with_noise)
    x5 = Conv2D(5, (5, 5),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev0_5x5')(input_with_noise)
    x = concatenate([x3, x4, x5])

    x3 = Conv2D(50, (3, 3),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev1_3x3')(x)
    x4 = Conv2D(10, (4, 4),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev1_4x4')(x)
    x5 = Conv2D(5, (5, 5),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev1_5x5')(x)
    x = concatenate([x3, x4, x5])

    x3 = Conv2D(50, (3, 3),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev2_3x3')(x)
    x4 = Conv2D(10, (4, 4),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev2_4x4')(x)
    x5 = Conv2D(5, (5, 5),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev2_5x5')(x)
    x = concatenate([x3, x4, x5])

    x3 = Conv2D(50, (3, 3),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev3_3x3')(x)
    x4 = Conv2D(10, (4, 4),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev3_4x4')(x)
    x5 = Conv2D(5, (5, 5),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev3_5x5')(x)
    x = concatenate([x3, x4, x5])

    x3 = Conv2D(50, (3, 3),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev4_3x3')(x)
    x4 = Conv2D(10, (4, 4),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev4_4x4')(x)
    x5 = Conv2D(5, (5, 5),
                strides=(1, 1),
                padding='same',
                activation='relu',
                name='conv_rev5_5x5')(x)
    x = concatenate([x3, x4, x5])

    output_Sprime = Conv2D(3, (3, 3),
                           strides=(1, 1),
                           padding='same',
                           activation='relu',
                           name='output_S')(x)

    if not fixed:
        return Model(inputs=reveal_input,
                     outputs=output_Sprime,
                     name='Decoder')
    else:
        return Network(inputs=reveal_input,
                       outputs=output_Sprime,
                       name='DecoderFixed')
Exemplo n.º 10
0
    def setup_model(self):

        # initial image pooling
        self.image_pooling = ImagePool(self.fake_pool_size)
        # optimizer
        self.opt_D = Adam(self.lr_D, self.beta_1, self.beta_2)
        self.opt_G = Adam(self.lr_G, self.beta_1, self.beta_2)

        # setup discriminator model
        self.D_A = model_discriminator(self.img_shape)
        self.D_B = model_discriminator(self.img_shape)

        self.D_A.summary()

        self.loss_weights_D = [0.5]
        self.img_A = Input(shape=self.img_shape)  # real image
        self.img_B = Input(shape=self.img_shape)

        # discriminator build
        self.guess_A = self.D_A(self.img_A)
        self.guess_B = self.D_B(self.img_B)

        self.D_A = Model(inputs=self.img_A,
                         outputs=self.guess_A,
                         name='D_A_Model')  #name for save model
        self.D_B = Model(inputs=self.img_B,
                         outputs=self.guess_B,
                         name='D_B_Model')

        self.D_A.compile(optimizer=self.opt_D, \
                    loss='mse', \
                    loss_weights=self.loss_weights_D, \
                    metrics=['accuracy'])

        self.D_B.compile(optimizer=self.opt_D, \
                    loss='mse', \
                    loss_weights=self.loss_weights_D, \
                    metrics=['accuracy'])

        # for generator model, we do not train discriminator
        self.D_A_static = Network(inputs=self.img_A,
                                  outputs=self.guess_A,
                                  name='D_A_static_model')
        self.D_B_static = Network(inputs=self.img_B,
                                  outputs=self.guess_B,
                                  name='D_B_static_model')

        #generator setup
        self.G_A2B = model_generator(self.channels,
                                     self.img_shape,
                                     name='G_A2B_model')
        self.G_B2A = model_generator(self.channels,
                                     self.img_shape,
                                     name='G_B2A_model')

        self.G_A2B.summary()

        # # import image
        # self.img_A = Input(shape=self.img_shape)
        # self.img_B = Input(shape=self.img_shape)

        # generate fake images, transfer image from A to B
        self.fake_B = self.G_A2B(self.img_A)
        self.fake_A = self.G_B2A(self.img_B)

        # reconstruction, transfer to original image from fake image
        self.reconstor_A = self.G_B2A(self.fake_B)
        self.reconstor_B = self.G_A2B(self.fake_A)

        self.D_A_static.trainable = False
        self.D_B_static.trainable = False

        # Discriminators determines validity of translated images
        self.valid_A = self.D_A_static(self.fake_A)
        self.valid_B = self.D_A_static(self.fake_B)

        # identity learning
        self.identity_A = self.G_B2A(self.img_A)
        self.identity_B = self.G_A2B(self.img_B)

        # combined two models and compile
        # ombined model trains generators to fool discriminators

        self.combined_model = Model(inputs=[self.img_A, self.img_B], \
                                    outputs=[self.valid_A, self.valid_B, \
                                    self.reconstor_A, self.reconstor_B, \
                                    self.identity_A, self.identity_B],
                                    name='Combined_G_model')

        self.combined_model.compile(loss=['mse', 'mse', \
                                           'mae', 'mae', \
                                           'mae', 'mae'],\
                                    loss_weights=[self.lambda_D, self.lambda_D, \
                                                  self.lambda_A, self.lambda_B, \
                                                  self.lambda_id_A, self.lambda_id_B],\
                                    optimizer=self.opt_G)

        self.config = tf.ConfigProto()
        self.config.gpu_options.allow_growth = True

        # Create a session with the above options specified.
        kerasbackend.tensorflow_backend.set_session(
            tf.Session(config=self.config))

        # patchGAN
        # output shape of D(PatchGAN)
        patch = int(self.img_rows / 2**4)
        self.disc_patch = (patch, patch, 1)
Exemplo n.º 11
0
    def __init__(self,
                 opt,
                 image_shape=(256 * 1, 256 * 1, 3),
                 load_training_data=True,
                 normalization=InstanceNormalization,
                 ):

        self.task = opt.task
        self.im_w = opt.im_w
        self.im_h = opt.im_h

        self.data_root = opt.data_root

        self.img_shape = image_shape
        self.channels = self.img_shape[-1]

        # Fetch data during training instead of pre caching all images
        self.use_data_generator = True
        self.generator_architecture = opt.generator_architecture
        self.use_norm = opt.use_norm
        self.add_extra_conv = opt.add_extra_conv

        self.image_shapeA = (opt.im_w * 1, opt.im_h * 1, 3)
        self.image_shapeA_in = (None, None, 3)
        if self.task == 'Long2Short_raw':
            self.image_shapeB = (opt.im_w * 1, opt.im_h * 1, 1)
            self.image_shapeB_in = (None, None, 3)
        else:
            self.image_shapeB = (opt.im_w * 1, opt.im_h * 1, 3)
            self.image_shapeB_in = (None, None, 3)

        # Identity loss - sometimes send images from B to G_A2B (and the opposite) to teach identity mappings
        self.use_identity_learning = opt.use_identity_learning
        self.identity_mapping_modulus = opt.identity_mapping_modulus  # Identity mapping will be done each time the iteration number is divisable with this number

        # PatchGAN - if false the discriminator learning rate should be decreased
        self.use_patchgan = opt.use_patchgan

        self.normalization = normalization

        # Loss hyperparameters
        self.lambda_1 = opt.lambda_1  # Cyclic loss weight A_2_B
        self.lambda_2 = opt.lambda_2  # Cyclic loss weight B_2_A
        self.lambda_D = opt.lambda_D  # Weight for loss from discriminator guess on synthetic images

        # Learning rates
        self.learning_rate_D = opt.lr_D
        self.learning_rate_G = opt.lr_G

        self.beta_1 = opt.beta_1
        self.beta_2 = opt.beta_2
        self.batch_size = 1
        self.clipvalue = opt.clipvalue
        self.epsilon_norm = opt.epsilon_norm

        # self.crop_res = opt.crop_res

        # Resize convolution - instead of transpose convolution in deconvolution layers (uk) - can reduce checkerboard artifacts but the blurring might affect the cycle-consistency
        self.use_resize_convolution = opt.use_resize_convolution

        # Supervised learning part
        self.use_supervised_learning = opt.use_supervised_learning
        self.supervised_weight = opt.supervised_weight
        self.supervised_loss = opt.supervised_loss

        # optimizer
        if opt.clipvalue is not None:
            self.opt_D = Adam(self.learning_rate_D, self.beta_1, self.beta_2, clipvalue=self.clipvalue)
            self.opt_G = Adam(self.learning_rate_G, self.beta_1, self.beta_2, clipvalue=self.clipvalue)
        else:
            self.opt_D = Adam(self.learning_rate_D, self.beta_1, self.beta_2)
            self.opt_G = Adam(self.learning_rate_G, self.beta_1, self.beta_2)

        # # ======= Discriminator model ==========

        if self.generator_architecture == 'ICCV':

            D_A = modelDiscriminator(self.image_shapeA, use_patchgan=self.use_patchgan,
                                     disc_use_4_layers=True)
            D_B = modelDiscriminator(self.image_shapeB, use_patchgan=self.use_patchgan,
                                     disc_use_4_layers=True)
            loss_weights_D = [0.5]  # 0.5 since we train on real and synthetic images

            loss_weights_D = [0.5]  # 0.5 since we train on real and synthetic images
        elif self.generator_architecture == 'unet_mini':
            D_A = unet_discriminator_mini(self.image_shapeA, use_norm=self.use_norm, epsilon=self.epsilon_norm,
                                          use_patchgan=self.use_patchgan)
            D_B = unet_discriminator_mini(self.image_shapeB, use_norm=self.use_norm, epsilon=self.epsilon_norm,
                                          use_patchgan=self.use_patchgan)
            loss_weights_D = [0.5]  # 0.5 since we train on real and synthetic images

        # Discriminator builds
        image_A = Input(self.image_shapeA)
        image_B = Input(self.image_shapeB)
        guess_A = D_A(image_A)
        guess_B = D_B(image_B)
        self.D_A = Model(inputs=image_A, outputs=guess_A, name='D_A_model')
        self.D_B = Model(inputs=image_B, outputs=guess_B, name='D_B_model')

        if self.use_patchgan:

            self.D_A.compile(optimizer=self.opt_D,
                             loss=self.lse,
                             loss_weights=loss_weights_D)
            self.D_B.compile(optimizer=self.opt_D,
                             loss=self.lse,
                             loss_weights=loss_weights_D)
        else:
            self.D_A.compile(optimizer=self.opt_D,
                             loss='binary_crossentropy',
                             loss_weights=loss_weights_D)
            self.D_B.compile(optimizer=self.opt_D,
                             loss='binary_crossentropy',
                             loss_weights=loss_weights_D)

        # Use Networks to avoid falsy keras error about weight descripancies
        self.D_A_static = Network(inputs=image_A, outputs=guess_A, name='D_A_static_model')
        self.D_B_static = Network(inputs=image_B, outputs=guess_B, name='D_B_static_model')

        # ============= Generator models =======================
        # Do note update discriminator weights during generator training
        self.D_A_static.trainable = False
        self.D_B_static.trainable = False

        # Generators
        if self.generator_architecture == 'ICCV':
            self.G_A2B = modelGenerator(conv_kernel_c7Ak=7,
                                        use_resize_convolution=self.use_resize_convolution, input=self.image_shapeA,
                                        output=self.image_shapeB, name='G_A2B_model')
            self.G_B2A = modelGenerator(conv_kernel_c7Ak=7,
                                        use_resize_convolution=self.use_resize_convolution, input=self.image_shapeB,
                                        output=self.image_shapeA, name='G_B2A_model')

        elif self.generator_architecture == 'unet_mini':
            self.G_A2B = unet_generator_mini(input=self.image_shapeA,
                                             output=self.image_shapeB,
                                             normalization=normalization,
                                             epsilon=self.epsilon_norm,
                                             use_norm=self.use_norm,
                                             add_extra_conv=self.add_extra_conv,
                                             use_resize_convolution=self.use_resize_convolution,
                                             name='G_A2B_model')
            self.G_B2A = unet_generator_mini(input=self.image_shapeB,
                                             output=self.image_shapeA,
                                             normalization=normalization,
                                             epsilon=self.epsilon_norm,
                                             use_norm=self.use_norm,
                                             add_extra_conv=self.add_extra_conv,
                                             use_resize_convolution=self.use_resize_convolution,
                                             name='G_B2A_model')

        if self.use_identity_learning:
            self.G_A2B.compile(optimizer=self.opt_G, loss='MAE')
            self.G_B2A.compile(optimizer=self.opt_G, loss='MAE')

        # Generator builds
        real_A = Input(shape=self.image_shapeA, name='real_A')
        real_B = Input(shape=self.image_shapeB, name='real_B')
        synthetic_B = self.G_A2B(real_A)
        synthetic_A = self.G_B2A(real_B)
        dA_guess_synthetic = self.D_A_static(synthetic_A)
        dB_guess_synthetic = self.D_B_static(synthetic_B)
        reconstructed_A = self.G_B2A(synthetic_B)
        reconstructed_B = self.G_A2B(synthetic_A)

        model_outputs = [reconstructed_A, reconstructed_B]

        compile_losses = [self.cycle_loss, self.cycle_loss, self.lse, self.lse]
        compile_weights = [self.lambda_1, self.lambda_2, self.lambda_D, self.lambda_D]

        model_outputs.append(dA_guess_synthetic)
        model_outputs.append(dB_guess_synthetic)

        if self.use_supervised_learning:
            model_outputs.append(synthetic_A)
            model_outputs.append(synthetic_B)
            if self.supervised_loss == 'MAE':
                compile_losses.append('MAE')
                compile_losses.append('MAE')

            compile_weights.append(self.supervised_weight)
            compile_weights.append(self.supervised_weight)

        self.G_model = Model(inputs=[real_A, real_B],
                             outputs=model_outputs,
                             name='G_model')

        self.G_model.compile(optimizer=self.opt_G,
                             loss=compile_losses,
                             loss_weights=compile_weights)

        # ======= Data ==========
        # Use 'None' to fetch all available images
        nr_A_test_imgs = 1000
        nr_B_test_imgs = 1000

        if self.use_data_generator:
            print('--- Using dataloader during training ---')
        else:
            print('--- Caching data ---')
        sys.stdout.flush()

        if load_training_data:
            if self.use_data_generator:
                self.data_generator = load_data(task=self.task, root=self.data_root, batch_size=self.batch_size,
                                                crop_size=self.im_w, generator=True)
                # Only store test images

            if opt.task == 'Vimeo2Long_SID':
                self.A_test, self.B_test, test_A_image_names, test_B_image_names = get_test_data(nr_A_test_imgs,
                                                                                                 nr_B_test_imgs)
            else:
                self.A_test = []
                self.B_test = []

        self.A_train = []
        self.B_train = []

        if not self.use_data_generator:
            print('Data has been loaded')
Exemplo n.º 12
0
def build_model():
    optimizer = Adadelta()

    # build Completion Network model
    org_img = Input(shape=input_shape, dtype='float32')
    mask = Input(shape=(input_shape[0], input_shape[1], 1))

    generator, completion_out = model_generator(org_img, mask)
    completion_model = generator.compile(loss='mse', optimizer=optimizer)

    # build Discriminator model
    in_pts = Input(shape=(4, ), dtype='int32')  # [y1,x1,y2,x2]
    discriminator = model_discriminator(input_shape, local_shape)
    d_container = Network(inputs=[org_img, in_pts],
                          outputs=discriminator([org_img, in_pts]))
    d_out = d_container([org_img, in_pts])
    d_model = Model([org_img, in_pts], d_out)
    d_model.compile(loss='binary_crossentropy', optimizer=optimizer)
    d_container.trainable = False

    # build Discriminator & Completion Network models
    all_model = Model([org_img, mask, in_pts], [completion_out, d_out])
    all_model.compile(loss=['mse', 'binary_crossentropy'],
                      loss_weights=[1.0, alpha],
                      optimizer=optimizer)

    X_train = filenames[:5000]
    valid = np.ones((batch_size, 1))  ## label
    fake = np.zeros((batch_size, 1))  ## label

    for n in range(n_epoch):
        progbar = generic_utils.Progbar(len(X_train))
        for i in range(int(len(X_train) // batch_size)):
            X_batch = X_train[i * batch_size:(i + 1) * batch_size]
            inputs = np.array([
                process_image(filename, input_shape[:2])
                for filename in X_batch
            ])

            points, masks = get_points(batch_size)
            completion_image = generator.predict([inputs, masks])
            g_loss = 0.0
            d_loss = 0.0
            if n < tc:
                g_loss = generator.train_on_batch([inputs, masks], inputs)
            else:
                d_loss_real = d_model.train_on_batch([inputs, points], valid)
                d_loss_fake = d_model.train_on_batch(
                    [completion_image, points], fake)
                d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
                if n >= tc + td:
                    g_loss = all_model.train_on_batch([inputs, masks, points],
                                                      [inputs, valid])
                    g_loss = g_loss[0] + alpha * g_loss[1]
            progbar.add(inputs.shape[0],
                        values=[("Epoch", int(n + 1)), ("D loss", d_loss),
                                ("G mse", g_loss)])
        # show_image
        show_image(batch_size, n, inputs, masks, completion_image)
    # save model
    generator.save("model/generator.h5")
    discriminator.save("model/discriminator.h5")
Exemplo n.º 13
0
D_A = Model(inputs=image_A, outputs=guess_A, name='D_A_Model')
D_B = Model(inputs=image_B, outputs=guess_B, name='D_B_Model')

D_A.compile(optimizer= opt_D, \
                  loss='mse', \
                  loss_weights=loss_weights_D,\
                  metrics=['accuracy'])

D_B.compile(optimizer= opt_D, \
                  loss='mse', \
                  loss_weights=loss_weights_D,\
            metrics=['accuracy'])

# Use Networks to avoid falsy keras error about weight descripancies
D_A_static = Network(inputs=image_A, outputs=guess_A, name='D_A_static_model')
D_B_static = Network(inputs=image_B, outputs=guess_B, name='D_B_static_model')

G_A2B = model_generator(name="G_A2B_model")
G_B2A = model_generator(name="G_B2A_model")

G_A2B.summary()

#import image
# img_A = Input(shape=img_shape)
# img_B = Input(shape=img_shape)

#generate fake images, transfer image from A to B
fake_B = G_A2B(image_A)
fake_A = G_B2A(image_B)