{"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":"# Interactively display images and guess if they're show cancer or healthy tissue\n\nThis kernel allows to browse through randomly selected images on at a time.  \nFirst you see the image without label and you can try to 'classify' it.  \nThen you'll see the image with label and check if you were correct.\n\nSpoiler: Even the most basic CNN will probably be better than you :-)","metadata":{}},{"cell_type":"code","source":"import os\n\nimport numpy as np\nfrom numpy.random import choice\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom PIL import ImageDraw, ImageFont, Image\n\nimport ipywidgets as widgets\nfrom ipywidgets import interactive, IntSlider\nfrom IPython.display import display\n\nimport torch\nfrom torchvision import transforms\nimport torchvision","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-12T10:17:53.410342Z","iopub.execute_input":"2022-03-12T10:17:53.410672Z","iopub.status.idle":"2022-03-12T10:17:53.421496Z","shell.execute_reply.started":"2022-03-12T10:17:53.410624Z","shell.execute_reply":"2022-03-12T10:17:53.420490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = '../input'\n\ntrain_dir = os.path.join(DATA_DIR, 'train')\nlabel_df = pd.read_csv(os.path.join(DATA_DIR, 'train_labels.csv'))    ","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-03-12T10:17:53.423294Z","iopub.execute_input":"2022-03-12T10:17:53.423525Z","iopub.status.idle":"2022-03-12T10:17:53.694157Z","shell.execute_reply.started":"2022-03-12T10:17:53.423492Z","shell.execute_reply":"2022-03-12T10:17:53.693060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def annotate_image(image: Image, text: str):\n    draw = ImageDraw.Draw(image)\n    draw.rectangle(((0, 0), (20, 12)), fill='white', outline='black')\n    draw.text((2, 0), text, fill='black')\n    return image\n\n\ndef sample_images(data_dir: str, files=None, n=15, label_df=None, box=True, return_img=False):\n    \n    if files is None:\n        files = choice(os.listdir(data_dir), size=n)\n        \n    images = []\n    for filename in files:\n        img = Image.open(os.path.join(train_dir, filename))\n        \n        if label_df is not None:\n            id_   = filename.split('.')[0]\n            label = label_df.query('id == @id_').label.item()\n            img   = annotate_image(img, f'y={label}')\n        \n        if box:\n            draw = ImageDraw.Draw(img)\n            draw.rectangle(((32, 32), (64, 64)), outline='green')\n            \n        images.append(img)\n\n    tensors = list(map(transforms.ToTensor(), images))\n    tensor  = torch.stack(tensors)\n    grid    = torchvision.utils.make_grid(tensor, nrow=n//int(np.ceil(n/5)))\n    \n    if return_img:\n        return grid\n    \n    plt.figure(figsize=(24, 10))\n    plt.imshow(grid.permute(1, 2, 0))\n    plt.grid(False)\n    plt.xticks([])\n    plt.yticks([]);\n    plt.show()\n\nsample_images(data_dir=train_dir, n=15, label_df=label_df, box=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:17:53.695556Z","iopub.execute_input":"2022-03-12T10:17:53.695888Z","iopub.status.idle":"2022-03-12T10:17:54.712985Z","shell.execute_reply.started":"2022-03-12T10:17:53.695827Z","shell.execute_reply":"2022-03-12T10:17:54.712166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def guess_label(n=10):\n    images = []\n    for _ in range(n):\n        file = choice(os.listdir(train_dir))  # raw and labeled version of the same image\n        raw = sample_images(train_dir, files=[file], label_df=None, return_img=True)\n        labeled = sample_images(train_dir, files=[file], label_df=label_df, return_img=True)\n        images.extend([raw, labeled])  # order: raw, labeld, raw, ....\n\n    def show_image(i):\n        plt.figure(figsize=(24, 10))\n        plt.imshow(images[i].permute(1, 2, 0))\n        plt.grid(False)\n        plt.xticks([])\n        plt.yticks([]);\n        plt.show()    \n\n    return interactive(show_image, i=IntSlider(min=0, max=n*2 - 1))","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:17:54.714449Z","iopub.execute_input":"2022-03-12T10:17:54.714739Z","iopub.status.idle":"2022-03-12T10:17:54.724940Z","shell.execute_reply.started":"2022-03-12T10:17:54.714689Z","shell.execute_reply":"2022-03-12T10:17:54.724121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Instruction\n\nJust start scrolling through the images.\n\nYou'll see the unlabled image first and the labeled image next.  \nThen, the next unlabeld image is shown and so on...","metadata":{}},{"cell_type":"code","source":"guess_label(n=10)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:17:54.727374Z","iopub.execute_input":"2022-03-12T10:17:54.727643Z","iopub.status.idle":"2022-03-12T10:17:58.445076Z","shell.execute_reply.started":"2022-03-12T10:17:54.727591Z","shell.execute_reply":"2022-03-12T10:17:58.444351Z"},"trusted":true},"execution_count":null,"outputs":[]}]}