{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 157 images belonging to 3 classes.\n", "Found 58 images belonging to 3 classes.\n", "Epoch 1/10\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-05-20 20:43:10.692611: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n", "\t [[{{node Placeholder/_0}}]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "5/5 [==============================] - ETA: 0s - loss: 1.2917 - accuracy: 0.6306" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-05-20 20:43:14.403495: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n", "\t [[{{node Placeholder/_0}}]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "5/5 [==============================] - 5s 823ms/step - loss: 1.2917 - accuracy: 0.6306 - val_loss: 0.7088 - val_accuracy: 0.7414\n", "Epoch 2/10\n", "5/5 [==============================] - 3s 625ms/step - loss: 0.6134 - accuracy: 0.8344 - val_loss: 0.0587 - val_accuracy: 0.9828\n", "Epoch 3/10\n", "5/5 [==============================] - 3s 694ms/step - loss: 0.0851 - accuracy: 0.9682 - val_loss: 0.1311 - val_accuracy: 0.9310\n", "Epoch 4/10\n", "5/5 [==============================] - 3s 688ms/step - loss: 0.1061 - accuracy: 0.9427 - val_loss: 0.0191 - val_accuracy: 1.0000\n", "Epoch 5/10\n", "5/5 [==============================] - 3s 680ms/step - loss: 0.0302 - accuracy: 0.9936 - val_loss: 0.0478 - val_accuracy: 0.9828\n", "Epoch 6/10\n", "5/5 [==============================] - 3s 684ms/step - loss: 0.0363 - accuracy: 0.9936 - val_loss: 0.0378 - val_accuracy: 0.9828\n", "Epoch 7/10\n", "5/5 [==============================] - 3s 660ms/step - loss: 0.0126 - accuracy: 0.9936 - val_loss: 0.0217 - val_accuracy: 0.9828\n", "Epoch 8/10\n", "5/5 [==============================] - 3s 611ms/step - loss: 0.0072 - accuracy: 1.0000 - val_loss: 0.0092 - val_accuracy: 1.0000\n", "Epoch 9/10\n", "5/5 [==============================] - 3s 683ms/step - loss: 0.0024 - accuracy: 1.0000 - val_loss: 0.0059 - val_accuracy: 1.0000\n", "Epoch 10/10\n", "5/5 [==============================] - 3s 631ms/step - loss: 0.0032 - accuracy: 1.0000 - val_loss: 0.0051 - val_accuracy: 1.0000\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-05-20 20:43:45.348055: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n", "\t [[{{node Placeholder/_0}}]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "2/2 [==============================] - 1s 289ms/step - loss: 0.0051 - accuracy: 1.0000\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`.\n", "WARNING:absl:There is a known slowdown when using v2.11+ Keras optimizers on M1/M2 Macs. Falling back to the legacy Keras optimizer, i.e., `tf.keras.optimizers.legacy.Adam`.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Validation Loss after Transfer Learning: 0.005056549794971943\n", "Validation Accuracy after Transfer Learning: 1.0\n", "Epoch 1/10\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-05-20 20:43:46.612117: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n", "\t [[{{node Placeholder/_0}}]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "5/5 [==============================] - ETA: 0s - loss: 0.1052 - accuracy: 0.9554" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-05-20 20:43:53.429648: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n", "\t [[{{node Placeholder/_0}}]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "5/5 [==============================] - 8s 1s/step - loss: 0.1052 - accuracy: 0.9554 - val_loss: 0.0045 - val_accuracy: 1.0000\n", "Epoch 2/10\n", "5/5 [==============================] - 6s 1s/step - loss: 0.1112 - accuracy: 0.9363 - val_loss: 0.0040 - val_accuracy: 1.0000\n", "Epoch 3/10\n", "5/5 [==============================] - 6s 1s/step - loss: 0.0515 - accuracy: 0.9745 - val_loss: 0.0038 - val_accuracy: 1.0000\n", "Epoch 4/10\n", "5/5 [==============================] - 5s 1s/step - loss: 0.0589 - accuracy: 0.9809 - val_loss: 0.0037 - val_accuracy: 1.0000\n", "Epoch 5/10\n", "5/5 [==============================] - 6s 1s/step - loss: 0.0747 - accuracy: 0.9809 - val_loss: 0.0036 - val_accuracy: 1.0000\n", "Epoch 6/10\n", "5/5 [==============================] - 5s 1s/step - loss: 0.0269 - accuracy: 1.0000 - val_loss: 0.0036 - val_accuracy: 1.0000\n", "Epoch 7/10\n", "5/5 [==============================] - 5s 1s/step - loss: 0.0508 - accuracy: 0.9936 - val_loss: 0.0035 - val_accuracy: 1.0000\n", "Epoch 8/10\n", "5/5 [==============================] - 5s 1s/step - loss: 0.0117 - accuracy: 1.0000 - val_loss: 0.0035 - val_accuracy: 1.0000\n", "Epoch 9/10\n", "5/5 [==============================] - 5s 1s/step - loss: 0.0391 - accuracy: 0.9936 - val_loss: 0.0034 - val_accuracy: 1.0000\n", "Epoch 10/10\n", "5/5 [==============================] - 6s 1s/step - loss: 0.0171 - accuracy: 1.0000 - val_loss: 0.0032 - val_accuracy: 1.0000\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-05-20 20:44:43.829725: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n", "\t [[{{node Placeholder/_0}}]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "2/2 [==============================] - 1s 252ms/step - loss: 0.0032 - accuracy: 1.0000\n", "Validation Loss after Fine-Tuning: 0.003204665146768093\n", "Validation Accuracy after Fine-Tuning: 1.0\n" ] } ], "source": [ "import tensorflow as tf\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "from tensorflow.keras.applications import MobileNetV2\n", "from tensorflow.keras.layers import GlobalAveragePooling2D, Dense\n", "from tensorflow.keras.models import Model\n", "import os\n", "\n", "# Folders\n", "train_dir = 'train_dataset'\n", "val_dir = 'validation_dataset'\n", "\n", "# Check if folders exist\n", "assert os.path.exists(train_dir), \"Trainingsverzeichnis existiert nicht\"\n", "assert os.path.exists(val_dir), \"Validierungsverzeichnis existiert nicht\"\n", "\n", "# Data augmentation and normalization\n", "train_datagen = ImageDataGenerator(\n", " rescale=1./255,\n", " rotation_range=40,\n", " width_shift_range=0.2,\n", " height_shift_range=0.2,\n", " shear_range=0.2,\n", " zoom_range=0.2,\n", " horizontal_flip=True,\n", " fill_mode='nearest'\n", ")\n", "\n", "val_datagen = ImageDataGenerator(rescale=1./255)\n", "\n", "# Data generators\n", "train_generator = train_datagen.flow_from_directory(\n", " train_dir,\n", " target_size=(224, 224),\n", " batch_size=32,\n", " class_mode='categorical'\n", ")\n", "\n", "val_generator = val_datagen.flow_from_directory(\n", " val_dir,\n", " target_size=(224, 224),\n", " batch_size=32,\n", " class_mode='categorical'\n", ")\n", "\n", "# Create model\n", "base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n", "x = base_model.output\n", "x = GlobalAveragePooling2D()(x)\n", "x = Dense(512, activation='relu')(x)\n", "predictions = Dense(train_generator.num_classes, activation='softmax')(x)\n", "\n", "model = Model(inputs=base_model.input, outputs=predictions)\n", "\n", "# Freeze base model layers\n", "for layer in base_model.layers:\n", " layer.trainable = False\n", "\n", "model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n", "\n", "# Train model with freezed base model layers\n", "history_transfer_learning = model.fit(\n", " train_generator,\n", " epochs=10,\n", " validation_data=val_generator\n", ")\n", "\n", "# Evaluation of performance after transfer learning\n", "val_loss, val_accuracy = model.evaluate(val_generator)\n", "print(f\"Validation Loss after Transfer Learning: {val_loss}\")\n", "print(f\"Validation Accuracy after Transfer Learning: {val_accuracy}\")\n", "\n", "# Fine-tuning\n", "for layer in base_model.layers:\n", " layer.trainable = True\n", "\n", "model.compile(optimizer=tf.keras.optimizers.Adam(1e-5), loss='categorical_crossentropy', metrics=['accuracy'])\n", "\n", "# Train model with fine-tuning\n", "history_fine_tuning = model.fit(\n", " train_generator,\n", " epochs=10,\n", " validation_data=val_generator\n", ")\n", "\n", "# Evaluation of performance after fine-tuning\n", "val_loss_fine_tuning, val_accuracy_fine_tuning = model.evaluate(val_generator)\n", "print(f\"Validation Loss after Fine-Tuning: {val_loss_fine_tuning}\")\n", "print(f\"Validation Accuracy after Fine-Tuning: {val_accuracy_fine_tuning}\")\n", "\n", "# Save model\n", "model.save('pokemon_classifier_model.keras')\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }