{ "cells": [ { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "# File renaming for consistency\n", "import os\n", "from os import path\n", "\n", "for count, filename in enumerate(os.listdir(\"deadlift2\")):\n", " src = \"deadlift2/\" + filename\n", " string = \"deadlift2/deadlift_\" + str(count) + \".jpg\"\n", " os.rename(src, string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-to-classify-photos-of-dogs-and-cats/" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(549,) ()\n" ] } ], "source": [ "from os import listdir\n", "from numpy import asarray\n", "from numpy import save\n", "from keras.preprocessing.image import load_img\n", "from keras.preprocessing.image import img_to_array\n", "\n", "folder = \"train/\"\n", "photos, labels = list(), list()\n", "\n", "for file in listdir(folder):\n", " output = 0.0\n", " if file.startswith(\"squat\"):\n", " output = 1.0\n", " if file.startswith(\"deadlift\"):\n", " output = 2.0\n", " photo = load_img(folder + file, target_size=(150,150))\n", " photo = img_to_array\n", " \n", " photos.append(photo)\n", " labels.append(output)\n", "photos = asarray(photos)\n", "labels = asarray(output)\n", "print(photos.shape, labels.shape)\n", "\n", "save(\"exercise_photos.npy\", photos)\n", "save(\"exercise_labels.npy\", photos)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(549,) (549,)\n" ] } ], "source": [ "from numpy import load\n", "photos = load(\"exercise_photos.npy\",allow_pickle=True)\n", "labels = load(\"exercise_labels.npy\",allow_pickle=True)\n", "\n", "print(photos.shape, labels.shape)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "# Directory Generation\n", "from os import makedirs\n", "dataset_home = \"dataset/\"\n", "subdirs = [\"train/\", \"test/\"]\n", "for subdir in subdirs:\n", " # create label subdirectories\n", " labeldirs = [\"bench/\", \"squat/\", \"deadlift/\"]\n", " for labldir in labeldirs:\n", " newdir = dataset_home + subdir + labldir\n", " makedirs(newdir)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "# Segment into testing and training images\n", "import random\n", "from shutil import copyfile\n", "random.seed(1)\n", "ratio = 0.2\n", "dataset_home = \"dataset/\"\n", "src_directory = \"images/\"\n", "for file in listdir(src_directory):\n", " src = src_directory + '/' + file\n", " dst_dir = \"train/\"\n", " if random.random() < ratio:\n", " dst_dir = \"test/\"\n", " if file.startswith(\"bench\"):\n", " dst = dataset_home + dst_dir + \"bench/\" + file\n", " elif file.startswith(\"squat\"):\n", " dst = dataset_home + dst_dir + \"squat/\" + file\n", " else:\n", " dst = dataset_home + dst_dir + \"deadlift/\" + file\n", " copyfile(src, dst) " ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 448 images belonging to 3 classes.\n", "1\n", "Found 101 images belonging to 3 classes.\n" ] }, { "ename": "InvalidArgumentError", "evalue": " Matrix size-incompatible: In[0]: [128,3], In[1]: [128,1]\n\t [[node gradient_tape/sequential_21/dense_41/MatMul (defined at :115) ]] [Op:__inference_train_function_17586]\n\nFunction call stack:\ntrain_function\n", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mInvalidArgumentError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 122\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 123\u001b[0m \u001b[1;31m# entry point, run the test harness\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 124\u001b[1;33m \u001b[0mrun_test_harness\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m\u001b[0m in \u001b[0;36mrun_test_harness\u001b[1;34m()\u001b[0m\n\u001b[0;32m 113\u001b[0m class_mode='categorical', batch_size=128, target_size=(150, 150))\n\u001b[0;32m 114\u001b[0m \u001b[1;31m# fit model\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 115\u001b[1;33m history = model.fit(train_it, steps_per_epoch=len(train_it),\n\u001b[0m\u001b[0;32m 116\u001b[0m validation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n\u001b[0;32m 117\u001b[0m \u001b[1;31m# evaluate model\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py\u001b[0m in \u001b[0;36m_method_wrapper\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 106\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_method_wrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 107\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_in_multi_worker_mode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# pylint: disable=protected-access\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 108\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 109\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 110\u001b[0m \u001b[1;31m# Running inside `run_distribute_coordinator` already.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[0;32m 1096\u001b[0m batch_size=batch_size):\n\u001b[0;32m 1097\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mon_train_batch_begin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1098\u001b[1;33m \u001b[0mtmp_logs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtrain_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1099\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mdata_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshould_sync\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1100\u001b[0m \u001b[0mcontext\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0masync_wait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m 778\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 779\u001b[0m \u001b[0mcompiler\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"nonXla\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 780\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 781\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 782\u001b[0m \u001b[0mnew_tracing_count\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_tracing_count\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m_call\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m 838\u001b[0m \u001b[1;31m# Lifting succeeded, so variables are initialized and we can run the\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 839\u001b[0m \u001b[1;31m# stateless function.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 840\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_stateless_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 841\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 842\u001b[0m \u001b[0mcanon_args\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcanon_kwds\u001b[0m \u001b[1;33m=\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 2827\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_lock\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2828\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_define_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2829\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_filtered_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# pylint: disable=protected-access\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2830\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2831\u001b[0m \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_filtered_call\u001b[1;34m(self, args, kwargs, cancellation_manager)\u001b[0m\n\u001b[0;32m 1841\u001b[0m \u001b[0;31m`\u001b[0m\u001b[0margs\u001b[0m\u001b[0;31m`\u001b[0m \u001b[1;32mand\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;31m`\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1842\u001b[0m \"\"\"\n\u001b[1;32m-> 1843\u001b[1;33m return self._call_flat(\n\u001b[0m\u001b[0;32m 1844\u001b[0m [t for t in nest.flatten((args, kwargs), expand_composites=True)\n\u001b[0;32m 1845\u001b[0m if isinstance(t, (ops.Tensor,\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_call_flat\u001b[1;34m(self, args, captured_inputs, cancellation_manager)\u001b[0m\n\u001b[0;32m 1921\u001b[0m and executing_eagerly):\n\u001b[0;32m 1922\u001b[0m \u001b[1;31m# No tape is watching; skip to running the function.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1923\u001b[1;33m return self._build_call_outputs(self._inference_function.call(\n\u001b[0m\u001b[0;32m 1924\u001b[0m ctx, args, cancellation_manager=cancellation_manager))\n\u001b[0;32m 1925\u001b[0m forward_backward = self._select_forward_and_backward_functions(\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36mcall\u001b[1;34m(self, ctx, args, cancellation_manager)\u001b[0m\n\u001b[0;32m 543\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0m_InterpolateFunctionError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 544\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcancellation_manager\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 545\u001b[1;33m outputs = execute.execute(\n\u001b[0m\u001b[0;32m 546\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msignature\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 547\u001b[0m \u001b[0mnum_outputs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_num_outputs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\execute.py\u001b[0m in \u001b[0;36mquick_execute\u001b[1;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[0;32m 57\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 58\u001b[0m \u001b[0mctx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mensure_initialized\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 59\u001b[1;33m tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,\n\u001b[0m\u001b[0;32m 60\u001b[0m inputs, attrs, num_outputs)\n\u001b[0;32m 61\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mcore\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_NotOkStatusException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mInvalidArgumentError\u001b[0m: Matrix size-incompatible: In[0]: [128,3], In[1]: [128,1]\n\t [[node gradient_tape/sequential_21/dense_41/MatMul (defined at :115) ]] [Op:__inference_train_function_17586]\n\nFunction call stack:\ntrain_function\n" ] } ], "source": [ "# Baseline CNN Model\n", "import sys\n", "from matplotlib import pyplot\n", "import keras\n", "from keras.utils import to_categorical\n", "from keras.models import Sequential\n", "from keras.layers import Conv2D\n", "from keras.layers import MaxPooling2D\n", "from keras.layers import Dense\n", "from keras.layers import Flatten\n", "from keras.layers import Dropout\n", "from keras.optimizers import SGD\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Dropout, Flatten\n", "from keras.layers import Conv2D, MaxPooling2D\n", "\n", " \n", "# one block VGG\n", "\"\"\"\n", "def define_model():\n", " model = Sequential()\n", " model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(150, 150, 3)))\n", " model.add(MaxPooling2D((2, 2)))\n", " model.add(Flatten())\n", " model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n", " model.add(Dense(1, activation='sigmoid'))\n", " # compile model\n", " opt = SGD(lr=0.001, momentum=0.9)\n", " model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n", " return model\n", "\"\"\"\n", "\"\"\"\n", "# two block VGG\n", "def define_model():\n", " model = Sequential()\n", " model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(150, 150, 3)))\n", " model.add(MaxPooling2D((2, 2)))\n", " model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n", " model.add(MaxPooling2D((2, 2)))\n", " model.add(Flatten())\n", " model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n", " model.add(Dense(1, activation='sigmoid'))\n", " # compile model\n", " opt = SGD(lr=0.001, momentum=0.9)\n", " model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n", " return model\n", "\"\"\"\n", "\n", "\n", "def define_model():\n", " model = Sequential()\n", " model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))\n", " model.add(MaxPooling2D((2, 2)))\n", " model.add(Dropout(0.2))\n", " model.add(Flatten())\n", " model.add(Dense(128, activation='relu'))\n", " model.add(Dense(1, activation='softmax'))\n", " # compile model\n", " #opt = SGD(lr=0.001, momentum=0.9)\n", " model.compile(optimizer=keras.optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n", " return model\n", "\n", "\"\"\"\n", "# three block VGG\n", "def define_model():\n", "\n", " cnn1 = Sequential()\n", " cnn1.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))\n", " cnn1.add(MaxPooling2D((2, 2)))\n", " cnn1.add(Dropout(0.2))\n", "\n", " cnn1.add(Flatten())\n", "\n", " cnn1.add(Dense(128, activation='relu'))\n", " cnn1.add(Dense(10, activation='softmax'))\n", "\n", " cnn1.compile(loss=keras.losses.categorical_crossentropy,\n", " optimizer=keras.optimizers.Adam(),\n", " metrics=['accuracy'])\n", " return cnn1\n", "\n", "\"\"\"\n", "\n", "# plot diagnostic learning curves\n", "def summarize_diagnostics(history):\n", " # plot loss\n", " pyplot.subplot(211)\n", " pyplot.title('Cross Entropy Loss')\n", " pyplot.plot(history.history['loss'], color='blue', label='train')\n", " pyplot.plot(history.history['val_loss'], color='orange', label='test')\n", " # plot accuracy\n", " pyplot.subplot(212)\n", " pyplot.title('Classification Accuracy')\n", " pyplot.plot(history.history['accuracy'], color='blue', label='train')\n", " pyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n", " # save plot to file\n", " filename = sys.argv[0].split('/')[-1]\n", " pyplot.savefig(filename + '_plot.png')\n", " pyplot.close()\n", " \n", "# run the test harness for evaluating a model\n", "def run_test_harness():\n", " # define model\n", " model = define_model()\n", " # create data generator\n", " datagen = ImageDataGenerator(rescale=1.0/255.0)\n", " # prepare iterators\n", " train_it = datagen.flow_from_directory('dataset/train/',\n", " class_mode='categorical', batch_size=128, target_size=(150, 150))\n", " test_it = datagen.flow_from_directory('dataset/test/',\n", " class_mode='categorical', batch_size=128, target_size=(150, 150))\n", " # fit model\n", " history = model.fit(train_it, steps_per_epoch=len(train_it),\n", " validation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n", " # evaluate model\n", " _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)\n", " print('> %.3f' % (acc * 100.0))\n", " # learning curves\n", " summarize_diagnostics(history)\n", " \n", "# entry point, run the test harness\n", "run_test_harness()\n" ] }, { "cell_type": "code", "execution_count": null, "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }