{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" }, "colab": { "name": "JIGSAW_baseline.ipynb", "provenance": [], "collapsed_sections": [], "toc_visible": true } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "r_UoYN5PzsZh" }, "source": [ "![AIcrowd-Logo](https://raw.githubusercontent.com/AIcrowd/AIcrowd/master/app/assets/images/misc/aicrowd-horizontal.png)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "QnktOs5tzsZi" }, "source": [ "# Getting Started Code for [JIGSAW Challenge](www.aicrowd.com/challenges/jigsaw) on AIcrowd\n", "#### Author : Sharada Mohanty" ] }, { "cell_type": "markdown", "metadata": { "id": "FSQaFNqj4Xqg" }, "source": [ "This baseline creates the image of desired size and places the puzzle pieces randomly." ] }, { "cell_type": "markdown", "metadata": { "id": "g3NTJjh6zsZj" }, "source": [ "## Download Necessary Packages 📚" ] }, { "cell_type": "code", "metadata": { "id": "52VNLEdxzsZl" }, "source": [ "!pip install numpy\n", "!pip install pandas" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "v1252SIXzsZp" }, "source": [ "## Download Data\n", "The first step is to download out train test data. We will be training a model on the train data and make predictions on test data. We submit our predictions.\n" ] }, { "cell_type": "code", "metadata": { "scrolled": true, "id": "y2jOSZWezsZq" }, "source": [ "!rm -rf data\n", "!mkdir data\n", "!wget https://datasets.aicrowd.com/default/aicrowd-practice-challenges/public/jigsaw/v0.1/puzzles.tar.gz\n", "!wget https://datasets.aicrowd.com/default/aicrowd-practice-challenges/public/jigsaw/v0.1/metadata.csv\n", "\n", "!mkdir data/puzzles \n", "!tar -C data/puzzles -xvzf puzzles.tar.gz \n", "\n", "!mv metadata.csv data/metadata.csv" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "2HnIJcEHzsZu" }, "source": [ "\n", "## Import packages" ] }, { "cell_type": "code", "metadata": { "id": "XKUyBzLWzsZv" }, "source": [ "import pandas as pd\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from PIL import Image\n", "import glob\n", "import tempfile\n", "import os \n", "import random\n", "import tqdm\n", "import tarfile\n", "%matplotlib inline" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "OYmcu6cezsZy" }, "source": [ "## Load Data" ] }, { "cell_type": "code", "metadata": { "id": "nIvcQSYNzsZz" }, "source": [ "PUZZLES_DIRECTORY = \"data/puzzles\"\n", "METADATA_FILE = \"data/metadata.csv\"\n", "\n", "OUTPUT_PATH = \"data/submission.tar.gz\"\n", "metadata_df = pd.read_csv(METADATA_FILE)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "YsvIsudZ4j-d" }, "source": [ "Create directory to store the solved image." ] }, { "cell_type": "code", "metadata": { "id": "Da3JIibY3W-9" }, "source": [ "TEMP_SUBMISSION_DIR = tempfile.TemporaryDirectory()\n", "TEMP_SUBMISSION_DIR_PATH = TEMP_SUBMISSION_DIR.name\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "eGBW_ioK4tPd" }, "source": [ "**This is a very naive approach of creating a new image of the desired size, and paste all the individual puzzle pieces at random locations.**\n", "\n", "\n" ] }, { "cell_type": "code", "metadata": { "id": "qh7mGFfr3bbA" }, "source": [ "for index, row in tqdm.tqdm(metadata_df.iterrows(), total=metadata_df.shape[0]):\n", " # Get the height and widhts of all the images from metadata.csv.\n", " \n", "\n", " puzzle_id = row[\"puzzle_id\"]\n", " image_width = row[\"width\"]\n", " image_height = row[\"height\"]\n", "\n", " puzzle_directory = os.path.join(\n", " PUZZLES_DIRECTORY,\n", " str(puzzle_id)\n", " )\n", " solved_puzzle_im = Image.new(\"RGBA\",\n", " (image_width, image_height)\n", " ) # Initially create RGBA images, and then drop A channel later\n", "\n", " for _puzzle_piece in glob.glob(os.path.join(puzzle_directory, \"*.png\")):\n", " puzzle_piece_im = Image.open(_puzzle_piece)\n", " pp_width, pp_height = puzzle_piece_im.size\n", "\n", " # Find Random location \n", " random_x = random.randint(0, image_width - pp_width)\n", " random_y = random.randint(0, image_height - pp_height)\n", "\n", " solved_puzzle_im.paste(puzzle_piece_im, (random_x, random_y))\n", "\n", " del puzzle_piece_im\n", "\n", " solved_puzzle_im.convert(\"RGB\").save(\n", " os.path.join(TEMP_SUBMISSION_DIR_PATH, \"{}.jpg\".format(str(puzzle_id)))\n", " )\n", " del solved_puzzle_im\n", "\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "j4GEh4rb_Irj" }, "source": [ "# Visualize the generated Image" ] }, { "cell_type": "code", "metadata": { "id": "_FSJixiE6M7y" }, "source": [ "img_path = os.path.join(TEMP_SUBMISSION_DIR_PATH,'2.jpg')\n", "img = Image.open(img_path)\n", "img" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "BhEwJBmW5VGR" }, "source": [ "# Create the tar file with all the solved images and store it in the OUTPUT PATH." ] }, { "cell_type": "code", "metadata": { "id": "KMRVv9Cw3n29" }, "source": [ "with tarfile.open(OUTPUT_PATH, mode=\"w:gz\") as tar_file:\n", " for _filepath in glob.glob(os.path.join(TEMP_SUBMISSION_DIR_PATH, \"*.jpg\")):\n", " print(_filepath)\n", " _, filename = os.path.split(_filepath)\n", " tar_file.add(_filepath, arcname=filename)\n", "\n", "print(\"Wrote output file to : \", OUTPUT_PATH)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "k-WPsT0Szsag" }, "source": [ "## To download the generated tar.gz in colab run the below command" ] }, { "cell_type": "code", "metadata": { "id": "c2lFyA17zsah" }, "source": [ "try:\n", " from google.colab import files\n", " files.download(OUTPUT_PATH) \n", "except:\n", " print(\"Option Only avilable in Google Colab\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "KKZfXEPUzsak" }, "source": [ "### Well Done! 👍 We are all set to make a submission and see your name on leaderborad. Lets navigate to [challenge page](https://www.aicrowd.com/challenges/jigsaw) and make one." ] }, { "cell_type": "code", "metadata": { "id": "Pi2lTDqd51u6" }, "source": [ "" ], "execution_count": null, "outputs": [] } ] }