widzenie-komputerowe-projekt/training/AlexNet.ipynb
2023-02-01 18:42:47 +01:00

23 KiB
Raw Blame History

from IPython.display import Image, SVG, display
from tqdm import tqdm
import matplotlib.pyplot as plt
import sys
import subprocess
import pkg_resources
import numpy as np
import pandas as pd
import os
from skimage.io import imread
import cv2 as cv
from pathlib import Path
import random
from shutil import copyfile, rmtree
import json
import seaborn as sns
import matplotlib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import tensorflow as tf
from tensorflow import keras
import os
import time
from training.data_load import load_data

train_ds, test_ds, validation_ds = load_data((227, 227), './new_data_transformed/train', './new_data_transformed/test')
def tensorboard_callback(model_name):
    return keras.callbacks.TensorBoard(os.path.join(f"./logs/{model_name}", time.strftime("run_%Y_%m_%d-%H_%M_%S")))
model = keras.models.Sequential([
    keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=(227,227,3)),
    keras.layers.BatchNormalization(),
    keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
    keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
    keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
    keras.layers.Flatten(),
    keras.layers.Dense(4096, activation='relu'),
    keras.layers.Dense(4096, activation='relu'),
    keras.layers.Dense(6, activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy', optimizer=tf.optimizers.SGD(learning_rate=.001), metrics=['accuracy'])
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 55, 55, 96)        34944     
                                                                 
 batch_normalization (BatchN  (None, 55, 55, 96)       384       
 ormalization)                                                   
                                                                 
 max_pooling2d (MaxPooling2D  (None, 27, 27, 96)       0         
 )                                                               
                                                                 
 conv2d_1 (Conv2D)           (None, 27, 27, 256)       614656    
                                                                 
 batch_normalization_1 (Batc  (None, 27, 27, 256)      1024      
 hNormalization)                                                 
                                                                 
 max_pooling2d_1 (MaxPooling  (None, 13, 13, 256)      0         
 2D)                                                             
                                                                 
 conv2d_2 (Conv2D)           (None, 13, 13, 384)       885120    
                                                                 
 batch_normalization_2 (Batc  (None, 13, 13, 384)      1536      
 hNormalization)                                                 
                                                                 
 conv2d_3 (Conv2D)           (None, 13, 13, 384)       1327488   
                                                                 
 batch_normalization_3 (Batc  (None, 13, 13, 384)      1536      
 hNormalization)                                                 
                                                                 
 conv2d_4 (Conv2D)           (None, 13, 13, 256)       884992    
                                                                 
 batch_normalization_4 (Batc  (None, 13, 13, 256)      1024      
 hNormalization)                                                 
                                                                 
 max_pooling2d_2 (MaxPooling  (None, 6, 6, 256)        0         
 2D)                                                             
                                                                 
 flatten (Flatten)           (None, 9216)              0         
                                                                 
 dense (Dense)               (None, 4096)              37752832  
                                                                 
 dense_1 (Dense)             (None, 4096)              16781312  
                                                                 
 dense_2 (Dense)             (None, 6)                 24582     
                                                                 
=================================================================
Total params: 58,311,430
Trainable params: 58,308,678
Non-trainable params: 2,752
_________________________________________________________________
model.fit(train_ds,
          epochs=50,
          validation_data=validation_ds,
          validation_freq=1,
          callbacks=[tensorboard_callback("AlexNet")])
Epoch 1/50
45/45 [==============================] - 13s 82ms/step - loss: 1.5723 - accuracy: 0.4535 - val_loss: 1.8094 - val_accuracy: 0.1676
Epoch 2/50
45/45 [==============================] - 3s 60ms/step - loss: 0.8888 - accuracy: 0.6771 - val_loss: 1.8356 - val_accuracy: 0.1733
Epoch 3/50
45/45 [==============================] - 3s 62ms/step - loss: 0.7100 - accuracy: 0.7292 - val_loss: 1.8753 - val_accuracy: 0.1534
Epoch 4/50
45/45 [==============================] - 3s 61ms/step - loss: 0.5200 - accuracy: 0.8222 - val_loss: 1.9177 - val_accuracy: 0.1477
Epoch 5/50
45/45 [==============================] - 3s 60ms/step - loss: 0.3887 - accuracy: 0.8868 - val_loss: 2.0252 - val_accuracy: 0.1591
Epoch 6/50
45/45 [==============================] - 3s 60ms/step - loss: 0.2841 - accuracy: 0.9340 - val_loss: 2.0583 - val_accuracy: 0.2273
Epoch 7/50
45/45 [==============================] - 3s 60ms/step - loss: 0.2114 - accuracy: 0.9569 - val_loss: 2.1366 - val_accuracy: 0.2216
Epoch 8/50
45/45 [==============================] - 3s 60ms/step - loss: 0.1738 - accuracy: 0.9764 - val_loss: 2.1499 - val_accuracy: 0.3153
Epoch 9/50
45/45 [==============================] - 3s 60ms/step - loss: 0.1298 - accuracy: 0.9854 - val_loss: 2.0241 - val_accuracy: 0.3693
Epoch 10/50
45/45 [==============================] - 3s 60ms/step - loss: 0.1152 - accuracy: 0.9896 - val_loss: 1.5216 - val_accuracy: 0.4631
Epoch 11/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0893 - accuracy: 0.9958 - val_loss: 1.2411 - val_accuracy: 0.5511
Epoch 12/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0922 - accuracy: 0.9910 - val_loss: 0.9918 - val_accuracy: 0.6477
Epoch 13/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0665 - accuracy: 0.9972 - val_loss: 0.9535 - val_accuracy: 0.6648
Epoch 14/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0628 - accuracy: 0.9965 - val_loss: 0.7136 - val_accuracy: 0.7415
Epoch 15/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0624 - accuracy: 0.9972 - val_loss: 0.6692 - val_accuracy: 0.7642
Epoch 16/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0534 - accuracy: 0.9979 - val_loss: 0.6320 - val_accuracy: 0.7898
Epoch 17/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0436 - accuracy: 0.9993 - val_loss: 0.6613 - val_accuracy: 0.7841
Epoch 18/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0401 - accuracy: 0.9972 - val_loss: 0.6103 - val_accuracy: 0.7869
Epoch 19/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0314 - accuracy: 0.9993 - val_loss: 0.6222 - val_accuracy: 0.8040
Epoch 20/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0308 - accuracy: 0.9986 - val_loss: 0.6182 - val_accuracy: 0.7841
Epoch 21/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0309 - accuracy: 0.9993 - val_loss: 0.6246 - val_accuracy: 0.7926
Epoch 22/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0256 - accuracy: 1.0000 - val_loss: 0.6276 - val_accuracy: 0.7983
Epoch 23/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0259 - accuracy: 1.0000 - val_loss: 0.6332 - val_accuracy: 0.8011
Epoch 24/50
45/45 [==============================] - 3s 62ms/step - loss: 0.0223 - accuracy: 1.0000 - val_loss: 0.6229 - val_accuracy: 0.7955
Epoch 25/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0225 - accuracy: 1.0000 - val_loss: 0.6089 - val_accuracy: 0.8011
Epoch 26/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0217 - accuracy: 1.0000 - val_loss: 0.6463 - val_accuracy: 0.7926
Epoch 27/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0221 - accuracy: 1.0000 - val_loss: 0.6237 - val_accuracy: 0.8068
Epoch 28/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0196 - accuracy: 1.0000 - val_loss: 0.6484 - val_accuracy: 0.7898
Epoch 29/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0204 - accuracy: 0.9993 - val_loss: 0.6200 - val_accuracy: 0.7926
Epoch 30/50
45/45 [==============================] - 3s 60ms/step - loss: 0.0194 - accuracy: 0.9993 - val_loss: 0.6186 - val_accuracy: 0.8040
Epoch 31/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0171 - accuracy: 1.0000 - val_loss: 0.6418 - val_accuracy: 0.8068
Epoch 32/50
45/45 [==============================] - 3s 62ms/step - loss: 0.0172 - accuracy: 1.0000 - val_loss: 0.6234 - val_accuracy: 0.8011
Epoch 33/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0168 - accuracy: 1.0000 - val_loss: 0.6278 - val_accuracy: 0.8068
Epoch 34/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0153 - accuracy: 1.0000 - val_loss: 0.6527 - val_accuracy: 0.7898
Epoch 35/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0138 - accuracy: 1.0000 - val_loss: 0.6198 - val_accuracy: 0.7926
Epoch 36/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0130 - accuracy: 1.0000 - val_loss: 0.6359 - val_accuracy: 0.7869
Epoch 37/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0130 - accuracy: 1.0000 - val_loss: 0.6123 - val_accuracy: 0.8153
Epoch 38/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0116 - accuracy: 1.0000 - val_loss: 0.6061 - val_accuracy: 0.8040
Epoch 39/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0113 - accuracy: 1.0000 - val_loss: 0.6256 - val_accuracy: 0.8011
Epoch 40/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0142 - accuracy: 0.9993 - val_loss: 0.6386 - val_accuracy: 0.8011
Epoch 41/50
45/45 [==============================] - 3s 62ms/step - loss: 0.0119 - accuracy: 1.0000 - val_loss: 0.6137 - val_accuracy: 0.8040
Epoch 42/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0100 - accuracy: 1.0000 - val_loss: 0.6392 - val_accuracy: 0.8068
Epoch 43/50
45/45 [==============================] - 3s 62ms/step - loss: 0.0098 - accuracy: 1.0000 - val_loss: 0.6461 - val_accuracy: 0.8097
Epoch 44/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0113 - accuracy: 1.0000 - val_loss: 0.6131 - val_accuracy: 0.8125
Epoch 45/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0095 - accuracy: 1.0000 - val_loss: 0.6376 - val_accuracy: 0.8125
Epoch 46/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0117 - accuracy: 0.9986 - val_loss: 0.6414 - val_accuracy: 0.7841
Epoch 47/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0094 - accuracy: 1.0000 - val_loss: 0.6224 - val_accuracy: 0.8068
Epoch 48/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0095 - accuracy: 1.0000 - val_loss: 0.5973 - val_accuracy: 0.8153
Epoch 49/50
45/45 [==============================] - 3s 62ms/step - loss: 0.0088 - accuracy: 1.0000 - val_loss: 0.6366 - val_accuracy: 0.8068
Epoch 50/50
45/45 [==============================] - 3s 61ms/step - loss: 0.0093 - accuracy: 1.0000 - val_loss: 0.6526 - val_accuracy: 0.8040
<keras.callbacks.History at 0x7efd9022ee80>
!tensorboard dev upload --logdir logs/AlexNet --name AlexNetFish
***** TensorBoard Uploader *****

This will upload your TensorBoard logs to https://tensorboard.dev/ from
the following directory:

logs/AlexNet

This TensorBoard will be visible to everyone. Do not upload sensitive
data.

Your use of this service is subject to Google's Terms of Service
<https://policies.google.com/terms> and Privacy Policy
<https://policies.google.com/privacy>, and TensorBoard.dev's Terms of Service
<https://tensorboard.dev/policy/terms/>.

This notice will not be shown again while you are logged into the uploader.
To log out, run `tensorboard dev auth revoke`.

Continue? (yes/NO) yes

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
Enter the authorization code: 4/1AWtgzh59_rKzQT7gGHgAyOnRtMf7ppSZuYlb-25UkSRY4IdlSjRmYmH_AfE

Upload started and will continue reading any new data as it's added to the logdir.

To stop uploading, press Ctrl-C.

New experiment created. View your TensorBoard at: https://tensorboard.dev/experiment/0xVh6RMqTQiv0BCdbdLLBg/

[2023-02-01T08:10:10] Started scanning logdir.
[2023-02-01T08:10:11] Total uploaded: 300 scalars, 0 tensors, 1 binary objects (75.6 kB)


Interrupted. View your TensorBoard at https://tensorboard.dev/experiment/0xVh6RMqTQiv0BCdbdLLBg/
Traceback (most recent call last):
  File "/usr/local/bin/tensorboard", line 8, in <module>
    sys.exit(run_main())
  File "/usr/local/lib/python3.8/dist-packages/tensorboard/main.py", line 46, in run_main
    app.run(tensorboard.main, flags_parser=tensorboard.configure)
  File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 308, in run
    _run_main(main, args)
  File "/usr/local/lib/python3.8/dist-packages/absl/app.py", line 254, in _run_main
    sys.exit(main(argv))
  File "/usr/local/lib/python3.8/dist-packages/tensorboard/program.py", line 276, in main
    return runner(self.flags) or 0
  File "/usr/local/lib/python3.8/dist-packages/tensorboard/uploader/uploader_subcommand.py", line 692, in run
    return _run(flags, self._experiment_url_callback)
  File "/usr/local/lib/python3.8/dist-packages/tensorboard/uploader/uploader_subcommand.py", line 125, in _run
    intent.execute(server_info, channel)
  File "/usr/local/lib/python3.8/dist-packages/tensorboard/uploader/uploader_subcommand.py", line 508, in execute
    sys.stdout.write(end_message + "\n")
KeyboardInterrupt
^C
model.evaluate(test_ds)
9/9 [==============================] - 0s 32ms/step - loss: 0.9122 - accuracy: 0.7188
[0.9122310280799866, 0.71875]
from training.model_freeze import freeze_model

freeze_model(model, './frozen_models', "frozen_alex_net")