{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import glob\n", "import numpy as np\n", "import random\n", "\n", "import cv2\n", "import matplotlib.pyplot as plt\n", "from PIL import Image\n", "\n", "from torchvision import transforms as pil_transforms\n", "from torchvision.transforms import functional as F_pil\n", "\n", "import sys\n", "sys.path.insert(0, '..')\n", "from opencv_transforms import transforms\n", "from opencv_transforms import functional as F\n", "\n", "from setup_testing_directory import get_testing_directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datadir = get_testing_directory()\n", "print(datadir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_images = glob.glob(datadir + '/**/*.JPEG', recursive=True)\n", "train_images.sort()\n", "print('Number of training images: {:,}'.format(len(train_images)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.seed(1)\n", "imfile = random.choice(train_images)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def plot_pil_and_opencv(pil_image, opencv_image, orientation='row'):\n", " if orientation == 'row':\n", " rows, cols = 1,3\n", " size = (8, 4)\n", " else: \n", " rows, cols = 3,1\n", " size = (12, 6)\n", " fig, axes = plt.subplots(rows, cols,figsize=size)\n", " ax = axes[0]\n", " ax.imshow(pil_image)\n", " ax.set_title('PIL')\n", "\n", " ax = axes[1]\n", " ax.imshow(opencv_image)\n", " ax.set_title('opencv')\n", "\n", " ax = axes[2]\n", " l1 = np.abs(pil_image - opencv_image).mean(axis=2)\n", " ax.imshow(l1)\n", " ax.set_title('| PIL - opencv|\\nMAE:{:.4f}'.format(l1.mean()))\n", " plt.tight_layout()\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pil_image = Image.open(imfile)\n", "image = cv2.cvtColor(cv2.imread(imfile, 1), cv2.COLOR_BGR2RGB)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_pil_and_opencv(pil_image, image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pil_resized = pil_transforms.Resize((224, 224))(pil_image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "resized = transforms.Resize(224)(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_pil_and_opencv(pil_resized, resized)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def L1(pil: Image, image: np.ndarray) -> float:\n", " return np.mean(np.abs(np.asarray(pil) - image))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "TOL = 1e-4\n", "\n", "l1 = L1(pil_resized, resized)\n", "assert l1 - 88.9559 < TOL" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.seed(1)\n", "pil = pil_transforms.RandomRotation(10)(pil_image)\n", "random.seed(1)\n", "np_img = transforms.RandomRotation(10)(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_pil_and_opencv(pil, np_img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pil = pil_transforms.FiveCrop((224, 224))(pil_image)\n", "cv = transforms.FiveCrop((224,224))(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pil_stacked = np.hstack([np.asarray(i) for i in pil])\n", "cv_stacked = np.hstack(cv)\n", "\n", "plot_pil_and_opencv(pil_stacked, cv_stacked, orientation='col')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pil_stacked.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l1" ] } ], "metadata": { "kernelspec": { "display_name": "opencv_transforms", "language": "python", "name": "opencv_transforms" }, "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }