{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport re\nimport shutil\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom kaggle_datasets import KaggleDatasets\nimport tensorflow_addons as tfa\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom scipy import linalg","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:12.284888Z","iopub.execute_input":"2021-06-13T15:07:12.285298Z","iopub.status.idle":"2021-06-13T15:07:17.553058Z","shell.execute_reply.started":"2021-06-13T15:07:12.285215Z","shell.execute_reply":"2021-06-13T15:07:17.552105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alpha = 0.2\nshape = [256, 256, 3]\nLambda_cycle = 15\nLambda_identity = 10\nBATCH_SIZE = 30\n#EPOCHS = 1\nAUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:17.554597Z","iopub.execute_input":"2021-06-13T15:07:17.555007Z","iopub.status.idle":"2021-06-13T15:07:17.559892Z","shell.execute_reply.started":"2021-06-13T15:07:17.554968Z","shell.execute_reply":"2021-06-13T15:07:17.558635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas:', strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:17.561890Z","iopub.execute_input":"2021-06-13T15:07:17.562694Z","iopub.status.idle":"2021-06-13T15:07:23.108184Z","shell.execute_reply.started":"2021-06-13T15:07:17.562650Z","shell.execute_reply":"2021-06-13T15:07:23.107335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GCS_PATH = KaggleDatasets().get_gcs_path('gan-getting-started')\n\nMONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/monet*.tfrec'))\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/photo*.tfrec'))\n\ndef count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)\n\nn_monet_samples = count_data_items(MONET_FILENAMES)\nn_photo_samples = count_data_items(PHOTO_FILENAMES)   #获取图片的数量\n\ndef data_augment(image):   #数据增强\n    p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    if p_crop > .5:\n        image = tf.image.resize(image, [286, 286]) #resizing to 286 x 286 x 3\n        image = tf.image.random_crop(image, size=[256, 256, 3]) # randomly cropping to 256 x 256 x 3\n        if p_crop > .9:\n            image = tf.image.resize(image, [300, 300])\n            image = tf.image.random_crop(image, size=[256, 256, 3])\n    \n    if p_rotate > .9:\n        image = tf.image.rot90(image, k=3) # rotate 270º\n    elif p_rotate > .7:\n        image = tf.image.rot90(image, k=2) # rotate 180º\n    elif p_rotate > .5:\n        image = tf.image.rot90(image, k=1) # rotate 90º\n        \n        ## random mirroring\n    if p_spatial > .6:\n        image = tf.image.random_flip_left_right(image)\n        image = tf.image.random_flip_up_down(image)\n        if p_spatial > .9:\n            image = tf.image.transpose(image)\n    \n    return image\n\nIMAGE_SIZE = [256, 256]\n\ndef decode_image(image):\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = (tf.cast(image, tf.float32) / 127.5) - 1\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    return image\n\ndef read_tfrecord(example):\n    tfrecord_format = {\n        \"image_name\": tf.io.FixedLenFeature([], tf.string),\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"target\": tf.io.FixedLenFeature([], tf.string)\n    }\n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example['image'])\n    return image\n\ndef load_dataset(filenames):   #数据集解码\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)\n    return dataset\n\ndef get_gan_dataset(monet_files, photo_files, augment=None, repeat=True, shuffle=True, batch_size=1):\n\n    monet_ds = load_dataset(monet_files)\n    photo_ds = load_dataset(photo_files)\n    \n    if augment:\n        monet_ds = monet_ds.map(augment, num_parallel_calls=AUTOTUNE)\n        photo_ds = photo_ds.map(augment, num_parallel_calls=AUTOTUNE)\n        \n    if repeat:\n        monet_ds = monet_ds.repeat()\n        photo_ds = photo_ds.repeat()\n    if shuffle:\n        monet_ds = monet_ds.shuffle(2048)\n        photo_ds = photo_ds.shuffle(2048)\n        \n    monet_ds = monet_ds.batch(batch_size, drop_remainder=True)\n    photo_ds = photo_ds.batch(batch_size, drop_remainder=True)\n    monet_ds = monet_ds.cache()\n    photo_ds = photo_ds.cache()\n    monet_ds = monet_ds.prefetch(AUTOTUNE)\n    photo_ds = photo_ds.prefetch(AUTOTUNE)\n    \n    gan_ds = tf.data.Dataset.zip((monet_ds, photo_ds))  #经过数据增强后将数据集进行组合\n    \n    return gan_ds,photo_ds,monet_ds","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:23.109993Z","iopub.execute_input":"2021-06-13T15:07:23.110300Z","iopub.status.idle":"2021-06-13T15:07:23.587965Z","shell.execute_reply.started":"2021-06-13T15:07:23.110272Z","shell.execute_reply":"2021-06-13T15:07:23.586970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#获得完整的数据集\nfull_dataset,my_photo_ds,my_monet_ds = get_gan_dataset(MONET_FILENAMES, PHOTO_FILENAMES, augment=data_augment, repeat=True, shuffle=True, batch_size=BATCH_SIZE)\n\n#这段代码用于可视化数据集，调试上方的程序\nexample_monet , example_photo = next(iter(full_dataset))\nplt.subplot(121)\nplt.title('Real photo')\nplt.imshow(example_photo[2] * 0.5 + 0.5)\n\nplt.subplot(122)\nplt.title('Monet painting')\nplt.imshow(example_monet[2]* 0.5 + 0.5)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:23.591519Z","iopub.execute_input":"2021-06-13T15:07:23.591836Z","iopub.status.idle":"2021-06-13T15:07:31.800250Z","shell.execute_reply.started":"2021-06-13T15:07:23.591809Z","shell.execute_reply":"2021-06-13T15:07:31.799509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_photo_ds = load_dataset(PHOTO_FILENAMES).batch(32*strategy.num_replicas_in_sync).prefetch(32)\nmonet_ds_fid = load_dataset(MONET_FILENAMES).batch(32*strategy.num_replicas_in_sync).prefetch(32)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:31.801447Z","iopub.execute_input":"2021-06-13T15:07:31.801883Z","iopub.status.idle":"2021-06-13T15:07:31.849749Z","shell.execute_reply.started":"2021-06-13T15:07:31.801853Z","shell.execute_reply":"2021-06-13T15:07:31.848995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    #inception_model = tf.keras.applications.InceptionV3(input_shape=(256,256,3),pooling=\"avg\",include_top=False)\n    inception_model = tf.keras.applications.InceptionV3(input_shape=(256,256,3),pooling=\"avg\",include_top=False)\n\n    mix3  = inception_model.get_layer(\"mixed3\").output\n    f0 = tf.keras.layers.GlobalMaxPooling2D()(mix3)\n\n    inception_model = tf.keras.Model(inputs=inception_model.input, outputs=f0)\n    inception_model.trainable = False\n    \ndef calculate_activation_statistics_mod(images,fid_model):\n        act=fid_model.predict(images)\n        mu = np.mean(act, axis=0)\n        sigma = np.cov(act, rowvar=False)\n        return mu, sigma\nmyFID_mu2, myFID_sigma2 = calculate_activation_statistics_mod(monet_ds_fid,inception_model)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:31.850868Z","iopub.execute_input":"2021-06-13T15:07:31.851324Z","iopub.status.idle":"2021-06-13T15:07:53.957447Z","shell.execute_reply.started":"2021-06-13T15:07:31.851294Z","shell.execute_reply":"2021-06-13T15:07:53.956027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(myFID_mu2.shape,myFID_sigma2.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:53.962442Z","iopub.execute_input":"2021-06-13T15:07:53.963042Z","iopub.status.idle":"2021-06-13T15:07:53.970151Z","shell.execute_reply.started":"2021-06-13T15:07:53.962990Z","shell.execute_reply":"2021-06-13T15:07:53.968955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calculate_frechet_distance(mu1,sigma1,mu2,sigma2):\n        fid_epsilon = 1e-14\n        mu1 = np.atleast_1d(mu1)\n        mu2 = np.atleast_1d(mu2)\n        sigma1 = np.atleast_2d(sigma1)\n        sigma2 = np.atleast_2d(sigma2)\n\n        assert mu1.shape == mu2.shape, 'Training and test mean vectors have different lengths'\n        assert sigma1.shape == sigma2.shape, 'Training and test covariances have different dimensions'\n\n        # product might be almost singular\n        covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)\n        if not np.isfinite(covmean).all():\n            msg = f'fid calculation produces singular product; adding {fid_epsilon} to diagonal of cov estimates'\n            warnings.warn(msg)\n            offset = np.eye(sigma1.shape[0]) * fid_epsilon\n            covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))\n            \n        # numerical error might give slight imaginary component\n        if np.iscomplexobj(covmean):\n            if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):\n                m = np.max(np.abs(covmean.imag))\n                raise ValueError(f'Imaginary component {m}')\n            covmean = covmean.real\n        tr_covmean = np.trace(covmean)\n        return (mu1 - mu2).dot(mu1 - mu2) + np.trace(sigma1) + np.trace(sigma2) - 2 * tr_covmean\n\ndef FID(images,gen_model,inception_model=inception_model,myFID_mu2=myFID_mu2, myFID_sigma2=myFID_sigma2):\n            with strategy.scope():\n                inp = layers.Input(shape=[256, 256, 3], name='input_image')\n                x  = gen_model(inp)\n                x=inception_model(x)\n                fid_model = tf.keras.Model(inputs=inp, outputs=x)\n                \n            mu1, sigma1 = calculate_activation_statistics_mod(images,fid_model)\n\n            fid_value = calculate_frechet_distance(mu1, sigma1,myFID_mu2, myFID_sigma2)\n\n            return fid_value","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:53.972770Z","iopub.execute_input":"2021-06-13T15:07:53.976004Z","iopub.status.idle":"2021-06-13T15:07:54.119366Z","shell.execute_reply.started":"2021-06-13T15:07:53.975941Z","shell.execute_reply":"2021-06-13T15:07:54.118093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_CHANNELS = 3\ndef downsample(filters, size, apply_instancenorm=True):\n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)\n\n    result = keras.Sequential() \n    result.add(layers.Conv2D(filters, size, strides=2, padding='same',kernel_initializer=initializer, use_bias=False))\n    #这里没有做池化、Dropout，通过卷积减少图像体积，与VGG会有点不一样\n    if apply_instancenorm: #是否进行标准化（InstanceNormalization优于batchNormalization）\n        result.add(tfa.layers.InstanceNormalization(gamma_initializer=gamma_init))\n    \n    result.add(layers.LeakyReLU()) #采用LeakyReLU激活，参数并没有设置\n\n    return result","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:54.121110Z","iopub.execute_input":"2021-06-13T15:07:54.121446Z","iopub.status.idle":"2021-06-13T15:07:54.145185Z","shell.execute_reply.started":"2021-06-13T15:07:54.121414Z","shell.execute_reply":"2021-06-13T15:07:54.143970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def upsample(filters, size, apply_dropout=False):\n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)\n\n    result = keras.Sequential()\n    result.add(layers.Conv2DTranspose(filters, size, strides=2,padding='same',kernel_initializer=initializer, use_bias=False))\n    #基本上不用偏置\n    result.add(tfa.layers.InstanceNormalization(gamma_initializer=gamma_init))\n    \n    if apply_dropout:\n        result.add(layers.Dropout(0.5))\n\n    result.add(layers.ReLU())  #用ReLU激活\n\n    return result #注意：result是个model,是可以通过model(x)来作用于数据的","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:54.146658Z","iopub.execute_input":"2021-06-13T15:07:54.147027Z","iopub.status.idle":"2021-06-13T15:07:54.168012Z","shell.execute_reply.started":"2021-06-13T15:07:54.146996Z","shell.execute_reply":"2021-06-13T15:07:54.167011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Generator():\n    inputs = layers.Input(shape=[256,256,3])\n\n    # bs = batch size\n    down_stack = [\n        downsample(64, 4, apply_instancenorm=False), # (bs, 128, 128, 64)\n        downsample(128, 4), # (bs, 64, 64, 128)\n        downsample(256, 4), # (bs, 32, 32, 256)\n        downsample(512, 4), # (bs, 16, 16, 512)\n        downsample(512, 4), # (bs, 8, 8, 512)\n        downsample(512, 4), # (bs, 4, 4, 512)\n        downsample(512, 4), # (bs, 2, 2, 512)\n        downsample(512, 4), # (bs, 1, 1, 512)\n    ]#首先下采样（这里层次可以添加（调整downSample的stride）！！）（参考VGG可是不止这么少的，不过down+up倒是足够）\n\n    up_stack = [\n        upsample(512, 4, apply_dropout=True), # (bs, 2, 2, 1024)\n        upsample(512, 4, apply_dropout=True), # (bs, 4, 4, 1024)\n        upsample(512, 4, apply_dropout=True), # (bs, 8, 8, 1024)\n        upsample(512, 4), # (bs, 16, 16, 1024)\n        upsample(256, 4), # (bs, 32, 32, 512)\n        upsample(128, 4), # (bs, 64, 64, 256)\n        upsample(64, 4), # (bs, 128, 128, 128)\n    ]#然后上采样\n    #上面只是建立模型，下面是将模型用到数据集上\n    \n    initializer = tf.random_normal_initializer(0., 0.02)\n    last = layers.Conv2DTranspose(OUTPUT_CHANNELS, 4,strides=2,padding='same',\n                                  kernel_initializer=initializer,activation='tanh') # (bs, 256, 256, 3)\n\n    x = inputs\n\n    skips = []\n    for down in down_stack:\n        x = down(x)\n        skips.append(x)   #实现下采样\n\n    skips = reversed(skips[:-1])\n\n    for up, skip in zip(up_stack, skips):\n        x = up(x)\n        x = layers.Concatenate()([x, skip]) #这里是提高质量的核心，通过连接将当前上采样层和下采样层连接，即保留了卷积的特征，\n        #又能在下采样的基础上进行上采样\n\n    x = last(x)  #最后一层通过上采样生成[255,255,3]的假图\n\n    return keras.Model(inputs=inputs, outputs=x) #返回生成器模型","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:54.169450Z","iopub.execute_input":"2021-06-13T15:07:54.170046Z","iopub.status.idle":"2021-06-13T15:07:54.183707Z","shell.execute_reply.started":"2021-06-13T15:07:54.170009Z","shell.execute_reply":"2021-06-13T15:07:54.182724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator_g = Generator()\ntf.keras.utils.plot_model(generator_g, show_shapes=True, dpi=64)\n\nphoto = (example_photo[0,...] * 0.5 + 0.5)\nexample_gen_output_y = generator_g(photo[tf.newaxis,...], training=False)\n\nplt.subplot(1,2,1)\nplt.imshow(photo, vmin=0, vmax=255) \n\nplt.subplot(1,2,2)\nplt.imshow(example_gen_output_y[0]) \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:54.185079Z","iopub.execute_input":"2021-06-13T15:07:54.185687Z","iopub.status.idle":"2021-06-13T15:07:56.937704Z","shell.execute_reply.started":"2021-06-13T15:07:54.185650Z","shell.execute_reply":"2021-06-13T15:07:56.936587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Discriminator():\n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)\n\n    inp = layers.Input(shape=[256, 256, 3], name='input_image')\n\n    x = inp\n\n    down1 = downsample(64, 4, False)(x) # (bs, 128, 128, 64)\n    down2 = downsample(128, 4)(down1) # (bs, 64, 64, 128)\n    down3 = downsample(256, 4)(down2) # (bs, 32, 32, 256)\n\n    zero_pad1 = layers.ZeroPadding2D()(down3) # (bs, 34, 34, 256)\n    conv = layers.Conv2D(512, 4, strides=1,\n                         kernel_initializer=initializer,\n                         use_bias=False)(zero_pad1) # (bs, 31, 31, 512)\n    #层数上比较少，后面基本通过补零再卷积来提取特征\n    norm1 = tfa.layers.InstanceNormalization(gamma_initializer=gamma_init)(conv)\n\n    leaky_relu = layers.LeakyReLU()(norm1)\n\n    zero_pad2 = layers.ZeroPadding2D()(leaky_relu) # (bs, 33, 33, 512)\n\n    last = layers.Conv2D(1, 4, strides=1,\n                         kernel_initializer=initializer)(zero_pad2) # (bs, 30, 30, 1)\n\n    return tf.keras.Model(inputs=inp, outputs=last)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:56.939656Z","iopub.execute_input":"2021-06-13T15:07:56.940071Z","iopub.status.idle":"2021-06-13T15:07:56.949369Z","shell.execute_reply.started":"2021-06-13T15:07:56.940026Z","shell.execute_reply":"2021-06-13T15:07:56.948673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator_y = Discriminator()\ntf.keras.utils.plot_model(discriminator_y, show_shapes=True, dpi=64) ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:56.950316Z","iopub.execute_input":"2021-06-13T15:07:56.950744Z","iopub.status.idle":"2021-06-13T15:07:57.316407Z","shell.execute_reply.started":"2021-06-13T15:07:56.950702Z","shell.execute_reply":"2021-06-13T15:07:57.315116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    monet_generator = Generator() # transforms photos to Monet-esque paintings\n    photo_generator = Generator() # transforms Monet paintings to be more like photos\n\n    monet_discriminator = Discriminator() # differentiates real Monet paintings and generated Monet paintings\n    photo_discriminator = Discriminator() # differentiates real photos and generated photos","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:07:57.318692Z","iopub.execute_input":"2021-06-13T15:07:57.319106Z","iopub.status.idle":"2021-06-13T15:08:06.569094Z","shell.execute_reply.started":"2021-06-13T15:07:57.319069Z","shell.execute_reply":"2021-06-13T15:08:06.568194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"photo = (example_photo[0,...] * 0.5 + 0.5)\nmonet = (example_monet[0,...] * 0.5 + 0.5)\n\n# From photo we generate Monet (fake) and regenerate the photo (cycle) again\nexample_gen_output_monet_fake = monet_generator(photo[tf.newaxis,...], training=False)\nexample_gen_output_photo_cycle =  photo_generator(example_gen_output_monet_fake, training=False)\n\n# We run the discriminator for Monet (fake)\nexample_disc_out_monet = monet_discriminator(example_gen_output_monet_fake, training=False)\n\n\n# From Monet we generate photo (fake) and regenerate Monet (cycle) again\nexample_gen_output_photo_fake =  photo_generator(monet[tf.newaxis,...], training=False)\nexample_gen_output_monet_cycle = monet_generator(example_gen_output_photo_fake, training=False)\n\n# We execute the discriminator for Photo (fake)\nexample_disc_out_photo = photo_discriminator(example_gen_output_photo_fake, training=False)\n\n\n# We present results, as the network is not trained, the outputs are not good, \n# but we modify the scala to be able to have some example images\n\nplt.figure(figsize=(10,10))\n\n# Input Photo\nplt.subplot(2,4,1)\nplt.imshow(photo, vmin=0, vmax=255) \n\n# Fake Monet\nplt.subplot(2,4,2)\n#m = example_gen_output_monet_fake[0,...].numpy()\n#print(np.min(m), np.max(m))\ncontrast = 100 \nplt.imshow(example_gen_output_monet_fake[0,...]*contrast) \n\n# Photo Cycle\nplt.subplot(2,4,3)\n#m = example_gen_output_photo_cycle[0,...].numpy()\n#print(np.min(m), np.max(m))\ncontrast = 100\nplt.imshow(example_gen_output_photo_cycle[0,...]*contrast) \n\n# Monet discriminator result\n#plt.subplot(2,4,4)\n#m = example_disc_out_monet[0,...,-1].numpy()\n#print(np.min(m), np.max(m))\n#contrast = 1000\n#im = plt.imshow(m*contrast, vmin=-20, vmax=20, cmap='RdBu_r')\n#plt.colorbar(im,fraction=0.046, pad=0.04)\n\n\n\n# Input Monet\nplt.subplot(2,4,5)\nplt.imshow(monet, vmin=0, vmax=255) \n\n# Fake Photo\nplt.subplot(2,4,6)\n#m = example_gen_output_photo_fake[0,...].numpy()\n#print(np.min(m), np.max(m))\ncontrast = 100\nplt.imshow(example_gen_output_photo_fake[0,...]*contrast) \n\n# Monet Cycle\nplt.subplot(2,4,7)\n#m = example_gen_output_monet_cycle[0,...].numpy()\n#print(np.min(m), np.max(m))\ncontrast = 100\nplt.imshow(example_gen_output_monet_cycle[0,...]*contrast) \n\n# Photo discriminator result  \n#plt.subplot(2,4,8)\n#m = example_disc_out_photo[0,...,-1].numpy()\n#print(np.min(m), np.max(m))\n#contrast = 1000\n#im = plt.imshow(contrast, vmin=-20, vmax=20, cmap='RdBu_r')\n#plt.colorbar(im,fraction=0.046, pad=0.04)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:06.570234Z","iopub.execute_input":"2021-06-13T15:08:06.570638Z","iopub.status.idle":"2021-06-13T15:08:08.529550Z","shell.execute_reply.started":"2021-06-13T15:08:06.570596Z","shell.execute_reply":"2021-06-13T15:08:08.528301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CycleGan(keras.Model):\n    def __init__(\n        self,\n        monet_generator,\n        photo_generator,\n        monet_discriminator,\n        photo_discriminator,\n        lambda_cycle=15,\n    ):\n        super(CycleGan, self).__init__()\n        self.m_gen = monet_generator\n        self.p_gen = photo_generator\n        self.m_disc = monet_discriminator\n        self.p_disc = photo_discriminator\n        self.lambda_cycle = lambda_cycle\n        \n    def compile(\n        self,\n        m_gen_optimizer,\n        p_gen_optimizer,\n        m_disc_optimizer,\n        p_disc_optimizer,\n        gen_loss_fn,\n        disc_loss_fn,\n        cycle_loss_fn,\n        identity_loss_fn\n    ):\n        super(CycleGan, self).compile()\n        self.m_gen_optimizer = m_gen_optimizer\n        self.p_gen_optimizer = p_gen_optimizer\n        self.m_disc_optimizer = m_disc_optimizer\n        self.p_disc_optimizer = p_disc_optimizer\n        self.gen_loss_fn = gen_loss_fn\n        self.disc_loss_fn = disc_loss_fn\n        self.cycle_loss_fn = cycle_loss_fn\n        self.identity_loss_fn = identity_loss_fn\n        \n    def train_step(self, batch_data):\n        real_monet, real_photo = batch_data\n        \n        with tf.GradientTape(persistent=True) as tape:\n            # photo to monet back to photo\n            fake_monet = self.m_gen(real_photo, training=True)\n            cycled_photo = self.p_gen(fake_monet, training=True)\n\n            # monet to photo back to monet\n            fake_photo = self.p_gen(real_monet, training=True)\n            cycled_monet = self.m_gen(fake_photo, training=True)\n\n            # generating itself\n            same_monet = self.m_gen(real_monet, training=True)\n            same_photo = self.p_gen(real_photo, training=True)\n\n            # discriminator used to check, inputing real images\n            disc_real_monet = self.m_disc(real_monet, training=True)\n            disc_real_photo = self.p_disc(real_photo, training=True)\n\n            # discriminator used to check, inputing fake images\n            disc_fake_monet = self.m_disc(fake_monet, training=True)\n            disc_fake_photo = self.p_disc(fake_photo, training=True)\n\n            # evaluates generator loss\n            monet_gen_loss = self.gen_loss_fn(disc_fake_monet)\n            photo_gen_loss = self.gen_loss_fn(disc_fake_photo)\n\n            # evaluates total cycle consistency loss\n            total_cycle_loss = self.cycle_loss_fn(real_monet, cycled_monet, self.lambda_cycle) + self.cycle_loss_fn(real_photo, cycled_photo, self.lambda_cycle)\n\n            # evaluates total generator loss\n            total_monet_gen_loss = monet_gen_loss + total_cycle_loss + self.identity_loss_fn(real_monet, same_monet, self.lambda_cycle)\n            total_photo_gen_loss = photo_gen_loss + total_cycle_loss + self.identity_loss_fn(real_photo, same_photo, self.lambda_cycle)\n\n            # evaluates discriminator loss\n            monet_disc_loss = self.disc_loss_fn(disc_real_monet, disc_fake_monet)\n            photo_disc_loss = self.disc_loss_fn(disc_real_photo, disc_fake_photo)\n\n        # Calculate the gradients for generator and discriminator\n        monet_generator_gradients = tape.gradient(total_monet_gen_loss,\n                                                  self.m_gen.trainable_variables)\n        photo_generator_gradients = tape.gradient(total_photo_gen_loss,\n                                                  self.p_gen.trainable_variables)\n\n        monet_discriminator_gradients = tape.gradient(monet_disc_loss,\n                                                      self.m_disc.trainable_variables)\n        photo_discriminator_gradients = tape.gradient(photo_disc_loss,\n                                                      self.p_disc.trainable_variables)\n\n        # Apply the gradients to the optimizer\n        self.m_gen_optimizer.apply_gradients(zip(monet_generator_gradients,\n                                                 self.m_gen.trainable_variables))\n\n        self.p_gen_optimizer.apply_gradients(zip(photo_generator_gradients,\n                                                 self.p_gen.trainable_variables))\n\n        self.m_disc_optimizer.apply_gradients(zip(monet_discriminator_gradients,\n                                                  self.m_disc.trainable_variables))\n\n        self.p_disc_optimizer.apply_gradients(zip(photo_discriminator_gradients,\n                                                  self.p_disc.trainable_variables))\n        \n        return {\n            \"monet_gen_loss\": total_monet_gen_loss,\n            \"photo_gen_loss\": total_photo_gen_loss,\n            \"monet_disc_loss\": monet_disc_loss,\n            \"photo_disc_loss\": photo_disc_loss\n        }","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:08.531251Z","iopub.execute_input":"2021-06-13T15:08:08.531570Z","iopub.status.idle":"2021-06-13T15:08:08.550360Z","shell.execute_reply.started":"2021-06-13T15:08:08.531539Z","shell.execute_reply":"2021-06-13T15:08:08.549321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():#并行运算\n    def discriminator_loss(real, generated):\n        real_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)(tf.ones_like(real), real)\n\n        generated_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)(tf.zeros_like(generated), generated)\n        #这里用交叉熵，但吴恩达最新教程推荐Mse(可以试着换)\n        total_disc_loss = real_loss + generated_loss\n\n        return total_disc_loss * 0.4","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:08.551434Z","iopub.execute_input":"2021-06-13T15:08:08.551809Z","iopub.status.idle":"2021-06-13T15:08:08.569485Z","shell.execute_reply.started":"2021-06-13T15:08:08.551769Z","shell.execute_reply":"2021-06-13T15:08:08.568100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    def generator_loss(generated):\n        return tf.keras.losses.BinaryCrossentropy(from_logits=True,\n                                                  reduction=tf.keras.losses.Reduction.NONE)(tf.ones_like(generated), generated)\nwith strategy.scope():\n    def calc_cycle_loss(real_image, cycled_image, LAMBDA):\n        loss1 = tf.reduce_mean(tf.abs(real_image - cycled_image))\n\n        return Lambda_cycle * loss1\n\nwith strategy.scope():\n    def identity_loss(real_image, same_image, LAMBDA):\n        loss = tf.reduce_mean(tf.abs(real_image - same_image))\n        return Lambda_identity * loss","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:08.571756Z","iopub.execute_input":"2021-06-13T15:08:08.572216Z","iopub.status.idle":"2021-06-13T15:08:08.583194Z","shell.execute_reply.started":"2021-06-13T15:08:08.572147Z","shell.execute_reply":"2021-06-13T15:08:08.582197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    monet_generator_optimizer = tf.keras.optimizers.Adam(0.002, decay = 0, beta_1=0.5)\n    photo_generator_optimizer = tf.keras.optimizers.Adam(0.002, beta_1=0.5)\n\n    monet_discriminator_optimizer = tf.keras.optimizers.Adam(0.002, beta_1=0.5)\n    photo_discriminator_optimizer = tf.keras.optimizers.Adam(0.002, beta_1=0.5)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:08.584325Z","iopub.execute_input":"2021-06-13T15:08:08.584763Z","iopub.status.idle":"2021-06-13T15:08:08.600897Z","shell.execute_reply.started":"2021-06-13T15:08:08.584734Z","shell.execute_reply":"2021-06-13T15:08:08.599816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    cycle_gan_model = CycleGan(\n        monet_generator, photo_generator, monet_discriminator, photo_discriminator\n    )\n\n    cycle_gan_model.compile(\n        m_gen_optimizer = monet_generator_optimizer,\n        p_gen_optimizer = photo_generator_optimizer,\n        m_disc_optimizer = monet_discriminator_optimizer,\n        p_disc_optimizer = photo_discriminator_optimizer,\n        gen_loss_fn = generator_loss,\n        disc_loss_fn = discriminator_loss,\n        cycle_loss_fn = calc_cycle_loss,\n        identity_loss_fn = identity_loss\n    )\n    \n#cycle_gan_model.fit(\n    #full_dataset,\n    #epochs=EPOCHS,\n    #steps_per_epoch=(max(n_monet_samples, n_photo_samples)//BATCH_SIZE),\n#)#这里是训练啦","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:08.602236Z","iopub.execute_input":"2021-06-13T15:08:08.602514Z","iopub.status.idle":"2021-06-13T15:08:08.663174Z","shell.execute_reply.started":"2021-06-13T15:08:08.602488Z","shell.execute_reply":"2021-06-13T15:08:08.662076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfids=[]\nbest_fid=999999999\nfor epoch in range(1,32):\n\n    print(\"Epoch = \",epoch)\n    hist=cycle_gan_model.fit(full_dataset,steps_per_epoch=1500, epochs=1).history\n    cur_fid=FID(fast_photo_ds,monet_generator)\n#     disc_m_loss1.append(hist[\"monet_disc_loss1\"][0][0][0])\n#     disc_p_loss1.append(hist[\"photo_disc_loss1\"][0][0][0])\n    fids.append(cur_fid)\n    print(\"After epoch #{} FID = {}\\n\".format(epoch,cur_fid))\n    if cur_fid<best_fid:\n            best_fid=cur_fid\n            monet_generator.save('monet_generator.h5')\n\n\n# hist=cycle_gan_model.fit(gan_ds,steps_per_epoch=30, epochs=EPOCHS).history\n# hist=cycle_gan_model.fit(gan_ds,steps_per_epoch=30,validation_data=([1]), epochs=3).history","metadata":{"execution":{"iopub.status.busy":"2021-06-13T15:08:08.667117Z","iopub.execute_input":"2021-06-13T15:08:08.667415Z","iopub.status.idle":"2021-06-13T17:26:08.840748Z","shell.execute_reply.started":"2021-06-13T15:08:08.667387Z","shell.execute_reply":"2021-06-13T17:26:08.838591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.plot(disc_m_loss1, label='monet_disc_loss1')\n# plt.plot(disc_p_loss1, label='photo_disc_loss1')\nplt.plot(np.array(fids), label='FID')\n\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:26:08.843962Z","iopub.execute_input":"2021-06-13T17:26:08.844401Z","iopub.status.idle":"2021-06-13T17:26:09.038771Z","shell.execute_reply.started":"2021-06-13T17:26:08.844355Z","shell.execute_reply":"2021-06-13T17:26:09.037714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cycle_gan_model.built = True\n# cycle_gan_model.load_weights('monet.h5')\nwith strategy.scope():\n    monet_generator = tf.keras.models.load_model('monet_generator.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:26:09.040488Z","iopub.execute_input":"2021-06-13T17:26:09.040919Z","iopub.status.idle":"2021-06-13T17:26:13.755420Z","shell.execute_reply.started":"2021-06-13T17:26:09.040877Z","shell.execute_reply":"2021-06-13T17:26:13.754161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots(5, 3, figsize=(32, 32))\nfor i, img in enumerate(my_photo_ds.take(5)):\n    prediction = monet_generator(img, training=False)\n    cycledphoto = photo_generator(prediction, training=False)\n    prediction = (prediction * 127.5 + 127.5)[0].numpy().astype(np.uint8)\n    cycledphoto = (cycledphoto * 127.5 + 127.5)[0].numpy().astype(np.uint8)\n\n    img = (img[0] * 127.5 + 127.5).numpy().astype(np.uint8)\n\n    ax[i, 0].imshow(img)\n    ax[i, 1].imshow(prediction)\n    ax[i, 2].imshow(cycledphoto)\n    ax[i, 0].set_title(\"Input Photo\")\n    ax[i, 1].set_title(\"Monet-esque\")\n    ax[i, 2].set_title(\"Cycled Photo\")\n    ax[i, 0].axis(\"off\")\n    ax[i, 1].axis(\"off\")\n    ax[i, 2].axis(\"off\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:26:13.757210Z","iopub.execute_input":"2021-06-13T17:26:13.758209Z","iopub.status.idle":"2021-06-13T17:26:30.538771Z","shell.execute_reply.started":"2021-06-13T17:26:13.758091Z","shell.execute_reply":"2021-06-13T17:26:30.537577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots(5, 3, figsize=(32, 32))\nfor i, img in enumerate(my_monet_ds.take(5)):\n    prediction = photo_generator(img, training=False)\n    cycledphoto = monet_generator(prediction, training=False)\n    prediction = (prediction * 127.5 + 127.5)[0].numpy().astype(np.uint8)\n    cycledphoto = (cycledphoto * 127.5 + 127.5)[0].numpy().astype(np.uint8)\n\n    img = (img[0] * 127.5 + 127.5).numpy().astype(np.uint8)\n\n    ax[i, 0].imshow(img)\n    ax[i, 1].imshow(prediction)\n    ax[i, 2].imshow(cycledphoto)\n    ax[i, 0].set_title(\"Input Monet\")\n    ax[i, 1].set_title(\"Generated Photo\")\n    ax[i, 2].set_title(\"Cycled Monet\")\n    ax[i, 0].axis(\"off\")\n    ax[i, 1].axis(\"off\")\n    ax[i, 2].axis(\"off\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:26:30.540106Z","iopub.execute_input":"2021-06-13T17:26:30.540414Z","iopub.status.idle":"2021-06-13T17:26:47.345358Z","shell.execute_reply.started":"2021-06-13T17:26:30.540384Z","shell.execute_reply":"2021-06-13T17:26:47.343768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_iter = iter(my_photo_ds)\nfor n_sample in range(8):\n        example_sample = next(ds_iter)\n        generated_sample = monet_generator(example_sample)\n        \n        f = plt.figure(figsize=(32, 32))\n        \n        plt.subplot(121)\n        plt.title('Input image')\n        plt.imshow(example_sample[0] * 0.5 + 0.5)\n        plt.axis('off')\n        \n        plt.subplot(122)\n        plt.title('Generated image')\n        plt.imshow(generated_sample[0] * 0.5 + 0.5)\n        plt.axis('off')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:26:47.346951Z","iopub.execute_input":"2021-06-13T17:26:47.347280Z","iopub.status.idle":"2021-06-13T17:27:01.913862Z","shell.execute_reply.started":"2021-06-13T17:26:47.347245Z","shell.execute_reply":"2021-06-13T17:27:01.912802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_iter = iter(my_monet_ds)\nfor n_sample in range(8):\n\n        example_sample = next(ds_iter)\n        generated_sample = photo_generator(example_sample)\n        \n        f = plt.figure(figsize=(24, 24))\n        \n        plt.subplot(121)\n        plt.title('Input image')\n        plt.imshow(example_sample[0] * 0.5 + 0.5)\n        plt.axis('off')\n        \n        plt.subplot(122)\n        plt.title('Generated image')\n        plt.imshow(generated_sample[0] * 0.5 + 0.5)\n        plt.axis('off')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:27:01.915271Z","iopub.execute_input":"2021-06-13T17:27:01.915577Z","iopub.status.idle":"2021-06-13T17:27:21.794019Z","shell.execute_reply.started":"2021-06-13T17:27:01.915547Z","shell.execute_reply":"2021-06-13T17:27:21.792622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\ndef predict_and_save(input_ds, generator_model, output_path):\n    i = 1\n    for img in input_ds:\n        prediction = generator_model(img, training=False)[0].numpy() # make predition\n        prediction = (prediction * 127.5 + 127.5).astype(np.uint8)   # re-scale\n        im = PIL.Image.fromarray(prediction)\n        im.save(f'{output_path}{str(i)}.jpg')\n        i += 1\n\nos.makedirs('../images/') # Create folder to save generated images\n\npredict_and_save(load_dataset(PHOTO_FILENAMES).batch(1), monet_generator, '../images/')","metadata":{"execution":{"iopub.status.busy":"2021-06-13T17:27:21.795581Z","iopub.execute_input":"2021-06-13T17:27:21.796000Z","iopub.status.idle":"2021-06-13T18:03:50.339812Z","shell.execute_reply.started":"2021-06-13T17:27:21.795961Z","shell.execute_reply":"2021-06-13T18:03:50.338619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.make_archive('/kaggle/working/images/', 'zip', '../images')\n\nprint(f\"Generated samples: {len([name for name in os.listdir('../images/') if os.path.isfile(os.path.join('../images/', name))])}\")","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:03:50.341463Z","iopub.execute_input":"2021-06-13T18:03:50.341800Z","iopub.status.idle":"2021-06-13T18:03:54.423002Z","shell.execute_reply.started":"2021-06-13T18:03:50.341769Z","shell.execute_reply":"2021-06-13T18:03:54.421825Z"},"trusted":true},"execution_count":null,"outputs":[]}]}