widzenie-komputerowe-projekt/training/AlexNet.ipynb

451 lines
23 KiB
Plaintext
Raw Normal View History

2023-02-01 18:42:47 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RaaVleVhamV5"
},
"outputs": [],
"source": [
"from IPython.display import Image, SVG, display\n",
"from tqdm import tqdm\n",
"import matplotlib.pyplot as plt\n",
"import sys\n",
"import subprocess\n",
"import pkg_resources\n",
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"from skimage.io import imread\n",
"import cv2 as cv\n",
"from pathlib import Path\n",
"import random\n",
"from shutil import copyfile, rmtree\n",
"import json\n",
"import seaborn as sns\n",
"import matplotlib\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import LabelEncoder\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"import os\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from training.data_load import load_data\n",
"\n",
"train_ds, test_ds, validation_ds = load_data((227, 227), './new_data_transformed/train', './new_data_transformed/test')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "se5yACYzZcmm"
},
"outputs": [],
"source": [
"def tensorboard_callback(model_name):\n",
" return keras.callbacks.TensorBoard(os.path.join(f\"./logs/{model_name}\", time.strftime(\"run_%Y_%m_%d-%H_%M_%S\")))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yvRRfMKbcTwu"
},
"outputs": [],
"source": [
"model = keras.models.Sequential([\n",
" keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=(227,227,3)),\n",
" keras.layers.BatchNormalization(),\n",
" keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),\n",
" keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), activation='relu', padding=\"same\"),\n",
" keras.layers.BatchNormalization(),\n",
" keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),\n",
" keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding=\"same\"),\n",
" keras.layers.BatchNormalization(),\n",
" keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding=\"same\"),\n",
" keras.layers.BatchNormalization(),\n",
" keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding=\"same\"),\n",
" keras.layers.BatchNormalization(),\n",
" keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),\n",
" keras.layers.Flatten(),\n",
" keras.layers.Dense(4096, activation='relu'),\n",
" keras.layers.Dense(4096, activation='relu'),\n",
" keras.layers.Dense(6, activation='softmax')\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eRI-u6HLcU_H",
"outputId": "7ea0a139-c9ab-4092-e082-bea02eca4e93"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" conv2d (Conv2D) (None, 55, 55, 96) 34944 \n",
" \n",
" batch_normalization (BatchN (None, 55, 55, 96) 384 \n",
" ormalization) \n",
" \n",
" max_pooling2d (MaxPooling2D (None, 27, 27, 96) 0 \n",
" ) \n",
" \n",
" conv2d_1 (Conv2D) (None, 27, 27, 256) 614656 \n",
" \n",
" batch_normalization_1 (Batc (None, 27, 27, 256) 1024 \n",
" hNormalization) \n",
" \n",
" max_pooling2d_1 (MaxPooling (None, 13, 13, 256) 0 \n",
" 2D) \n",
" \n",
" conv2d_2 (Conv2D) (None, 13, 13, 384) 885120 \n",
" \n",
" batch_normalization_2 (Batc (None, 13, 13, 384) 1536 \n",
" hNormalization) \n",
" \n",
" conv2d_3 (Conv2D) (None, 13, 13, 384) 1327488 \n",
" \n",
" batch_normalization_3 (Batc (None, 13, 13, 384) 1536 \n",
" hNormalization) \n",
" \n",
" conv2d_4 (Conv2D) (None, 13, 13, 256) 884992 \n",
" \n",
" batch_normalization_4 (Batc (None, 13, 13, 256) 1024 \n",
" hNormalization) \n",
" \n",
" max_pooling2d_2 (MaxPooling (None, 6, 6, 256) 0 \n",
" 2D) \n",
" \n",
" flatten (Flatten) (None, 9216) 0 \n",
" \n",
" dense (Dense) (None, 4096) 37752832 \n",
" \n",
" dense_1 (Dense) (None, 4096) 16781312 \n",
" \n",
" dense_2 (Dense) (None, 6) 24582 \n",
" \n",
"=================================================================\n",
"Total params: 58,311,430\n",
"Trainable params: 58,308,678\n",
"Non-trainable params: 2,752\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.compile(loss='sparse_categorical_crossentropy', optimizer=tf.optimizers.SGD(learning_rate=.001), metrics=['accuracy'])\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Jxzfxvy3cWBP",
"outputId": "fa3b738e-a125-4b19-984a-4b1d83df771f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/50\n",
"45/45 [==============================] - 13s 82ms/step - loss: 1.5723 - accuracy: 0.4535 - val_loss: 1.8094 - val_accuracy: 0.1676\n",
"Epoch 2/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.8888 - accuracy: 0.6771 - val_loss: 1.8356 - val_accuracy: 0.1733\n",
"Epoch 3/50\n",
"45/45 [==============================] - 3s 62ms/step - loss: 0.7100 - accuracy: 0.7292 - val_loss: 1.8753 - val_accuracy: 0.1534\n",
"Epoch 4/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.5200 - accuracy: 0.8222 - val_loss: 1.9177 - val_accuracy: 0.1477\n",
"Epoch 5/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.3887 - accuracy: 0.8868 - val_loss: 2.0252 - val_accuracy: 0.1591\n",
"Epoch 6/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.2841 - accuracy: 0.9340 - val_loss: 2.0583 - val_accuracy: 0.2273\n",
"Epoch 7/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.2114 - accuracy: 0.9569 - val_loss: 2.1366 - val_accuracy: 0.2216\n",
"Epoch 8/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.1738 - accuracy: 0.9764 - val_loss: 2.1499 - val_accuracy: 0.3153\n",
"Epoch 9/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.1298 - accuracy: 0.9854 - val_loss: 2.0241 - val_accuracy: 0.3693\n",
"Epoch 10/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.1152 - accuracy: 0.9896 - val_loss: 1.5216 - val_accuracy: 0.4631\n",
"Epoch 11/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0893 - accuracy: 0.9958 - val_loss: 1.2411 - val_accuracy: 0.5511\n",
"Epoch 12/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0922 - accuracy: 0.9910 - val_loss: 0.9918 - val_accuracy: 0.6477\n",
"Epoch 13/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0665 - accuracy: 0.9972 - val_loss: 0.9535 - val_accuracy: 0.6648\n",
"Epoch 14/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0628 - accuracy: 0.9965 - val_loss: 0.7136 - val_accuracy: 0.7415\n",
"Epoch 15/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0624 - accuracy: 0.9972 - val_loss: 0.6692 - val_accuracy: 0.7642\n",
"Epoch 16/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0534 - accuracy: 0.9979 - val_loss: 0.6320 - val_accuracy: 0.7898\n",
"Epoch 17/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0436 - accuracy: 0.9993 - val_loss: 0.6613 - val_accuracy: 0.7841\n",
"Epoch 18/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0401 - accuracy: 0.9972 - val_loss: 0.6103 - val_accuracy: 0.7869\n",
"Epoch 19/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0314 - accuracy: 0.9993 - val_loss: 0.6222 - val_accuracy: 0.8040\n",
"Epoch 20/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0308 - accuracy: 0.9986 - val_loss: 0.6182 - val_accuracy: 0.7841\n",
"Epoch 21/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0309 - accuracy: 0.9993 - val_loss: 0.6246 - val_accuracy: 0.7926\n",
"Epoch 22/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0256 - accuracy: 1.0000 - val_loss: 0.6276 - val_accuracy: 0.7983\n",
"Epoch 23/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0259 - accuracy: 1.0000 - val_loss: 0.6332 - val_accuracy: 0.8011\n",
"Epoch 24/50\n",
"45/45 [==============================] - 3s 62ms/step - loss: 0.0223 - accuracy: 1.0000 - val_loss: 0.6229 - val_accuracy: 0.7955\n",
"Epoch 25/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0225 - accuracy: 1.0000 - val_loss: 0.6089 - val_accuracy: 0.8011\n",
"Epoch 26/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0217 - accuracy: 1.0000 - val_loss: 0.6463 - val_accuracy: 0.7926\n",
"Epoch 27/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0221 - accuracy: 1.0000 - val_loss: 0.6237 - val_accuracy: 0.8068\n",
"Epoch 28/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0196 - accuracy: 1.0000 - val_loss: 0.6484 - val_accuracy: 0.7898\n",
"Epoch 29/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0204 - accuracy: 0.9993 - val_loss: 0.6200 - val_accuracy: 0.7926\n",
"Epoch 30/50\n",
"45/45 [==============================] - 3s 60ms/step - loss: 0.0194 - accuracy: 0.9993 - val_loss: 0.6186 - val_accuracy: 0.8040\n",
"Epoch 31/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0171 - accuracy: 1.0000 - val_loss: 0.6418 - val_accuracy: 0.8068\n",
"Epoch 32/50\n",
"45/45 [==============================] - 3s 62ms/step - loss: 0.0172 - accuracy: 1.0000 - val_loss: 0.6234 - val_accuracy: 0.8011\n",
"Epoch 33/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0168 - accuracy: 1.0000 - val_loss: 0.6278 - val_accuracy: 0.8068\n",
"Epoch 34/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0153 - accuracy: 1.0000 - val_loss: 0.6527 - val_accuracy: 0.7898\n",
"Epoch 35/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0138 - accuracy: 1.0000 - val_loss: 0.6198 - val_accuracy: 0.7926\n",
"Epoch 36/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0130 - accuracy: 1.0000 - val_loss: 0.6359 - val_accuracy: 0.7869\n",
"Epoch 37/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0130 - accuracy: 1.0000 - val_loss: 0.6123 - val_accuracy: 0.8153\n",
"Epoch 38/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0116 - accuracy: 1.0000 - val_loss: 0.6061 - val_accuracy: 0.8040\n",
"Epoch 39/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0113 - accuracy: 1.0000 - val_loss: 0.6256 - val_accuracy: 0.8011\n",
"Epoch 40/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0142 - accuracy: 0.9993 - val_loss: 0.6386 - val_accuracy: 0.8011\n",
"Epoch 41/50\n",
"45/45 [==============================] - 3s 62ms/step - loss: 0.0119 - accuracy: 1.0000 - val_loss: 0.6137 - val_accuracy: 0.8040\n",
"Epoch 42/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0100 - accuracy: 1.0000 - val_loss: 0.6392 - val_accuracy: 0.8068\n",
"Epoch 43/50\n",
"45/45 [==============================] - 3s 62ms/step - loss: 0.0098 - accuracy: 1.0000 - val_loss: 0.6461 - val_accuracy: 0.8097\n",
"Epoch 44/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0113 - accuracy: 1.0000 - val_loss: 0.6131 - val_accuracy: 0.8125\n",
"Epoch 45/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0095 - accuracy: 1.0000 - val_loss: 0.6376 - val_accuracy: 0.8125\n",
"Epoch 46/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0117 - accuracy: 0.9986 - val_loss: 0.6414 - val_accuracy: 0.7841\n",
"Epoch 47/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0094 - accuracy: 1.0000 - val_loss: 0.6224 - val_accuracy: 0.8068\n",
"Epoch 48/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0095 - accuracy: 1.0000 - val_loss: 0.5973 - val_accuracy: 0.8153\n",
"Epoch 49/50\n",
"45/45 [==============================] - 3s 62ms/step - loss: 0.0088 - accuracy: 1.0000 - val_loss: 0.6366 - val_accuracy: 0.8068\n",
"Epoch 50/50\n",
"45/45 [==============================] - 3s 61ms/step - loss: 0.0093 - accuracy: 1.0000 - val_loss: 0.6526 - val_accuracy: 0.8040\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7efd9022ee80>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(train_ds,\n",
" epochs=50,\n",
" validation_data=validation_ds,\n",
" validation_freq=1,\n",
" callbacks=[tensorboard_callback(\"AlexNet\")])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hrbrA2t3Zv4d",
"outputId": "6874f3db-eacb-4c69-ed72-6616db55303d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"***** TensorBoard Uploader *****\n",
"\n",
"This will upload your TensorBoard logs to https://tensorboard.dev/ from\n",
"the following directory:\n",
"\n",
"logs/AlexNet\n",
"\n",
"This TensorBoard will be visible to everyone. Do not upload sensitive\n",
"data.\n",
"\n",
"Your use of this service is subject to Google's Terms of Service\n",
"<https://policies.google.com/terms> and Privacy Policy\n",
"<https://policies.google.com/privacy>, and TensorBoard.dev's Terms of Service\n",
"<https://tensorboard.dev/policy/terms/>.\n",
"\n",
"This notice will not be shown again while you are logged into the uploader.\n",
"To log out, run `tensorboard dev auth revoke`.\n",
"\n",
"Continue? (yes/NO) yes\n",
"\n",
"Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=373649185512-8v619h5kft38l4456nm2dj4ubeqsrvh6.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&state=4s45OaCdh7CXhhMYUBHIgm6u6oiMTE&prompt=consent&access_type=offline\n",
"Enter the authorization code: 4/1AWtgzh59_rKzQT7gGHgAyOnRtMf7ppSZuYlb-25UkSRY4IdlSjRmYmH_AfE\n",
"\n",
"Upload started and will continue reading any new data as it's added to the logdir.\n",
"\n",
"To stop uploading, press Ctrl-C.\n",
"\n",
"New experiment created. View your TensorBoard at: https://tensorboard.dev/experiment/0xVh6RMqTQiv0BCdbdLLBg/\n",
"\n",
"\u001b[1m[2023-02-01T08:10:10]\u001b[0m Started scanning logdir.\n",
"\u001b[1m[2023-02-01T08:10:11]\u001b[0m Total uploaded: 300 scalars, 0 tensors, 1 binary objects (75.6 kB)\n",
"\n",
"\n",
"Interrupted. View your TensorBoard at https://tensorboard.dev/experiment/0xVh6RMqTQiv0BCdbdLLBg/\n",
"Traceback (most recent call last):\n",
" File \"/usr/local/bin/tensorboard\", line 8, in <module>\n",
" sys.exit(run_main())\n",
" File \"/usr/local/lib/python3.8/dist-packages/tensorboard/main.py\", line 46, in run_main\n",
" app.run(tensorboard.main, flags_parser=tensorboard.configure)\n",
" File \"/usr/local/lib/python3.8/dist-packages/absl/app.py\", line 308, in run\n",
" _run_main(main, args)\n",
" File \"/usr/local/lib/python3.8/dist-packages/absl/app.py\", line 254, in _run_main\n",
" sys.exit(main(argv))\n",
" File \"/usr/local/lib/python3.8/dist-packages/tensorboard/program.py\", line 276, in main\n",
" return runner(self.flags) or 0\n",
" File \"/usr/local/lib/python3.8/dist-packages/tensorboard/uploader/uploader_subcommand.py\", line 692, in run\n",
" return _run(flags, self._experiment_url_callback)\n",
" File \"/usr/local/lib/python3.8/dist-packages/tensorboard/uploader/uploader_subcommand.py\", line 125, in _run\n",
" intent.execute(server_info, channel)\n",
" File \"/usr/local/lib/python3.8/dist-packages/tensorboard/uploader/uploader_subcommand.py\", line 508, in execute\n",
" sys.stdout.write(end_message + \"\\n\")\n",
"KeyboardInterrupt\n",
"^C\n"
]
}
],
"source": [
"!tensorboard dev upload --logdir logs/AlexNet --name AlexNetFish"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VdNLX57nc_Gs",
"outputId": "4353b693-12cd-45ac-f249-8b287281754b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9/9 [==============================] - 0s 32ms/step - loss: 0.9122 - accuracy: 0.7188\n"
]
},
{
"data": {
"text/plain": [
"[0.9122310280799866, 0.71875]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.evaluate(test_ds)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"id": "aaPsyfODLBgo",
"outputId": "7a1c3dd3-a662-4e6d-9cba-28aef9067d73"
},
"outputs": [],
"source": [
"from training.model_freeze import freeze_model\n",
"\n",
"freeze_model(model, './frozen_models', \"frozen_alex_net\")"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"machine_shape": "hm",
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "um",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:41:22) [MSC v.1929 64 bit (AMD64)]"
},
"vscode": {
"interpreter": {
"hash": "876e189cbbe99a9a838ece62aae1013186c4bb7e0254a10cfa2f9b2381853efb"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}