File size: 16,694 Bytes
d8c5c5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Import der benΓΆtigten Bibliotheken\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras.applications import ResNet50\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\n",
"from tensorflow.keras.models import Model\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vorbereitung der Daten\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 78 images belonging to 6 classes.\n",
"Found 16 images belonging to 6 classes.\n"
]
}
],
"source": [
"# Daten-Vorbereitung\n",
"base_dir = 'C:\\Daten\\Studium Wirtschaftsinformatik\\Semester 6 TZ\\KI-Anwendungen\\Γbungen\\Γbung2\\Abschluss\\DatensΓ€tze\\StrohhΓΌte' # Pfad zum ΓΌbergeordneten Ordner, der die Klassenordner enthΓ€lt\n",
"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",
" validation_split=0.2 # Behalte die Aufteilung fΓΌr Training und Validation bei\n",
")\n",
"train_generator = datagen.flow_from_directory(\n",
" base_dir,\n",
" target_size=(224, 224), # Assuming using ResNet input dimensions\n",
" batch_size=32, # Adjust according to your system capability\n",
" class_mode='categorical',\n",
" subset='training' # Use the 'subset' argument for splitting\n",
")\n",
"\n",
"validation_generator = datagen.flow_from_directory(\n",
" base_dir,\n",
" target_size=(224, 224),\n",
" batch_size=32,\n",
" class_mode='categorical',\n",
" subset='validation'\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Modell Setup"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Modell-Setup\n",
"base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n",
"base_model.trainable = False # Zuerst wird das Basismodell eingefroren\n",
"\n",
"x = GlobalAveragePooling2D()(base_model.output)\n",
"x = Dense(1024, activation='relu')(x)\n",
"x = Dropout(0.5)(x) # Dropout hinzugefΓΌgt, um Overfitting zu reduzieren\n",
"predictions = Dense(3, activation='softmax')(x)\n",
"\n",
"model = Model(inputs=base_model.input, outputs=predictions)\n",
"model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Training des Models"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/20\n"
]
},
{
"ename": "ValueError",
"evalue": "Arguments `target` and `output` must have the same shape. Received: target.shape=(None, 6), output.shape=(None, 3)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[4], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Trainieren des Modells\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m history \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43mtrain_generator\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43msteps_per_epoch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrain_generator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msamples\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mtrain_generator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalidation_generator\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidation_steps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalidation_generator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msamples\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mvalidation_generator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[0;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[43mModelCheckpoint\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mbest_model.keras\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msave_best_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[43mEarlyStopping\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmonitor\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mval_loss\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpatience\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43mReduceLROnPlateau\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmonitor\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mval_loss\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfactor\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0.2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpatience\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m 13\u001b[0m \u001b[43m)\u001b[49m\n",
"File \u001b[1;32mc:\\Users\\Jeremy Kuwegu\\anaconda3\\envs\\kia\\lib\\site-packages\\keras\\src\\utils\\traceback_utils.py:122\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 119\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n\u001b[0;32m 120\u001b[0m \u001b[38;5;66;03m# To get the full stack trace, call:\u001b[39;00m\n\u001b[0;32m 121\u001b[0m \u001b[38;5;66;03m# `keras.config.disable_traceback_filtering()`\u001b[39;00m\n\u001b[1;32m--> 122\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(filtered_tb) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 123\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m 124\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m filtered_tb\n",
"File \u001b[1;32mc:\\Users\\Jeremy Kuwegu\\anaconda3\\envs\\kia\\lib\\site-packages\\keras\\src\\backend\\tensorflow\\nn.py:554\u001b[0m, in \u001b[0;36mcategorical_crossentropy\u001b[1;34m(target, output, from_logits, axis)\u001b[0m\n\u001b[0;32m 552\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m e1, e2 \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(target\u001b[38;5;241m.\u001b[39mshape, output\u001b[38;5;241m.\u001b[39mshape):\n\u001b[0;32m 553\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m e1 \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m e2 \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m e1 \u001b[38;5;241m!=\u001b[39m e2:\n\u001b[1;32m--> 554\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 555\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mArguments `target` and `output` must have the same shape. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 556\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReceived: \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 557\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtarget.shape=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtarget\u001b[38;5;241m.\u001b[39mshape\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, output.shape=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00moutput\u001b[38;5;241m.\u001b[39mshape\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 558\u001b[0m )\n\u001b[0;32m 560\u001b[0m output, from_logits \u001b[38;5;241m=\u001b[39m _get_logits(\n\u001b[0;32m 561\u001b[0m output, from_logits, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSoftmax\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcategorical_crossentropy\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 562\u001b[0m )\n\u001b[0;32m 563\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m from_logits:\n",
"\u001b[1;31mValueError\u001b[0m: Arguments `target` and `output` must have the same shape. Received: target.shape=(None, 6), output.shape=(None, 3)"
]
}
],
"source": [
"# Trainieren des Modells\n",
"history = model.fit(\n",
" train_generator,\n",
" steps_per_epoch=train_generator.samples // train_generator.batch_size,\n",
" validation_data=validation_generator,\n",
" validation_steps=validation_generator.samples // validation_generator.batch_size,\n",
" epochs=20,\n",
" callbacks=[\n",
" ModelCheckpoint('best_model.keras', save_best_only=True),\n",
" EarlyStopping(monitor='val_loss', patience=5),\n",
" ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=2)\n",
" ]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fine Tuning des Modells"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m148s\u001b[0m 12s/step - accuracy: 0.7070 - loss: 1.0460 - val_accuracy: 0.6094 - val_loss: 0.9729\n",
"Epoch 2/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 135ms/step - accuracy: 1.0000 - loss: 0.0769 - val_accuracy: 0.5714 - val_loss: 1.0434\n",
"Epoch 3/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m100s\u001b[0m 10s/step - accuracy: 0.9677 - loss: 0.1108 - val_accuracy: 0.5469 - val_loss: 0.9639\n",
"Epoch 4/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m10s\u001b[0m 111ms/step - accuracy: 1.0000 - loss: 0.0381 - val_accuracy: 0.7143 - val_loss: 0.9019\n",
"Epoch 5/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m96s\u001b[0m 10s/step - accuracy: 0.9992 - loss: 0.0220 - val_accuracy: 0.2969 - val_loss: 1.1206\n",
"Epoch 6/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m12s\u001b[0m 164ms/step - accuracy: 1.0000 - loss: 0.0226 - val_accuracy: 0.1429 - val_loss: 1.1233\n",
"Epoch 7/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m97s\u001b[0m 10s/step - accuracy: 1.0000 - loss: 0.0062 - val_accuracy: 0.1719 - val_loss: 1.4363\n",
"Epoch 8/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m11s\u001b[0m 103ms/step - accuracy: 0.9688 - loss: 0.0287 - val_accuracy: 0.1429 - val_loss: 1.4406\n",
"Epoch 9/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m98s\u001b[0m 10s/step - accuracy: 0.9907 - loss: 0.0160 - val_accuracy: 0.2344 - val_loss: 1.4151\n",
"Epoch 10/10\n",
"\u001b[1m9/9\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m11s\u001b[0m 101ms/step - accuracy: 1.0000 - loss: 0.0038 - val_accuracy: 0.0000e+00 - val_loss: 1.6847\n"
]
}
],
"source": [
"# Fine-Tuning des Modells\n",
"for layer in base_model.layers:\n",
" layer.trainable = True\n",
"\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])\n",
"history_fine = model.fit(\n",
" train_generator,\n",
" steps_per_epoch=train_generator.samples // train_generator.batch_size,\n",
" validation_data=validation_generator,\n",
" validation_steps=validation_generator.samples // validation_generator.batch_size,\n",
" epochs=10\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bewertung und Ergebnisse"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m2/2\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 3s/step - accuracy: 0.2708 - loss: 1.4508\n",
"Performance vor dem Fine-Tuning: 0.640625\n",
"Performance nach dem Fine-Tuning: 0.0\n"
]
}
],
"source": [
"# Ergebnisse bewerten\n",
"eval_result = model.evaluate(validation_generator, steps=validation_generator.samples // validation_generator.batch_size)\n",
"print(f'Performance vor dem Fine-Tuning: {history.history[\"val_accuracy\"][-1]}')\n",
"print(f'Performance nach dem Fine-Tuning: {history_fine.history[\"val_accuracy\"][-1]}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. \n"
]
}
],
"source": [
"model.save('mein_modell.h5') "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "kia",
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|