{"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_minor":4,"nbformat":4,"cells":[{"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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:36:49.558505Z","iopub.execute_input":"2022-05-25T04:36:49.558774Z","iopub.status.idle":"2022-05-25T04:36:49.562368Z","shell.execute_reply.started":"2022-05-25T04:36:49.558738Z","shell.execute_reply":"2022-05-25T04:36:49.561721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport os\nimport time\nimport glob\nfrom PIL import *\nimport skimage\nimport cv2\nimport matplotlib.pyplot as plt\n\n\n# from google.colab.patches import cv2_imshow\n# IMAGE_PATH = '../input/draper-satellite-image-chronology/train_sm/train_sm/set107_*.jpeg'\n\nWIDTH = 100\nHEIGHT = 100\nSEQUENCE = np.array([])\nBASIC_SEQUENCE = np.array([])\nNEXT_SEQUENCE = np.array([])\nNUMBER = 0\n\ndef image_initialize(image):\n    picture = Image.open(image)\n    picture = picture.resize((WIDTH, HEIGHT), Image.ANTIALIAS)\n    picture = picture.convert('L')\n    data = np.array(picture.getdata()).reshape(WIDTH, HEIGHT, 1)\n#     cv2_imshow(data)\n    plt.figure(figsize=(5,5))\n    plt.imshow(data)\n    return data\n\nfor file in glob.glob('../input/draper-satellite-image-chronology/train_sm/train_sm/set107_*.jpeg'):\n    image_initialize(file)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:36:53.096174Z","iopub.execute_input":"2022-05-25T04:36:53.096429Z","iopub.status.idle":"2022-05-25T04:36:55.350220Z","shell.execute_reply.started":"2022-05-25T04:36:53.096401Z","shell.execute_reply":"2022-05-25T04:36:55.349494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport os\nimport time\nimport glob\nfrom PIL import *\nimport skimage\n\n\nWIDTH = 100\nHEIGHT = 100\nSEQUENCE = np.array([])\nBASIC_SEQUENCE = np.array([])\nNEXT_SEQUENCE = np.array([])\nNUMBER = 0\n\ndef image_initialize(image):\n    picture = Image.open(image)\n    picture = picture.resize((WIDTH, HEIGHT), Image.ANTIALIAS)\n    picture = picture.convert('L')\n#     picture.save('./temp.png')  \n    data = np.array(picture.getdata()).reshape(WIDTH, HEIGHT, 1)\n    return data\n\nfor file in glob.glob(\"../input/draper-satellite-image-chronology/train_sm/train_sm/set107_*.jpeg\"):\n    image_array = (image_initialize(file))/255\n    SEQUENCE = np.append(SEQUENCE, image_array)\n    NUMBER += 1\n    print(SEQUENCE[0])\n    print(NUMBER)\n    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))\n\nSEQUENCE = SEQUENCE.reshape(NUMBER, WIDTH * HEIGHT)\n\nnp.savez('./sequence_array.npz', sequence_array=SEQUENCE)\nprint('Data saved.')\nprint(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:36:56.376017Z","iopub.execute_input":"2022-05-25T04:36:56.376543Z","iopub.status.idle":"2022-05-25T04:36:57.163950Z","shell.execute_reply.started":"2022-05-25T04:36:56.376507Z","shell.execute_reply":"2022-05-25T04:36:57.162590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ssim_loss(y_true, y_pred):\n  return tf.reduce_mean(tf.image.ssim(y_true, y_pred, 1.0))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:36:57.499835Z","iopub.execute_input":"2022-05-25T04:36:57.500287Z","iopub.status.idle":"2022-05-25T04:36:57.504605Z","shell.execute_reply.started":"2022-05-25T04:36:57.500252Z","shell.execute_reply":"2022-05-25T04:36:57.503620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def psnr(y_true,y_pred):\n    max_pixel = 1.0\n    return (10.0 * K.log((max_pixel ** 2) / (K.mean(K.square(y_pred - y_true), axis=-1)))) / 2.303","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:36:58.594391Z","iopub.execute_input":"2022-05-25T04:36:58.595249Z","iopub.status.idle":"2022-05-25T04:36:58.599881Z","shell.execute_reply.started":"2022-05-25T04:36:58.595210Z","shell.execute_reply":"2022-05-25T04:36:58.599130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom PIL import Image\nimport os\n# from keras.models import Sequential\n# from keras.layers.convolutional import Conv3D\n# from keras.layers.convolutional_recurrent import ConvLSTM2D\n# from keras.layers import BatchNormalization\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import Conv3D,ConvLSTM2D,BatchNormalization,MaxPooling2D, Dense, Flatten,GRU,Conv2D,Reshape,Permute,Lambda,Input,SimpleRNN\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras import backend as K\nimport pylab as pl\nimport time\n# from keras.utils import multi_gpu_model\nfrom keras import optimizers\n# from skimage.metrics import structural_similarity as ssim\n# import imutils\nimport cv2\n# from google.colab.patches import cv2_imshow\n\ndef custom_layer(tensor):\n    tensor=tensor[:,None]\n    return tensor\n\n\n#Loading array\nSEQUENCE = np.load('./sequence_array.npz')['sequence_array']  # load array\nn_samples = len(SEQUENCE)\n\nWIDTH = 100\nHEIGHT = 100\nn_frames = 1\n\n# step =1\nSEQUENCE = SEQUENCE.reshape(n_samples, WIDTH, HEIGHT, 1)\nBASIC_SEQUENCE = np.zeros((n_samples-n_frames, n_frames, WIDTH, HEIGHT, 1))\nNEXT_SEQUENCE = np.zeros((n_samples-n_frames, n_frames, WIDTH, HEIGHT, 1))\n\nfor i in range(n_frames):\n    BASIC_SEQUENCE[:, i, :, :, :] = SEQUENCE[i:i+n_samples-n_frames]\n    NEXT_SEQUENCE[:, i, :, :, :] = SEQUENCE[i+1:i+n_samples-n_frames+1]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:36:59.338413Z","iopub.execute_input":"2022-05-25T04:36:59.338962Z","iopub.status.idle":"2022-05-25T04:36:59.357269Z","shell.execute_reply.started":"2022-05-25T04:36:59.338925Z","shell.execute_reply":"2022-05-25T04:36:59.356445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(ConvLSTM2D(filters=256, kernel_size=(3, 3),input_shape=(None, WIDTH, HEIGHT, 1), padding=\"same\", return_sequences=True))\nmodel.add(BatchNormalization())\nmodel.add(ConvLSTM2D(filters=256, kernel_size=(3, 3), padding=\"same\", return_sequences=True))\nmodel.add(BatchNormalization())\nmodel.add(Conv3D(filters=1, kernel_size=(3, 3, 3), activation=\"relu\", padding='same', data_format=\"channels_last\"))\nmodel.compile(loss=ssim_loss, optimizer=\"adadelta\",metrics=[tf.keras.metrics.MeanSquaredError(),tf.keras.metrics.RootMeanSquaredError(),ssim_loss,psnr])","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:00.491418Z","iopub.execute_input":"2022-05-25T04:37:00.491968Z","iopub.status.idle":"2022-05-25T04:37:04.429605Z","shell.execute_reply.started":"2022-05-25T04:37:00.491928Z","shell.execute_reply":"2022-05-25T04:37:04.428871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary(line_length=100)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:04.431748Z","iopub.execute_input":"2022-05-25T04:37:04.431993Z","iopub.status.idle":"2022-05-25T04:37:04.439449Z","shell.execute_reply.started":"2022-05-25T04:37:04.431960Z","shell.execute_reply":"2022-05-25T04:37:04.438788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainingfraction = 1.0\ntrain_size = round(n_samples * trainingfraction)\n\n### Train the network\n# model.fit(BASIC_SEQUENCE[:train_size], NEXT_SEQUENCE[:train_size],  epochs=10, validation_split=0.05)\nt_start = time.time()\nH=model.fit(BASIC_SEQUENCE[:train_size], NEXT_SEQUENCE[:train_size], verbose=2, epochs=10, batch_size=2)\nt_end = time.time()\n\nprint('Training Elapsed time:')\nprint(t_end - t_start)\n\n### Take an example from the test set and predict the next steps\nindex = 1\n\nnum_test_frames = 0 ### Number of frames to predict\n\n\ntrain_pred = BASIC_SEQUENCE[index][:n_frames-num_test_frames:, ::, ::, ::]  ##(track)\nfor j in range(n_frames):\n    t_start = time.time()\n    new_pos = model.predict(train_pred[np.newaxis, :, :, :, :])\n    t_end = time.time()\n    print('Testing Elapsed time:')\n    print(t_end - t_start)\n    new = new_pos[:, -1, :, :, :]\n    train_pred = np.concatenate((train_pred, new), axis=0)\n\n### Compare predictions to the truth\ntruth = BASIC_SEQUENCE[index][:, :, :, :]\n\nfor i in range(n_frames):\n    fig = plt.figure(figsize=(10, 5))\n    ax = fig.add_subplot(121)\n    ### In left panel show original then predicted frames\n    # ax.text(1, 3, 'Predictions', fontsize=20, color='w')\n    plt.text(1, 3, 'Predictions', fontsize=20)\n    pred = train_pred[i, ::, ::, 0]\n    plt.imshow(pred, cmap='binary')\n    plt.savefig('%i_Prediction.png' % (i + 1))\n\nfor i in range(n_frames):   \n    fig = plt.figure(figsize=(10, 5))\n    ax = fig.add_subplot(122)\n    plt.text(1, 3, 'Ground truth', fontsize=20)\n    gt = truth[i, ::, ::, 0]\n    plt.imshow(gt, cmap='binary')\n    plt.savefig('%i_GroundTruth.png' % (i + 1))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:04.440756Z","iopub.execute_input":"2022-05-25T04:37:04.441171Z","iopub.status.idle":"2022-05-25T04:37:19.247024Z","shell.execute_reply.started":"2022-05-25T04:37:04.441135Z","shell.execute_reply":"2022-05-25T04:37:19.246355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\n\ndef MSE(img1, img2):\n        squared_diff = (img2-img1) ** 2\n        summed = np.sum(squared_diff)\n        num_pix = img1.shape[0] * img1.shape[1] #img1 and 2 should have same shape\n        err = summed / num_pix\n        return err\n\ndef RMSE(img1, img2):\n        squared_diff = (img2-img1) ** 2\n        summed = np.sum(squared_diff)\n        num_pix = img1.shape[0] * img1.shape[1] #img1 and 2 should have same shape\n        err = summed / num_pix\n        err=math.sqrt(err)\n        return err","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:19.249553Z","iopub.execute_input":"2022-05-25T04:37:19.249827Z","iopub.status.idle":"2022-05-25T04:37:19.256107Z","shell.execute_reply.started":"2022-05-25T04:37:19.249790Z","shell.execute_reply":"2022-05-25T04:37:19.255211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.metrics import structural_similarity as ssim\n#Load Data\nimageA = cv2.imread('./1_Prediction.png')\nimageB = cv2.imread('./1_GroundTruth.png')\n\n# compute the Structural Similarity Index (SSIM) between the two\n# images, ensuring that the difference image is returned\n(score, diff) = ssim(imageA, imageB, full=True, multichannel=True)\nprint(\"SSIM: {}\".format(score))\n\nmse_score=MSE(imageA,imageB)\n\nrmse_score=RMSE(imageA,imageB)\n\nprint(\"MSE score: {}\".format(mse_score))\nprint(\"RMSE score: {}\".format(rmse_score))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:19.257556Z","iopub.execute_input":"2022-05-25T04:37:19.257822Z","iopub.status.idle":"2022-05-25T04:37:19.784921Z","shell.execute_reply.started":"2022-05-25T04:37:19.257788Z","shell.execute_reply":"2022-05-25T04:37:19.784186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(H.history['ssim_loss'])\nplt.plot(H.history['mean_squared_error'])\nplt.plot(H.history['root_mean_squared_error'])\nplt.title('Metrics of the Model')\nplt.ylabel('Metrics')\nplt.xlabel('Epochs')\nplt.legend(['ssim_loss','mean_squared_error','root_mean_squared_error'])\n# plt.rcParams[\"figure.figsize\"] = (10,10)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:19.786387Z","iopub.execute_input":"2022-05-25T04:37:19.786880Z","iopub.status.idle":"2022-05-25T04:37:19.976235Z","shell.execute_reply.started":"2022-05-25T04:37:19.786841Z","shell.execute_reply":"2022-05-25T04:37:19.975510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(H.history['ssim_loss'])\nplt.ylabel('Metrics')\nplt.xlabel('Epochs')\nplt.title('SSIM LOSS')\nplt.legend(['ssim_loss'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:19.977371Z","iopub.execute_input":"2022-05-25T04:37:19.977598Z","iopub.status.idle":"2022-05-25T04:37:20.160148Z","shell.execute_reply.started":"2022-05-25T04:37:19.977565Z","shell.execute_reply":"2022-05-25T04:37:20.159513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(H.history['mean_squared_error'])\nplt.ylabel('Metrics')\nplt.xlabel('Epochs')\nplt.title('Mean Squared Error')\nplt.legend(['mean_squared_error'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:20.161488Z","iopub.execute_input":"2022-05-25T04:37:20.161764Z","iopub.status.idle":"2022-05-25T04:37:20.341139Z","shell.execute_reply.started":"2022-05-25T04:37:20.161728Z","shell.execute_reply":"2022-05-25T04:37:20.340492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(H.history['root_mean_squared_error'])\nplt.ylabel('Metrics')\nplt.xlabel('Epochs')\nplt.title('Root Mean Squared Error')\nplt.legend(['root_mean_squared_error'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:37:20.342480Z","iopub.execute_input":"2022-05-25T04:37:20.342920Z","iopub.status.idle":"2022-05-25T04:37:20.525059Z","shell.execute_reply.started":"2022-05-25T04:37:20.342886Z","shell.execute_reply":"2022-05-25T04:37:20.524410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}