{ "cells": [ { "cell_type": "code", "execution_count": 52, "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", "from sklearn.neural_network import MLPClassifier\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import classification_report\n", "import re" ] }, { "cell_type": "code", "execution_count": 26, "id": "sapphire-monte", "metadata": {}, "outputs": [], "source": [ "def preprocessing(image):\n", " scale_percent = 10\n", " width = int(image.shape[1] * scale_percent / 100)\n", " height = int(image.shape[0] * scale_percent / 100)\n", " dim = (width, height)\n", " return cv2.resize(image, dim, interpolation = cv2.INTER_AREA)\n", "\n", "\n", "def read_data(data_images):\n", " x, y = [], []\n", " for image in data_images:\n", " img = cv2.imread(image, cv2.IMREAD_COLOR)\n", " img = preprocessing(img)\n", " y_label = re.search(r\"(?<=-).(?=-)\", image).group(0)\n", " x.append(img)\n", " y.append(y_label)\n", " return x, y" ] }, { "cell_type": "code", "execution_count": 6, "id": "greenhouse-needle", "metadata": {}, "outputs": [], "source": [ "location = \"capturedframe/\"\n", "data_images = os.listdir(location)\n", "# for x in data_images:\n", "# os.rename(location+x, \"tree-1-\"+ x[13:])\n", "data_images = [location + x for x in data_images if x.endswith(\".png\")]\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "black-channel", "metadata": {}, "outputs": [], "source": [ "print()" ] }, { "cell_type": "code", "execution_count": 28, "id": "built-palestinian", "metadata": {}, "outputs": [], "source": [ "x, y = read_data(data_images)" ] }, { "cell_type": "code", "execution_count": 73, "id": "amber-wisconsin", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']\n" ] } ], "source": [ "print(y)" ] }, { "cell_type": "code", "execution_count": 31, "id": "instant-frequency", "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(x,y, test_size=0.2, random_state=81)" ] }, { "cell_type": "code", "execution_count": 37, "id": "dried-college", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(60, 80, 3)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train[0].shape" ] }, { "cell_type": "code", "execution_count": 38, "id": "tutorial-interpretation", "metadata": {}, "outputs": [], "source": [ "X_train = np.array([x / 255.0 for x in X_train], dtype=np.float64)\n", "X_test = np.array([x / 255.0 for x in X_test], dtype=np.float64)\n", "\n", "y_train = np.array(y_train, dtype=np.int64)\n", "y_test = np.array(y_test, dtype=np.int64)\n" ] }, { "cell_type": "code", "execution_count": 43, "id": "green-being", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[0.00073818 0.00086121 0.00070742]\n", " [0.0009381 0.00112265 0.0009381 ]\n", " [0.00104575 0.00129181 0.00107651]\n", " ...\n", " [0.00246059 0.00273741 0.00247597]\n", " [0.00229143 0.00267589 0.00241446]\n", " [0.00232218 0.00276817 0.00247597]]\n", "\n", " [[0.00089196 0.00099962 0.00081507]\n", " [0.00107651 0.00130719 0.00109189]\n", " [0.0009381 0.00112265 0.0009381 ]\n", " ...\n", " [0.00244521 0.00276817 0.00250673]\n", " [0.00218378 0.00270665 0.0023837 ]\n", " [0.00219915 0.002599 0.0023837 ]]\n", "\n", " [[0.0012303 0.00124567 0.00103037]\n", " [0.00113802 0.00132257 0.00110727]\n", " [0.00099962 0.0012303 0.00103037]\n", " ...\n", " [0.00233756 0.00279892 0.00249135]\n", " [0.00226067 0.00264514 0.00232218]\n", " [0.00226067 0.00267589 0.00236832]]\n", "\n", " ...\n", "\n", " [[0.00084583 0.00101499 0.00083045]\n", " [0.00090734 0.00112265 0.00092272]\n", " [0.00090734 0.00109189 0.00089196]\n", " ...\n", " [0.00229143 0.00292195 0.002599 ]\n", " [0.00210688 0.00255286 0.00224529]\n", " [0.00226067 0.00270665 0.00250673]]\n", "\n", " [[0.00087659 0.00101499 0.00079969]\n", " [0.00079969 0.0009381 0.00075356]\n", " [0.00089196 0.00107651 0.00089196]\n", " ...\n", " [0.00247597 0.00290657 0.00264514]\n", " [0.00236832 0.00270665 0.00246059]\n", " [0.00235294 0.00293733 0.002599 ]]\n", "\n", " [[0.0009381 0.00112265 0.00092272]\n", " [0.00084583 0.00099962 0.00079969]\n", " [0.00084583 0.00099962 0.00081507]\n", " ...\n", " [0.00282968 0.00315263 0.00290657]\n", " [0.00276817 0.0031065 0.0028912 ]\n", " [0.00224529 0.00278354 0.00230681]]]\n" ] } ], "source": [ "print((X_train[0]))" ] }, { "cell_type": "code", "execution_count": 84, "id": "natural-cutting", "metadata": {}, "outputs": [], "source": [ "model = Sequential()" ] }, { "cell_type": "code", "execution_count": 85, "id": "conservative-hypothetical", "metadata": {}, "outputs": [], "source": [ "model.add(Conv2D(32, (3,3), activation='relu', input_shape=(X_train[0].shape)))\n", "model.add(MaxPooling2D((2,2)))\n", "model.add(Conv2D(64, (3,3), activation='relu'))\n", "model.add(MaxPooling2D((2,2)))\n", "\n", "model.add(Conv2D(32, (3,3), activation='relu'))\n", "\n", "model.add(MaxPooling2D((2,2)))\n", "model.add(Flatten())\n", "model.add(Dense(256, activation='relu'))\n", "model.add(Dense(2, activation='sigmoid'))" ] }, { "cell_type": "code", "execution_count": 86, "id": "illegal-zoning", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_6\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "conv2d_16 (Conv2D) (None, 58, 78, 32) 896 \n", "_________________________________________________________________\n", "max_pooling2d_12 (MaxPooling (None, 29, 39, 32) 0 \n", "_________________________________________________________________\n", "conv2d_17 (Conv2D) (None, 27, 37, 64) 18496 \n", "_________________________________________________________________\n", "max_pooling2d_13 (MaxPooling (None, 13, 18, 64) 0 \n", "_________________________________________________________________\n", "conv2d_18 (Conv2D) (None, 11, 16, 32) 18464 \n", "_________________________________________________________________\n", "max_pooling2d_14 (MaxPooling (None, 5, 8, 32) 0 \n", "_________________________________________________________________\n", "flatten_6 (Flatten) (None, 1280) 0 \n", "_________________________________________________________________\n", "dense_12 (Dense) (None, 256) 327936 \n", "_________________________________________________________________\n", "dense_13 (Dense) (None, 2) 514 \n", "=================================================================\n", "Total params: 366,306\n", "Trainable params: 366,306\n", "Non-trainable params: 0\n", "_________________________________________________________________\n", "None\n" ] } ], "source": [ "print(model.summary())" ] }, { "cell_type": "code", "execution_count": 87, "id": "cardiac-highland", "metadata": {}, "outputs": [], "source": [ "model.compile(optimizer='adam',\n", " loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n", " metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 88, "id": "informed-baker", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/10\n", "9/9 [==============================] - 1s 62ms/step - loss: 0.4567 - accuracy: 0.9173 - val_loss: 0.0150 - val_accuracy: 1.0000\n", "Epoch 2/10\n", "9/9 [==============================] - 0s 52ms/step - loss: 0.0021 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 3/10\n", "9/9 [==============================] - 0s 50ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 4/10\n", "9/9 [==============================] - 0s 50ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 5/10\n", "9/9 [==============================] - 0s 51ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 6/10\n", "9/9 [==============================] - 0s 50ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 7/10\n", "9/9 [==============================] - 0s 53ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 8/10\n", "9/9 [==============================] - 0s 52ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 9/10\n", "9/9 [==============================] - 0s 50ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n", "Epoch 10/10\n", "9/9 [==============================] - 0s 49ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000\n" ] } ], "source": [ "history = model.fit(X_train, y_train, epochs=10,\n", " validation_data=(X_test, y_test))" ] }, { "cell_type": "code", "execution_count": 72, "id": "inclusive-chess", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3/3 - 0s - loss: 0.0000e+00 - accuracy: 1.0000\n" ] } ], "source": [ "test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)" ] }, { "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 }