Format: application/octet-stream
⬇ Download File (14.0 KB)
   1  {
   2   "cells": [
   3    {
   4     "cell_type": "code",
   5     "execution_count": 1,
   6     "id": "cad950d4",
   7     "metadata": {},
   8     "outputs": [
   9      {
  10       "name": "stdout",
  11       "output_type": "stream",
  12       "text": [
  13        "D:/cats-v-dogs created\n",
  14        "D:/cats-v-dogs/training created\n",
  15        "D:/cats-v-dogs/testing created\n",
  16        "D:/cats-v-dogs/training/cats created\n",
  17        "D:/cats-v-dogs/training/dogs created\n",
  18        "D:/cats-v-dogs/testing/cats created\n",
  19        "D:/cats-v-dogs/testing/dogs created\n",
  20        "666.jpg is zero length, so ignoring\n"
  21       ]
  22      }
  23     ],
  24     "source": [
  25      "import tensorflow as tf\n",
  26      "from tensorflow import keras\n",
  27      "import os\n",
  28      "import random\n",
  29      "from shutil import copyfile\n",
  30      "\n",
  31      "\n",
  32      "\n",
  33      "def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):\n",
  34      "    all_files = []\n",
  35      "    \n",
  36      "    for file_name in os.listdir(SOURCE):\n",
  37      "        file_path = SOURCE + file_name\n",
  38      "\n",
  39      "        if os.path.getsize(file_path):\n",
  40      "            all_files.append(file_name)\n",
  41      "        else:\n",
  42      "            print('{} is zero length, so ignoring'.format(file_name))\n",
  43      "    \n",
  44      "    n_files = len(all_files)\n",
  45      "    split_point = int(n_files * SPLIT_SIZE)\n",
  46      "    \n",
  47      "    shuffled = random.sample(all_files, n_files)\n",
  48      "    \n",
  49      "    train_set = shuffled[:split_point]\n",
  50      "    test_set = shuffled[split_point:]\n",
  51      "    \n",
  52      "    for file_name in train_set:\n",
  53      "        copyfile(SOURCE + file_name, TRAINING + file_name)\n",
  54      "        \n",
  55      "    for file_name in test_set:\n",
  56      "        copyfile(SOURCE + file_name, TESTING + file_name)\n",
  57      "\n",
  58      "to_create = [\n",
  59      "    'D:/cats-v-dogs',\n",
  60      "    'D:/cats-v-dogs/training',\n",
  61      "    'D:/cats-v-dogs/testing',\n",
  62      "    'D:/cats-v-dogs/training/cats',\n",
  63      "    'D:/cats-v-dogs/training/dogs',\n",
  64      "    'D:/cats-v-dogs/testing/cats',\n",
  65      "    'D:/cats-v-dogs/testing/dogs'\n",
  66      "]\n",
  67      "\n",
  68      "for directory in to_create:\n",
  69      "    try:\n",
  70      "        os.mkdir(directory)\n",
  71      "        print(directory, 'created')\n",
  72      "    except:\n",
  73      "        print(directory, 'failed')\n",
  74      "\n",
  75      "CAT_SOURCE_DIR = r\"D:/PettImages/Cat/\"\n",
  76      "TRAINING_CATS_DIR = r\"D:/cats-v-dogs/training/cats/\"\n",
  77      "TESTING_CATS_DIR = r\"D:/cats-v-dogs/testing/cats/\"\n",
  78      "DOG_SOURCE_DIR = r\"D:/PettImages/Dog/\"\n",
  79      "TRAINING_DOGS_DIR = r\"D:/cats-v-dogs/training/dogs/\"\n",
  80      "TESTING_DOGS_DIR = r\"D:/cats-v-dogs/testing/dogs/\"\n",
  81      "\n",
  82      "split_size = .7\n",
  83      "split_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)\n",
  84      "split_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)\n",
  85      "\n",
  86      "\n",
  87      "\n",
  88      "\n",
  89      "\n",
  90      "\n",
  91      "\n",
  92      "\n"
  93     ]
  94    },
  95    {
  96     "cell_type": "code",
  97     "execution_count": null,
  98     "id": "aa6a7fa8",
  99     "metadata": {},
 100     "outputs": [
 101      {
 102       "name": "stdout",
 103       "output_type": "stream",
 104       "text": [
 105        "Found 2801 images belonging to 2 classes.\n",
 106        "Found 1202 images belonging to 2 classes.\n",
 107        "Model: \"sequential\"\n",
 108        "_________________________________________________________________\n",
 109        "Layer (type)                 Output Shape              Param #   \n",
 110        "=================================================================\n",
 111        "conv2d (Conv2D)              (None, 148, 148, 32)      896       \n",
 112        "_________________________________________________________________\n",
 113        "max_pooling2d (MaxPooling2D) (None, 74, 74, 32)        0         \n",
 114        "_________________________________________________________________\n",
 115        "conv2d_1 (Conv2D)            (None, 72, 72, 64)        18496     \n",
 116        "_________________________________________________________________\n",
 117        "max_pooling2d_1 (MaxPooling2 (None, 36, 36, 64)        0         \n",
 118        "_________________________________________________________________\n",
 119        "conv2d_2 (Conv2D)            (None, 34, 34, 64)        36928     \n",
 120        "_________________________________________________________________\n",
 121        "max_pooling2d_2 (MaxPooling2 (None, 17, 17, 64)        0         \n",
 122        "_________________________________________________________________\n",
 123        "conv2d_3 (Conv2D)            (None, 15, 15, 128)       73856     \n",
 124        "_________________________________________________________________\n",
 125        "max_pooling2d_3 (MaxPooling2 (None, 7, 7, 128)         0         \n",
 126        "_________________________________________________________________\n",
 127        "flatten (Flatten)            (None, 6272)              0         \n",
 128        "_________________________________________________________________\n",
 129        "dense (Dense)                (None, 512)               3211776   \n",
 130        "_________________________________________________________________\n",
 131        "dense_1 (Dense)              (None, 64)                32832     \n",
 132        "_________________________________________________________________\n",
 133        "dense_2 (Dense)              (None, 1)                 65        \n",
 134        "=================================================================\n",
 135        "Total params: 3,374,849\n",
 136        "Trainable params: 3,374,849\n",
 137        "Non-trainable params: 0\n",
 138        "_________________________________________________________________\n",
 139        "Epoch 1/50\n",
 140        "29/29 [==============================] - 167s 6s/step - loss: 0.6929 - accuracy: 0.5112 - val_loss: 0.6913 - val_accuracy: 0.5300\n",
 141        "Epoch 2/50\n",
 142        "29/29 [==============================] - 135s 5s/step - loss: 0.6916 - accuracy: 0.5095 - val_loss: 0.6901 - val_accuracy: 0.5549\n",
 143        "Epoch 3/50\n",
 144        "29/29 [==============================] - 136s 5s/step - loss: 0.6901 - accuracy: 0.5562 - val_loss: 0.6896 - val_accuracy: 0.5699\n",
 145        "Epoch 4/50\n",
 146        "29/29 [==============================] - 133s 5s/step - loss: 0.6894 - accuracy: 0.5619 - val_loss: 0.6882 - val_accuracy: 0.5524\n",
 147        "Epoch 5/50\n",
 148        "29/29 [==============================] - 133s 5s/step - loss: 0.6876 - accuracy: 0.5612 - val_loss: 0.6881 - val_accuracy: 0.5100\n",
 149        "Epoch 6/50\n",
 150        "29/29 [==============================] - 133s 5s/step - loss: 0.6869 - accuracy: 0.5477 - val_loss: 0.6867 - val_accuracy: 0.5932\n",
 151        "Epoch 7/50\n",
 152        "29/29 [==============================] - 133s 5s/step - loss: 0.6873 - accuracy: 0.5441 - val_loss: 0.6854 - val_accuracy: 0.5158\n",
 153        "Epoch 8/50\n",
 154        "29/29 [==============================] - 133s 5s/step - loss: 0.6862 - accuracy: 0.5602 - val_loss: 0.6843 - val_accuracy: 0.5349\n",
 155        "Epoch 9/50\n",
 156        "29/29 [==============================] - 133s 5s/step - loss: 0.6862 - accuracy: 0.5659 - val_loss: 0.6831 - val_accuracy: 0.5632\n",
 157        "Epoch 10/50\n",
 158        "29/29 [==============================] - 133s 5s/step - loss: 0.6855 - accuracy: 0.5787 - val_loss: 0.6820 - val_accuracy: 0.5699\n",
 159        "Epoch 11/50\n",
 160        "29/29 [==============================] - 133s 5s/step - loss: 0.6830 - accuracy: 0.5652 - val_loss: 0.6813 - val_accuracy: 0.5998\n",
 161        "Epoch 12/50\n",
 162        "29/29 [==============================] - 132s 5s/step - loss: 0.6818 - accuracy: 0.5869 - val_loss: 0.6849 - val_accuracy: 0.5574\n",
 163        "Epoch 13/50\n",
 164        "29/29 [==============================] - 133s 5s/step - loss: 0.6821 - accuracy: 0.5698 - val_loss: 0.6790 - val_accuracy: 0.5857\n",
 165        "Epoch 14/50\n",
 166        "29/29 [==============================] - 3630s 125s/step - loss: 0.6798 - accuracy: 0.5801 - val_loss: 0.6772 - val_accuracy: 0.5899\n",
 167        "Epoch 15/50\n",
 168        "29/29 [==============================] - 168s 6s/step - loss: 0.6802 - accuracy: 0.5930 - val_loss: 0.6764 - val_accuracy: 0.5699\n",
 169        "Epoch 16/50\n",
 170        "29/29 [==============================] - 179s 6s/step - loss: 0.6768 - accuracy: 0.6026 - val_loss: 0.6751 - val_accuracy: 0.5849\n",
 171        "Epoch 17/50\n",
 172        "29/29 [==============================] - 154s 5s/step - loss: 0.6770 - accuracy: 0.5837 - val_loss: 0.6761 - val_accuracy: 0.5599\n",
 173        "Epoch 18/50\n",
 174        "29/29 [==============================] - 171s 6s/step - loss: 0.6750 - accuracy: 0.5866 - val_loss: 0.6723 - val_accuracy: 0.5899\n",
 175        "Epoch 19/50\n",
 176        "29/29 [==============================] - 185s 6s/step - loss: 0.6746 - accuracy: 0.5869 - val_loss: 0.6716 - val_accuracy: 0.5882\n",
 177        "Epoch 20/50\n",
 178        "29/29 [==============================] - 168s 6s/step - loss: 0.6743 - accuracy: 0.5884 - val_loss: 0.6702 - val_accuracy: 0.5965\n",
 179        "Epoch 21/50\n",
 180        "29/29 [==============================] - 163s 6s/step - loss: 0.6728 - accuracy: 0.5994 - val_loss: 0.6727 - val_accuracy: 0.5566\n",
 181        "Epoch 22/50\n",
 182        "29/29 [==============================] - 158s 5s/step - loss: 0.6694 - accuracy: 0.5987 - val_loss: 0.6696 - val_accuracy: 0.5807\n",
 183        "Epoch 23/50\n",
 184        "29/29 [==============================] - 153s 5s/step - loss: 0.6717 - accuracy: 0.5987 - val_loss: 0.6708 - val_accuracy: 0.5649\n",
 185        "Epoch 24/50\n",
 186        " 4/29 [===>..........................] - ETA: 1:28 - loss: 0.6726 - accuracy: 0.6075"
 187       ]
 188      }
 189     ],
 190     "source": [
 191      "\n",
 192      "\n",
 193      "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
 194      "train_img= ImageDataGenerator(rescale=1 / 255,\n",
 195      "    rotation_range=180,\n",
 196      "    width_shift_range=.2,\n",
 197      "    height_shift_range=.2,\n",
 198      "    shear_range=.2,\n",
 199      "    zoom_range=.2,\n",
 200      "    horizontal_flip=True,\n",
 201      "    fill_mode='nearest' )\n",
 202      "\n",
 203      "val_img=ImageDataGenerator(rescale=1/255)\n",
 204      "\n",
 205      "train_img_gen= train_img.flow_from_directory('D:/cats-v-dogs/training', target_size=(150,150), batch_size=100,  class_mode='binary')\n",
 206      "validation_img_gen = val_img.flow_from_directory('D:/cats-v-dogs/testing', target_size=(150,150), batch_size=100, class_mode='binary')\n",
 207      "\n",
 208      "\n",
 209      "\n",
 210      "model= keras.models.Sequential([\n",
 211      "        keras.layers.Conv2D(32,(3,3),input_shape=(150,150,3), activation= 'relu' ),\n",
 212      "        keras.layers.MaxPool2D(2,2),\n",
 213      "        \n",
 214      "        keras.layers.Conv2D(64,(3,3), activation= 'relu'),\n",
 215      "        keras.layers.MaxPool2D(2,2),\n",
 216      "    \n",
 217      "        keras.layers.Conv2D(64,(3,3), activation= 'relu'),\n",
 218      "        keras.layers.MaxPool2D(2,2),\n",
 219      "    \n",
 220      "        keras.layers.Conv2D(128,(3,3), activation= 'relu'),\n",
 221      "        keras.layers.MaxPool2D(2,2),\n",
 222      "    \n",
 223      "        keras.layers.Flatten(),\n",
 224      "        keras.layers.Dense(units=512, activation='relu'),\n",
 225      "        keras.layers.Dense(units=64, activation='relu'),\n",
 226      "        keras.layers.Dense(units=1, activation= 'sigmoid')\n",
 227      "])\n",
 228      "\n",
 229      "model.summary()\n",
 230      "\n",
 231      "from tensorflow.keras.optimizers import RMSprop\n",
 232      "\n",
 233      "model.compile(RMSprop(lr=0.00001), loss='binary_crossentropy', metrics=['accuracy'])\n",
 234      "\n",
 235      "history= model.fit(train_img_gen, epochs=50, verbose=1,validation_data=validation_img_gen)\n",
 236      "\n",
 237      "\n",
 238      "\n"
 239     ]
 240    },
 241    {
 242     "cell_type": "code",
 243     "execution_count": null,
 244     "id": "50d5b341",
 245     "metadata": {},
 246     "outputs": [],
 247     "source": [
 248      "# PLOT LOSS AND ACCURACY\n",
 249      "%matplotlib inline\n",
 250      "\n",
 251      "import matplotlib.image  as mpimg\n",
 252      "import matplotlib.pyplot as plt\n",
 253      "\n",
 254      "#-----------------------------------------------------------\n",
 255      "# Retrieve a list of list results on training and test data\n",
 256      "# sets for each training epoch\n",
 257      "#-----------------------------------------------------------\n",
 258      "acc=history.history['accuracy']\n",
 259      "val_acc=history.history['val_accuracy']\n",
 260      "loss=history.history['loss']\n",
 261      "val_loss=history.history['val_loss']\n",
 262      "\n",
 263      "epochs=range(len(acc)) # Get number of epochs\n",
 264      "\n",
 265      "#------------------------------------------------\n",
 266      "# Plot training and validation accuracy per epoch\n",
 267      "#------------------------------------------------\n",
 268      "plt.plot(epochs, acc, 'r', label=\"Training Accuracy\")\n",
 269      "plt.plot(epochs, val_acc, 'b', label=\"Validation Accuracy\")\n",
 270      "plt.legend()\n",
 271      "plt.title('Training and validation accuracy')\n",
 272      "plt.figure()\n",
 273      "\n",
 274      "#------------------------------------------------\n",
 275      "# Plot training and validation loss per epoch\n",
 276      "#------------------------------------------------\n",
 277      "plt.plot(epochs, loss, 'r', \"Training Loss\")\n",
 278      "plt.plot(epochs, val_loss, 'b', \"Validation Loss\")\n",
 279      "\n",
 280      "\n",
 281      "plt.title('Training and validation loss')\n",
 282      "\n",
 283      "# Desired output. Charts with training and validation metrics. No crash :)\n"
 284     ]
 285    },
 286    {
 287     "cell_type": "code",
 288     "execution_count": null,
 289     "id": "10dc297c",
 290     "metadata": {},
 291     "outputs": [],
 292     "source": [
 293      "import numpy as np\n",
 294      "from google.colab import files\n",
 295      "from keras.preprocessing import image\n",
 296      "\n",
 297      "uploaded = files.upload()\n",
 298      "\n",
 299      "for fn in uploaded.keys():\n",
 300      " \n",
 301      "  # predicting images\n",
 302      "    path = '/content/' + fn\n",
 303      "    img = image.load_img(path, target_size=(150,150))\n",
 304      "    x = image.img_to_array(img)\n",
 305      "    x = np.expand_dims(x, axis=0)\n",
 306      "\n",
 307      "    images = np.vstack([x])\n",
 308      "    classes = model.predict(images, batch_size=10)\n",
 309      "    print(classes[0])\n",
 310      "    if classes[0]>0.5:\n",
 311      "        print(fn + \" is a dog\")\n",
 312      "    else:\n",
 313      "        print(fn + \" is a cat\")"
 314     ]
 315    }
 316   ],
 317   "metadata": {
 318    "kernelspec": {
 319     "display_name": "Python 3",
 320     "language": "python",
 321     "name": "python3"
 322    },
 323    "language_info": {
 324     "codemirror_mode": {
 325      "name": "ipython",
 326      "version": 3
 327     },
 328     "file_extension": ".py",
 329     "mimetype": "text/x-python",
 330     "name": "python",
 331     "nbconvert_exporter": "python",
 332     "pygments_lexer": "ipython3",
 333     "version": "3.8.8"
 334    }
 335   },
 336   "nbformat": 4,
 337   "nbformat_minor": 5
 338  }
 339