{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Install all prerequisites"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!git clone https://github.com/taesungp/contrastive-unpaired-translation.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install dominate visdom","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Losses\n\nAdversarial loss:\n\n$$\\mathcal{L}_{\\operatorname{GAN}}(G,D,X,Y) = \\mathbb{E}_{\\mathbf{y}\\sim Y}[\\log D(y)] + \\mathbb{E}_{\\mathbf{x}\\sim X}[\\log(1-D(G(x)))] $$\n\nThe cross-entropy loss is calculated, representing the probability of the positive example being selected over negatives\n\n$$\\ell(v,v^+,v^-) = -\\log \\bigg[ \\frac{\\exp (v \\cdot v^+ / \\tau)}{\\exp(v \\cdot v^+ / \\tau) + \\sum_{n=1}^{N} \\exp(v \\cdot v_n^- / \\tau)} \\bigg]$$\n\nPatchNCE loss\n$$ \\mathcal{L}_{\\operatorname{PatchNCE}}(G, H, X) = \\mathbb{E}_{\\mathbf{x}\\sim X} \\sum_{l=1}^L \\sum_{s=1}^{S_l} \\ell (\\hat{z}_l^s, z_l^s, z_l^{S \\setminus s}) $$"},{"metadata":{},"cell_type":"markdown","source":"# Train model"},{"metadata":{"trusted":true},"cell_type":"code","source":"!scp -r ../input/gan-getting-started/photo_jpg ./trainA\n!scp -r ../input/gan-getting-started/monet_jpg ./trainB","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python contrastive-unpaired-translation/train.py --dataroot ./ --name monet_gen --CUT_mode CUT --display_id 0 --n_epochs 7 --n_epochs_decay 2 --batch_size 4 --load_size 256 --lr 5e-05","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python contrastive-unpaired-translation/test.py --dataroot ./ --name monet_gen --CUT_mode CUT --phase train --num_test 7038","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ./results/monet_gen/train_latest/images/fake_B","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ./results/monet_gen/train_latest/images/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show & submit"},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom os import listdir\n\npwd = './results/monet_gen/train_latest/images/fake_B/'\nfilenames = listdir(pwd)\nfor f in filenames[:10]:\n    img = mpimg.imread(pwd + f)\n    plt.imshow(img)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\n\nshutil.make_archive('/kaggle/working/images', 'zip', pwd)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for d in ['./trainA', './trainB', './results']:\n    shutil.rmtree(d)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}