{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "XUZA3OvtRzK-" }, "source": [ "\n", "

\n", " \n", "

Cifar 10 資料庫
\n", "
圖片來源: https://www.kaggle.com/
\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "tS5xFeQwe9Xu" }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "source": [ "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Flatten\n", "from tensorflow.keras.layers import Conv2D, MaxPool2D\n", "from tensorflow.keras.optimizers import SGD, Adam\n", "from tensorflow.keras.datasets import cifar10\n", "from tensorflow.keras.utils import to_categorical\n", "from tensorflow.keras.preprocessing.image import load_img" ], "metadata": { "id": "kEEDoYWJqT8w" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "e8zYubaPfy-S" }, "source": [ "### 1. 讀入CIFAR--10資料集並進行資料預處理" ] }, { "cell_type": "markdown", "metadata": { "id": "YleM4DH4NoOA" }, "source": [ "CIFAR 10 是包含 10 種類的彩色小圖資料集,每張圖的尺寸為 $32\\times32$\n", "\n", "10 個類別分別是:飛機、交通工具、鳥、貓、鹿、狗、青蛙、馬、船、卡車" ] }, { "cell_type": "markdown", "metadata": { "id": "AOAGjiPogF0w" }, "source": [ "讀取 CIFAR 10 資料集" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4c5EYPsuNThg" }, "outputs": [], "source": [ "class_name = ['飛機', '汽車', '鳥', '貓', '鹿', '狗', '青蛙', '馬', '船', '卡車']" ] }, { "cell_type": "code", "source": [ "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", "x_train = x_train/255\n", "x_test = x_test/255\n", "y_train = to_categorical(y_train, 10)\n", "y_test = to_categorical(y_test, 10)" ], "metadata": { "id": "BykCOsFexlgE" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "plt.imshow(x_test[5])\n", "print(f\"這是{class_name[y_test[5].argmax()]}\");" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 283 }, "id": "pg7EMYqItzQA", "outputId": "fe10660a-b949-4e96-dbae-43f972871aaf" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "這是青蛙\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "
" ], "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD5CAYAAADhukOtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAetklEQVR4nO2dW4xk13We/1X3qq6+Tk/3NJvDGYqirNB2REkTQokVRbEghxEcUAICQXoQ+CB4jMACIsB5IBQgUoA8yEEkQQ+BjFFEmA4UUYwlQbQtJGIYA4xhg9aQJodDcijxMveZvk/fqruuKw9VIwyJ/e/ume6uHnv/HzCY6r1qn7PPPmedU7X/WmuZu0MI8fefzH4PQAjRH+TsQiSCnF2IRJCzC5EIcnYhEkHOLkQi5HbS2cweBPBNAFkA/83dvxp7/3Cl6BNDlaBts9Wi/VZr9WB7JsPvVQOlArVlqQVAp0NNTKa0bOyeaXxX7TbvleH9Yttsk/G3IxKrR7aXyfJLpNXh22w1m2RnvE/Gdv/Z0/bwfMSunagafYvjz+X4Vddohq/9zi3I4rVGE41WK3hCb9nZzSwL4L8C+DiAiwB+ZmZPuvsrrM/EUAXfePhjQduZ2Vm6r2defCPYPlAJ3zgA4B/de5jaRjxyotc3qK3p4ZOSr5Zpn9hFtbKySm3FYpHakOEXznItPP7leviGCQDtHL8xlqoHqG1xvUFtV+fI+dzgN/WhfInaIvcjtMBv0OvN8HEXy/yctVp8e50mv0FXi3z8B8f4PJ6fmQm2rzfIDRN8Op75+Vu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Bd6XwDwL/fpzG8C10l4EUAL/dzHAC+h+7HwSa6370+j27NvKcB/ALA/wEwtk/j+O8AXgJwCl1nm+rDOD6M7kf0UwBe6P37RL/nJDKOvs4JgH+IbhLXU+jeWP7DDdfs3wB4HcD/BFC8me3qF3RCJELqC3RCJIOcXYhEkLMLkQhydiESQc4uRCLI2YVIBDm7EIkgZxciEf4/8qn2gKjbL+EAAAAASUVORK5CYII=\n" }, "metadata": { "needs_background": "light" } } ] }, { "cell_type": "code", "source": [ "y_test[5].flatten()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_Es9mEkSuqLL", "outputId": "bc250e9a-7f6d-4993-db94-c646af04ee9c" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(10000, 1)" ] }, "metadata": {}, "execution_count": 15 } ] }, { "cell_type": "code", "source": [ "x_train = x_train/255\n", "x_test = x_test/255\n", "y_train = to_categorical(y_train, 10)\n", "y_test = to_categorical(y_test, 10)" ], "metadata": { "id": "NJiJS-j7uK_j" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "swp6zTOST8gD" }, "source": [ "### 2. 第一代:過去標準逐層打造神經網路的方法" ] }, { "cell_type": "markdown", "source": [ "在這個部分,我們將逐步帶領大家建立經典的 CNN 模型 LeNet-5 的變形。\n", "\n", "LeNet-5 分成兩個部分,分別為卷積層與全連接層,兩部份之間是透過扁平層 (Flatten) ,將卷積層最後輸出的 2 維向量壓扁成 1 維向量。" ], "metadata": { "id": "JBrNY0f-2oGg" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "jGEAVln9TRwa" }, "outputs": [], "source": [ "model = Sequential()\n", "model.add(Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same', activation='relu')) \n", "model.add(MaxPool2D())\n", "model.add(Conv2D(128, (3, 3), padding='same', activation='relu')) \n", "model.add(MaxPool2D())\n", "model.add(Conv2D(512, (3, 3), padding='same', activation='relu')) \n", "model.add(MaxPool2D())\n", "model.add(Flatten())\n", "model.add(Dense(256, activation='relu'))\n", "model.add(Dense(10, activation='softmax'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "floU0of05vJj", "outputId": "f08c8a60-c6ce-4ff5-92e7-c8c2f0bc936f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential\"\n", "___________________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "===========================================================================\n", " conv2d (Conv2D) (None, 32, 32, 32) 896 \n", " \n", " max_pooling2d (MaxPooling2D) (None, 16, 16, 32) 0 \n", " \n", " conv2d_1 (Conv2D) (None, 16, 16, 128) 36992 \n", " \n", " max_pooling2d_1 (MaxPooling2D) (None, 8, 8, 128) 0 \n", " \n", " conv2d_2 (Conv2D) (None, 8, 8, 512) 590336 \n", " \n", " max_pooling2d_2 (MaxPooling2D) (None, 4, 4, 512) 0 \n", " \n", " flatten (Flatten) (None, 8192) 0 \n", " \n", " dense (Dense) (None, 256) 2097408 \n", " \n", " dense_1 (Dense) (None, 10) 2570 \n", " \n", "===========================================================================\n", "Total params: 2,728,202\n", "Trainable params: 2,728,202\n", "Non-trainable params: 0\n", "___________________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "GWdFSj9OcYWl" }, "source": [ "#### 編譯模型: 設定模型訓練時的設定\n", "\n", "- Optimizer: Stochastic Gradient Descent (SGD)\n", "- Loss: categorical cross-entropy " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "86fvl2DHcKwj" }, "outputs": [], "source": [ "model.compile(loss='categorical_crossentropy', \n", " optimizer=Adam(),\n", " metrics=['accuracy'])" ] }, { "cell_type": "markdown", "metadata": { "id": "-Zz5yHOzdUgv" }, "source": [ "#### 訓練模型: 透過訓練來學習分類資料的函數" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "LVdaaVOJcKrk", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "590dad31-ff24-4259-c010-c8a6a8bf48ab" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/3\n", "391/391 [==============================] - 26s 38ms/step - loss: 1.4611 - accuracy: 0.4717 - val_loss: 1.1093 - val_accuracy: 0.6097\n", "Epoch 2/3\n", "391/391 [==============================] - 14s 36ms/step - loss: 0.9935 - accuracy: 0.6487 - val_loss: 1.0079 - val_accuracy: 0.6478\n", "Epoch 3/3\n", "391/391 [==============================] - 14s 36ms/step - loss: 0.8006 - accuracy: 0.7194 - val_loss: 0.7835 - val_accuracy: 0.7292\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 34 } ], "source": [ "model.fit(x_train, y_train, \n", " batch_size=128, \n", " epochs=3,\n", " validation_data=(x_test, y_test)\n", " )" ] }, { "cell_type": "markdown", "metadata": { "id": "5IJhvV_BSo9x" }, "source": [ "#### 模型預測: 預測資料集的準確率" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "btoBDRueSokB", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "37c4cb03-545c-4c02-f5b8-2242d9c94608" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1563/1563 [==============================] - 11s 7ms/step - loss: 0.6452 - accuracy: 0.7798\n", "313/313 [==============================] - 2s 7ms/step - loss: 0.7835 - accuracy: 0.7292\n" ] } ], "source": [ "score_train = model.evaluate(x_train, y_train)\n", "score_test = model.evaluate(x_test, y_test)" ] }, { "cell_type": "code", "source": [ "print(f'Train Accuracy: {score_train[1]*100}')\n", "print(f'Test Accuracy: {score_test[1]*100}')" ], "metadata": { "id": "_aZb0x5t2zcZ", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "df9cc553-b434-41c0-e646-f0e225649387" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Train Accuracy: 77.97799706459045\n", "Test Accuracy: 72.9200005531311\n" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "0G7lUOLHQEjr" }, "source": [ "## 3. 第二代:用串列將神經網路層一次放進模型中" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "14YYzYYwcKpE" }, "outputs": [], "source": [ "many_layers = [Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same', activation='relu'), \n", " MaxPool2D(), \n", " Conv2D(128, (3, 3), padding='same', activation='relu'), \n", " MaxPool2D(), \n", " Conv2D(512, (3, 3), padding='same', activation='relu'), \n", " MaxPool2D(),\n", " Flatten(), \n", " Dense(256, activation='relu'), \n", " Dense(10, activation='softmax'), \n", " ]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "xn7EzCCgqkDs" }, "outputs": [], "source": [ "second_model = Sequential(many_layers)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hvmVUw4krEJq", "outputId": "ab95abc1-f908-4f3b-e3bf-f612d77519cb" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential_1\"\n", "___________________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "===========================================================================\n", " conv2d_3 (Conv2D) (None, 32, 32, 32) 896 \n", " \n", " max_pooling2d_3 (MaxPooling2D) (None, 16, 16, 32) 0 \n", " \n", " conv2d_4 (Conv2D) (None, 16, 16, 128) 36992 \n", " \n", " max_pooling2d_4 (MaxPooling2D) (None, 8, 8, 128) 0 \n", " \n", " conv2d_5 (Conv2D) (None, 8, 8, 512) 590336 \n", " \n", " max_pooling2d_5 (MaxPooling2D) (None, 4, 4, 512) 0 \n", " \n", " flatten_1 (Flatten) (None, 8192) 0 \n", " \n", " dense_2 (Dense) (None, 256) 2097408 \n", " \n", " dense_3 (Dense) (None, 10) 2570 \n", " \n", "===========================================================================\n", "Total params: 2,728,202\n", "Trainable params: 2,728,202\n", "Non-trainable params: 0\n", "___________________________________________________________________________\n" ] } ], "source": [ "second_model.summary()" ] }, { "cell_type": "markdown", "source": [ "## 4. 第三代:用兩個串列定義神經網路,再一次放進模型中" ], "metadata": { "id": "8831zmPI27gx" } }, { "cell_type": "code", "source": [ "CNN_layers = [Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same',activation='relu'), \n", " MaxPool2D(), \n", " Conv2D(128, (3, 3), padding='same', activation='relu'), \n", " MaxPool2D(), \n", " Conv2D(512, (3, 3), padding='same', activation='relu'), \n", " MaxPool2D(), \n", " Flatten(), \n", " ]" ], "metadata": { "id": "BIF4WqQH27pw" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "FC_layers = [Dense(units=256, activation='relu'), \n", " Dense(units=10, activation='softmax'), \n", " ]" ], "metadata": { "id": "qMSAZe3U3IQk" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "third_model = Sequential(CNN_layers+FC_layers)" ], "metadata": { "id": "EOAbbhUR3TDE" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "third_model.summary()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nO1DDeNv3TFB", "outputId": "598a52f5-3e52-4772-cae2-b7d484be467d" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential_2\"\n", "___________________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "===========================================================================\n", " conv2d_6 (Conv2D) (None, 32, 32, 32) 896 \n", " \n", " max_pooling2d_6 (MaxPooling2D) (None, 16, 16, 32) 0 \n", " \n", " conv2d_7 (Conv2D) (None, 16, 16, 128) 36992 \n", " \n", " max_pooling2d_7 (MaxPooling2D) (None, 8, 8, 128) 0 \n", " \n", " conv2d_8 (Conv2D) (None, 8, 8, 512) 590336 \n", " \n", " max_pooling2d_8 (MaxPooling2D) (None, 4, 4, 512) 0 \n", " \n", " flatten_2 (Flatten) (None, 8192) 0 \n", " \n", " dense_4 (Dense) (None, 256) 2097408 \n", " \n", " dense_5 (Dense) (None, 10) 2570 \n", " \n", "===========================================================================\n", "Total params: 2,728,202\n", "Trainable params: 2,728,202\n", "Non-trainable params: 0\n", "___________________________________________________________________________\n" ] } ] }, { "cell_type": "markdown", "source": [ "### 5. 第四代:可以方便修改部份神經網路,不會動到別人!" ], "metadata": { "id": "BiBo4aPm3h0A" } }, { "cell_type": "code", "source": [ "new_FC_layers = [Dense(1024, activation='relu'), \n", " Dense(128, activation='relu'), \n", " Dense(10, activation='softmax'), \n", " ]\n" ], "metadata": { "id": "eXA_dDWr3THH" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "fourth_model = Sequential(CNN_layers+new_FC_layers)" ], "metadata": { "id": "nsN6AAC43TJN" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "fourth_model.summary()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_Pi2Xomo3TLP", "outputId": "f3291410-8a95-462e-88bf-75f28a7ff845" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model: \"sequential_3\"\n", "___________________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "===========================================================================\n", " conv2d_6 (Conv2D) (None, 32, 32, 32) 896 \n", " \n", " max_pooling2d_6 (MaxPooling2D) (None, 16, 16, 32) 0 \n", " \n", " conv2d_7 (Conv2D) (None, 16, 16, 128) 36992 \n", " \n", " max_pooling2d_7 (MaxPooling2D) (None, 8, 8, 128) 0 \n", " \n", " conv2d_8 (Conv2D) (None, 8, 8, 512) 590336 \n", " \n", " max_pooling2d_8 (MaxPooling2D) (None, 4, 4, 512) 0 \n", " \n", " flatten_2 (Flatten) (None, 8192) 0 \n", " \n", " dense_6 (Dense) (None, 1024) 8389632 \n", " \n", " dense_7 (Dense) (None, 128) 131200 \n", " \n", " dense_8 (Dense) (None, 10) 1290 \n", " \n", "===========================================================================\n", "Total params: 9,150,346\n", "Trainable params: 9,150,346\n", "Non-trainable params: 0\n", "___________________________________________________________________________\n" ] } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "Me5NQzpaxbOH" }, "execution_count": null, "outputs": [] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [], "machine_shape": "hm", "name": "冒險32_神經網路的另一個打造方式.ipynb", "provenance": [], "include_colab_link": true }, "kernelspec": { "display_name": "Python 3", "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.3" } }, "nbformat": 4, "nbformat_minor": 0 }