Open In Colab

Student Name : Sulthan A. Karimov

Username : sulthankarimov

Email : [email protected]

!pip install split-folders[full] matplotlib jupyter_http_over_ws imutils
Requirement already satisfied: matplotlib in /usr/local/lib/python3.11/dist-packages (3.8.3)
Requirement already satisfied: jupyter_http_over_ws in /usr/local/lib/python3.11/dist-packages (0.0.8)
Requirement already satisfied: imutils in /usr/local/lib/python3.11/dist-packages (0.5.4)
Requirement already satisfied: split-folders[full] in /usr/local/lib/python3.11/dist-packages (0.5.1)
Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from split-folders[full]) (4.66.4)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (4.49.0)
Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (1.4.5)
Requirement already satisfied: numpy<2,>=1.21 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (1.26.4)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (23.2)
Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (10.2.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/lib/python3/dist-packages (from matplotlib) (2.4.7)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.11/dist-packages (from matplotlib) (2.9.0.post0)
Requirement already satisfied: notebook>=5.0 in /usr/local/lib/python3.11/dist-packages (from jupyter_http_over_ws) (7.1.1)
Requirement already satisfied: six>=1.6.0 in /usr/lib/python3/dist-packages (from jupyter_http_over_ws) (1.16.0)
Requirement already satisfied: tornado>=4.5 in /usr/local/lib/python3.11/dist-packages (from jupyter_http_over_ws) (6.4)
Requirement already satisfied: jupyter-server<3,>=2.4.0 in /usr/local/lib/python3.11/dist-packages (from notebook>=5.0->jupyter_http_over_ws) (2.13.0)
Requirement already satisfied: jupyterlab-server<3,>=2.22.1 in /usr/local/lib/python3.11/dist-packages (from notebook>=5.0->jupyter_http_over_ws) (2.25.3)
Requirement already satisfied: jupyterlab<4.2,>=4.1.1 in /usr/local/lib/python3.11/dist-packages (from notebook>=5.0->jupyter_http_over_ws) (4.1.4)
Requirement already satisfied: notebook-shim<0.3,>=0.2 in /usr/local/lib/python3.11/dist-packages (from notebook>=5.0->jupyter_http_over_ws) (0.2.4)
Requirement already satisfied: anyio>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (4.3.0)
Requirement already satisfied: argon2-cffi in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (23.1.0)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (3.1.3)
Requirement already satisfied: jupyter-client>=7.4.4 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (8.6.0)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (5.7.1)
Requirement already satisfied: jupyter-events>=0.9.0 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.9.0)
Requirement already satisfied: jupyter-server-terminals in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.5.2)
Requirement already satisfied: nbconvert>=6.4.4 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (7.16.2)
Requirement already satisfied: nbformat>=5.3.0 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (5.9.2)
Requirement already satisfied: overrides in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (7.7.0)
Requirement already satisfied: prometheus-client in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.20.0)
Requirement already satisfied: pyzmq>=24 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (25.1.2)
Requirement already satisfied: send2trash>=1.8.2 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.8.2)
Requirement already satisfied: terminado>=0.8.3 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.18.0)
Requirement already satisfied: traitlets>=5.6.0 in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (5.14.1)
Requirement already satisfied: websocket-client in /usr/local/lib/python3.11/dist-packages (from jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.7.0)
Requirement already satisfied: async-lru>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (2.0.4)
Requirement already satisfied: httpx>=0.25.0 in /usr/local/lib/python3.11/dist-packages (from jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.27.0)
Requirement already satisfied: ipykernel in /usr/local/lib/python3.11/dist-packages (from jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (6.29.3)
Requirement already satisfied: jupyter-lsp>=2.0.0 in /usr/local/lib/python3.11/dist-packages (from jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (2.2.4)
Requirement already satisfied: babel>=2.10 in /usr/local/lib/python3.11/dist-packages (from jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (2.14.0)
Requirement already satisfied: json5>=0.9.0 in /usr/local/lib/python3.11/dist-packages (from jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (0.9.22)
Requirement already satisfied: jsonschema>=4.18.0 in /usr/local/lib/python3.11/dist-packages (from jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (4.21.1)
Requirement already satisfied: requests>=2.31 in /usr/local/lib/python3.11/dist-packages (from jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (2.31.0)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio>=3.1.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (3.6)
Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio>=3.1.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.3.1)
Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.0->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (2024.2.2)
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.0->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (1.0.4)
Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.25.0->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.14.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.1.5)
Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (23.2.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (2023.12.1)
Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (0.33.0)
Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.11/dist-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (0.18.0)
Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.11/dist-packages (from jupyter-core!=5.0.*,>=4.12->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (4.2.0)
Requirement already satisfied: python-json-logger>=2.0.4 in /usr/local/lib/python3.11/dist-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.0.7)
Requirement already satisfied: pyyaml>=5.3 in /usr/local/lib/python3.11/dist-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (6.0.1)
Requirement already satisfied: rfc3339-validator in /usr/local/lib/python3.11/dist-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.1.4)
Requirement already satisfied: rfc3986-validator>=0.1.1 in /usr/local/lib/python3.11/dist-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.1.1)
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (4.12.3)
Requirement already satisfied: bleach!=5.0.0 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (6.1.0)
Requirement already satisfied: defusedxml in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.7.1)
Requirement already satisfied: jupyterlab-pygments in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.3.0)
Requirement already satisfied: mistune<4,>=2.0.3 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (3.0.2)
Requirement already satisfied: nbclient>=0.5.0 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.9.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.5.1)
Requirement already satisfied: pygments>=2.4.1 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.17.2)
Requirement already satisfied: tinycss2 in /usr/local/lib/python3.11/dist-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.2.1)
Requirement already satisfied: fastjsonschema in /usr/local/lib/python3.11/dist-packages (from nbformat>=5.3.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.19.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests>=2.31->jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (3.3.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests>=2.31->jupyterlab-server<3,>=2.22.1->notebook>=5.0->jupyter_http_over_ws) (2.2.1)
Requirement already satisfied: ptyprocess in /usr/local/lib/python3.11/dist-packages (from terminado>=0.8.3->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.7.0)
Requirement already satisfied: argon2-cffi-bindings in /usr/local/lib/python3.11/dist-packages (from argon2-cffi->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (21.2.0)
Requirement already satisfied: comm>=0.1.1 in /usr/local/lib/python3.11/dist-packages (from ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.2.1)
Requirement already satisfied: debugpy>=1.6.5 in /usr/local/lib/python3.11/dist-packages (from ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (1.8.1)
Requirement already satisfied: ipython>=7.23.1 in /usr/local/lib/python3.11/dist-packages (from ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (8.22.2)
Requirement already satisfied: matplotlib-inline>=0.1 in /usr/local/lib/python3.11/dist-packages (from ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.1.6)
Requirement already satisfied: nest-asyncio in /usr/local/lib/python3.11/dist-packages (from ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (1.6.0)
Requirement already satisfied: psutil in /usr/local/lib/python3.11/dist-packages (from ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (5.9.8)
Requirement already satisfied: webencodings in /usr/local/lib/python3.11/dist-packages (from bleach!=5.0.0->nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (0.5.1)
Requirement already satisfied: decorator in /usr/local/lib/python3.11/dist-packages (from ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /usr/local/lib/python3.11/dist-packages (from ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.19.1)
Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /usr/local/lib/python3.11/dist-packages (from ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (3.0.43)
Requirement already satisfied: stack-data in /usr/local/lib/python3.11/dist-packages (from ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.6.3)
Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.11/dist-packages (from ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (4.9.0)
Requirement already satisfied: fqdn in /usr/local/lib/python3.11/dist-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.5.1)
Requirement already satisfied: isoduration in /usr/local/lib/python3.11/dist-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (20.11.0)
Requirement already satisfied: jsonpointer>1.13 in /usr/local/lib/python3.11/dist-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.4)
Requirement already satisfied: uri-template in /usr/local/lib/python3.11/dist-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.3.0)
Requirement already satisfied: webcolors>=1.11 in /usr/local/lib/python3.11/dist-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.13)
Requirement already satisfied: cffi>=1.0.1 in /usr/local/lib/python3.11/dist-packages (from argon2-cffi-bindings->argon2-cffi->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.16.0)
Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.11/dist-packages (from beautifulsoup4->nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.5)
Requirement already satisfied: pycparser in /usr/local/lib/python3.11/dist-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.21)
Requirement already satisfied: parso<0.9.0,>=0.8.3 in /usr/local/lib/python3.11/dist-packages (from jedi>=0.16->ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.8.3)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.11/dist-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.2.13)
Requirement already satisfied: arrow>=0.15.0 in /usr/local/lib/python3.11/dist-packages (from isoduration->jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (1.3.0)
Requirement already satisfied: executing>=1.2.0 in /usr/local/lib/python3.11/dist-packages (from stack-data->ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (2.0.1)
Requirement already satisfied: asttokens>=2.1.0 in /usr/local/lib/python3.11/dist-packages (from stack-data->ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (2.4.1)
Requirement already satisfied: pure-eval in /usr/local/lib/python3.11/dist-packages (from stack-data->ipython>=7.23.1->ipykernel->jupyterlab<4.2,>=4.1.1->notebook>=5.0->jupyter_http_over_ws) (0.2.2)
Requirement already satisfied: types-python-dateutil>=2.8.10 in /usr/local/lib/python3.11/dist-packages (from arrow>=0.15.0->isoduration->jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook>=5.0->jupyter_http_over_ws) (2.8.19.20240106)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
# import all needed libraries
import zipfile, os, shutil, splitfolders, re, random
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

from imutils import paths
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.backend import clear_session
from sklearn.metrics import classification_report
2024-06-13 06:38:22.014324: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
# defining directories
local_dir = 'datasets/images/'
dataset_name = 'rockpaperscissors'
local_data = local_dir + dataset_name
local_zip = local_data + '.zip'
# dowload dataset (pass if exist)
!test -f $local_zip || wget --no-check-certificate \
 https://github.com/dicodingacademy/assets/releases/download/release/rockpaperscissors.zip \
 -O $local_zip
# extract dataset
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall(local_dir)
zip_ref.close()
# prepare train & validation split
shutil.rmtree(local_data + '/rps-cv-images')
os.remove(local_data + '/README_rpc-cv-images.txt')
print(os.listdir(local_data))

base_dir = local_dir + '/rps'
if os.path.exists(base_dir) == True:
  shutil.rmtree(base_dir)

splitfolders.ratio(local_data, base_dir, ratio=(.6,.4))
train_dir = os.path.join(base_dir, 'train')
print('amount of training sample : ', sum(len(files) for _, _, files in os.walk(re.escape(base_dir) + r'/train')))
validation_dir = os.path.join(base_dir, 'val')
print('amount of validation sample : ', sum(len(files) for _, _, files in os.walk(re.escape(base_dir) + r'/val')))
['paper', 'rock', 'scissors']
amount of training sample :  1312
amount of validation sample :  876
Copying files: 2188 files [00:00, 5302.33 files/s]
# prepare test folder for classification report
files_list = []

for root, dirs, files in os.walk(local_data):
  for dir in dirs:
    results = os.walk(local_data + '/' + dir)
    for result in results:
      cat_list = []
      for file in result[2]:
        if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
          cat_list.append(os.path.join(root + '/' + dir, file))
    files_list.append(cat_list)

for imgs in files_list:
  category = re.findall('(?<=rs\/).*?(?=\/)', imgs[0])[0]
  filesToCopy = random.sample(imgs, 4)
  destPath = base_dir + '/test/' + str(category)
  if os.path.isdir(destPath) == False:
    os.makedirs(destPath)
  for file in filesToCopy:
    shutil.copy(file, destPath)

test_dir = os.path.join(base_dir, 'test')
# directory summary
print(os.listdir(train_dir))
print(os.listdir(validation_dir))
print(os.listdir(test_dir))
['paper', 'rock', 'scissors']
['paper', 'rock', 'scissors']
['paper', 'rock', 'scissors']
# preparing generator
train_datagen = ImageDataGenerator(
    rescale = 1./255,
    rotation_range = 20,
    horizontal_flip = True,
    shear_range = 0.2,
    fill_mode = 'nearest',
    preprocessing_function = preprocess_input,
)

test_datagen = ImageDataGenerator(
    rescale = 1./255)
# defining rgb mean for every generator
mean = np.array([123.68, 116.779, 103.939], dtype = 'float32')
train_datagen.mean = mean
test_datagen.mean = mean
# flow data to generator
BATCH_SIZE = 32
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size = (224,224),
    batch_size = BATCH_SIZE,
    class_mode = 'categorical',
    shuffle = False,
)

validation_generator = test_datagen.flow_from_directory(
    validation_dir,
    target_size = (224,224),
    batch_size = BATCH_SIZE,
    class_mode = 'categorical',
    shuffle = False,
)

test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size = (224,224),
    batch_size = BATCH_SIZE,
    class_mode = 'categorical',
    shuffle = False,
)

totalTrain = len(list(paths.list_images(train_dir)))
totalVal = len(list(paths.list_images(validation_dir)))
totalTest = len(list(paths.list_images(test_dir)))
Found 1312 images belonging to 3 classes.
Found 876 images belonging to 3 classes.
Found 12 images belonging to 3 classes.
# preparing baseModel
baseModel = VGG16(weights="imagenet", include_top=False,
    input_tensor=Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(3, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
2024-06-13 06:38:33.026430: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.036228: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.036873: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.038422: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.039011: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.039222: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.100423: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.100868: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.101216: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-13 06:38:33.101481: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1928] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 3503 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0
# freeze hidden layers to preserve model features
for layer in baseModel.layers:
    layer.trainable = False
# prepare callbacks
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau

EarlyStop = EarlyStopping(
    monitor = 'val_loss',
    patience = 4,
    verbose = 1,
    restore_best_weights = True,
    min_delta = 0.1
)

ModelCP = ReduceLROnPlateau(
    monitor = 'val_loss',
    factor = 0.5,
    patience = 1,
    verbose = 1
)

callbacks = [EarlyStop, ModelCP]
# compile the model with frozen layers
print("[INFO] compiling model...")
opt = SGD(learning_rate=1e-4, momentum=0.9)
model.compile(loss="categorical_crossentropy", optimizer=opt,
    metrics=["accuracy"])
print("[INFO] training head...")
H = model.fit(
    x=train_generator,
    steps_per_epoch=totalTrain // BATCH_SIZE,
    validation_data=validation_generator,
    validation_steps=totalVal // BATCH_SIZE,
    epochs=25,
    callbacks=callbacks,
)
[INFO] compiling model...
[INFO] training head...
Epoch 1/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 65s 764ms/step - accuracy: 0.3194 - loss: 1.6568 - val_accuracy: 0.3299 - val_loss: 1.1905 - learning_rate: 1.0000e-04
Epoch 2/25

Epoch 2: ReduceLROnPlateau reducing learning rate to 4.999999873689376e-05.
41/41 ━━━━━━━━━━━━━━━━━━━━ 13s 324ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 0.0000e+00 - val_loss: 2.0495 - learning_rate: 1.0000e-04
Epoch 3/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 34s 762ms/step - accuracy: 0.4824 - loss: 1.1331 - val_accuracy: 0.6551 - val_loss: 0.8608 - learning_rate: 5.0000e-05
Epoch 4/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 0.8333 - val_loss: 0.7623 - learning_rate: 5.0000e-05
Epoch 5/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 471ms/step - accuracy: 0.5609 - loss: 0.9040
Epoch 5: ReduceLROnPlateau reducing learning rate to 2.499999936844688e-05.
41/41 ━━━━━━━━━━━━━━━━━━━━ 35s 789ms/step - accuracy: 0.5616 - loss: 0.9034 - val_accuracy: 0.3414 - val_loss: 0.9531 - learning_rate: 5.0000e-05
Epoch 6/25

Epoch 6: ReduceLROnPlateau reducing learning rate to 1.249999968422344e-05.
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 0.0000e+00 - val_loss: 1.4488 - learning_rate: 2.5000e-05
Epoch 7/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 37s 851ms/step - accuracy: 0.4162 - loss: 1.0887 - val_accuracy: 0.9155 - val_loss: 0.7140 - learning_rate: 1.2500e-05
Epoch 8/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 1.0000 - val_loss: 0.6769 - learning_rate: 1.2500e-05
Epoch 9/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 553ms/step - accuracy: 0.8038 - loss: 0.7564
Epoch 9: ReduceLROnPlateau reducing learning rate to 6.24999984211172e-06.
41/41 ━━━━━━━━━━━━━━━━━━━━ 39s 879ms/step - accuracy: 0.8039 - loss: 0.7561 - val_accuracy: 0.8727 - val_loss: 0.6917 - learning_rate: 1.2500e-05
Epoch 10/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 1.0000 - val_loss: 0.6729 - learning_rate: 6.2500e-06
Epoch 11/25
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 539ms/step - accuracy: 0.7804 - loss: 0.7520
Epoch 11: ReduceLROnPlateau reducing learning rate to 3.12499992105586e-06.
41/41 ━━━━━━━━━━━━━━━━━━━━ 38s 869ms/step - accuracy: 0.7808 - loss: 0.7515 - val_accuracy: 0.8692 - val_loss: 0.6797 - learning_rate: 6.2500e-06
Epoch 11: early stopping
Restoring model weights from the end of the best epoch: 7.
/usr/local/lib/python3.11/dist-packages/keras/src/trainers/data_adapters/py_dataset_adapter.py:122: UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored.
  self._warn_if_super_not_called()
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1718260718.096354    2416 service.cc:145] XLA service 0x7e2f6c00bac0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1718260718.096393    2416 service.cc:153]   StreamExecutor device (0): NVIDIA GeForce GTX 960M, Compute Capability 5.0
2024-06-13 06:38:38.140178: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-06-13 06:38:38.387163: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:465] Loaded cuDNN version 8906
2024-06-13 06:38:42.864229: W external/local_tsl/tsl/framework/bfc_allocator.cc:296] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.46GiB with freed_by_count=0. The caller indicates that this is not a failure, but this may mean that there could be performance gains if more memory were available.
2024-06-13 06:38:42.864269: W external/local_tsl/tsl/framework/bfc_allocator.cc:296] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.46GiB with freed_by_count=0. The caller indicates that this is not a failure, but this may mean that there could be performance gains if more memory were available.
2024-06-13 06:38:43.727778: W external/local_tsl/tsl/framework/bfc_allocator.cc:296] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.04GiB with freed_by_count=0. The caller indicates that this is not a failure, but this may mean that there could be performance gains if more memory were available.
2024-06-13 06:38:44.727981: E external/local_xla/xla/service/slow_operation_alarm.cc:65] Trying algorithm eng11{k2=1,k3=0} for conv (f32[32,64,224,224]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,64,224,224]{3,2,1,0}, f32[64,64,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}} is taking a while...
2024-06-13 06:38:45.144180: E external/local_xla/xla/service/slow_operation_alarm.cc:133] The operation took 1.416328597s
Trying algorithm eng11{k2=1,k3=0} for conv (f32[32,64,224,224]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,64,224,224]{3,2,1,0}, f32[64,64,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}} is taking a while...
2024-06-13 06:38:49.990206: E external/local_xla/xla/service/slow_operation_alarm.cc:65] Trying algorithm eng36{k2=4,k3=0} for conv (f32[32,128,112,112]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,128,112,112]{3,2,1,0}, f32[128,128,3,3]{3,2,1,0}, f32[128]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}} is taking a while...
2024-06-13 06:38:51.947501: E external/local_xla/xla/service/slow_operation_alarm.cc:133] The operation took 2.957386015s
Trying algorithm eng36{k2=4,k3=0} for conv (f32[32,128,112,112]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,128,112,112]{3,2,1,0}, f32[128,128,3,3]{3,2,1,0}, f32[128]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}} is taking a while...
2024-06-13 06:38:52.947650: E external/local_xla/xla/service/slow_operation_alarm.cc:65] Trying algorithm eng36{k2=3,k3=0} for conv (f32[32,128,112,112]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,128,112,112]{3,2,1,0}, f32[128,128,3,3]{3,2,1,0}, f32[128]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}} is taking a while...
2024-06-13 06:38:53.103744: E external/local_xla/xla/service/slow_operation_alarm.cc:133] The operation took 1.156164154s
Trying algorithm eng36{k2=3,k3=0} for conv (f32[32,128,112,112]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,128,112,112]{3,2,1,0}, f32[128,128,3,3]{3,2,1,0}, f32[128]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}} is taking a while...
2024-06-13 06:38:54.454487: W external/local_tsl/tsl/framework/bfc_allocator.cc:296] Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.55GiB with freed_by_count=0. The caller indicates that this is not a failure, but this may mean that there could be performance gains if more memory were available.
I0000 00:00:1718260747.621493    2416 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
2024-06-13 06:39:38.617402: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:39:38.617449: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:39:38.617466: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:39:38.617484: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
/usr/lib/python3.11/contextlib.py:155: UserWarning: Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.
  self.gen.throw(typ, value, traceback)
