{ "cells": [ { "cell_type": "markdown", "id": "321eea1b", "metadata": { "id": "321eea1b" }, "source": [ "# **SENet**\n", "此份程式碼會介紹如何使用 tf.keras 的方式建構 SENet 的模型架構。" ] }, { "cell_type": "markdown", "id": "b697e18e", "metadata": { "id": "b697e18e" }, "source": [ "![image](https://hackmd.io/_uploads/SyC2RkB_6.png)\n", "\n", "- [source paper](https://arxiv.org/abs/1709.01507)" ] }, { "cell_type": "markdown", "id": "c1641211", "metadata": { "id": "c1641211" }, "source": [ "## 匯入套件" ] }, { "cell_type": "code", "execution_count": null, "id": "efa38274", "metadata": { "id": "efa38274" }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Tensorflow 相關套件\n", "import tensorflow as tf\n", "from tensorflow.keras import datasets, layers, Model, Sequential, losses" ] }, { "cell_type": "markdown", "id": "f4836eef", "metadata": { "id": "f4836eef" }, "source": [ "## 載入資料集" ] }, { "cell_type": "code", "execution_count": null, "id": "00e8b7f1", "metadata": { "id": "00e8b7f1" }, "outputs": [], "source": [ "(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()\n", "\n", "# Expand dimensions\n", "x_train = tf.expand_dims(x_train, axis=3, name=None)\n", "x_test = tf.expand_dims(x_test, axis=3, name=None)\n", "print(f'x_train shape: {x_train.shape}')\n", "print(f'x_test shape: {x_test.shape}')\n", "print('----------')\n", "\n", "# Grayscale to RGB\n", "x_train = tf.repeat(x_train, 3, axis=3)\n", "x_test = tf.repeat(x_test, 3, axis=3)\n", "print(f'x_train shape: {x_train.shape}')\n", "print(f'x_test shape: {x_test.shape}')\n", "print('----------')\n", "\n", "# Split dataset into training and validation data\n", "x_val = x_train[int(x_train.shape[0]*0.8):, :, :, :]\n", "y_val = y_train[int(y_train.shape[0]*0.8):]\n", "x_train = x_train[:int(x_train.shape[0]*0.8), :, :, :]\n", "y_train = y_train[:int(y_train.shape[0]*0.8)]\n", "print(f'x_train shape: {x_train.shape}, x_val shape: {x_val.shape}')\n", "print(f'y_train shape: {y_train.shape}, y_val shape: {y_val.shape}')" ] }, { "cell_type": "markdown", "id": "b6f088ec", "metadata": { "id": "b6f088ec" }, "source": [ "## SENet Arhietecture" ] }, { "cell_type": "markdown", "id": "a040859b", "metadata": { "id": "a040859b" }, "source": [ "![image](https://hackmd.io/_uploads/BkI60kr_6.png)\n", "\n", "- [source paper](https://arxiv.org/abs/1709.01507)" ] }, { "cell_type": "code", "execution_count": null, "id": "5f1e8c58", "metadata": { "id": "5f1e8c58" }, "outputs": [], "source": [ "labels_num = 10" ] }, { "cell_type": "code", "execution_count": null, "id": "089b681d", "metadata": { "id": "089b681d" }, "outputs": [], "source": [ "tf.keras.backend.clear_session()\n", "inputs = layers.Input(shape=x_train.shape[1:])\n", "x = layers.Resizing(224, 224,\n", " interpolation=\"bilinear\",\n", " input_shape=x_train.shape[1:])(inputs)\n", "filters_num = 64\n", "\n", "# transform\n", "x = layers.Conv2D(filters_num, (7, 7),\n", " strides=1, padding='same')(x)\n", "x = layers.BatchNormalization()(x)\n", "x = layers.Activation('relu')(x)\n", "x = layers.MaxPooling2D((2, 2), strides=1,\n", " padding='same')(x)\n", "x = layers.Conv2D(filters_num, (3, 3),\n", " strides=1, padding='same')(x)\n", "x = layers.BatchNormalization()(x)\n", "x = layers.Activation('relu')(x)\n", "x = layers.MaxPooling2D((2, 2), strides=1,\n", " padding='same')(x)\n", "\n", "# squeeze\n", "squeeze = layers.GlobalAveragePooling2D()(x)\n", "squeeze = layers.Reshape((1, 1, filters_num))(squeeze)\n", "\n", "# excitation\n", "excitation = layers.Dense(filters_num, activation='relu')(squeeze)\n", "excitation = layers.Dense(filters_num, activation='sigmoid')(excitation)\n", "\n", "# scale\n", "scale = x * excitation\n", "\n", "scale = layers.GlobalAveragePooling2D()(scale)\n", "outputs = layers.Dense(labels_num, activation='softmax')(scale)" ] }, { "cell_type": "code", "execution_count": null, "id": "e58e8171", "metadata": { "id": "e58e8171" }, "outputs": [], "source": [ "SENet_model = Model(inputs=inputs, outputs=outputs)" ] }, { "cell_type": "code", "execution_count": null, "id": "7d5fdeb1", "metadata": { "id": "7d5fdeb1" }, "outputs": [], "source": [ "SENet_model.summary()" ] }, { "cell_type": "code", "execution_count": null, "id": "455447b5", "metadata": { "id": "455447b5" }, "outputs": [], "source": [ "batch_size = 4\n", "inputs = np.ones((batch_size, x_train.shape[1], x_train.shape[2], 3),\n", " dtype=np.float32)\n", "SENet_model(inputs)" ] }, { "cell_type": "code", "execution_count": null, "id": "72e67dec", "metadata": { "id": "72e67dec" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }