def test_gan_utility_funcs(simple_gan: GAN): xy_shp = simple_gan_z_shape[1:] x = np.zeros(xy_shp, dtype=np.float32) y = np.zeros(xy_shp, dtype=np.float32) simple_gan.interpolate(x, y) z_point = simple_gan.random_z_point() neighbors = simple_gan.neighborhood(z_point, std=0.05) diff = np.stack([neighbors[0]]*len(neighbors)) - neighbors assert np.abs(diff).mean() < 0.1
def test_render_gan_generator(): nb_driver = 20 def driver(z): return Dense(nb_driver)(z) def tag3d_network_dense(x): mask = sequential( [Dense(16), Reshape((1, 4, 4)), UpSampling2D((16, 16))])(x) depth_map = sequential( [Dense(16), Reshape((1, 4, 4)), UpSampling2D((4, 4))])(x) return mask, depth_map def merge_mask(subsample): def call(x): if subsample: x = MaxPooling2D(subsample)(x) return Convolution2D(1, 3, 3, border_mode='same')(x) return call def light_generator(ins): seq = sequential([Convolution2D(1, 3, 3, border_mode='same')])(concat(ins)) return UpSampling2D((4, 4))(seq), UpSampling2D((4, 4))(seq), \ UpSampling2D((4, 4))(seq), def offset_front(x): return sequential( [Dense(16), Reshape((1, 4, 4)), UpSampling2D((4, 4))])(concat(x)) def offset_middle(x): return UpSampling2D()(concat(x)) def offset_back(x): feature_map = sequential([ UpSampling2D(), ])(concat(x)) return feature_map, Convolution2D(1, 3, 3, border_mode='same')(feature_map) def mask_post(x): return sequential([Convolution2D(1, 3, 3, border_mode='same')])(concat(x)) def mask_weight_blending(x): return sequential([ Flatten(), Dense(1), ])(x) def discriminator_fn(x): return gan_outputs(sequential([ Flatten(), Dense(1), ])(concat(x)), fake_for_gen=(0, 10), fake_for_dis=(0, 10), real=(10, 20)) gen = render_gan_generator( mask_driver=driver, tag_3d_model_network=tag3d_network_dense, light_merge_mask16=merge_mask(None), offset_merge_light16=merge_mask((4, 4)), offset_merge_mask16=merge_mask(None), offset_merge_mask32=merge_mask(None), lighting_generator=light_generator, offset_front=offset_front, offset_middle=offset_middle, offset_back=offset_back, mask_weight_blending32=mask_weight_blending, mask_weight_blending64=mask_weight_blending, mask_postprocess=mask_post, z_for_driver=(0, 10), z_for_offset=(10, 20), z_for_bits=(20, 32), ) z_shape = (32, ) real_shape = (1, 64, 64) z = Input(shape=z_shape, name='z') fake = Input(shape=real_shape, name='fake') real = Input(shape=real_shape, name='real') generator = Container([z], gen([z])) discriminator = Container([fake, real], discriminator_fn([fake, real])) gan = GAN(generator, discriminator, z_shape, real_shape) gan.build(Adam(), Adam(), gan_binary_crossentropy) for l in gan.generator.layers: print("{}: {}, {}".format(l.name, l.output_shape, getattr(l, 'regularizers', []))) bs = 10 z_in = np.random.sample((bs, ) + z_shape) real_in = np.random.sample((bs, ) + real_shape) gan.compile_generate() gan.generate({'z': z_in}) fn = gan.compile_custom_layers(['fake', 'mask', 'mask_with_lighting']) fn({'z': z_in, 'real': real_in})
def _build_gan(self): self.generator_given_z.compile(self.generator_optimizer, 'binary_crossentropy') self.discriminator.compile(self.discriminator_optimizer, 'binary_crossentropy') self.gan = GAN(self.generator_given_z, self.discriminator)
def test_render_gan_generator(): nb_driver = 20 def driver(z): return Dense(nb_driver)(z) def tag3d_network_dense(x): mask = sequential([ Dense(16), Reshape((1, 4, 4)), UpSampling2D((16, 16)) ])(x) depth_map = sequential([ Dense(16), Reshape((1, 4, 4)), UpSampling2D((4, 4)) ])(x) return mask, depth_map def merge_mask(subsample): def call(x): if subsample: x = MaxPooling2D(subsample)(x) return Convolution2D(1, 3, 3, border_mode='same')(x) return call def light_generator(ins): seq = sequential([ Convolution2D(1, 3, 3, border_mode='same') ])(concat(ins)) return UpSampling2D((4, 4))(seq), UpSampling2D((4, 4))(seq), \ UpSampling2D((4, 4))(seq), def offset_front(x): return sequential([ Dense(16), Reshape((1, 4, 4)), UpSampling2D((4, 4)) ])(concat(x)) def offset_middle(x): return UpSampling2D()(concat(x)) def offset_back(x): feature_map = sequential([ UpSampling2D(), ])(concat(x)) return feature_map, Convolution2D(1, 3, 3, border_mode='same')(feature_map) def mask_post(x): return sequential([ Convolution2D(1, 3, 3, border_mode='same') ])(concat(x)) def mask_weight_blending(x): return sequential([ Flatten(), Dense(1), ])(x) def discriminator_fn(x): return gan_outputs(sequential([ Flatten(), Dense(1), ])(concat(x)), fake_for_gen=(0, 10), fake_for_dis=(0, 10), real=(10, 20)) gen = render_gan_generator( mask_driver=driver, tag_3d_model_network=tag3d_network_dense, light_merge_mask16=merge_mask(None), offset_merge_light16=merge_mask((4, 4)), offset_merge_mask16=merge_mask(None), offset_merge_mask32=merge_mask(None), lighting_generator=light_generator, offset_front=offset_front, offset_middle=offset_middle, offset_back=offset_back, mask_weight_blending32=mask_weight_blending, mask_weight_blending64=mask_weight_blending, mask_postprocess=mask_post, z_for_driver=(0, 10), z_for_offset=(10, 20), z_for_bits=(20, 32), ) z_shape = (32, ) real_shape = (1, 64, 64) z = Input(shape=z_shape, name='z') fake = Input(shape=real_shape, name='fake') real = Input(shape=real_shape, name='real') generator = Container([z], gen([z])) discriminator = Container([fake, real], discriminator_fn([fake, real])) gan = GAN(generator, discriminator, z_shape, real_shape) gan.build(Adam(), Adam(), gan_binary_crossentropy) for l in gan.generator.layers: print("{}: {}, {}".format( l.name, l.output_shape, getattr(l, 'regularizers', []))) bs = 10 z_in = np.random.sample((bs,) + z_shape) real_in = np.random.sample((bs,) + real_shape) gan.compile_generate() gan.generate({'z': z_in}) fn = gan.compile_custom_layers(['fake', 'mask', 'mask_with_lighting']) fn({'z': z_in, 'real': real_in})