Open In Colab

Student Name : Sulthan A. Karimov

Username : sulthankarimov

Email : [email protected]

!pip install split-folders[full] matplotlib jupyter_http_over_ws
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: 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 os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' 
import tensorflow as tf
print(tf.__version__)
2.16.1
try:
  import google.colab
  IN_COLAB = True
except:
  IN_COLAB = False

IN_COLAB
False
# sometime i run this notebook on my laptop for testing purpose
local_dir = (
    'datasets/images/' if IN_COLAB == False
    else '/tmp/'
)

dataset_name = 'rockpaperscissors'
local_data = local_dir + dataset_name
local_zip = local_data + '.zip'
print(local_zip)
datasets/images/rockpaperscissors.zip
!test -f $local_zip || wget --no-check-certificate \
 https://github.com/dicodingacademy/assets/releases/download/release/rockpaperscissors.zip \
 -o $local_zip
import zipfile, os, shutil, splitfolders, re
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall(local_dir)
zip_ref.close()
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()
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, 5444.71 files/s]
os.listdir(train_dir)
['paper', 'rock', 'scissors']
os.listdir(validation_dir)
['paper', 'rock', 'scissors']
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.vgg16 import preprocess_input

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)
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size = (224,224),
    batch_size = 4,
    class_mode = 'categorical',
    shuffle = False,
)

validation_generator = test_datagen.flow_from_directory(
    validation_dir,
    target_size = (224,224),
    batch_size = 4,
    class_mode = 'categorical',
    shuffle = False,
)
Found 1312 images belonging to 3 classes.
Found 876 images belonging to 3 classes.
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from imutils import paths
import numpy as np
import pickle
import random
import os
print("[INFO] loading network...")
model = VGG16(weights="imagenet", include_top=False)
train_features = model.predict(train_generator)
train_features = train_features.reshape((train_features.shape[0], 7 * 7 * 512))
val_features = model.predict(validation_generator)
val_features = val_features.reshape((val_features.shape[0], 7 * 7 * 512))
[INFO] loading network...
  2/328 ━━━━━━━━━━━━━━━━━━━━ 45s 141ms/step328/328 ━━━━━━━━━━━━━━━━━━━━ 54s 144ms/step
219/219 ━━━━━━━━━━━━━━━━━━━━ 32s 146ms/step
/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:1718172438.489068     309 service.cc:145] XLA service 0x7869d00032a0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1718172438.489112     309 service.cc:153]   StreamExecutor device (0): NVIDIA GeForce GTX 960M, Compute Capability 5.0
I0000 00:00:1718172445.254437     309 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
train_features
array([[0.15979376, 0.        , 0.        , ..., 0.        , 0.6380874 ,
        0.        ],
       [0.14911175, 0.        , 0.        , ..., 0.00925446, 0.84793645,
        0.        ],
       [0.1327081 , 0.        , 0.        , ..., 0.1794551 , 0.6249763 ,
        0.        ],
       ...,
       [0.16579124, 0.        , 0.        , ..., 0.        , 0.5942712 ,
        0.        ],
       [0.09918394, 0.        , 0.        , ..., 0.        , 0.8006563 ,
        0.        ],
       [0.2933985 , 0.        , 0.        , ..., 0.        , 0.6294993 ,
        0.        ]], dtype=float32)
train_data = train_features.reshape(1312,7,14,256)
train_data.shape
(1312, 7, 14, 256)
# import the necessary packages
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
import numpy as np
import pickle
import os

trainX, trainY = train_features, train_generator.classes
testX, testY = val_features, validation_generator.classes

# train the model
print("[INFO] training model...")
modelLogReg = LogisticRegression(solver="lbfgs", multi_class="auto",
    max_iter=150)
modelLogReg.fit(trainX, trainY)
# evaluate the model
print("[INFO] evaluating...")
preds = modelLogReg.predict(testX)
print(classification_report(testY, preds, target_names=train_generator.class_indices.keys()))
[INFO] training model...
[INFO] evaluating...
              precision    recall  f1-score   support

       paper       0.98      0.99      0.99       285
        rock       0.98      1.00      0.99       291
    scissors       1.00      0.98      0.99       300

    accuracy                           0.99       876
   macro avg       0.99      0.99      0.99       876
weighted avg       0.99      0.99      0.99       876
/usr/local/lib/python3.11/dist-packages/sklearn/linear_model/_logistic.py:1247: FutureWarning: 'multi_class' was deprecated in version 1.5 and will be removed in 1.7. From then on, it will always use 'multinomial'. Leave it to its default value to avoid this warning.
  warnings.warn(
import os
import numpy as np
#from google.colab import files
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 = 'datasets/images/rps/test/'
uploaded = os.listdir(testdir)
fig = plt.figure(figsize= (10, 10))
#fig.tight_layout(pad=5.0)
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 = testdir + uploaded[i]
    img = image.load_img(path, target_size = (224,224))

    ax = fig.add_subplot(4, 4, i+1)
    ax.imshow(img)
    images = img_to_array(img)
    images = np.expand_dims(images, axis=0)
    images = preprocess_input(images)
    #images = np.vstack([x])
    features = model.predict(images, batch_size=4)
    features = features.reshape((features.shape[0], 7 * 7 * 512))
    pred = modelLogReg.predict(features)
    ax.title.set_text(list(train_generator.class_indices.keys())[int(pred[0])])
FileNotFoundError: [Errno 2] No such file or directory: 'datasets/images/rps/test/'
Back to top