{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-06T04:31:26.488068Z","iopub.execute_input":"2023-04-06T04:31:26.489241Z","iopub.status.idle":"2023-04-06T04:31:26.526429Z","shell.execute_reply.started":"2023-04-06T04:31:26.489076Z","shell.execute_reply":"2023-04-06T04:31:26.525465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom io import BytesIO\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport torch\n\nimport torch.optim as optim\nimport requests\nfrom torchvision import transforms,models","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:31:26.528759Z","iopub.execute_input":"2023-04-06T04:31:26.529562Z","iopub.status.idle":"2023-04-06T04:31:29.082058Z","shell.execute_reply.started":"2023-04-06T04:31:26.529503Z","shell.execute_reply":"2023-04-06T04:31:29.081070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg = models.vgg19(pretrained=True).features\nfor p in vgg.parameters():\n    p.requires_grad_(False)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:31:29.083378Z","iopub.execute_input":"2023-04-06T04:31:29.084114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nvgg.to(device)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_img(img_path, max_size=128, shape=None):\n    if \"http\" in img_path:\n        response=requests.get(img_path)\n        img = Image.open(BytesIO(response.content)).convert('RGB')\n    else:\n        img = Image.open(img_path).convert('RGB')\n    if max(img.size)>max_size:\n        size=max_size\n    else:\n        size=max(img.size)\n    if shape is not None:\n        size=shape\n    in_transform = transforms.Compose([transforms.Resize(size),transforms.ToTensor(),transforms.Normalize((0.485, 0.456,0.406),(0.299, 0.224,0.225))\n                                      ]) \n    img=in_transform(img)[:3,:,:].unsqueeze(0)\n    \n    \n    return img","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"content_img = load_img('../input/neural-style-transfer/Mona darling.jpg').to(device)\nstyle_img = load_img('../input/neural-style-transfer/Starry_Night.jpg').to(device)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def im_convert(tensor):\n    img = tensor.to(\"cpu\").clone().detach()\n    img = img.numpy().squeeze()\n    img = img.transpose(1,2,0)\n    img = img*np.array((0.229,0.224,0.225))+ np.array((0.485,0.456,0.406))\n    img = img.clip(0,1)\n    \n    return img","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fix,(ax1,ax2)=plt.subplots(1,2, figsize=(12,5))\nax1.imshow(im_convert(content_img))\nax2.imshow(im_convert(style_img))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_features(image, model, layers=None):\n    if layers is None:\n        layers={'0': 'conv1_1',\n                  '5': 'conv2_1', \n                  '10': 'conv3_1', \n                  '19': 'conv4_1',\n                  '21': 'conv4_2',  ## content representation\n                  '28': 'conv5_1'}\n        features={}\n        x=image\n        for name,layer in model._modules.items():\n            x=layer(x)\n            if name in layers:\n                features[layers[name]]=x\n        return features        ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gramMatrix(tensor):\n    _,d,h,w=tensor.size()\n    tensor = tensor.view(d,h*w)\n    gram=torch.mm(tensor,tensor.t())\n    return gram","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"content_features = get_features(content_img,vgg)\nstyle_features = get_features(style_img,vgg)\nstyle_grams = {layer: gramMatrix(style_features[layer]) for layer in style_features}\n\ntarget = content_img.clone().requires_grad_(True).to(device)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"style_weights = {'conv1_1': 1,\n                 'conv2_1': 0.75,\n                 'conv3_1': 0.2,\n                 'conv4_1': 0.2,\n                 'conv5_1': 0.2}\n\ncontent_weight = 1  # alpha\nstyle_weight = 1e3","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show=100\noptimizer =optim.Adam([target],lr=0.003)\nepoches = 1001\nfor i in range(1,epoches+1):\n    target_features=get_features(target,vgg)\n    content_loss = torch.mean((target_features['conv4_2']-content_features['conv4_2'])**2)\n    style_loss=0\n    for layer in style_weights:\n        target_feature = target_features[layer]\n        target_gram = gramMatrix(target_feature)\n        _, d, h, w = target_feature.shape\n        style_gram = style_grams[layer]\n        \n        layer_style_loss = style_weights[layer] * torch.mean((target_gram - style_gram)**2)\n       \n        style_loss += layer_style_loss / (d * h * w)\n    total_loss = content_weight * content_loss + style_weight * style_loss\n    optimizer.zero_grad()\n    total_loss.backward()\n    optimizer.step()\n    if  i % show == 0:\n        print('Total loss: ', total_loss.item())\n        plt.imshow(im_convert(target))\n        plt.show()    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))\nax1.imshow(im_convert(content_img))\nax2.imshow(im_convert(target))\nplt.axis('off')\nfig=plt.gcf()\nfig.savefig('submission_img.png',bbox_inches='tight')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# im_convert(target)\n# plt.imshow(np.squeeze(target))\n# plt.axis('off')\n# f1=plt.gcf()\n# f1.savefig('submission_img.png',bbox_inches='tight')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}