{"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":"c7eca376-94a4-40c1-b468-2a802a00a549","_cell_guid":"c0b2394f-b035-4f56-bb84-1a0cd76aee20","collapsed":false,"jupyter":{"outputs_hidden":false},"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:32:03.480302Z","iopub.execute_input":"2022-05-25T04:32:03.480741Z","iopub.status.idle":"2022-05-25T04:32:10.422612Z","shell.execute_reply.started":"2022-05-25T04:32:03.480631Z","shell.execute_reply":"2022-05-25T04:32:10.421604Z"},"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:32:27.651406Z","iopub.execute_input":"2022-05-25T04:32:27.651891Z","iopub.status.idle":"2022-05-25T04:32:30.882545Z","shell.execute_reply.started":"2022-05-25T04:32:27.651848Z","shell.execute_reply":"2022-05-25T04:32:30.880864Z"},"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:32:33.706722Z","iopub.execute_input":"2022-05-25T04:32:33.707029Z","iopub.status.idle":"2022-05-25T04:32:34.607811Z","shell.execute_reply.started":"2022-05-25T04:32:33.706997Z","shell.execute_reply":"2022-05-25T04:32:34.605648Z"},"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:32:35.209236Z","iopub.execute_input":"2022-05-25T04:32:35.210062Z","iopub.status.idle":"2022-05-25T04:32:35.215639Z","shell.execute_reply.started":"2022-05-25T04:32:35.210019Z","shell.execute_reply":"2022-05-25T04:32:35.214223Z"},"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:32:36.301337Z","iopub.execute_input":"2022-05-25T04:32:36.302075Z","iopub.status.idle":"2022-05-25T04:32:36.309890Z","shell.execute_reply.started":"2022-05-25T04:32:36.302013Z","shell.execute_reply":"2022-05-25T04:32:36.307900Z"},"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:32:37.068595Z","iopub.execute_input":"2022-05-25T04:32:37.068892Z","iopub.status.idle":"2022-05-25T04:32:37.090962Z","shell.execute_reply.started":"2022-05-25T04:32:37.068859Z","shell.execute_reply":"2022-05-25T04:32:37.089963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=256, kernel_size=(3, 3),input_shape=(None, WIDTH, HEIGHT, 1), padding=\"same\"))\nmodel.add(Reshape((10000,256)))\nmodel.add(GRU(256,return_sequences=True))\nmodel.add(BatchNormalization())\nmodel.add(Reshape((100,100,256)))\nmodel.add(Lambda(custom_layer))\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:32:37.965085Z","iopub.execute_input":"2022-05-25T04:32:37.965725Z","iopub.status.idle":"2022-05-25T04:32:41.846766Z","shell.execute_reply.started":"2022-05-25T04:32:37.965672Z","shell.execute_reply":"2022-05-25T04:32:41.845394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary(line_length=100)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T04:32:48.301041Z","iopub.execute_input":"2022-05-25T04:32:48.301332Z","iopub.status.idle":"2022-05-25T04:32:48.317212Z","shell.execute_reply.started":"2022-05-25T04:32:48.301301Z","shell.execute_reply":"2022-05-25T04:32:48.316088Z"},"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 = 3\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:32:49.705790Z","iopub.execute_input":"2022-05-25T04:32:49.706863Z","iopub.status.idle":"2022-05-25T04:33:08.966159Z","shell.execute_reply.started":"2022-05-25T04:32:49.706804Z","shell.execute_reply":"2022-05-25T04:33:08.965065Z"},"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:33:13.584211Z","iopub.execute_input":"2022-05-25T04:33:13.584523Z","iopub.status.idle":"2022-05-25T04:33:13.592455Z","shell.execute_reply.started":"2022-05-25T04:33:13.584476Z","shell.execute_reply":"2022-05-25T04:33:13.591413Z"},"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:33:14.784608Z","iopub.execute_input":"2022-05-25T04:33:14.785480Z","iopub.status.idle":"2022-05-25T04:33:15.680688Z","shell.execute_reply.started":"2022-05-25T04:33:14.785445Z","shell.execute_reply":"2022-05-25T04:33:15.679666Z"},"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:33:17.573855Z","iopub.execute_input":"2022-05-25T04:33:17.574638Z","iopub.status.idle":"2022-05-25T04:33:17.814072Z","shell.execute_reply.started":"2022-05-25T04:33:17.574602Z","shell.execute_reply":"2022-05-25T04:33:17.813055Z"},"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:33:18.566661Z","iopub.execute_input":"2022-05-25T04:33:18.566964Z","iopub.status.idle":"2022-05-25T04:33:18.789724Z","shell.execute_reply.started":"2022-05-25T04:33:18.566906Z","shell.execute_reply":"2022-05-25T04:33:18.788886Z"},"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:33:19.401859Z","iopub.execute_input":"2022-05-25T04:33:19.402490Z","iopub.status.idle":"2022-05-25T04:33:19.631847Z","shell.execute_reply.started":"2022-05-25T04:33:19.402456Z","shell.execute_reply":"2022-05-25T04:33:19.630839Z"},"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:33:23.757986Z","iopub.execute_input":"2022-05-25T04:33:23.760433Z","iopub.status.idle":"2022-05-25T04:33:24.110152Z","shell.execute_reply.started":"2022-05-25T04:33:23.760379Z","shell.execute_reply":"2022-05-25T04:33:24.109065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}