Skip to content

Instantly share code, notes, and snippets.

@kiransair
Created March 13, 2024 06:42
Show Gist options
  • Save kiransair/85fc6263f2e245a455606d6035ba094c to your computer and use it in GitHub Desktop.
Save kiransair/85fc6263f2e245a455606d6035ba094c to your computer and use it in GitHub Desktop.
TF_Forum_23113.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPqSgzQrOVVjuBMdxGpHvVz",
"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/85fc6263f2e245a455606d6035ba094c/tf_forum_23113.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": "ARIz6D5FfCKY"
},
"outputs": [],
"source": [
"IMG_SIZE_1 = 320\n",
"IMG_SIZE_2 = 320\n",
"N_CHANNELS = 3\n",
"N_CLASSES = 11\n",
"SEED = 123"
]
},
{
"cell_type": "code",
"source": [
"from tensorflow import keras\n",
"from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Concatenate, Cropping2D, Dense\n",
"from tensorflow.keras.layers import Input, Add, Conv2DTranspose, Activation\n",
"from tensorflow.keras.models import Sequential, Model\n",
"from tensorflow.keras.applications import VGG16\n",
"from tensorflow.keras.optimizers import SGD, Adam\n",
"from tensorflow.keras.losses import SparseCategoricalCrossentropy, MeanSquaredError, BinaryCrossentropy\n",
"from tensorflow.keras.utils import plot_model\n",
"\n",
"vgg16_model = VGG16() # maybe try to use VGG18\n",
"\n",
"input_shape = (IMG_SIZE_2, IMG_SIZE_1, N_CHANNELS)\n",
"\n",
"inputs = Input(input_shape)\n",
"\n",
"vgg16_model = VGG16(include_top = False, weights = 'imagenet', input_tensor = inputs)\n",
"\n",
"pool3 = vgg16_model.get_layer('block3_pool').output\n",
"pool4 = vgg16_model.get_layer('block4_pool').output\n",
"pool5 = vgg16_model.get_layer('block5_pool').output\n",
"\n",
"conv_6 = Conv2D(1024, (7,7), activation='relu', padding='same', name='conv_6')(pool5)\n",
"conv_7 = Conv2D(1024, (1, 1), activation='relu', padding='same', name='conv_7')(conv_6)\n",
"\n",
"conv_8 = Conv2D(N_CLASSES, (1, 1), activation='relu', padding='same', name='conv_8')(pool4)\n",
"conv_9 = Conv2D(N_CLASSES, (1, 1), activation='relu', padding='same', name='conv_9')(pool3)\n",
"\n",
"deconv_7 = Conv2DTranspose(N_CLASSES, kernel_size=(2,2), strides=(2,2))(conv_7)\n",
"add_1 = Add()([deconv_7, conv_8])\n",
"deconv_8 = Conv2DTranspose(N_CLASSES, kernel_size=(2,2), strides=(2,2))(add_1)\n",
"\n",
"deconv_10 = Conv2DTranspose(N_CLASSES, kernel_size=(16,16), strides=(16,16))(add_1)\n",
"output_layer = Activation('softmax')(deconv_10)\n",
"\n",
"model = Model(inputs=vgg16_model.input, outputs=output_layer)\n",
"model.summary()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bVl55RF8fFCZ",
"outputId": "2c7ab085-d2fb-4cd1-c0bc-e6fc5b3f63e1"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5\n",
"553467096/553467096 [==============================] - 4s 0us/step\n",
"Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n",
"58889256/58889256 [==============================] - 0s 0us/step\n",
"Model: \"model\"\n",
"__________________________________________________________________________________________________\n",
" Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
" input_2 (InputLayer) [(None, 320, 320, 3)] 0 [] \n",
" \n",
" block1_conv1 (Conv2D) (None, 320, 320, 64) 1792 ['input_2[0][0]'] \n",
" \n",
" block1_conv2 (Conv2D) (None, 320, 320, 64) 36928 ['block1_conv1[0][0]'] \n",
" \n",
" block1_pool (MaxPooling2D) (None, 160, 160, 64) 0 ['block1_conv2[0][0]'] \n",
" \n",
" block2_conv1 (Conv2D) (None, 160, 160, 128) 73856 ['block1_pool[0][0]'] \n",
" \n",
" block2_conv2 (Conv2D) (None, 160, 160, 128) 147584 ['block2_conv1[0][0]'] \n",
" \n",
" block2_pool (MaxPooling2D) (None, 80, 80, 128) 0 ['block2_conv2[0][0]'] \n",
" \n",
" block3_conv1 (Conv2D) (None, 80, 80, 256) 295168 ['block2_pool[0][0]'] \n",
" \n",
" block3_conv2 (Conv2D) (None, 80, 80, 256) 590080 ['block3_conv1[0][0]'] \n",
" \n",
" block3_conv3 (Conv2D) (None, 80, 80, 256) 590080 ['block3_conv2[0][0]'] \n",
" \n",
" block3_pool (MaxPooling2D) (None, 40, 40, 256) 0 ['block3_conv3[0][0]'] \n",
" \n",
" block4_conv1 (Conv2D) (None, 40, 40, 512) 1180160 ['block3_pool[0][0]'] \n",
" \n",
" block4_conv2 (Conv2D) (None, 40, 40, 512) 2359808 ['block4_conv1[0][0]'] \n",
" \n",
" block4_conv3 (Conv2D) (None, 40, 40, 512) 2359808 ['block4_conv2[0][0]'] \n",
" \n",
" block4_pool (MaxPooling2D) (None, 20, 20, 512) 0 ['block4_conv3[0][0]'] \n",
" \n",
" block5_conv1 (Conv2D) (None, 20, 20, 512) 2359808 ['block4_pool[0][0]'] \n",
" \n",
" block5_conv2 (Conv2D) (None, 20, 20, 512) 2359808 ['block5_conv1[0][0]'] \n",
" \n",
" block5_conv3 (Conv2D) (None, 20, 20, 512) 2359808 ['block5_conv2[0][0]'] \n",
" \n",
" block5_pool (MaxPooling2D) (None, 10, 10, 512) 0 ['block5_conv3[0][0]'] \n",
" \n",
" conv_6 (Conv2D) (None, 10, 10, 1024) 2569113 ['block5_pool[0][0]'] \n",
" 6 \n",
" \n",
" conv_7 (Conv2D) (None, 10, 10, 1024) 1049600 ['conv_6[0][0]'] \n",
" \n",
" conv2d_transpose (Conv2DTr (None, 20, 20, 11) 45067 ['conv_7[0][0]'] \n",
" anspose) \n",
" \n",
" conv_8 (Conv2D) (None, 20, 20, 11) 5643 ['block4_pool[0][0]'] \n",
" \n",
" add (Add) (None, 20, 20, 11) 0 ['conv2d_transpose[0][0]', \n",
" 'conv_8[0][0]'] \n",
" \n",
" conv2d_transpose_2 (Conv2D (None, 320, 320, 11) 30987 ['add[0][0]'] \n",
" Transpose) \n",
" \n",
" activation (Activation) (None, 320, 320, 11) 0 ['conv2d_transpose_2[0][0]'] \n",
" \n",
"==================================================================================================\n",
"Total params: 41537121 (158.45 MB)\n",
"Trainable params: 41537121 (158.45 MB)\n",
"Non-trainable params: 0 (0.00 Byte)\n",
"__________________________________________________________________________________________________\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import tensorflow as tf\n",
"import numpy as np"
],
"metadata": {
"id": "1Qfcbae7fImr"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"IMG_SIZE_1 = 320\n",
"IMG_SIZE_2 = 320\n",
"N_CHANNELS = 3 # Number of channels in input images\n",
"N_CLASSES = 11 # Number of segmentation classes\n",
"\n",
"# Generate random data\n",
"num_samples = 100 # Number of training samples\n",
"x_train = np.random.rand(num_samples, IMG_SIZE_2, IMG_SIZE_1, N_CHANNELS)\n",
"y_train = np.random.randint(0, N_CLASSES, size=(num_samples, IMG_SIZE_2, IMG_SIZE_1))\n"
],
"metadata": {
"id": "KmULBuqTfTSl"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"m_iou = tf.keras.metrics.MeanIoU(2)\n",
"model.compile(optimizer=Adam(),\n",
"loss='categorical_crossentropy',\n",
"metrics=[m_iou])"
],
"metadata": {
"id": "-fHoAg02fT7k"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from tensorflow.keras.utils import to_categorical\n",
"y_train_one_hot = to_categorical(y_train, num_classes=N_CLASSES)"
],
"metadata": {
"id": "ffXGQORvfhZl"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model_history = model.fit(x_train,y_train_one_hot, epochs=2)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "15afb5_efYBu",
"outputId": "4ae92908-3704-4bbc-b3b1-9cf93a827679"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/2\n",
"4/4 [==============================] - 454s 102s/step - loss: 2.4013 - mean_io_u: 0.4208\n",
"Epoch 2/2\n",
"4/4 [==============================] - 446s 101s/step - loss: 2.3981 - mean_io_u: 0.4208\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "2D5ahVbAfaLa"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment