{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"63dfb11a-c0c3-4e1e-163b-8a1471cb73b5"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c4ac4d94-58f0-6567-1b7e-b96f34d8ce4b"},"outputs":[],"source":"from __future__ import print_function\n\nimport numpy as np\nimport tensorflow as tf\n\nfrom keras.applications.inception_v3 import InceptionV3, preprocess_input\nfrom keras.preprocessing import image\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.layers.core import Dropout\nfrom keras import backend as K\nfrom keras.optimizers import SGD\n\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport pandas as pd\n\nfrom six.moves import cPickle as pickle\n#from six.moves import range\n#from scipy import ndimage\nimport os\n\nimport PIL.Image\nfrom cStringIO import StringIO\nimport IPython.display"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ec5882ce-a47e-8d59-8da8-6c8a2247fd15"},"outputs":[],"source":"from io import StringIO\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"05c361e3-7185-aa18-75a5-b7890e49ef65"},"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\ndef 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    \ndef 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":"code","execution_count":null,"metadata":{"_cell_guid":"83e3b7a9-2acc-57a0-3b44-fc6b7f40d17d"},"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":"cd257840-2b30-7840-9379-9b367c24d909"},"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":"code","execution_count":null,"metadata":{"_cell_guid":"71bbc26b-8404-3b5e-722d-4ffaa7e5092b"},"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":"code","execution_count":null,"metadata":{"_cell_guid":"12ab2c73-e3a1-2aa2-52b1-8ed1dc3c8d30"},"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)]"}],"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}