From 8a2b6e54a68239f9a6d3df7ace1339f1df471e68 Mon Sep 17 00:00:00 2001 From: pbiskup Date: Sat, 4 Dec 2021 18:01:55 +0100 Subject: [PATCH] init --- README.md | 0 cnn.ipynb | 307 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 README.md create mode 100644 cnn.ipynb diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/cnn.ipynb b/cnn.ipynb new file mode 100644 index 0000000..74d47bd --- /dev/null +++ b/cnn.ipynb @@ -0,0 +1,307 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 160, + "id": "b026b65f", + "metadata": {}, + "outputs": [], + "source": [ + "import tensorflow as tf\n", + "from tensorflow.keras import datasets\n", + "from tensorflow.keras.models import Sequential\n", + "from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n", + "import os\n", + "from sklearn.model_selection import train_test_split\n", + "import cv2\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 162, + "id": "f4b3c889", + "metadata": {}, + "outputs": [], + "source": [ + "def preprocess(img):\n", + " scale_percent = 10\n", + " width = int(img.shape[1] * scale_percent / 100)\n", + " height = int(img.shape[0] * scale_percent / 100)\n", + " dim = (width, height)\n", + " resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)\n", + "# resized = resized.flatten()\n", + " return resized\n", + "\n", + "def readData(data_links):\n", + " x, y = [], []\n", + " for link in data_links:\n", + " img = cv2.imread(link, cv2.IMREAD_COLOR)\n", + " img = preprocess(img)\n", + " label = link.split(\"/\")[1].split('_')[1]\n", + " new_label = 4\n", + " \n", + " if label == '3':\n", + " new_label = 0\n", + " elif label == '5':\n", + " new_label = 1\n", + " elif label == '8':\n", + " new_label = 2\n", + " else:\n", + " print(\"chuj\")\n", + " \n", + " x.append(img)\n", + " y.append(new_label)\n", + "\n", + " return x, y\n", + "\n", + "data_links = os.listdir(\"data/\")\n", + "data_links = [\"data/\" + x for x in data_links]" + ] + }, + { + "cell_type": "code", + "execution_count": 163, + "id": "b9df2aa9", + "metadata": {}, + "outputs": [], + "source": [ + "x, y = readData(data_links)" + ] + }, + { + "cell_type": "code", + "execution_count": 164, + "id": "a8977fc6", + "metadata": {}, + "outputs": [], + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)" + ] + }, + { + "cell_type": "code", + "execution_count": 165, + "id": "6764d3d3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "675\n" + ] + } + ], + "source": [ + "print(len(X_train))" + ] + }, + { + "cell_type": "code", + "execution_count": 166, + "id": "97e8bc4e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(60, 80, 3)" + ] + }, + "execution_count": 166, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_train[0].shape" + ] + }, + { + "cell_type": "code", + "execution_count": 167, + "id": "2df14985", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 2, 2, 2, 2, 1, 1, 0, 0, 2, 0, 2, 2, 0, 2, 2, 0, 1, 2, 1, 0, 1, 2, 0, 1, 0, 2, 0, 0, 2, 1, 0, 2, 1, 0, 1, 1, 1, 2, 0, 0, 1, 0, 2, 0, 0, 2, 1, 0, 0, 0, 1, 0, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 0, 1, 2, 2, 2, 1, 1, 1, 2, 2, 0, 2, 2, 1, 0, 2, 0, 1, 2, 0, 1, 0, 1, 1, 2, 0, 0, 2, 1, 2, 0, 1, 1, 2, 0, 2, 0, 1, 1, 0, 0, 0, 1, 1, 2, 2, 2, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 2, 0, 1, 2, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 1, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 2, 2, 2, 2, 1, 2, 0, 0, 0, 0, 0,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0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 2, 0, 1, 1, 0, 0, 0, 1, 2, 1, 2, 1, 1, 1, 2, 0, 2, 1, 1, 0, 0, 0, 2, 2, 0, 1, 2, 2, 1, 1, 1, 2, 2, 2, 0, 0, 1, 1, 0, 2, 1, 2, 0, 1, 2, 1, 1, 0, 2, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 1, 2, 2, 2, 0, 1, 1, 0, 0, 1, 0, 1, 0, 2, 0, 1, 0, 2, 1, 2, 0, 1, 1, 0, 1, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 2, 2, 0, 2, 1, 0, 2, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 2, 2, 1, 2, 0, 1, 1, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 0, 0, 2, 0, 2, 2, 2, 0, 2, 1, 2, 1, 1, 1, 2, 2, 2, 1, 0, 2, 2, 1, 1, 1, 0, 1, 0, 1, 0, 0, 2]\n" + ] + } + ], + "source": [ + "print(y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 168, + "id": "2d3f1e5e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 1 2 0 0 0 2 2 1 2 2 2 2 0 1 2 0 2 1 1 0 0 2 0 1 0 1 0 1 0 0 2 2 0 1 1 1\n", + " 2 0 2 2 2 0 1 1 0 0 0 2 2 2 1 0 0 1 0 1 2 1 0 2 0 1 0 2 0 2 1 0 0 0 0 1 0\n", + " 1 0 1 2 0 2 0 2 1 1 1 2 1 2 1 2 0 2 0 2 2 1 1 0 1 2 1 2 2 2 2 0 0 0 0 1 1\n", + " 0 0 0 0 0 0 2 0 2 2 0 1 1 2 1 1 2 2 1 2 2 1 0 1 2 1 2 0 2 1 0 2 1 1 2 2 2\n", + " 2 1 2 2 0 0 0 0 0 1 1 1 0 2 2 2 2 2 2 0 0 0 2 2 0 2 0 1 0 1 1 1 2 0 1 0 1\n", + " 1 0 0 0 1 1 1 2 0 2 1 2 0 2 2 0 2 0 2 2 2 1 2 1 2 0 2 1 2 2 2 2 0 1 0 1 2\n", + " 2 1 1]\n" + ] + } + ], + "source": [ + "X_train = np.array([i / 255.0 for i in X_train], dtype=np.float64)\n", + "X_test = np.array([i / 255.0 for i in X_test], dtype=np.float64)\n", + "y_train = np.array(y_train, dtype=np.int64)\n", + "y_test = np.array(y_test, dtype=np.int64)\n", + "\n", + "print(y_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "id": "2a10ce01", + "metadata": {}, + "outputs": [], + "source": [ + "model = Sequential()\n", + "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(Flatten())\n", + "model.add(Dense(512, activation='relu'))\n", + "model.add(Dense(3, activation='sigmoid'))" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "id": "67f6cef9", + "metadata": {}, + "outputs": [], + "source": [ + "model.compile(optimizer='adam',\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n", + " metrics=['accuracy'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 172, + "id": "802da4db", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/10\n", + "22/22 [==============================] - 2s 98ms/step - loss: 1.1007 - accuracy: 0.4000 - val_loss: 0.9776 - val_accuracy: 0.5422\n", + "Epoch 2/10\n", + "22/22 [==============================] - 2s 90ms/step - loss: 0.8537 - accuracy: 0.6667 - val_loss: 0.7531 - val_accuracy: 0.6489\n", + "Epoch 3/10\n", + "22/22 [==============================] - 2s 89ms/step - loss: 0.7288 - accuracy: 0.6948 - val_loss: 1.5778 - val_accuracy: 0.4978\n", + "Epoch 4/10\n", + "22/22 [==============================] - 2s 91ms/step - loss: 0.7752 - accuracy: 0.6593 - val_loss: 0.7269 - val_accuracy: 0.7333\n", + "Epoch 5/10\n", + "22/22 [==============================] - 2s 89ms/step - loss: 0.5980 - accuracy: 0.7585 - val_loss: 0.6848 - val_accuracy: 0.7067\n", + "Epoch 6/10\n", + "22/22 [==============================] - 2s 91ms/step - loss: 0.4614 - accuracy: 0.8326 - val_loss: 0.7548 - val_accuracy: 0.6978\n", + "Epoch 7/10\n", + "22/22 [==============================] - 2s 95ms/step - loss: 0.4666 - accuracy: 0.8119 - val_loss: 0.4485 - val_accuracy: 0.8356\n", + "Epoch 8/10\n", + "22/22 [==============================] - 2s 98ms/step - loss: 0.2721 - accuracy: 0.9156 - val_loss: 0.3949 - val_accuracy: 0.8311\n", + "Epoch 9/10\n", + "22/22 [==============================] - 2s 92ms/step - loss: 0.2106 - accuracy: 0.9289 - val_loss: 0.3096 - val_accuracy: 0.8933\n", + "Epoch 10/10\n", + "22/22 [==============================] - 2s 93ms/step - loss: 0.1317 - accuracy: 0.9556 - val_loss: 0.2744 - val_accuracy: 0.8800\n" + ] + } + ], + "source": [ + "history = model.fit(X_train, y_train, epochs=10, \n", + " validation_data=(X_test, y_test))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a27e69ff", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21696ef3", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebd5b1c1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57828ac4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1893f9da", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1448617", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}