{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "machine_shape": "hm"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MVKqY4zjbLzX",
        "outputId": "a696ab40-091f-48ef-d019-5e04b8e5c379"
      },
      "source": [
        "gpu_info = !nvidia-smi\n",
        "gpu_info = '\\n'.join(gpu_info)\n",
        "if gpu_info.find('failed') >= 0:\n",
        "  print('Select the Runtime > \"Change runtime type\" menu to enable a GPU accelerator, ')\n",
        "  print('and then re-execute this cell.')\n",
        "else:\n",
        "  print(gpu_info)\n",
        "    #For GPU tunning"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Thu Mar 25 22:09:00 2021       \n",
            "+-----------------------------------------------------------------------------+\n",
            "| NVIDIA-SMI 460.56       Driver Version: 460.32.03    CUDA Version: 11.2     |\n",
            "|-------------------------------+----------------------+----------------------+\n",
            "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
            "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
            "|                               |                      |               MIG M. |\n",
            "|===============================+======================+======================|\n",
            "|   0  Tesla V100-SXM2...  Off  | 00000000:00:04.0 Off |                    0 |\n",
            "| N/A   32C    P0    22W / 300W |      0MiB / 16160MiB |      0%      Default |\n",
            "|                               |                      |                  N/A |\n",
            "+-------------------------------+----------------------+----------------------+\n",
            "                                                                               \n",
            "+-----------------------------------------------------------------------------+\n",
            "| Processes:                                                                  |\n",
            "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
            "|        ID   ID                                                   Usage      |\n",
            "|=============================================================================|\n",
            "|  No running processes found                                                 |\n",
            "+-----------------------------------------------------------------------------+\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "I0Ilk9PubNPb",
        "outputId": "d8f028fa-2867-4ea2-e496-49443a48621e"
      },
      "source": [
        "from psutil import virtual_memory\n",
        "ram_gb = virtual_memory().total / 1e9\n",
        "print('Your runtime has {:.1f} gigabytes of available RAM\\n'.format(ram_gb))\n",
        "\n",
        "if ram_gb < 20:\n",
        "  print('To enable a high-RAM runtime, select the Runtime > \"Change runtime type\"')\n",
        "  print('menu, and then select High-RAM in the Runtime shape dropdown. Then, ')\n",
        "  print('re-execute this cell.')\n",
        "else:\n",
        "  print('You are using a high-RAM runtime!')\n",
        "    #FOR ram tunning"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Your runtime has 27.4 gigabytes of available RAM\n",
            "\n",
            "You are using a high-RAM runtime!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jEWLICopbNSC",
        "outputId": "f54c372c-86c0-4a23-818a-998197cb3f53"
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n",
        "import os\n",
        "os.chdir(\"/content/drive/My Drive\")\n",
        "!ls\n",
        "#For conneting DB"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n",
            " Brain_all_downsized2.npy  'H&Y look up table.xlsx'\n",
            " Brain_Label.npy\t    PPMI_FLIRT_BRAIN\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QEQbawx4bNVD"
      },
      "source": [
        "from tensorflow import keras\n",
        "\n",
        "from keras import Sequential\n",
        "import matplotlib.pyplot as plt\n",
        "from keras.layers import Dense, Flatten, Conv3D, Conv2D, MaxPooling3D\n",
        "from keras.utils import to_categorical\n",
        "\n",
        "import numpy as np  \n",
        "# import nessesary libs"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-3cqEIBjbNX6",
        "outputId": "cea2d829-f2df-4234-8a35-8e7a84480e99"
      },
      "source": [
        "\n",
        "# import keras\n",
        "shaped_data = np.load('Brain_all_downsized2.npy')\n",
        "final_label = np.load('Brain_Label.npy')\n",
        "final_label = final_label[:,0]\n",
        "from scipy import io as sio\n",
        "samples, width, height, depth = np.shape(shaped_data)\n",
        "shaped_data = shaped_data[1:samples,:,:,:]\n",
        "shaped_data = np.expand_dims(shaped_data,axis=4)\n",
        "print(np.shape(shaped_data))\n",
        "# import data and checking\n",
        "X = shaped_data\n",
        "y = final_label\n",
        "yTemp = y\n",
        "del shaped_data,final_label\n",
        "# making the necessary adjustments\n",
        "\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense,  Activation, Flatten, ConvLSTM2D, MaxPooling2D, Dropout, BatchNormalization\n",
        "\n",
        "\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(1130, 91, 109, 91, 1)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j-sM6jWksLAp"
      },
      "source": [],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GFcVYeQSbNay"
      },
      "source": [
        "print(\"Started\")\n",
        "#========================================================================================\n",
        "\n",
        "\n",
        "model = Sequential()\n",
        "\n",
        "\n",
        "model.add(Conv3D(filters=64, kernel_size=3, activation=\"relu\",input_shape=(width,height,depth,1)))\n",
        "model.add(MaxPooling3D(pool_size=2))\n",
        "model.add(BatchNormalization())\n",
        "\n",
        "model.add(Conv3D(filters=64, kernel_size=3, activation=\"relu\"))\n",
        "model.add(MaxPooling3D(pool_size=2))\n",
        "model.add(BatchNormalization())\n",
        " \n",
        "model.add(Conv3D(filters=64, kernel_size=3, activation=\"relu\"))\n",
        "model.add(MaxPooling3D(pool_size=2))\n",
        "model.add(BatchNormalization())\n",
        "\n",
        "model.add(Conv3D(filters=64, kernel_size=3, activation=\"relu\"))\n",
        "model.add(MaxPooling3D(pool_size=2))\n",
        "model.add(BatchNormalization())\n",
        "\n",
        "\n",
        "# Passing it to a Fully Connected layer\n",
        "model.add(Flatten())\n",
        "\n",
        "\n",
        "\n",
        "# 2nd Fully Connected Layer\n",
        "model.add(Dense(2048))\n",
        "model.add(Activation('relu'))\n",
        "# Add Dropout\n",
        "\n",
        "\n",
        "# 3rd Fully Connected Layer\n",
        "model.add(Dense(1000))\n",
        "model.add(Activation('relu'))\n",
        "# Add Dropout\n",
        "\n",
        "\n",
        "model.add(Dense(1))\n",
        "model.summary()\n",
        "\n",
        "\n",
        "\n",
        "model.compile(loss='mse', optimizer='adam')\n",
        "print(\"model compile oldu\")\n",
        "\n",
        "from sklearn import metrics\n",
        "from sklearn.model_selection import KFold \n",
        "\n",
        "tx,*_=np.shape(X)\n",
        "Y_Pred_all = np.zeros(tx)\n",
        "seed = 7\n",
        "fold=10\n",
        "np.random.seed(seed)\n",
        "kfold = KFold(n_splits=fold, shuffle = True, random_state = seed)\n",
        "batch_size = 8\n",
        "no_epochs = 30\n",
        "no_classes = 2    #for parkinson and control\n",
        "verbosity = 1\n",
        "learning_rate = 0.001\n",
        "# Model configuration\n",
        "count = 1\n",
        "for train, test in kfold.split(X,y):\n",
        "\n",
        "    print(count,\". Fold\")\n",
        "    count +=1\n",
        "    history = model.fit(X[train], y[train].ravel(),\n",
        "                        batch_size=batch_size,\n",
        "                        epochs=no_epochs,\n",
        "                        verbose=verbosity)\n",
        "    Y_Pred = model.predict(X[test])\n",
        "  \n",
        "    \n",
        "    j = 0\n",
        "    for i in test:\n",
        "      Y_Pred_all[i] = Y_Pred[j]\n",
        "      j+=1\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G2nyauoobNdr"
      },
      "source": [
        "Y_True = y\n",
        "#for evaluating\n",
        "import math\n",
        "explained_variance = metrics.explained_variance_score(Y_True, Y_Pred_all)\n",
        "max_error = metrics.max_error(Y_True, Y_Pred_all)\n",
        "mean_absolute_error = metrics.mean_absolute_error(Y_True, Y_Pred_all)\n",
        "mean_squared_error = metrics.mean_squared_error(Y_True, Y_Pred_all)\n",
        "root_mean_squared_error = metrics.mean_squared_error(Y_True, Y_Pred_all)\n",
        "    \n",
        "median_absolute_error = metrics.median_absolute_error(Y_True, Y_Pred_all)\n",
        "r2 =  metrics.r2_score(Y_True, Y_Pred_all)\n",
        "cc = math.sqrt(abs(r2))\n",
        "    \n",
        "print('explained_variance =', explained_variance)\n",
        "print('max_error =', max_error)\n",
        "print('mean_absolute_error =', mean_absolute_error)\n",
        "print('mean_squared_error =',mean_squared_error)\n",
        "print('root_mean_squared_error =', root_mean_squared_error)\n",
        "print('median_absolute_error =',median_absolute_error)\n",
        "print('r2 =',r2)\n",
        "print('cc =',cc)"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}