2021-12-19 23:09:15 +01:00
{
"cells": [
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 28,
2021-12-19 23:09:15 +01:00
"id": "comprehensive-talent",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import os\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D\n",
2021-12-20 04:30:49 +01:00
"from keras.preprocessing.image import ImageDataGenerator\n",
2021-12-19 23:09:15 +01:00
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.model_selection import train_test_split\n",
2021-12-20 04:30:49 +01:00
"from tensorflow.keras.optimizers import RMSprop\n",
2021-12-19 23:09:15 +01:00
"from sklearn.metrics import classification_report\n",
2021-12-20 04:30:49 +01:00
"import re\n",
"import matplotlib.pyplot as plt"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 15,
"id": "macro-michigan",
2021-12-19 23:09:15 +01:00
"metadata": {},
"outputs": [],
"source": [
2021-12-20 04:30:49 +01:00
"train_data_dir=\"../Trees\"\n",
"validation_data_dir=\"../Trees\"\n",
"batch_size=14\n",
"img_height, img_width = 60,80"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 16,
"id": "defined-briefing",
2021-12-19 23:09:15 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2021-12-20 04:30:49 +01:00
"Found 428 images belonging to 3 classes.\n",
"Found 105 images belonging to 3 classes.\n"
2021-12-19 23:09:15 +01:00
]
}
],
"source": [
2021-12-20 04:30:49 +01:00
"train_datagen = ImageDataGenerator(rescale=1./255,\n",
" shear_range=0.2,\n",
" zoom_range=0.2,\n",
" horizontal_flip=True,\n",
" validation_split=0.2) # set validation split\n",
"\n",
"train_generator = train_datagen.flow_from_directory(\n",
" train_data_dir,\n",
" target_size=(img_height, img_width),\n",
" batch_size=batch_size,\n",
" class_mode='categorical',\n",
" subset='training') # set as training data\n",
"\n",
"validation_generator = train_datagen.flow_from_directory(\n",
" validation_data_dir, # same directory as training data\n",
" target_size=(img_height, img_width),\n",
" batch_size=batch_size,\n",
" class_mode='categorical',\n",
" subset='validation')"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 17,
"id": "natural-cutting",
2021-12-19 23:09:15 +01:00
"metadata": {},
"outputs": [],
"source": [
2021-12-20 04:30:49 +01:00
"model = Sequential()"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 18,
"id": "conservative-hypothetical",
2021-12-19 23:09:15 +01:00
"metadata": {},
"outputs": [],
"source": [
2021-12-20 04:30:49 +01:00
"model.add(Conv2D(32, (3,3), activation='relu', input_shape=(60, 80, 3)))\n",
"model.add(MaxPooling2D((2,2)))\n",
"model.add(Conv2D(64, (3,3), activation='relu'))\n",
"model.add(MaxPooling2D((2,2)))\n",
"model.add(Conv2D(64, (3,3), activation='relu'))\n",
"model.add(MaxPooling2D((2,2)))\n",
"model.add(Flatten())\n",
"model.add(Dense(512, activation='relu'))\n",
"model.add(Dense(512, activation='relu'))\n",
2021-12-19 23:09:15 +01:00
"\n",
2021-12-20 04:30:49 +01:00
"model.add(Dense(3, activation='softmax'))"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 23,
"id": "irish-monitoring",
2021-12-19 23:09:15 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2021-12-20 04:30:49 +01:00
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: pydot in c:\\program files\\python39\\lib\\site-packages (1.4.2)\n",
"Requirement already satisfied: pyparsing>=2.1.4 in c:\\users\\wbloc\\appdata\\roaming\\python\\python39\\site-packages (from pydot) (2.4.7)\n",
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: graphviz in c:\\program files\\python39\\lib\\site-packages (0.19.1)\n",
"('You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) ', 'for plot_model/model_to_dot to work.')\n"
2021-12-19 23:09:15 +01:00
]
}
],
"source": [
2021-12-20 04:30:49 +01:00
"from tensorflow.keras.utils import plot_model \n",
"!pip install pydot\n",
"!pip install graphviz\n",
"plot_model(model, to_file='model1_plot.png', show_shapes=True,show_dtype=True, show_layer_names=True, expand_nested=True,)"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 24,
2021-12-19 23:09:15 +01:00
"id": "illegal-zoning",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2021-12-20 04:30:49 +01:00
"Model: \"sequential_2\"\n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
2021-12-20 04:30:49 +01:00
"conv2d_6 (Conv2D) (None, 58, 78, 32) 896 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"max_pooling2d_6 (MaxPooling2 (None, 29, 39, 32) 0 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"conv2d_7 (Conv2D) (None, 27, 37, 64) 18496 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"max_pooling2d_7 (MaxPooling2 (None, 13, 18, 64) 0 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"conv2d_8 (Conv2D) (None, 11, 16, 64) 36928 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"max_pooling2d_8 (MaxPooling2 (None, 5, 8, 64) 0 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"flatten_2 (Flatten) (None, 2560) 0 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"dense_6 (Dense) (None, 512) 1311232 \n",
2021-12-19 23:09:15 +01:00
"_________________________________________________________________\n",
2021-12-20 04:30:49 +01:00
"dense_7 (Dense) (None, 512) 262656 \n",
"_________________________________________________________________\n",
"dense_8 (Dense) (None, 3) 1539 \n",
2021-12-19 23:09:15 +01:00
"=================================================================\n",
2021-12-20 04:30:49 +01:00
"Total params: 1,631,747\n",
"Trainable params: 1,631,747\n",
2021-12-19 23:09:15 +01:00
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"None\n"
]
}
],
"source": [
"print(model.summary())"
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 25,
2021-12-19 23:09:15 +01:00
"id": "cardiac-highland",
"metadata": {},
"outputs": [],
"source": [
2021-12-20 04:30:49 +01:00
"model.compile(optimizer=RMSprop(learning_rate=0.0001),\n",
" loss=tf.keras.losses.CategoricalCrossentropy(),\n",
2021-12-19 23:09:15 +01:00
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 26,
2021-12-19 23:09:15 +01:00
"id": "informed-baker",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 3s 393ms/step - loss: 0.9368 - accuracy: 0.5897 - val_loss: 0.7715 - val_accuracy: 0.6786\n",
2021-12-19 23:09:15 +01:00
"Epoch 2/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 352ms/step - loss: 0.8682 - accuracy: 0.6310 - val_loss: 0.7431 - val_accuracy: 0.6786\n",
2021-12-19 23:09:15 +01:00
"Epoch 3/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 352ms/step - loss: 0.7690 - accuracy: 0.6667 - val_loss: 0.6637 - val_accuracy: 0.7143\n",
2021-12-19 23:09:15 +01:00
"Epoch 4/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 351ms/step - loss: 0.7816 - accuracy: 0.6310 - val_loss: 0.6656 - val_accuracy: 0.6607\n",
2021-12-19 23:09:15 +01:00
"Epoch 5/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 346ms/step - loss: 0.7070 - accuracy: 0.6667 - val_loss: 0.7131 - val_accuracy: 0.5893\n",
2021-12-19 23:09:15 +01:00
"Epoch 6/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 351ms/step - loss: 0.6677 - accuracy: 0.6905 - val_loss: 0.5662 - val_accuracy: 0.7143\n",
2021-12-19 23:09:15 +01:00
"Epoch 7/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 350ms/step - loss: 0.6171 - accuracy: 0.7262 - val_loss: 0.7703 - val_accuracy: 0.5893\n",
2021-12-19 23:09:15 +01:00
"Epoch 8/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 351ms/step - loss: 0.6001 - accuracy: 0.7738 - val_loss: 0.4881 - val_accuracy: 0.8036\n",
2021-12-19 23:09:15 +01:00
"Epoch 9/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 362ms/step - loss: 0.4903 - accuracy: 0.7821 - val_loss: 0.4565 - val_accuracy: 0.9107\n",
2021-12-19 23:09:15 +01:00
"Epoch 10/10\n",
2021-12-20 04:30:49 +01:00
"6/6 [==============================] - 2s 351ms/step - loss: 0.5107 - accuracy: 0.7976 - val_loss: 0.5301 - val_accuracy: 0.8393\n"
2021-12-19 23:09:15 +01:00
]
}
],
"source": [
2021-12-20 04:30:49 +01:00
"history = model.fit(train_generator, steps_per_epoch=6, epochs=10, verbose=1,\n",
" validation_data = validation_generator, validation_steps = 4)"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
2021-12-20 04:30:49 +01:00
"execution_count": 29,
2021-12-19 23:09:15 +01:00
"id": "inclusive-chess",
"metadata": {},
"outputs": [
{
2021-12-20 04:30:49 +01:00
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x1c0ecf5e190>"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEKCAYAAAAfGVI8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAA5cElEQVR4nO3deVhV5fbA8e9iUAQVJ8QB5xlFU8mxnC1N00xNbbYcmq1udZvrVvd3mwfLSiwrmzQ1zaw0zQFLyzQVFYcUJ3BCFBCV+f39sdFQQQ94Nhs46/M8PJ5hn70XR91r73dYrxhjUEop5bm8nA5AKaWUszQRKKWUh9NEoJRSHk4TgVJKeThNBEop5eE0ESillIezLRGIyFQROSwim/J5X0RkoojsEJEoEWlnVyxKKaXyZ+cdwadAvwu83x9okvMzDvjAxliUUkrlw7ZEYIyJBI5eYJPBwDRj+R2oJCI17YpHKaVU3nwcPHZtYF+u57E5rx04d0MRGYd110BAQED75s2bF0mASilVWqxdu/aIMSYor/ecTAQuM8ZEABEA4eHhZs2aNQ5HpJRSJYuI7MnvPSdHDcUBdXI9D8l5TSmlVBFyMhHMA27NGT3UCUgyxpzXLKSUUspetjUNicjXQA+gmojEAs8BvgDGmA+BH4FrgB3ASWC0XbEopZTKn22JwBgz6iLvG+Beu46vlFLKNTqzWCmlPJwmAqWU8nCaCJRSysNpIlBKKQ+niUAppTycJgKllPJwmgiUUsrDaSJQSikPp4lAKaU8nCYCpZTycJoIlFLKw2kiUEopD6eJQCmlPJwmAqWU8nCaCJRSysNpIlBKKQ+niUAppTycJgKllPJwmgiUUs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"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
2021-12-19 23:09:15 +01:00
}
],
"source": [
2021-12-20 04:30:49 +01:00
"plt.plot(history.history['accuracy'], label='accuracy')\n",
"plt.plot(history.history['val_accuracy'], label = 'val_accuracy')\n",
"plt.xlabel('Epoch')\n",
"plt.ylabel('Accuracy')\n",
"plt.ylim([0.5, 1])\n",
"plt.legend(loc='lower right')"
2021-12-19 23:09:15 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "marine-satellite",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}