{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "0543601c-6414-2500-f19d-379971b6a4d7"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9bd8abbb-50bd-8014-4d28-cfac2c3d5d14"
      },
      "outputs": [],
      "source": [
        "def denormalize_input(x):\n",
        "    \"\"\"\n",
        "    Converts image pixels from -1:1 range to 0:255 range.\n",
        "    \"\"\"\n",
        "    x /= 2.\n",
        "    x += 0.5\n",
        "    x *= 255.\n",
        "    return x\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3f8fa260-247d-575d-3322-de14a095ff3d"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "2ca9e626-fa81-036f-8b40-bd87ebf5ebd8"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "637d55d0-7d33-4a3b-02ba-2e2c9eabf2f1"
      },
      "outputs": [],
      "source": [
        "def load_image_filenames(folders):\n",
        "    image_files = []\n",
        "    \n",
        "    for folder_tuple in folders:\n",
        "        folder = folder_tuple[0]\n",
        "        label_index = folder_tuple[1] - 1\n",
        "\n",
        "        image_filepaths = [os.path.join(folder, image_filename) for image_filename in os.listdir(folder)]\n",
        "        image_files.extend([(image_filepath, label_index) for image_filepath in image_filepaths])\n",
        "        \n",
        "    return image_files\n",
        "\n",
        "def load_image(filename, target_size):\n",
        "    try:\n",
        "        img = image.load_img(filename, target_size=target_size)\n",
        "    except IOError as e:\n",
        "        print('Could not read:', filename, ':', e, ', skipping.')\n",
        "        return None\n",
        "\n",
        "    x = image.img_to_array(img)\n",
        "    x = np.expand_dims(x, axis=0)\n",
        "    x = preprocess_input(x)\n",
        "    return x\n",
        "    \n",
        "def load_images(image_files, target_size):\n",
        "    x_list = []\n",
        "    y_list = []\n",
        "    \n",
        "    for image_file in image_files:\n",
        "        image_filepath = image_file[0]\n",
        "        label_index = image_file[1]\n",
        "\n",
        "        x = load_image(image_filepath, target_size)\n",
        "        if x == None:\n",
        "            continue\n",
        "            \n",
        "        x_list.append(x)\n",
        "\n",
        "        y = np.zeros((1, 3))\n",
        "        y[0, label_index] = 1\n",
        "        y_list.append(y)\n",
        "        \n",
        "    X = np.vstack(x_list)\n",
        "    y = np.vstack(y_list)\n",
        "    \n",
        "    return X, y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "1c153414-7a2e-0adb-e5af-6ef6b5db06fd"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f6f59991-5369-2f7c-37d0-9929f9c980ab"
      },
      "outputs": [],
      "source": [
        "def denormalize_input(x):\n",
        "    \"\"\"\n",
        "    Converts image pixels from -1:1 range to 0:255 range.\n",
        "    \"\"\"\n",
        "    x /= 2.\n",
        "    x += 0.5\n",
        "    x *= 255.\n",
        "    return x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3fc1a86c-1e2a-0ddf-8fe0-15cac5d4625b"
      },
      "outputs": [],
      "source": [
        "def show_array(a, fmt='png'):\n",
        "    \"\"\"\n",
        "    Displays an image inside of Jupyter notebook.\n",
        "    \"\"\"\n",
        "    a = np.uint8(a)\n",
        "    f = StringIO()\n",
        "    PIL.Image.fromarray(a).save(f, fmt)\n",
        "    IPython.display.display(IPython.display.Image(data=f.getvalue()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "7d5b0134-ee43-ffff-775d-0fbe98567a4d"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fd253efd-70c2-0fa7-cf23-9a27454f1ba0"
      },
      "outputs": [],
      "source": [
        "def show_report(model, X, y):\n",
        "    \"\"\"\n",
        "    Displays a confusion matrix and a classification report.\n",
        "    \"\"\"\n",
        "    y_predicted = np.argmax(model.predict(X), axis=1)\n",
        "    y_true = np.argmax(y, axis=1)\n",
        "\n",
        "    print(\"Confusion matrix (rows: true, columns: predicted)\")\n",
        "    print(confusion_matrix(y_true, y_predicted))\n",
        "    print(\"\")\n",
        "\n",
        "    print(\"Classification report\")\n",
        "    print(classification_report(y_true, y_predicted))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "9ee91ed6-ccbc-a81f-227a-dd19f66c8326"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6c3b0f8e-95b8-74cd-646a-4d45e0756205"
      },
      "outputs": [],
      "source": [
        "train_folders = [('train/Type_1/', 1), ('train/Type_2/', 2), ('train/Type_3/', 3), ]\n",
        "               # ('additional/Type_1/', 1), ('additional/Type_2/', 2), ('additional/Type_3/', 3)]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "62cdaefd-307c-a22c-3efb-839d113adc5c"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ec91ad57-42ae-4e85-8297-c04ca024a680"
      },
      "outputs": [],
      "source": [
        "target_size=(224, 224)\n",
        "train_fraction = 0.8"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4353a7a0-b631-91f8-7f9d-72fe3d081e8c"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "4115c3c4-c62a-56e2-98e6-7aa71f2ec3b3"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7b58f642-9b9a-d126-43d1-573bec376ebe"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "c191f9f9-0b39-a50d-d7ac-70e9da16e782"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7c122cc8-99e5-42de-165e-66aeb3c8bd1d",
        "collapsed": true
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3bf959d0-c060-59ef-f2c2-77508d85c17f"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3b3d8580-0a7b-36e7-2489-8183fb2b1897"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e9d02d01-1775-577d-d57c-a38992df882d"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f7e62cf3-b940-1a4c-fba8-4961783f6b26"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "541612fd-b849-89a9-8da7-a3528d9c7c19"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de7870ea-5e04-c356-8188-4b6fc458fc51"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "dc7fff2b-4d2c-994b-b177-e5bf7bf34a69"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a889e4f0-12a9-6e16-7221-0f1343f96c3f"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5bbbff19-b533-5ef8-136a-05ac34be0bf5",
        "collapsed": true
      },
      "outputs": [],
      "source": ""
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}