2024-06-13 06:39:51.896922: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:39:51.896973: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:39:51.896987: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:39:51.897001: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
2024-06-13 06:40:25.878261: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:40:25.878307: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:40:25.878325: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:40:25.878349: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
2024-06-13 06:40:26.110588: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:40:26.110637: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:40:26.110670: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:40:26.110696: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
2024-06-13 06:41:01.193988: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:41:01.194027: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:41:01.433249: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:41:01.433288: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:41:01.433300: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:41:01.433317: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
2024-06-13 06:41:38.865435: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:41:38.865565: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:41:39.121906: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:41:39.121993: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:42:17.951701: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:42:17.951753: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:42:17.951777: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:42:17.951811: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
2024-06-13 06:42:18.198832: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:42:18.198885: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:42:18.198902: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:42:18.198925: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
# create classification_report for trained model
print("[INFO] evaluating after fine-tuning network head...")
test_generator.reset()
predIdxs = model.predict(x=test_generator,
    steps=(totalTest // BATCH_SIZE) + 1)
predIdxs = np.argmax(predIdxs, axis=1)
print(classification_report(test_generator.classes, predIdxs,
    target_names=test_generator.class_indices.keys()))
[INFO] evaluating after fine-tuning network head...
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 523ms/step
              precision    recall  f1-score   support

       paper       1.00      1.00      1.00         4
        rock       1.00      0.75      0.86         4
    scissors       0.80      1.00      0.89         4

    accuracy                           0.92        12
   macro avg       0.93      0.92      0.92        12
weighted avg       0.93      0.92      0.92        12
# recreate model with unfrozen layers
clear_session()
train_generator.reset()
validation_generator.reset()
for layer in baseModel.layers[15:]:
    layer.trainable = True
# recompile and retrain the model after unfreezing layers
print("[INFO] re-compiling model...")
opt = SGD(learning_rate=1e-4, momentum=0.9)
model.compile(loss="categorical_crossentropy", optimizer=opt,
    metrics=["accuracy"])
H = model.fit(
    x=train_generator,
    steps_per_epoch=totalTrain // BATCH_SIZE,
    validation_data=validation_generator,
    validation_steps=totalVal // BATCH_SIZE,
    epochs=20,
    callbacks=callbacks,
)
[INFO] re-compiling model...
Epoch 1/20
41/41 ━━━━━━━━━━━━━━━━━━━━ 41s 841ms/step - accuracy: 0.8991 - loss: 0.3942 - val_accuracy: 0.8449 - val_loss: 0.3809 - learning_rate: 1.0000e-04
Epoch 2/20
41/41 ━━━━━━━━━━━━━━━━━━━━ 1s 16ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 1.0000 - val_loss: 0.1072 - learning_rate: 1.0000e-04
Epoch 3/20
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 540ms/step - accuracy: 0.8902 - loss: 0.3041
Epoch 3: ReduceLROnPlateau reducing learning rate to 4.999999873689376e-05.
41/41 ━━━━━━━━━━━━━━━━━━━━ 39s 861ms/step - accuracy: 0.8907 - loss: 0.3038 - val_accuracy: 0.9537 - val_loss: 0.1826 - learning_rate: 1.0000e-04
Epoch 4/20

Epoch 4: ReduceLROnPlateau reducing learning rate to 2.499999936844688e-05.
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 1.0000 - val_loss: 0.1245 - learning_rate: 5.0000e-05
Epoch 5/20
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 638ms/step - accuracy: 0.9678 - loss: 0.1691
Epoch 5: ReduceLROnPlateau reducing learning rate to 1.249999968422344e-05.
41/41 ━━━━━━━━━━━━━━━━━━━━ 43s 972ms/step - accuracy: 0.9676 - loss: 0.1694 - val_accuracy: 0.9595 - val_loss: 0.1536 - learning_rate: 2.5000e-05
Epoch 6/20

Epoch 6: ReduceLROnPlateau reducing learning rate to 6.24999984211172e-06.
41/41 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00 - val_accuracy: 1.0000 - val_loss: 0.1329 - learning_rate: 1.2500e-05
Epoch 6: early stopping
Restoring model weights from the end of the best epoch: 2.
2024-06-13 06:53:29.167122: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
/usr/lib/python3.11/contextlib.py:155: UserWarning: Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.
  self.gen.throw(typ, value, traceback)
2024-06-13 06:53:29.167245: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:53:29.745362: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:53:29.745418: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:53:29.745443: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:54:08.699110: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:54:08.699156: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:54:08.943815: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:54:08.943913: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_4]]
2024-06-13 06:54:52.113182: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:54:52.113229: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_4]]
2024-06-13 06:54:52.113261: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
2024-06-13 06:54:52.364841: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
2024-06-13 06:54:52.364891: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: OUT_OF_RANGE: End of sequence
     [[{{node IteratorGetNext}}]]
     [[IteratorGetNext/_2]]
