{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Data driven Security and Privacy ADS5035_01\nSimple solution, first time.\n\n\nInspired by https://www.kaggle.com/vincee/intel-image-classification-cnn-keras"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predefined variables and import statements"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport cv2\nimport matplotlib.pyplot as plt \nimport tensorflow as tf\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# To ensure we are talking about the same thing\nclass_names = ['real', 'fake']\nclass_names_label = {class_name:i for i, class_name in enumerate(class_names)}\n\nIMAGE_SIZE=(96,96) # <- smallest size picture found in train set","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"def loadData():\n    datasets = ['../input/ads5035-01/train', '../input/ads5035-01/validation']\n    output = []\n    for dataset in datasets:\n        \n        images = []\n        labels = []\n        \n        print(\"Loading {}\".format(dataset))\n        \n        for folder in os.listdir(dataset):\n            label = class_names_label[folder]\n            \n            for file in tqdm(os.listdir(os.path.join(dataset, folder))):\n                \n                image_path = os.path.join(os.path.join(dataset, folder), file)\n                \n                image = cv2.imread(image_path)\n\n                image = cv2.resize(image, IMAGE_SIZE, interpolation = cv2.INTER_AREA)\n\n                \n                images.append(image)\n                labels.append(label)\n                \n        image_array = np.array(images)\n        label_array = np.array(labels, dtype = 'int32')\n    \n        output.append((image_array, label_array))\n    \n    return output\n    \ndef loadTest():\n    testsets = ['../input/ads5035-01/test']\n    output = []\n    \n    for testset in testsets:\n    \n        images = []\n        \n        print(\"Loading {}\".format(testset))\n        \n        for file in tqdm(os.listdir(testset)):\n            \n            image_path = os.path.join(testset, file)\n            \n            image = cv2.imread(image_path)\n            image = cv2.resize(image, IMAGE_SIZE)\n            \n            images.append(image)\n        \n        image_array = np.array(images)\n        output.append(image_array)\n        \n    return output","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"(train_images, train_labels), (val_images, val_labels) = loadData()\ntrain_images, train_labels = shuffle(train_images, train_labels, random_state=25)\nval_images, val_labels = shuffle(val_images, val_labels, random_state=30)\ntest_images = loadTest()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualize the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_random_image(class_names, images, labels):\n    \"\"\"\n        Display a random image from the images array and its correspond label from the labels array.\n    \"\"\"\n    \n    index = np.random.randint(images.shape[0])\n    plt.figure()\n    plt.imshow(images[index])\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.title('Image #{} : '.format(index) + class_names[labels[index]])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display_random_image(class_names, train_images, train_labels)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model\n## Creation"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu', input_shape = (96, 96, 3)), \n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation=tf.nn.relu),\n    tf.keras.layers.Dense(6, activation=tf.nn.softmax)\n])\n\nmodel.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nhistory = model.fit(train_images, train_labels, batch_size=50, epochs=5, validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Check on the validation set"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_loss = model.evaluate(val_images, val_labels)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating the final submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(test_images)\npred_labels = np.argmax(pred, axis = 1)\npred_labels = [class_names[i].capitalize() for i in pred_labels]\n\noutput = pd.DataFrame({'id': range(1,len(pred)+1), 'category': pred_labels})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}