{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"duration":67.465129,"end_time":"2021-09-02T20:27:09.970707","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2021-09-02T20:26:02.505578","version":"2.1.0"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport numpy as np\nimport time\nfrom PIL import Image\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:46:02.357568Z","iopub.execute_input":"2025-06-22T15:46:02.357775Z","iopub.status.idle":"2025-06-22T15:46:15.618226Z","shell.execute_reply.started":"2025-06-22T15:46:02.357748Z","shell.execute_reply":"2025-06-22T15:46:15.617684Z"},"id":"EuYCyA5gl4E4","papermill":{"duration":4.920469,"end_time":"2021-09-02T20:26:14.635681","exception":false,"start_time":"2021-09-02T20:26:09.715212","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_image(image_path):\n    max_dim=512\n    img = tf.io.read_file(image_path)\n    img = tf.image.decode_image(img, channels=3)# decodes the image into a tensor\n    img = tf.image.convert_image_dtype(img, tf.float32)\n\n    shape = tf.cast(tf.shape(img)[:-1], tf.float32)\n    long_dim = max(shape)\n    scale = max_dim / long_dim\n    new_shape = tf.cast(shape * scale, tf.int32)\n\n    img = tf.image.resize(img, new_shape)\n    img = img[tf.newaxis, :]# broadcasting the image array so that it has a batch dimension\n\n    return img","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:46:15.618772Z","iopub.execute_input":"2025-06-22T15:46:15.619199Z","iopub.status.idle":"2025-06-22T15:46:15.624280Z","shell.execute_reply.started":"2025-06-22T15:46:15.619179Z","shell.execute_reply":"2025-06-22T15:46:15.623594Z"},"id":"Ak8NXP86mWq5","papermill":{"duration":0.026565,"end_time":"2021-09-02T20:26:14.711732","exception":false,"start_time":"2021-09-02T20:26:14.685167","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def imshow(image, title=None):\n    if(len(image.shape) > 3):\n        image=np.squeeze(image, axis=0)\n    plt.imshow(image)\n    if(title):# if there's a title mention it\n        plt.title(title)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:46:15.626632Z","iopub.execute_input":"2025-06-22T15:46:15.628964Z","iopub.status.idle":"2025-06-22T15:46:15.658371Z","shell.execute_reply.started":"2025-06-22T15:46:15.628945Z","shell.execute_reply":"2025-06-22T15:46:15.657679Z"},"id":"YPoIYOtzrasS","papermill":{"duration":0.02414,"end_time":"2021-09-02T20:26:14.752339","exception":false,"start_time":"2021-09-02T20:26:14.728199","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 选取目标图片和风格图片\ncontent_img=load_image('/kaggle/input/gan-getting-started/photo_jpg/00882b7e1d.jpg')\nstyle_img=load_image('/kaggle/input/gan-getting-started/monet_jpg/8c8011c291.jpg')\n\nplt.figure(figsize=(12,12))\nplt.subplot(1, 2, 1)\nimshow(content_img, 'Target image')\nplt.subplot(1, 2, 2)\nimshow(style_img, 'Style image')","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:20.770496Z","iopub.execute_input":"2025-06-22T15:55:20.771011Z","iopub.status.idle":"2025-06-22T15:55:21.372007Z","shell.execute_reply.started":"2025-06-22T15:55:20.770987Z","shell.execute_reply":"2025-06-22T15:55:21.371305Z"},"id":"02kdlfJet2Ji","outputId":"dbb90c51-b0d0-4890-91f7-7eba455b1129","papermill":{"duration":2.515886,"end_time":"2021-09-02T20:26:17.284734","exception":false,"start_time":"2021-09-02T20:26:14.768848","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(content_img.shape)\nprint(style_img.shape)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:21.373052Z","iopub.execute_input":"2025-06-22T15:55:21.373272Z","iopub.status.idle":"2025-06-22T15:55:21.377116Z","shell.execute_reply.started":"2025-06-22T15:55:21.373255Z","shell.execute_reply":"2025-06-22T15:55:21.376439Z"},"id":"vVFKspAUGTDd","outputId":"2bfb1ed3-1d95-4146-b071-5b71cd0de82d","papermill":{"duration":0.039153,"end_time":"2021-09-02T20:26:17.354885","exception":false,"start_time":"2021-09-02T20:26:17.315732","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the content image representation and load the model\nx=tf.keras.applications.vgg19.preprocess_input(content_img*255)# needs preprocessing for the model to be initialized\nx=tf.image.resize(x, (256,256))# the vgg19 model takes images in 256\nvgg_model=tf.keras.applications.VGG19(include_top=False, weights='imagenet')\nvgg_model.trainable=False\nvgg_model.summary()","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:21.377831Z","iopub.execute_input":"2025-06-22T15:55:21.378114Z","iopub.status.idle":"2025-06-22T15:55:21.743700Z","shell.execute_reply.started":"2025-06-22T15:55:21.378098Z","shell.execute_reply":"2025-06-22T15:55:21.743163Z"},"id":"acSjybfw5k35","outputId":"67972759-d0c5-42b7-e955-62fb69dfabd7","papermill":{"duration":0.919651,"end_time":"2021-09-02T20:26:18.305946","exception":false,"start_time":"2021-09-02T20:26:17.386295","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"content_layers=['block4_conv2']\nstyle_layers = ['block1_conv1',\n                'block2_conv1',\n                'block3_conv1', \n                'block4_conv1', \n                'block5_conv1']","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:21.745298Z","iopub.execute_input":"2025-06-22T15:55:21.746022Z","iopub.status.idle":"2025-06-22T15:55:21.749303Z","shell.execute_reply.started":"2025-06-22T15:55:21.745997Z","shell.execute_reply":"2025-06-22T15:55:21.748517Z"},"id":"ULHAsnwRgcfG","papermill":{"duration":0.041261,"end_time":"2021-09-02T20:26:18.386171","exception":false,"start_time":"2021-09-02T20:26:18.344910","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def my_model(layer_names):\n    # Retrieve the output layers corresponding to the content and style layers\n    vgg_model = tf.keras.applications.VGG19(include_top=False, weights='imagenet')\n    vgg_model.trainable = False\n    outputs = [vgg_model.get_layer(name).output for name in layer_names]\n    model=tf.keras.Model([vgg_model.input], outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:21.749965Z","iopub.execute_input":"2025-06-22T15:55:21.750153Z","iopub.status.idle":"2025-06-22T15:55:21.763431Z","shell.execute_reply.started":"2025-06-22T15:55:21.750138Z","shell.execute_reply":"2025-06-22T15:55:21.762691Z"},"id":"9NUkwQi9j7Gd","papermill":{"duration":0.041519,"end_time":"2021-09-02T20:26:18.461037","exception":false,"start_time":"2021-09-02T20:26:18.419518","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"style_extractor = my_model(style_layers)\nstyle_outputs = style_extractor(style_img*255)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:21.764298Z","iopub.execute_input":"2025-06-22T15:55:21.764563Z","iopub.status.idle":"2025-06-22T15:55:22.134689Z","shell.execute_reply.started":"2025-06-22T15:55:21.764540Z","shell.execute_reply":"2025-06-22T15:55:22.133920Z"},"id":"NZWP6435XbGa","papermill":{"duration":2.905625,"end_time":"2021-09-02T20:26:21.400366","exception":false,"start_time":"2021-09-02T20:26:18.494741","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gram_matrix(input_tensor): # input_tensor is of shape ch, n_H, n_W\n    result = tf.linalg.einsum('bijc,bijd->bcd', input_tensor, input_tensor)\n    input_shape = tf.shape(input_tensor)\n    num_locations = tf.cast(input_shape[1]*input_shape[2], tf.float32) # Unrolls n_H and n_W\n    return result/(num_locations)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.135370Z","iopub.execute_input":"2025-06-22T15:55:22.135575Z","iopub.status.idle":"2025-06-22T15:55:22.139547Z","shell.execute_reply.started":"2025-06-22T15:55:22.135560Z","shell.execute_reply":"2025-06-22T15:55:22.138917Z"},"id":"mTlc1rO7lljq","papermill":{"duration":0.046116,"end_time":"2021-09-02T20:26:21.482554","exception":false,"start_time":"2021-09-02T20:26:21.436438","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class entire_model(tf.keras.models.Model):\n    def __init__(self, style_layers, content_layers):\n        super(entire_model, self).__init__()\n        self.vgg=my_model(style_layers + content_layers)\n        self.style_layers=style_layers\n        self.content_layers=content_layers\n        self.num_style_layers=len(style_layers)\n        self.vgg.trainable=False\n\n    def call(self, inputs):\n        inputs=inputs*255.0\n        preprocessed_input=tf.keras.applications.vgg19.preprocess_input(inputs)\n        outputs=self.vgg(preprocessed_input)\n\n        style_outputs, content_outputs=(outputs[:self.num_style_layers], outputs[self.num_style_layers:])\n        style_outputs=[gram_matrix(layer) for layer in style_outputs]\n\n        content_dict = {content_name:value\n                    for content_name, value\n                    in zip(self.content_layers, content_outputs)}\n\n        style_dict = {style_name:value\n                  for style_name, value\n                  in zip(self.style_layers, style_outputs)}\n\n        return {'content': content_dict, 'style': style_dict}","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.140202Z","iopub.execute_input":"2025-06-22T15:55:22.140451Z","iopub.status.idle":"2025-06-22T15:55:22.160084Z","shell.execute_reply.started":"2025-06-22T15:55:22.140428Z","shell.execute_reply":"2025-06-22T15:55:22.159389Z"},"id":"PAMfMhUK5qjs","papermill":{"duration":0.048126,"end_time":"2021-09-02T20:26:21.565332","exception":false,"start_time":"2021-09-02T20:26:21.517206","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"extractor=entire_model(style_layers, content_layers)\nstyle_targets = extractor(style_img)['style']\ncontent_targets = extractor(content_img)['content']\n\nresults = extractor(tf.constant(content_img))","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.160961Z","iopub.execute_input":"2025-06-22T15:55:22.161246Z","iopub.status.idle":"2025-06-22T15:55:22.629331Z","shell.execute_reply.started":"2025-06-22T15:55:22.161222Z","shell.execute_reply":"2025-06-22T15:55:22.628518Z"},"id":"zTZDU5Y0Dy43","papermill":{"duration":0.666376,"end_time":"2021-09-02T20:26:22.266627","exception":false,"start_time":"2021-09-02T20:26:21.600251","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"style_weight=40\ncontent_weight=10\n\nstyle_weights = {'block1_conv1': 0.2,\n                 'block2_conv1': 0.19,\n                 'block3_conv1': 0.24,\n                 'block4_conv1': 0.11,\n                 'block5_conv1': 0.26}","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.631461Z","iopub.execute_input":"2025-06-22T15:55:22.632261Z","iopub.status.idle":"2025-06-22T15:55:22.635557Z","shell.execute_reply.started":"2025-06-22T15:55:22.632233Z","shell.execute_reply":"2025-06-22T15:55:22.634907Z"},"id":"HUwwlgzvJxS6","papermill":{"duration":0.042759,"end_time":"2021-09-02T20:26:22.344002","exception":false,"start_time":"2021-09-02T20:26:22.301243","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def total_cost(outputs):\n    style_outputs=outputs['style']\n    content_outputs=outputs['content']\n    style_loss=tf.add_n([style_weights[name]*tf.reduce_mean((style_outputs[name]-style_targets[name])**2)\n                        for name in style_outputs.keys()])\n    style_loss*=style_weight/len(style_layers)# Normalize\n\n    content_loss = tf.add_n([tf.reduce_mean((content_outputs[name]-content_targets[name])**2)\n                             for name in content_outputs.keys()])\n    content_loss*=content_weight/len(content_layers)\n    loss=style_loss+content_loss\n    return loss","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.636186Z","iopub.execute_input":"2025-06-22T15:55:22.636443Z","iopub.status.idle":"2025-06-22T15:55:22.658987Z","shell.execute_reply.started":"2025-06-22T15:55:22.636420Z","shell.execute_reply":"2025-06-22T15:55:22.658213Z"},"id":"gojU17ArLXMt","papermill":{"duration":0.251556,"end_time":"2021-09-02T20:26:22.629829","exception":false,"start_time":"2021-09-02T20:26:22.378273","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"generate_image = tf.Variable(content_img)\n\ndef clip_0_1(image):\n    return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.659775Z","iopub.execute_input":"2025-06-22T15:55:22.660080Z","iopub.status.idle":"2025-06-22T15:55:22.673581Z","shell.execute_reply.started":"2025-06-22T15:55:22.660055Z","shell.execute_reply":"2025-06-22T15:55:22.672915Z"},"id":"ir076-JMOMpr","papermill":{"duration":0.070264,"end_time":"2021-09-02T20:26:22.759637","exception":false,"start_time":"2021-09-02T20:26:22.689373","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"opt = tf.optimizers.Adam(learning_rate=0.02, beta_1=0.99, epsilon=1e-1)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.674326Z","iopub.execute_input":"2025-06-22T15:55:22.674551Z","iopub.status.idle":"2025-06-22T15:55:22.691213Z","shell.execute_reply.started":"2025-06-22T15:55:22.674529Z","shell.execute_reply":"2025-06-22T15:55:22.690675Z"},"id":"ki8m2cLQOjee","papermill":{"duration":0.070848,"end_time":"2021-09-02T20:26:22.889500","exception":false,"start_time":"2021-09-02T20:26:22.818652","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"@tf.function()\ndef train_step(image):\n    with tf.GradientTape() as tape:\n        outputs = extractor(image)\n        loss = total_cost(outputs)\n\n    grad = tape.gradient(loss, image)\n    opt.apply_gradients([(grad, image)])\n    image.assign(clip_0_1(image))","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.691941Z","iopub.execute_input":"2025-06-22T15:55:22.692149Z","iopub.status.idle":"2025-06-22T15:55:22.708474Z","shell.execute_reply.started":"2025-06-22T15:55:22.692134Z","shell.execute_reply":"2025-06-22T15:55:22.707974Z"},"id":"b8p6gBM9Nbg0","papermill":{"duration":0.046297,"end_time":"2021-09-02T20:26:22.990018","exception":false,"start_time":"2021-09-02T20:26:22.943721","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_iterations=1500\nfor i in range(num_iterations):\n    if(i%300==0):\n        plt.figure(figsize=(12,12))\n        plt.subplot(1, 2, 1)\n        imshow(np.squeeze(generate_image.read_value(), 0), f'Step {i}')\n        plt.subplot(1, 2, 2)\n        imshow(style_img, 'Style Image')\n    train_step(generate_image)","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:55:22.709796Z","iopub.execute_input":"2025-06-22T15:55:22.710008Z","iopub.status.idle":"2025-06-22T15:56:27.659682Z","shell.execute_reply.started":"2025-06-22T15:55:22.709993Z","shell.execute_reply":"2025-06-22T15:56:27.658943Z"},"id":"q6i1G-QmyI_f","outputId":"6275fa2d-927c-4862-f717-324d91cc01c7","papermill":{"duration":45.738302,"end_time":"2021-09-02T20:27:08.762605","exception":false,"start_time":"2021-09-02T20:26:23.024303","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_step(generate_image)\nplt.imshow(np.squeeze(generate_image.read_value(), 0))\nplt.axis('off')\nfig1 = plt.gcf()\nfig1.savefig('new_image.png', bbox_inches='tight')","metadata":{"execution":{"iopub.status.busy":"2025-06-22T15:56:27.660555Z","iopub.execute_input":"2025-06-22T15:56:27.660814Z","iopub.status.idle":"2025-06-22T15:56:27.957795Z","shell.execute_reply.started":"2025-06-22T15:56:27.660793Z","shell.execute_reply":"2025-06-22T15:56:27.957005Z"},"id":"-Y96xR8uybmk","outputId":"eec15caa-b4f1-477e-c66b-bd67c6c31529","papermill":{"duration":0.252398,"end_time":"2021-09-02T20:27:09.138140","exception":false,"start_time":"2021-09-02T20:27:08.885742","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.121207,"end_time":"2021-09-02T20:27:09.641903","exception":false,"start_time":"2021-09-02T20:27:09.520696","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null}]}