-
-
Save kiransair/7b4319c2c3a734bb5c42c1ee80f32fbf to your computer and use it in GitHub Desktop.
TF_Forum_23161.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyMyKImBG/ocvTTl0qVf8cNJ", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/kiransair/7b4319c2c3a734bb5c42c1ee80f32fbf/tf_forum_23161.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "id": "aN3L1k2iixcg" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import tensorflow as tf" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import keras\n", | |
| "from keras import Sequential,layers" | |
| ], | |
| "metadata": { | |
| "id": "Qw5mZLxai5z2" | |
| }, | |
| "execution_count": 2, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "model = Sequential([\n", | |
| " layers.Conv3D(filters=16, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.Conv3D(filters=16, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.MaxPool3D(pool_size=2),\n", | |
| " layers.BatchNormalization(),\n", | |
| "\n", | |
| " layers.Conv3D(filters=32, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.Conv3D(filters=32, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.MaxPool3D(pool_size=2),\n", | |
| " layers.BatchNormalization(),\n", | |
| "\n", | |
| " layers.Conv3D(filters=64, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.Conv3D(filters=64, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.MaxPool3D(pool_size=2),\n", | |
| " layers.BatchNormalization(),\n", | |
| "\n", | |
| " layers.Conv3D(filters=128, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.Conv3D(filters=128, kernel_size=3, padding='same'),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.MaxPool3D(pool_size=2),\n", | |
| " layers.BatchNormalization(),\n", | |
| "\n", | |
| " layers.GlobalAveragePooling3D(),\n", | |
| " layers.Dense(units=256),\n", | |
| " layers.LeakyReLU(),\n", | |
| " layers.BatchNormalization(),\n", | |
| " layers.Dropout(0.4),\n", | |
| " layers.Dense(units=1, activation=\"sigmoid\"),\n", | |
| "])" | |
| ], | |
| "metadata": { | |
| "id": "sRalMSHpi6I7" | |
| }, | |
| "execution_count": 3, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "model.compile(optimizer='adam',\n", | |
| " loss = tf.keras.losses.BinaryFocalCrossentropy(gamma=5, apply_class_balancing=True),\n", | |
| " metrics=['accuracy'])\n", | |
| "model.build(input_shape= (128,None,None,None,1))\n", | |
| "model.summary()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "hWpfWKONi7xS", | |
| "outputId": "54897305-3038-47cf-e5c7-3e469e358271" | |
| }, | |
| "execution_count": 5, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Model: \"sequential\"\n", | |
| "_________________________________________________________________\n", | |
| " Layer (type) Output Shape Param # \n", | |
| "=================================================================\n", | |
| " conv3d (Conv3D) (128, None, None, None, 448 \n", | |
| " 16) \n", | |
| " \n", | |
| " leaky_re_lu (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 16) \n", | |
| " \n", | |
| " batch_normalization (Batch (128, None, None, None, 64 \n", | |
| " Normalization) 16) \n", | |
| " \n", | |
| " conv3d_1 (Conv3D) (128, None, None, None, 6928 \n", | |
| " 16) \n", | |
| " \n", | |
| " leaky_re_lu_1 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 16) \n", | |
| " \n", | |
| " batch_normalization_1 (Bat (128, None, None, None, 64 \n", | |
| " chNormalization) 16) \n", | |
| " \n", | |
| " max_pooling3d (MaxPooling3 (128, None, None, None, 0 \n", | |
| " D) 16) \n", | |
| " \n", | |
| " batch_normalization_2 (Bat (128, None, None, None, 64 \n", | |
| " chNormalization) 16) \n", | |
| " \n", | |
| " conv3d_2 (Conv3D) (128, None, None, None, 13856 \n", | |
| " 32) \n", | |
| " \n", | |
| " leaky_re_lu_2 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 32) \n", | |
| " \n", | |
| " batch_normalization_3 (Bat (128, None, None, None, 128 \n", | |
| " chNormalization) 32) \n", | |
| " \n", | |
| " conv3d_3 (Conv3D) (128, None, None, None, 27680 \n", | |
| " 32) \n", | |
| " \n", | |
| " leaky_re_lu_3 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 32) \n", | |
| " \n", | |
| " batch_normalization_4 (Bat (128, None, None, None, 128 \n", | |
| " chNormalization) 32) \n", | |
| " \n", | |
| " max_pooling3d_1 (MaxPoolin (128, None, None, None, 0 \n", | |
| " g3D) 32) \n", | |
| " \n", | |
| " batch_normalization_5 (Bat (128, None, None, None, 128 \n", | |
| " chNormalization) 32) \n", | |
| " \n", | |
| " conv3d_4 (Conv3D) (128, None, None, None, 55360 \n", | |
| " 64) \n", | |
| " \n", | |
| " leaky_re_lu_4 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 64) \n", | |
| " \n", | |
| " batch_normalization_6 (Bat (128, None, None, None, 256 \n", | |
| " chNormalization) 64) \n", | |
| " \n", | |
| " conv3d_5 (Conv3D) (128, None, None, None, 110656 \n", | |
| " 64) \n", | |
| " \n", | |
| " leaky_re_lu_5 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 64) \n", | |
| " \n", | |
| " batch_normalization_7 (Bat (128, None, None, None, 256 \n", | |
| " chNormalization) 64) \n", | |
| " \n", | |
| " max_pooling3d_2 (MaxPoolin (128, None, None, None, 0 \n", | |
| " g3D) 64) \n", | |
| " \n", | |
| " batch_normalization_8 (Bat (128, None, None, None, 256 \n", | |
| " chNormalization) 64) \n", | |
| " \n", | |
| " conv3d_6 (Conv3D) (128, None, None, None, 221312 \n", | |
| " 128) \n", | |
| " \n", | |
| " leaky_re_lu_6 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 128) \n", | |
| " \n", | |
| " batch_normalization_9 (Bat (128, None, None, None, 512 \n", | |
| " chNormalization) 128) \n", | |
| " \n", | |
| " conv3d_7 (Conv3D) (128, None, None, None, 442496 \n", | |
| " 128) \n", | |
| " \n", | |
| " leaky_re_lu_7 (LeakyReLU) (128, None, None, None, 0 \n", | |
| " 128) \n", | |
| " \n", | |
| " batch_normalization_10 (Ba (128, None, None, None, 512 \n", | |
| " tchNormalization) 128) \n", | |
| " \n", | |
| " max_pooling3d_3 (MaxPoolin (128, None, None, None, 0 \n", | |
| " g3D) 128) \n", | |
| " \n", | |
| " batch_normalization_11 (Ba (128, None, None, None, 512 \n", | |
| " tchNormalization) 128) \n", | |
| " \n", | |
| " global_average_pooling3d ( (128, 128) 0 \n", | |
| " GlobalAveragePooling3D) \n", | |
| " \n", | |
| " dense (Dense) (128, 256) 33024 \n", | |
| " \n", | |
| " leaky_re_lu_8 (LeakyReLU) (128, 256) 0 \n", | |
| " \n", | |
| " batch_normalization_12 (Ba (128, 256) 1024 \n", | |
| " tchNormalization) \n", | |
| " \n", | |
| " dropout (Dropout) (128, 256) 0 \n", | |
| " \n", | |
| " dense_1 (Dense) (128, 1) 257 \n", | |
| " \n", | |
| "=================================================================\n", | |
| "Total params: 915921 (3.49 MB)\n", | |
| "Trainable params: 913969 (3.49 MB)\n", | |
| "Non-trainable params: 1952 (7.62 KB)\n", | |
| "_________________________________________________________________\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import numpy as np\n", | |
| "\n", | |
| "num_samples = 1000\n", | |
| "depth = 32\n", | |
| "height = 64\n", | |
| "width = 64\n", | |
| "\n", | |
| "X_train = np.random.rand(num_samples, depth, height, width, 1)\n", | |
| "\n", | |
| "y_train = np.random.randint(0, 2, size=(num_samples, 1))\n", | |
| "\n", | |
| "from sklearn.model_selection import train_test_split\n", | |
| "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n" | |
| ], | |
| "metadata": { | |
| "id": "tJCxfMWEi9eV" | |
| }, | |
| "execution_count": 6, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "history = model.fit(X_train, y_train, batch_size=32, epochs=2, validation_data=(X_val, y_val))" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "d7mP7B_DjBqM", | |
| "outputId": "cbe8173c-2387-4d2f-a097-1cb4cfde0f25" | |
| }, | |
| "execution_count": 7, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Epoch 1/2\n", | |
| "25/25 [==============================] - 853s 34s/step - loss: 0.1433 - accuracy: 0.4888 - val_loss: 0.0215 - val_accuracy: 0.5050\n", | |
| "Epoch 2/2\n", | |
| "25/25 [==============================] - 832s 33s/step - loss: 0.0597 - accuracy: 0.5600 - val_loss: 0.0501 - val_accuracy: 0.5050\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [], | |
| "metadata": { | |
| "id": "NXj_aacpjE_x" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment