From 2093f84c5f9eef52a1430a164d0808bc0abe4fec Mon Sep 17 00:00:00 2001 From: Krzysztof Bojakowski Date: Sun, 5 May 2024 18:36:53 +0200 Subject: [PATCH] Small fixes, model wasnt building --- src/main.py | 2 +- src/model/test_model.py | 8 +- testing.ipynb | 363 ---------------------------------------- 3 files changed, 5 insertions(+), 368 deletions(-) delete mode 100644 testing.ipynb diff --git a/src/main.py b/src/main.py index 90c6a4c..3c8c214 100644 --- a/src/main.py +++ b/src/main.py @@ -4,4 +4,4 @@ from model.test_model import TestModel if __name__ == "__main__": model = TestModel() history = model.fit() - model.save() + model.save("model/test_model_final.keras") diff --git a/src/model/test_model.py b/src/model/test_model.py index 3819942..08de600 100644 --- a/src/model/test_model.py +++ b/src/model/test_model.py @@ -13,7 +13,7 @@ class TestModel: # Start of config self.config.layer_1 = 512 self.config.activation_1 = "relu" - self.config.dropout = random.uniform(0.01, 0.80), + self.config.dropout = random.uniform(0.01, 0.80) self.config.layer_2 = 10 self.config.activation_2 = "softmax" self.config.optimizer = "sgd" @@ -26,7 +26,7 @@ class TestModel: def __build_model(self): return tf.keras.models.Sequential([ - tf.keras.layers.Input(shape=(28,28)), + tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(self.config.layer_1, activation=self.config.activation_1), tf.keras.layers.Dropout(self.config.dropout), tf.keras.layers.Dense(self.config.layer_2, activation=self.config.activation_2) @@ -60,6 +60,6 @@ class TestModel: callbacks=wandb_callbacks ) - def save(self): - self.model.save("test_model/final_model.keras") + def save(self, filepath): + self.model.save(filepath) diff --git a/testing.ipynb b/testing.ipynb deleted file mode 100644 index 1d4ffaa..0000000 --- a/testing.ipynb +++ /dev/null @@ -1,363 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "Tracking run with wandb version 0.16.6" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "Run data is saved locally in /mnt/c/Users/krzys/OneDrive/Studia/inz-uczenia-maszynowego/Detection-of-plant-diseases/wandb/run-20240416_232247-bfji8amn" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "Syncing run floral-energy-3 to Weights & Biases (docs)
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - " View project at https://wandb.ai/uczenie-maszynowe-projekt/Detection%20of%20plant%20diseases" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - " View run at https://wandb.ai/uczenie-maszynowe-projekt/Detection%20of%20plant%20diseases/runs/bfji8amn" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/8\n", - "44/47 [===========================>..] - ETA: 0s - loss: 2.1872 - accuracy: 0.2224INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 2s 32ms/step - loss: 2.1734 - accuracy: 0.2344 - val_loss: 1.9111 - val_accuracy: 0.5380\n", - "Epoch 2/8\n", - "40/47 [========================>.....] - ETA: 0s - loss: 1.7703 - accuracy: 0.5437INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 1s 31ms/step - loss: 1.7483 - accuracy: 0.5527 - val_loss: 1.5486 - val_accuracy: 0.6880\n", - "Epoch 3/8\n", - "46/47 [============================>.] - ETA: 0s - loss: 1.4466 - accuracy: 0.6818INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 2s 33ms/step - loss: 1.4444 - accuracy: 0.6829 - val_loss: 1.2824 - val_accuracy: 0.7460\n", - "Epoch 4/8\n", - "44/47 [===========================>..] - ETA: 0s - loss: 1.2232 - accuracy: 0.7362INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 2s 32ms/step - loss: 1.2162 - accuracy: 0.7390 - val_loss: 1.0886 - val_accuracy: 0.7880\n", - "Epoch 5/8\n", - "44/47 [===========================>..] - ETA: 0s - loss: 1.0583 - accuracy: 0.7694INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 1s 28ms/step - loss: 1.0519 - accuracy: 0.7711 - val_loss: 0.9497 - val_accuracy: 0.8020\n", - "Epoch 6/8\n", - "41/47 [=========================>....] - ETA: 0s - loss: 0.9382 - accuracy: 0.7897INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 1s 28ms/step - loss: 0.9339 - accuracy: 0.7902 - val_loss: 0.8484 - val_accuracy: 0.8180\n", - "Epoch 7/8\n", - "47/47 [==============================] - ETA: 0s - loss: 0.8496 - accuracy: 0.8043INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "47/47 [==============================] - 1s 27ms/step - loss: 0.8496 - accuracy: 0.8043 - val_loss: 0.7735 - val_accuracy: 0.8220\n", - "Epoch 8/8\n", - "44/47 [===========================>..] - ETA: 0s - loss: 0.7790 - accuracy: 0.8180INFO:tensorflow:Assets written to: models/assets\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: models/assets\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Adding directory to artifact (./models)... Done. 0.1s\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\r", - "47/47 [==============================] - 1s 29ms/step - loss: 0.7779 - accuracy: 0.8183 - val_loss: 0.7165 - val_accuracy: 0.8260\n" - ] - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "316da49b179f47019f8cf5c9c72353fe" - } - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "\n", - "