2024-06-13 06:54:52.364915: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 1113105719973859051
2024-06-13 06:54:52.364932: I tensorflow/core/framework/local_rendezvous.cc:422] Local rendezvous recv item cancelled. Key hash: 17891322345123413466
# create classification_report for full model
print("[INFO] evaluating after fine-tuning network...")
test_generator.reset()
predIdxs = model.predict(x=test_generator,
    steps=(totalTest // BATCH_SIZE) + 1)
predIdxs = np.argmax(predIdxs, axis=1)
print(classification_report(test_generator.classes, predIdxs,
    target_names=test_generator.class_indices.keys()))
[INFO] evaluating after fine-tuning network...
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 512ms/step
              precision    recall  f1-score   support

       paper       1.00      1.00      1.00         4
        rock       1.00      1.00      1.00         4
    scissors       1.00      1.00      1.00         4

    accuracy                           1.00        12
   macro avg       1.00      1.00      1.00        12
weighted avg       1.00      1.00      1.00        12
import os
import numpy as np
from tensorflow.keras.preprocessing import image
from sklearn.metrics import multilabel_confusion_matrix
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

testdir = test_dir
uploaded = list(paths.list_images(testdir))
fig = plt.figure(figsize= (10, 10))
plt.subplots_adjust(left=0.1,
                    bottom=0.1,
                    right=0.9,
                    top=0.9,
                    wspace=0.4,
                    hspace=0.4)
for i in range(len(uploaded)):
    path = uploaded[i]
    img = image.load_img(path, target_size = (224,224))

    ax = fig.add_subplot(4, 4, i+1)
    ax.imshow(img)
    images = image.img_to_array(img)
    images = np.expand_dims(images, axis=0)
    images = preprocess_input(images)
    pred = model.predict(images)
    ax.title.set_text(list(train_generator.class_indices.keys())[np.argmax(pred, axis = 1)[0]])
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 22ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 21ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 21ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 21ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step

Back to top