{"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":"markdown","source":"### Downloading All Pretrained Models and Repositories","metadata":{}},{"cell_type":"code","source":"### EDSR\n\n# Cloning the EDSR Repo\n!git clone https://github.com/krasserm/super-resolution.git\n\n# Downloading the Pre-trained model\n!wget https://martin-krasser.de/sisr/weights-edsr-16-x4.tar.gz\n!tar xvfz weights-edsr-16-x4.tar.gz\n\n### Real-ESRGAN\n\n# Cloning of Real-ESRGAN Repo\n!git clone https://github.com/xinntao/Real-ESRGAN.git\n%cd Real-ESRGAN\n\n# Setting up the environment\n!pip install basicsr\n!pip install facexlib\n!pip install gfpgan\n!pip install -r requirements.txt\n!python setup.py develop\n\n# Downloading the pre-trained model\n!wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P experiments/pretrained_models\n    \n### BSRGAN\n!git clone https://github.com/cszn/BSRGAN.git\n    \n# Downloading the pre-trained model\n!wget https://github.com/cszn/KAIR/releases/download/v1.0/BSRGAN.pth -P BSRGAN/model_zoo\n\n!rm -r SwinIR\n\n### SwinIR\n!git clone https://github.com/JingyunLiang/SwinIR.git\n!pip install timm\n\n# Downloading the pre-trained model\n!wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth -P experiments/pretrained_models","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-18T17:54:12.924189Z","iopub.execute_input":"2023-10-18T17:54:12.924581Z","iopub.status.idle":"2023-10-18T17:55:42.736066Z","shell.execute_reply.started":"2023-10-18T17:54:12.924497Z","shell.execute_reply":"2023-10-18T17:55:42.734873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import Libraries","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport os\nimport glob\nimport shutil\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-18T17:55:42.738105Z","iopub.execute_input":"2023-10-18T17:55:42.738815Z","iopub.status.idle":"2023-10-18T17:55:42.922413Z","shell.execute_reply.started":"2023-10-18T17:55:42.738779Z","shell.execute_reply":"2023-10-18T17:55:42.921565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Preprocessing Images","metadata":{}},{"cell_type":"code","source":"test_dir = \"/kaggle/input/airbus-ship-detection/test_v2/\"\nlr_dir = 'BSRGAN/testsets/RealSRSet/'\nhr_dir = '/OriginalImages/'\n\n\nif not os.path.isdir('/OriginalImages'):\n    os.mkdir('/OriginalImages')\nelse:\n    shutil.rmtree(hr_dir)\n    os.mkdir('/OriginalImages')\n\nif not os.path.isdir('BSRGAN/testsets/RealSRSet'):\n    os.mkdir('BSRGAN/testsets/RealSRSet')\nelse:\n    shutil.rmtree(lr_dir)\n    os.mkdir('BSRGAN/testsets/RealSRSet')\n    \n    \n    \n\ndef is_image_file(filename):\n    return any(filename.endswith(extension) for extension in ['.png', '.jpg', '.jpeg', '.PNG', '.JPG', '.JPEG'])\n\nlr_img_array_list = []\n\nfor img in os.listdir(test_dir)[0:30]:\n    if is_image_file(img):\n        img_array = cv2.imread(test_dir + img)\n        img_array = cv2.resize(img_array, (128,128))\n        lr_img_array = cv2.resize(img_array,(32,32))\n        lr_img_array_list.append(lr_img_array)\n        cv2.imwrite(hr_dir + img, img_array)\n        cv2.imwrite(lr_dir + img, lr_img_array)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T17:55:42.923658Z","iopub.execute_input":"2023-10-18T17:55:42.923965Z","iopub.status.idle":"2023-10-18T17:55:44.502541Z","shell.execute_reply.started":"2023-10-18T17:55:42.923937Z","shell.execute_reply":"2023-10-18T17:55:44.501406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if  os.path.isdir('/results'):\n    shutil.rmtree('/results')\nos.mkdir('/results')","metadata":{"execution":{"iopub.status.busy":"2023-10-18T17:55:44.506813Z","iopub.execute_input":"2023-10-18T17:55:44.507698Z","iopub.status.idle":"2023-10-18T17:55:44.512808Z","shell.execute_reply.started":"2023-10-18T17:55:44.507654Z","shell.execute_reply":"2023-10-18T17:55:44.511792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDSR","metadata":{}},{"cell_type":"code","source":"### EDSR Model\n%cd ..\n%cd super-resolution\nfrom model.common import resolve\nfrom model.edsr import edsr\nfrom utils import load_image, plot_sample\n\nmodel = edsr(scale=4, num_res_blocks=16)\n\n%cd ..\nmodel.load_weights('weights/edsr-16-x4/weights.h5')\nsr = resolve(model, lr_img_array_list)","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-18T17:55:44.514306Z","iopub.execute_input":"2023-10-18T17:55:44.514951Z","iopub.status.idle":"2023-10-18T17:55:59.667402Z","shell.execute_reply.started":"2023-10-18T17:55:44.514912Z","shell.execute_reply":"2023-10-18T17:55:59.666496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# EDSR Results\n%cd Real-ESRGAN\n\nresults_edsr = '/results/EDSR/'\nif not os.path.isdir(results_edsr):\n    os.mkdir(results_edsr)\n\nfor img,img_array in zip(os.listdir(hr_dir),sr):\n    cv2.imwrite(results_edsr + img, img_array.numpy())","metadata":{"execution":{"iopub.status.busy":"2023-10-18T17:55:59.668475Z","iopub.execute_input":"2023-10-18T17:55:59.668742Z","iopub.status.idle":"2023-10-18T17:55:59.705221Z","shell.execute_reply.started":"2023-10-18T17:55:59.668705Z","shell.execute_reply":"2023-10-18T17:55:59.704363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Real ESRGAN, BSRGAN, SwinIR","metadata":{}},{"cell_type":"code","source":"### Real-ESRGAN \n!python inference_realesrgan.py -n RealESRGAN_x4plus -i 'BSRGAN/testsets/RealSRSet' -o '/results/ESRGAN' --outscale 4 --face_enhance\n\n### BSRGAN\n%cd BSRGAN\n!python main_test_bsrgan.py\n%cd ..\nshutil.copytree('BSRGAN/testsets/RealSRSet_results_x4/', '/results/BSRGAN')\n\n### SwinIR\n!python SwinIR/main_test_swinir.py --task real_sr --model_path experiments/pretrained_models/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth --folder_lq BSRGAN/testsets/RealSRSet --scale 4\nshutil.copytree('results/swinir_real_sr_x4/', '/results/SwinIR')","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-18T17:55:59.706285Z","iopub.execute_input":"2023-10-18T17:55:59.706554Z","iopub.status.idle":"2023-10-18T17:56:09.900958Z","shell.execute_reply.started":"2023-10-18T17:55:59.70652Z","shell.execute_reply":"2023-10-18T17:56:09.899775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plotting Results","metadata":{}},{"cell_type":"code","source":"###  Image visualizations\n\n# Display setup\ndef display(img1, img2, img3, img4, img5, img6):\n    \n  fig = plt.figure(figsize=(20, 10))\n\n  ax1 = fig.add_subplot(1, 6, 1) \n  plt.title('Input image', fontsize=16)\n  ax1.axis('off')\n  ax2 = fig.add_subplot(1, 6, 2) \n  plt.title('EDSR', fontsize=16)\n  ax2.axis('off')\n  ax3 = fig.add_subplot(1, 6, 3)\n  plt.title('Real-ESRGAN output', fontsize=16)\n  ax3.axis('off')\n  ax4 = fig.add_subplot(1, 6, 4)\n  plt.title('BSRGAN Image', fontsize=16)\n  ax4.axis('off')\n  ax5 = fig.add_subplot(1, 6, 5)\n  plt.title('SwinIR Image', fontsize=16)\n  ax5.axis('off')\n  ax6 = fig.add_subplot(1, 6, 6)\n  plt.title('Original Image', fontsize=16)\n  ax6.axis('off')\n  ax1.imshow(img1)\n  ax2.imshow(img2)\n  ax3.imshow(img3)\n  ax4.imshow(img4)\n  ax5.imshow(img5)\n  ax6.imshow(img6)\n\ndef imread(img_path):\n  img = cv2.imread(img_path)\n  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n  return img\n\n# Input and results folder\ninput_folder = 'BSRGAN/testsets/RealSRSet'\noriginal_folder = '/OriginalImages/'\nresult_edsr = '/results/EDSR/'\nresult_esrgan = '/results/ESRGAN'\nresult_bsrgan =  '/results/BSRGAN'\nresult_swinir = '/results/SwinIR'\n\ninput_list = sorted(glob.glob(os.path.join(input_folder, '*')))\noriginal_list = sorted(glob.glob(os.path.join(original_folder, '*')))\noutput_edsr = sorted(glob.glob(os.path.join(result_edsr, '*')))\noutput_esrgan = sorted(glob.glob(os.path.join(result_esrgan, '*')))\noutput_bsrgan = sorted(glob.glob(os.path.join(result_bsrgan, '*')))\noutput_swinir= sorted(glob.glob(os.path.join(result_swinir, '*')))\n# Small subset of input for demo\nindex_list = [0,1,3,5,17,22]\nindex_list_edsr = [29,1,27,7,23,12]\ndemo_inp_list = [input_list[i] for i in index_list]\ndemo_original_list = [original_list[i] for i in index_list]\ndemo_out_edsr = [output_edsr[i] for i in index_list_edsr]\ndemo_out_esrgan = [output_esrgan[i] for i in index_list]\ndemo_out_bsrgan = [output_bsrgan[i] for i in index_list]\ndemo_out_swinir = [output_swinir[i] for i in index_list]\n\n\n# Print images\nfor input_path,original_path,out_edsr,out_esr,out_bsr,out_swin in zip(demo_inp_list,demo_original_list,demo_out_edsr,demo_out_esrgan,demo_out_bsrgan,demo_out_swinir):\n  img_input = imread(input_path)\n  img_original = imread(original_path)\n  img_edsr = imread(out_edsr)\n  img_esr = imread(out_esr)\n  img_bsr = imread(out_bsr)\n  img_swin = imread(out_swin)\n  display(img_input,img_edsr,img_esr,img_bsr,img_swin,img_original)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T18:06:19.234285Z","iopub.execute_input":"2023-10-18T18:06:19.234765Z","iopub.status.idle":"2023-10-18T18:06:19.280606Z","shell.execute_reply.started":"2023-10-18T18:06:19.234705Z","shell.execute_reply":"2023-10-18T18:06:19.279611Z"},"trusted":true},"execution_count":null,"outputs":[]}]}