Run history:


batch/accuracy▁▁▁▂▂▄▅▅▅▅▆▆▆▇▇▇▇▇▇▇▇▇▇▇████████████████
batch/batch_step▁▁▁▂▂▂▂▂▂▃▃▃▃▃▃▄▄▄▄▄▅▅▅▅▅▅▆▆▆▆▆▆▇▇▇▇▇███
batch/learning_rate▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
batch/loss███▇▇▆▆▆▅▅▅▄▄▄▄▄▃▃▃▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁
epoch/accuracy▁▅▆▇▇███
epoch/epoch▁▂▃▄▅▆▇█
epoch/learning_rate▁▁▁▁▁▁▁▁
epoch/loss█▆▄▃▂▂▁▁
epoch/val_accuracy▁▅▆▇▇███
epoch/val_loss█▆▄▃▂▂▁▁

Run summary:


batch/accuracy0.81726
batch/batch_step395
batch/learning_rate0.01
batch/loss0.77969
epoch/accuracy0.81825
epoch/epoch7
epoch/learning_rate0.01
epoch/loss0.77791
epoch/val_accuracy0.826
epoch/val_loss0.71648

" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - " View run floral-energy-3 at: https://wandb.ai/uczenie-maszynowe-projekt/Detection%20of%20plant%20diseases/runs/bfji8amn
View project at: https://wandb.ai/uczenie-maszynowe-projekt/Detection%20of%20plant%20diseases
Synced 5 W&B file(s), 0 media file(s), 42 artifact file(s) and 0 other file(s)" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "Find logs at: ./wandb/run-20240416_232247-bfji8amn/logs" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# This script needs these libraries to be installed:\n", - "# tensorflow, numpy\n", - "\n", - "import wandb\n", - "from wandb.keras import WandbMetricsLogger, WandbModelCheckpoint\n", - "\n", - "import random\n", - "import numpy as np\n", - "import tensorflow as tf\n", - "\n", - "\n", - "# Start a run, tracking hyperparameters\n", - "wandb.init(\n", - " # set the wandb project where this run will be logged\n", - " project=\"Detection of plant diseases\",\n", - "\n", - " # track hyperparameters and run metadata with wandb.config\n", - " config={\n", - " \"layer_1\": 512,\n", - " \"activation_1\": \"relu\",\n", - " \"dropout\": random.uniform(0.01, 0.80),\n", - " \"layer_2\": 10,\n", - " \"activation_2\": \"softmax\",\n", - " \"optimizer\": \"sgd\",\n", - " \"loss\": \"sparse_categorical_crossentropy\",\n", - " \"metric\": \"accuracy\",\n", - " \"epoch\": 8,\n", - " \"batch_size\": 256\n", - " }\n", - ")\n", - "\n", - "# [optional] use wandb.config as your config\n", - "config = wandb.config\n", - "\n", - "# get the data\n", - "mnist = tf.keras.datasets.mnist\n", - "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", - "x_train, x_test = x_train / 255.0, x_test / 255.0\n", - "x_train, y_train = x_train[::5], y_train[::5]\n", - "x_test, y_test = x_test[::20], y_test[::20]\n", - "labels = [str(digit) for digit in range(np.max(y_train) + 1)]\n", - "\n", - "# build a model\n", - "model = tf.keras.models.Sequential([\n", - " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", - " tf.keras.layers.Dense(config.layer_1, activation=config.activation_1),\n", - " tf.keras.layers.Dropout(config.dropout),\n", - " tf.keras.layers.Dense(config.layer_2, activation=config.activation_2)\n", - " ])\n", - "\n", - "# compile the model\n", - "model.compile(optimizer=config.optimizer,\n", - " loss=config.loss,\n", - " metrics=[config.metric]\n", - " )\n", - "\n", - "# WandbMetricsLogger will log train and validation metrics to wandb\n", - "# WandbModelCheckpoint will upload model checkpoints to wandb\n", - "history = model.fit(x=x_train, y=y_train,\n", - " epochs=config.epoch,\n", - " batch_size=config.batch_size,\n", - " validation_data=(x_test, y_test),\n", - " callbacks=[\n", - " WandbMetricsLogger(log_freq=5),\n", - " WandbModelCheckpoint(\"models\")\n", - " ])\n", - "\n", - "# [optional] finish the wandb run, necessary in notebooks\n", - "wandb.finish()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.12" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}