Newer
Older
"id": "62c6c5c3-d2ba-4e79-b533-828c3083e8ea",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 4458 images belonging to 6 classes.\n",
"Found 1111 images belonging to 6 classes.\n",
"Epoch 1/30\n",
"278/278 [==============================] - 60s 213ms/step - loss: 1.8360 - accuracy: 0.2733 - val_loss: 1.3567 - val_accuracy: 0.4556 - lr: 1.0000e-04\n",
"Epoch 2/30\n",
"278/278 [==============================] - 59s 213ms/step - loss: 1.2122 - accuracy: 0.5000 - val_loss: 0.8902 - val_accuracy: 0.8062 - lr: 1.0000e-04\n",
"Epoch 3/30\n",
"278/278 [==============================] - 59s 214ms/step - loss: 0.7822 - accuracy: 0.6952 - val_loss: 0.6691 - val_accuracy: 0.8469 - lr: 1.0000e-04\n",
"Epoch 4/30\n",
"278/278 [==============================] - 59s 213ms/step - loss: 0.4722 - accuracy: 0.8253 - val_loss: 0.4470 - val_accuracy: 0.9004 - lr: 1.0000e-04\n",
"Epoch 5/30\n",
"278/278 [==============================] - 59s 214ms/step - loss: 0.2882 - accuracy: 0.9070 - val_loss: 0.3356 - val_accuracy: 0.9167 - lr: 1.0000e-04\n",
"Epoch 6/30\n",
"278/278 [==============================] - 59s 214ms/step - loss: 0.2105 - accuracy: 0.9397 - val_loss: 0.3678 - val_accuracy: 0.9176 - lr: 1.0000e-07\n",
"Epoch 7/30\n",
"278/278 [==============================] - 59s 212ms/step - loss: 0.2022 - accuracy: 0.9372 - val_loss: 0.3469 - val_accuracy: 0.9149 - lr: 1.0000e-10\n",
"Epoch 8/30\n",
"278/278 [==============================] - 59s 212ms/step - loss: 0.1898 - accuracy: 0.9433 - val_loss: 0.3509 - val_accuracy: 0.9149 - lr: 1.0000e-13\n",
"Epoch 9/30\n",
"278/278 [==============================] - 59s 212ms/step - loss: 0.1995 - accuracy: 0.9412 - val_loss: 0.3384 - val_accuracy: 0.9221 - lr: 1.0000e-16\n",
"Epoch 10/30\n",
"278/278 [==============================] - 59s 213ms/step - loss: 0.1918 - accuracy: 0.9464 - val_loss: 0.3548 - val_accuracy: 0.9139 - lr: 1.0000e-19\n",
"70/70 [==============================] - 12s 169ms/step - loss: 0.3298 - accuracy: 0.9289\n",
"Test loss: 0.3297525942325592, Test accuracy: 0.9288929104804993\n",
"Model: \"sequential_4\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" \n",
"=================================================================\n",
"Total params: 41,095,622\n",
"Trainable params: 26,380,934\n",
"Non-trainable params: 14,714,688\n",
"_________________________________________________________________\n",
]
}
],
"source": [
"import time\n",
"from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
"from keras.losses import CategoricalCrossentropy\n",
"from tensorflow.keras.callbacks import EarlyStopping,LearningRateScheduler\n",
"from tensorflow.keras.optimizers import Adam\n",
"import tensorflow as tf\n",
"from tensorflow.keras import layers, models\n",
"from tensorflow.keras.applications import VGG16\n",
"\n",
"from Func.getSubFolders import count_sub_folders\n",
"path = 'Data'\n",
"output = 'Model/keras_model.h5'\n",
"start_time = time.time()\n",
"\n",
"physical_devices = tf.config.list_physical_devices('GPU')\n",
"print(\"Num GPUs Available: \", len(physical_devices))\n",
"if len(physical_devices) > 0:\n",
" try:\n",
" # Set memory growth to true\n",
" for device in physical_devices:\n",
" tf.config.experimental.set_memory_growth(device, True)\n",
" except RuntimeError as e:\n",
"else:\n",
" print(\"No GPU available. Using CPU.\")\n",
"def lr_schedule(epoch, lr):\n",
" if epoch < 5:\n",
" return lr\n",
" elif epoch < 10:\n",
" return lr * 0.001\n",
" else:\n",
" return lr * 0.01\n",
"\n",
"\n",
"\n",
"# Step 1: Load and Preprocess Images\n",
"datagen = ImageDataGenerator(\n",
" rescale=1. / 255,\n",
" validation_split=0.2,\n",
" width_shift_range=0.2,\n",
" height_shift_range=0.2,\n",
" shear_range=0.2,\n",
" zoom_range=0.2,\n",
"# Step 2: Label the Data\n",
"train_set = datagen.flow_from_directory(\n",
" path,\n",
" target_size=(224, 224),\n",
" batch_size=16,\n",
" class_mode='categorical',\n",
" subset='training'\n",
")\n",
"\n",
"test_set = datagen.flow_from_directory(\n",
" path,\n",
" target_size=(224, 224),\n",
" batch_size=16,\n",
" class_mode='categorical',\n",
" subset='validation'\n",
")\n",
"\n",
"#class_indices = test_set.class_indices\n",
"#plot_images(images, labels, class_indices)\n",
"base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) # EXPLAINNNNN\n",
"base_model.trainable = False\n",
"# Step 4: Build the Model\n",
"\n",
"model = Sequential([\n",
" base_model,\n",
" Flatten(),\n",
" Dense(units=1024, activation='relu'),\n",
" Dropout(0.5),\n",
" Dense(units=512, activation='relu'),\n",
" Dropout(0.5),\n",
" Dense(units=256, activation='relu'),\n",
" Dropout(0.5),\n",
" Dense(units=128, activation='relu'),\n",
" Dropout(0.5),\n",
" Dense(units=count_sub_folders(path), activation='softmax')\n",
"])\n",
"# Compile the Model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001),\n",
" loss=CategoricalCrossentropy(from_logits=False),\n",
" metrics=['accuracy'])\n",
"\n",
"# Step 6: Train the Model\n",
"early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n",
"lr_scheduler = LearningRateScheduler(lr_schedule)\n",
"\n",
"model.fit(\n",
" train_set,\n",
" steps_per_epoch=train_set.samples // train_set.batch_size,\n",
" validation_data=test_set,\n",
" validation_steps=test_set.samples // test_set.batch_size,\n",
" epochs=30,\n",
" callbacks=[early_stopping, lr_scheduler]\n",
")\n",
"\n",
"# Step 7: Evaluate the Model\n",
"loss, accuracy = model.evaluate(test_set)\n",
"print(f'Test loss: {loss}, Test accuracy: {accuracy}')\n",
"\n",
"# Save the trained model\n",
"model.save(output)\n",
"end_time = time.time()\n",
"\n",
"execute_time = (end_time - start_time) / 60\n",
"\n",
"model.summary()\n",
"\n",
"# Print the result\n",
"print(\"Last build:\", datetime.datetime.now().strftime(\"%d/%m/%y\"))\n",
"print(f\"It took: {execute_time:0.2f} minutes\")"
]
},
{
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}