diff --git a/DataScience/final_model.ipynb b/DataScience/final_model.ipynb
new file mode 100644
index 0000000..e9102b1
--- /dev/null
+++ b/DataScience/final_model.ipynb
@@ -0,0 +1,1066 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "Copy of vgg19.ipynb",
+ "provenance": [],
+ "collapsed_sections": [],
+ "toc_visible": true,
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "_HbB6dzHJLcx",
+ "colab_type": "code",
+ "outputId": "e0fc9c48-cc82-4f5d-b2b0-2f78480971b4",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "from google.colab import drive\n",
+ "drive.mount('/gdrive')"
+ ],
+ "execution_count": 110,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Mounted at /gdrive\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "NQa52OEFJMjf",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "\n",
+ "%matplotlib inline\n",
+ "import tensorflow.keras\n",
+ "import matplotlib.pyplot as plt\n",
+ "import PIL\n",
+ "import tensorflow as tf\n",
+ "import numpy as np\n",
+ "import os"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "9G5MHX5xBCXL",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from tensorflow.keras.models import Model, Sequential\n",
+ "from tensorflow.keras.layers import Dense, Flatten, Dropout\n",
+ "from tensorflow.keras.applications import VGG16\n",
+ "from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions\n",
+ "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
+ "from tensorflow.keras.optimizers import Adam, RMSprop"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "gHX-JSDFJObT",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from __future__ import absolute_import, division, print_function, unicode_literals\n",
+ "#!pip install tensorflow-gpu==1.14.0"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Y6StOM3IJTpQ",
+ "colab_type": "code",
+ "outputId": "386554da-a5b9-42dd-9ae8-d2a260556d7f",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "#import tensorflow as tf\n",
+ "\n",
+ "from tensorflow.keras.models import Model, Sequential\n",
+ "from tensorflow.keras.layers import Dense, Flatten, Dropout\n",
+ "from tensorflow.keras.applications import VGG16\n",
+ "from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions\n",
+ "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
+ "from tensorflow.keras.optimizers import Adam, RMSprop\n",
+ "from tensorflow.keras import layers\n",
+ "tf.__version__"
+ ],
+ "execution_count": 73,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "'1.15.0-rc3'"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 73
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "65O8jYj_JXDT",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "def path_join(dirname, filenames):\n",
+ " return [os.path.join(dirname, filename) for filename in filenames]"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "074RtdVSJY3z",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "def load_images(image_paths):\n",
+ " # Load the images from disk.\n",
+ " images = [plt.imread(path) for path in image_paths]\n",
+ "\n",
+ " # Convert to a numpy array and return it.\n",
+ " return np.asarray(images)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "3cLJN89CJcwU",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "train_dir = '../gdrive/My Drive/NASA/train'\n",
+ "test_dir = '../gdrive/My Drive/NASA/test'"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "l6wkPZiJJfss",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from tensorflow.keras.applications.vgg19 import VGG19\n",
+ "from tensorflow.keras.applications.resnet50 import ResNet50\n",
+ "from tensorflow.keras.applications.vgg19 import preprocess_input\n",
+ "\n",
+ "from tensorflow.keras.preprocessing import image\n",
+ "from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions\n",
+ "import numpy as np"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "7fMgfqGdJiK3",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "model = VGG19(include_top=True, weights='imagenet')"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "dAcPk0D8aAH6",
+ "colab_type": "code",
+ "outputId": "2887579f-535a-49a5-d836-f0472d781342",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "input_shape = model.layers[0].output_shape[0][1:3]\n",
+ "input_shape"
+ ],
+ "execution_count": 79,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "(224, 224)"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 79
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "dn_VdJFuAfUF",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "###try one pic\n",
+ "def predict(image_path):\n",
+ " \n",
+ " # convert image to .jpg if it is .png\n",
+ " if image_path.lower().endswith('.png'):\n",
+ " img = PIL.Image.open(image_path)\n",
+ " rgb_im = img.convert('RGB')\n",
+ " image_path=image_path.replace(\"png\", \"jpg\").replace(\"PNG\",\"jpg\")\n",
+ " rgb_im.save(image_path, quality=95)\n",
+ "\n",
+ " # Load and resize the image using PIL.\n",
+ " img = PIL.Image.open(image_path)\n",
+ " print('input_shape')\n",
+ " img_resized = img.resize(input_shape, PIL.Image.LANCZOS)\n",
+ "\n",
+ " # Plot the image.\n",
+ " #plt.imshow(img_resized)\n",
+ " #plt.show()\n",
+ "\n",
+ " # Convert the PIL image to a numpy-array with the proper shape.\n",
+ " img_array = np.expand_dims(np.array(img_resized), axis=0)\n",
+ "\n",
+ " # Use the VGG16 model to make a prediction.\n",
+ " # This outputs an array with 1000 numbers corresponding to\n",
+ " # the classes of the ImageNet-dataset.\n",
+ " pred = model.predict(img_array)\n",
+ " \n",
+ " # Decode the output of the VGG16 model.\n",
+ " pred_decoded = decode_predictions(pred)[0]\n",
+ "\n",
+ " # Print the predictions.\n",
+ " for code, name, score in pred_decoded:\n",
+ " print(\"{0:>6.2%} : {1}\".format(score, name))"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "MkUDIprBB_cJ",
+ "colab_type": "code",
+ "outputId": "0c7fe4dc-797b-4c5a-aa3e-0b694ec84264",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 126
+ }
+ },
+ "source": [
+ "predict(image_path= '../gdrive/My Drive/NASA/test/after/after_23.jpg')"
+ ],
+ "execution_count": 112,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "input_shape\n",
+ "46.63% : throne\n",
+ " 7.95% : breastplate\n",
+ " 6.50% : cuirass\n",
+ " 1.94% : vault\n",
+ " 1.62% : palace\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "PGujl-gkCLgv",
+ "colab_type": "code",
+ "outputId": "239af2ac-6022-4b82-a7f0-0cc2d46998af",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 1000
+ }
+ },
+ "source": [
+ "model.summary()"
+ ],
+ "execution_count": 82,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Model: \"vgg19\"\n",
+ "_________________________________________________________________\n",
+ "Layer (type) Output Shape Param # \n",
+ "=================================================================\n",
+ "input_3 (InputLayer) [(None, 224, 224, 3)] 0 \n",
+ "_________________________________________________________________\n",
+ "block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 \n",
+ "_________________________________________________________________\n",
+ "block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 \n",
+ "_________________________________________________________________\n",
+ "block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 \n",
+ "_________________________________________________________________\n",
+ "block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 \n",
+ "_________________________________________________________________\n",
+ "block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 \n",
+ "_________________________________________________________________\n",
+ "block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 \n",
+ "_________________________________________________________________\n",
+ "block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 \n",
+ "_________________________________________________________________\n",
+ "block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 \n",
+ "_________________________________________________________________\n",
+ "block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 \n",
+ "_________________________________________________________________\n",
+ "block3_conv4 (Conv2D) (None, 56, 56, 256) 590080 \n",
+ "_________________________________________________________________\n",
+ "block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 \n",
+ "_________________________________________________________________\n",
+ "block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 \n",
+ "_________________________________________________________________\n",
+ "block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block4_conv4 (Conv2D) (None, 28, 28, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 \n",
+ "_________________________________________________________________\n",
+ "block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_conv4 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 \n",
+ "_________________________________________________________________\n",
+ "flatten (Flatten) (None, 25088) 0 \n",
+ "_________________________________________________________________\n",
+ "fc1 (Dense) (None, 4096) 102764544 \n",
+ "_________________________________________________________________\n",
+ "fc2 (Dense) (None, 4096) 16781312 \n",
+ "_________________________________________________________________\n",
+ "predictions (Dense) (None, 1000) 4097000 \n",
+ "=================================================================\n",
+ "Total params: 143,667,240\n",
+ "Trainable params: 143,667,240\n",
+ "Non-trainable params: 0\n",
+ "_________________________________________________________________\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Tc8T4cLgKFHl",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "datagen_train = ImageDataGenerator(\n",
+ " rescale=1./255,\n",
+ " rotation_range=180,\n",
+ " width_shift_range=0.1,\n",
+ " height_shift_range=0.1,\n",
+ " shear_range=0.1,\n",
+ " zoom_range=[0.9, 1.5],\n",
+ " horizontal_flip=True,\n",
+ " vertical_flip=True,\n",
+ " fill_mode='nearest')"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "zXrC28R1K7wC",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "datagen_test = ImageDataGenerator(rescale=1./255) "
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "WXNmRdbDK9RZ",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "batch_size = 10 "
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "X9V5lIazK-yf",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ " if True:\n",
+ " save_to_dir = None\n",
+ "else:\n",
+ " save_to_dir='augmented_images/'"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "fmbcsDFSLBJo",
+ "colab_type": "code",
+ "outputId": "162ebe89-0c7b-410c-aa72-69dccbf09e44",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "generator_train = datagen_train.flow_from_directory(directory=train_dir,\n",
+ " target_size=input_shape,\n",
+ " batch_size=batch_size,\n",
+ " shuffle=True,\n",
+ " save_to_dir=save_to_dir)"
+ ],
+ "execution_count": 117,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Found 77 images belonging to 2 classes.\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "FHxPc6ZfLk5I",
+ "colab_type": "code",
+ "outputId": "7ba59eca-5d13-4d29-a87c-37fde0b2e2f3",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "generator_test = datagen_test.flow_from_directory(directory=test_dir,\n",
+ " target_size=input_shape,\n",
+ " batch_size=batch_size,\n",
+ " shuffle=False)"
+ ],
+ "execution_count": 118,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Found 25 images belonging to 2 classes.\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "iiuwf1b8LCtQ",
+ "colab_type": "code",
+ "outputId": "833998cb-693f-4ead-d859-3806c2f50b38",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "steps_test = generator_test.n / batch_size\n",
+ "steps_test"
+ ],
+ "execution_count": 119,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "2.5"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 119
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "2n4GHiryLibd",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "image_paths_train = path_join(train_dir, generator_train.filenames)\n",
+ "image_paths_test = path_join(test_dir, generator_test.filenames)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "R_lL-p0ELpUu",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "cls_train = generator_train.classes\n",
+ "cls_test = generator_test.classes"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Q1iBUIH_Lq-x",
+ "colab_type": "code",
+ "outputId": "79770dd9-fd31-433a-8c17-a755bc471703",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "class_names = list(generator_train.class_indices.keys())\n",
+ "class_names"
+ ],
+ "execution_count": 122,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "['after', 'before']"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 122
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Xs4Btw7_LsWo",
+ "colab_type": "code",
+ "outputId": "9219135b-417b-4d4d-b940-50da8ca98715",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "num_classes = generator_train.num_classes\n",
+ "num_classes"
+ ],
+ "execution_count": 123,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "2"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 123
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "c8fHG74cLuPJ",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from sklearn.utils.class_weight import compute_class_weight"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "EGBbkeObLxYX",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ " class_weight = compute_class_weight(class_weight='balanced',\n",
+ " classes=np.unique(cls_train),\n",
+ " y=cls_train)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "-_j8zDp5Lyjg",
+ "colab_type": "code",
+ "outputId": "a182ff72-3a55-4bd1-911b-877831a54d9a",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "class_weight "
+ ],
+ "execution_count": 126,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "array([0.9625 , 1.04054054])"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 126
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "clUZsgvbL4AF",
+ "colab_type": "code",
+ "outputId": "76338dfe-1b2b-41d4-98b2-005a05d74562",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 1000
+ }
+ },
+ "source": [
+ "model.summary() "
+ ],
+ "execution_count": 127,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Model: \"vgg19\"\n",
+ "_________________________________________________________________\n",
+ "Layer (type) Output Shape Param # \n",
+ "=================================================================\n",
+ "input_3 (InputLayer) [(None, 224, 224, 3)] 0 \n",
+ "_________________________________________________________________\n",
+ "block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 \n",
+ "_________________________________________________________________\n",
+ "block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 \n",
+ "_________________________________________________________________\n",
+ "block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 \n",
+ "_________________________________________________________________\n",
+ "block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 \n",
+ "_________________________________________________________________\n",
+ "block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 \n",
+ "_________________________________________________________________\n",
+ "block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 \n",
+ "_________________________________________________________________\n",
+ "block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 \n",
+ "_________________________________________________________________\n",
+ "block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 \n",
+ "_________________________________________________________________\n",
+ "block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 \n",
+ "_________________________________________________________________\n",
+ "block3_conv4 (Conv2D) (None, 56, 56, 256) 590080 \n",
+ "_________________________________________________________________\n",
+ "block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 \n",
+ "_________________________________________________________________\n",
+ "block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 \n",
+ "_________________________________________________________________\n",
+ "block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block4_conv4 (Conv2D) (None, 28, 28, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 \n",
+ "_________________________________________________________________\n",
+ "block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_conv4 (Conv2D) (None, 14, 14, 512) 2359808 \n",
+ "_________________________________________________________________\n",
+ "block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 \n",
+ "_________________________________________________________________\n",
+ "flatten (Flatten) (None, 25088) 0 \n",
+ "_________________________________________________________________\n",
+ "fc1 (Dense) (None, 4096) 102764544 \n",
+ "_________________________________________________________________\n",
+ "fc2 (Dense) (None, 4096) 16781312 \n",
+ "_________________________________________________________________\n",
+ "predictions (Dense) (None, 1000) 4097000 \n",
+ "=================================================================\n",
+ "Total params: 143,667,240\n",
+ "Trainable params: 143,667,240\n",
+ "Non-trainable params: 0\n",
+ "_________________________________________________________________\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "twgrpq5LL826",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "transfer_layer = model.get_layer('fc2') "
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "FlczWAnrgrK1",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from tensorflow.keras import layers"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "u2-mbRUlO6zX",
+ "colab_type": "code",
+ "outputId": "7282dd8e-ca7b-402a-f185-0dcf91a3554c",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ }
+ },
+ "source": [
+ "conv_model = Model(inputs=model.input, outputs=transfer_layer.output)\n",
+ "model.input"
+ ],
+ "execution_count": 130,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 130
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "rHPzx7m3hywg",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "new_model = Sequential()\n",
+ "new_model.add(conv_model)\n",
+ "\n",
+ "# Add the final layer for the actual classification.\n",
+ "new_model.add(Dense(num_classes, activation='softmax'))"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "fJosDg-DlWYD",
+ "colab_type": "code",
+ "outputId": "a91f2595-3334-42e4-dea1-3e184357cd28",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 235
+ }
+ },
+ "source": [
+ "new_model.summary()"
+ ],
+ "execution_count": 132,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Model: \"sequential_3\"\n",
+ "_________________________________________________________________\n",
+ "Layer (type) Output Shape Param # \n",
+ "=================================================================\n",
+ "model_5 (Model) (None, 4096) 139570240 \n",
+ "_________________________________________________________________\n",
+ "dense_3 (Dense) (None, 2) 8194 \n",
+ "=================================================================\n",
+ "Total params: 139,578,434\n",
+ "Trainable params: 139,578,434\n",
+ "Non-trainable params: 0\n",
+ "_________________________________________________________________\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "6aL9iTi6V3jO",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "optimizer = Adam(lr=1e-5)\n",
+ "metrics = ['categorical_accuracy']\n",
+ "loss = 'categorical_crossentropy'\n",
+ "new_model.compile(optimizer = optimizer,loss=loss, metrics=metrics)\n",
+ "epochs = 10\n",
+ "steps_per_epoch = 100\n"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "bO1Im-0ixIDr",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from tensorflow.keras.callbacks import TensorBoard, EarlyStopping"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "-e_3fmHqg6Yd",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 563
+ },
+ "outputId": "da054d4d-8e0d-4061-e04e-463753cd8fb0"
+ },
+ "source": [
+ "history = new_model.fit_generator(generator=generator_train,\n",
+ " epochs=epochs,\n",
+ " steps_per_epoch=steps_per_epoch,\n",
+ " class_weight=class_weight,\n",
+ " validation_data=generator_test,\n",
+ " validation_steps=steps_test)"
+ ],
+ "execution_count": 136,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Epoch 1/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.4232 - categorical_accuracy: 0.7834Epoch 1/10\n",
+ "100/100 [==============================] - 2196s 22s/step - loss: 0.4212 - categorical_accuracy: 0.7846 - val_loss: 1.0793 - val_categorical_accuracy: 0.6800\n",
+ "Epoch 2/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.2430 - categorical_accuracy: 0.8851Epoch 1/10\n",
+ "100/100 [==============================] - 2192s 22s/step - loss: 0.2419 - categorical_accuracy: 0.8859 - val_loss: 1.1584 - val_categorical_accuracy: 0.5600\n",
+ "Epoch 3/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.1296 - categorical_accuracy: 0.9476Epoch 1/10\n",
+ "100/100 [==============================] - 2177s 22s/step - loss: 0.1285 - categorical_accuracy: 0.9481 - val_loss: 0.9108 - val_categorical_accuracy: 0.6800\n",
+ "Epoch 4/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0360 - categorical_accuracy: 0.9874Epoch 1/10\n",
+ "100/100 [==============================] - 2169s 22s/step - loss: 0.0357 - categorical_accuracy: 0.9875 - val_loss: 1.1099 - val_categorical_accuracy: 0.7200\n",
+ "Epoch 5/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0323 - categorical_accuracy: 0.9916Epoch 1/10\n",
+ "100/100 [==============================] - 2174s 22s/step - loss: 0.0322 - categorical_accuracy: 0.9917 - val_loss: 1.1059 - val_categorical_accuracy: 0.6000\n",
+ "Epoch 6/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0049 - categorical_accuracy: 1.0000Epoch 1/10\n",
+ "100/100 [==============================] - 2177s 22s/step - loss: 0.0048 - categorical_accuracy: 1.0000 - val_loss: 1.6649 - val_categorical_accuracy: 0.6400\n",
+ "Epoch 7/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0011 - categorical_accuracy: 1.0000Epoch 1/10\n",
+ "100/100 [==============================] - 2177s 22s/step - loss: 0.0011 - categorical_accuracy: 1.0000 - val_loss: 1.7248 - val_categorical_accuracy: 0.6400\n",
+ "Epoch 8/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0174 - categorical_accuracy: 0.9916Epoch 1/10\n",
+ "100/100 [==============================] - 2187s 22s/step - loss: 0.0219 - categorical_accuracy: 0.9896 - val_loss: 1.9386 - val_categorical_accuracy: 0.6400\n",
+ "Epoch 9/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0789 - categorical_accuracy: 0.9696Epoch 1/10\n",
+ "100/100 [==============================] - 2186s 22s/step - loss: 0.0782 - categorical_accuracy: 0.9699 - val_loss: 1.8128 - val_categorical_accuracy: 0.6000\n",
+ "Epoch 10/10\n",
+ " 99/100 [============================>.] - ETA: 21s - loss: 0.0018 - categorical_accuracy: 1.0000Epoch 1/10\n",
+ "100/100 [==============================] - 2187s 22s/step - loss: 0.0018 - categorical_accuracy: 1.0000 - val_loss: 1.6079 - val_categorical_accuracy: 0.6000\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "5PfyXxlm9KSw",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 54
+ },
+ "outputId": "b85b1e7e-4821-4e54-ac1e-1817b26553aa"
+ },
+ "source": [
+ "from keras.models import load_model\n",
+ "\n",
+ "model_json = model.to_json()\n",
+ "with open(\"/../gdrive/My Drive/NASA/nasa_challenge_full.json\", \"w\") as json_file:\n",
+ " json_file.write(model_json)\n",
+ "# serialize weights to HDF5\n",
+ "model.save_weights(\"/gdrive/My Drive/NASA/nasa_challenge_full.h5\")\n",
+ "print(\"Saved model to disk\")\n",
+ " "
+ ],
+ "execution_count": 137,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Using TensorFlow backend.\n"
+ ],
+ "name": "stderr"
+ },
+ {
+ "output_type": "stream",
+ "text": [
+ "Saved model to disk\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "jk3mFA-ldsoy",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "model = load_model('/gdrive/My Drive/NASA_challenge_full.h5')"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "zUI151RW7ObG",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "'''from keras.callbacks import EarlyStopping\n",
+ "early_stopping = EarlyStopping(monitor='val_loss', patience=2)\n",
+ "model.fit(x, y, validation_split=0.2, callbacks=[early_stopping])'''"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file
diff --git a/DataScience/prediction.ipynb b/DataScience/prediction.ipynb
new file mode 100644
index 0000000..9b7a9e3
--- /dev/null
+++ b/DataScience/prediction.ipynb
@@ -0,0 +1,188 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "prediction.ipynb",
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "57UqqJSWcOK6",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "from sklearn.metrics.pairwise import cosine_similarity\n",
+ "import numpy as np\n",
+ "import tensorflow.keras\n",
+ "import PIL\n",
+ "import tensorflow as tf\n",
+ "import os\n",
+ "\n",
+ "from tensorflow.keras import layers\n",
+ "from tensorflow.keras.models import Model, Sequential\n",
+ "from tensorflow.keras.layers import Dense, Flatten, Dropout\n",
+ "from tensorflow.keras.applications.vgg19 import VGG19\n",
+ "\n",
+ "#import model\n",
+ "model = VGG19(include_top=True, weights='imagenet')"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "MMR9VMhefyyZ",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 122
+ },
+ "outputId": "a4adae79-2cfa-4bb0-be18-7627cf1bc942"
+ },
+ "source": [
+ "from google.colab import drive\n",
+ "drive.mount('/gdrive')"
+ ],
+ "execution_count": 5,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code\n",
+ "\n",
+ "Enter your authorization code:\n",
+ "··········\n",
+ "Mounted at /gdrive\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "voj9IcJoczx6",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "# define images by providing path\n",
+ "image1_b = PIL.Image.open('../gdrive/My Drive/NASA/test/before/before_11.JPG')\n",
+ "image1_a = PIL.Image.open('../gdrive/My Drive/NASA/test/after/after_11.JPG')\n"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "yYnswJ58c9p8",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "input_shape = model.layers[0].output_shape[0][1:3]\n",
+ "\n",
+ "# set specific layer to get the vectors\n",
+ "layer_name = 'fc2'\n",
+ "intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "gV7dy2PPfJdS",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "# get cosine similarity\n",
+ "def flood_index(before, after):\n",
+ " #take two vectors from the last layer of neural network compute a transformation of cosine similarity \n",
+ " # and output an index of damages\n",
+ " \n",
+ " # resizing images\n",
+ " image1_b_resized = image1_b.resize(input_shape, PIL.Image.LANCZOS)\n",
+ " image1_a_resized = image1_a.resize(input_shape, PIL.Image.LANCZOS)\n",
+ " # transform images to numpy arrays\n",
+ " image1_b_array = np.expand_dims(np.array(image1_b_resized), axis=0)\n",
+ " image1_a_array = np.expand_dims(np.array(image1_a_resized), axis=0)\n",
+ " # prediction from model\n",
+ " intermediate_output_a = intermediate_layer_model.predict(image1_a_array)\n",
+ " intermediate_output_b = intermediate_layer_model.predict(image1_b_array)\n",
+ " \n",
+ " #compute similarity\n",
+ " cos_sim=np.sqrt((1-cosine_similarity(intermediate_output_a, intermediate_output_b))/2)*10\n",
+ " return cos_sim[0][0]"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "LaNGZ96wfeBV",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 34
+ },
+ "outputId": "627fe633-6dc6-4883-900f-8d422c3b04d5"
+ },
+ "source": [
+ "flood_index(image1_b, image1_a)"
+ ],
+ "execution_count": 9,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "3.7292957"
+ ]
+ },
+ "metadata": {
+ "tags": []
+ },
+ "execution_count": 9
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "vByAR1AVf9MQ",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ ""
+ ],
+ "execution_count": 0,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file
diff --git a/DataScience/trained_model b/DataScience/trained_model
new file mode 100644
index 0000000..460debb
--- /dev/null
+++ b/DataScience/trained_model
@@ -0,0 +1,6 @@
+Model weights:
+https://drive.google.com/open?id=1-0GLnddtFgJzH7M8ETftkfNQlFfNp0t6
+
+
+Model architecture:
+https://drive.google.com/open?id=1hZgRPFG8Um08i0e6LHYaf8ibpzHIVOAk