710 lines
1.7 MiB
Plaintext
710 lines
1.7 MiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "6db5fd63",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"import numpy as np\n",
|
||
|
"import os\n",
|
||
|
"import PIL\n",
|
||
|
"import tensorflow as tf\n",
|
||
|
"\n",
|
||
|
"from tensorflow import keras\n",
|
||
|
"from tensorflow.keras import layers\n",
|
||
|
"from tensorflow.keras.models import Sequential"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "2cb154b9",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"tf.config.list_physical_devices('GPU')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "4edd961f",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"ename": "KeyboardInterrupt",
|
||
|
"evalue": "",
|
||
|
"output_type": "error",
|
||
|
"traceback": [
|
||
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||
|
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
||
|
"Cell \u001b[1;32mIn[3], line 10\u001b[0m\n\u001b[0;32m 8\u001b[0m image_path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(data_dir, image_class, image)\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m: \n\u001b[1;32m---> 10\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimread\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimage_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 11\u001b[0m tip \u001b[38;5;241m=\u001b[39m imghdr\u001b[38;5;241m.\u001b[39mwhat(image_path)\n\u001b[0;32m 12\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m tip \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m image_exts: \n",
|
||
|
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import cv2\n",
|
||
|
"import imghdr\n",
|
||
|
"import os\n",
|
||
|
"data_dir = 'trash/validate' \n",
|
||
|
"image_exts = ['jpeg','jpg', 'bmp', 'png']\n",
|
||
|
"for image_class in os.listdir(data_dir): \n",
|
||
|
" for image in os.listdir(os.path.join(data_dir, image_class)):\n",
|
||
|
" image_path = os.path.join(data_dir, image_class, image)\n",
|
||
|
" try: \n",
|
||
|
" img = cv2.imread(image_path)\n",
|
||
|
" tip = imghdr.what(image_path)\n",
|
||
|
" if tip not in image_exts: \n",
|
||
|
" print('Image not in ext list {}'.format(image_path))\n",
|
||
|
" os.remove(image_path)\n",
|
||
|
" except Exception as e: \n",
|
||
|
" print('Issue with image {}'.format(image_path))\n",
|
||
|
" # os.remove(image_path)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "a82eb04c",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"batch_size = 32\n",
|
||
|
"img_height = 180\n",
|
||
|
"img_width = 180"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "1bde4009",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Found 7524 files belonging to 4 classes.\n",
|
||
|
"Found 1531 files belonging to 4 classes.\n",
|
||
|
"Found 2562 files belonging to 4 classes.\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"train_ds = tf.keras.utils.image_dataset_from_directory(\n",
|
||
|
" \"trash/train\",\n",
|
||
|
" image_size = (img_height, img_width),\n",
|
||
|
" seed = 123,\n",
|
||
|
" batch_size = batch_size\n",
|
||
|
")\n",
|
||
|
"val_ds = tf.keras.utils.image_dataset_from_directory(\n",
|
||
|
" \"trash/validate\",\n",
|
||
|
" image_size = (img_height, img_width),\n",
|
||
|
" seed = 123,\n",
|
||
|
" batch_size = batch_size\n",
|
||
|
")\n",
|
||
|
"test_ds = tf.keras.utils.image_dataset_from_directory(\n",
|
||
|
" \"trash/test\",\n",
|
||
|
" image_size = (img_height, img_width),\n",
|
||
|
" seed = 123,\n",
|
||
|
" batch_size = batch_size\n",
|
||
|
")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "f4fe3a65",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"['biological', 'glass', 'paper', 'plastic']\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"class_names = train_ds.class_names\n",
|
||
|
"print(class_names)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "3bbea143",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxkAAAMsCAYAAAA4VG/hAAAAOXRFWHRTb2Z0d2FyZQBNYXR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HDBgw4C3HN3/zN/Pbv/3bPPfcc+90VwYMGPCQYJBFBjxIDDEZAwYMGPAeR1EUK7+fe+45fvEXf5Hf//t//zvToQEDBgwY8J7HEJMxYMCAAe9xPPPMM3z3d383zzzzDC+//DL/6B/9I7Is46/+1b/6TndtwIABAwa8RzGQjAEDBgx4j+Prv/7r+Zmf+RneeOMN8jzn9/7e38uP/diPnSq4NWDAgAEDBjwoDDEZAwYMGDBgwIABAwYMeKAYYjIGDBgwYMCAAQMGDBjwQDGQjAEDBgwYMGDAgAEDBjxQDCRjwIABAwYMGDBgwIABDxRD4PfbBGstIfjFAgYLFqS1CAtogxACpMQKqKymkYJKWA6rOf/f3/4f/PcXf4OD8oRC17RW01jDoqmpdYuVkOY5SEGWZSghscZSlSXL5ZLWWlpj0MYgrWGkJKJtMU2L0S1CQNM0mNZAo6ExyFKjakumJSObsJdO+cov/DB/9P/8w3zh+5/i2tYuqRFIKxBpipAKKwRWuHNECEAgLYj+xRAM+DzFrYO7l95WiMs/KPey7f3u2w9fOy+Urd/eprYvGwZ3P+e/0rIfcyzh73kNXNB+f4O34v3tdU70fgtW/z7QQ67dh7Puy/p9WP992XbeLB50u49cufpA23u78PHffgkppZtTrcUYA4AxZuM1EkLEbdeXBxhYecjitvb8Z9AK/Dy3aaUF0x1z03O0qV/W+v74bda327Rs03mH6xHXia6r/f36n03
|
||
|
"text/plain": [
|
||
|
"<Figure size 1000x1000 with 9 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"\n",
|
||
|
"plt.figure(figsize=(10, 10))\n",
|
||
|
"for images, labels in train_ds.take(1):\n",
|
||
|
" for i in range(9):\n",
|
||
|
" ax = plt.subplot(3, 3, i + 1)\n",
|
||
|
" plt.imshow(images[i].numpy().astype(\"uint8\"))\n",
|
||
|
" plt.title(class_names[labels[i]])\n",
|
||
|
" plt.axis(\"off\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"id": "ae666555",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"AUTOTUNE = tf.data.AUTOTUNE\n",
|
||
|
"\n",
|
||
|
"train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\n",
|
||
|
"val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"id": "aadbc1b5",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"normalization_layer = layers.Rescaling(1./255)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"id": "e515e074",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"0.0 1.0\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))\n",
|
||
|
"image_batch, labels_batch = next(iter(normalized_ds))\n",
|
||
|
"first_image = image_batch[0]\n",
|
||
|
"# Notice the pixel values are now in `[0,1]`.\n",
|
||
|
"print(np.min(first_image), np.max(first_image))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"id": "6d8901bf",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"num_classes = len(class_names)\n",
|
||
|
"\n",
|
||
|
"model = Sequential([\n",
|
||
|
" layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n",
|
||
|
" layers.Conv2D(16, 3, padding='same', activation='relu'),\n",
|
||
|
" layers.MaxPooling2D(),\n",
|
||
|
" layers.Conv2D(32, 3, padding='same', activation='relu'),\n",
|
||
|
" layers.MaxPooling2D(),\n",
|
||
|
" layers.Conv2D(64, 3, padding='same', activation='relu'),\n",
|
||
|
" layers.MaxPooling2D(),\n",
|
||
|
" layers.Flatten(),\n",
|
||
|
" layers.Dense(128, activation='relu'),\n",
|
||
|
" layers.Dense(num_classes)\n",
|
||
|
"])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"id": "13ace97f",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"model.compile(optimizer='adam',\n",
|
||
|
" loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
|
||
|
" metrics=['accuracy'])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"id": "8a493ba3",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Model: \"sequential\"\n",
|
||
|
"_________________________________________________________________\n",
|
||
|
" Layer (type) Output Shape Param # \n",
|
||
|
"=================================================================\n",
|
||
|
" rescaling_1 (Rescaling) (None, 180, 180, 3) 0 \n",
|
||
|
" \n",
|
||
|
" conv2d (Conv2D) (None, 180, 180, 16) 448 \n",
|
||
|
" \n",
|
||
|
" max_pooling2d (MaxPooling2D (None, 90, 90, 16) 0 \n",
|
||
|
" ) \n",
|
||
|
" \n",
|
||
|
" conv2d_1 (Conv2D) (None, 90, 90, 32) 4640 \n",
|
||
|
" \n",
|
||
|
" max_pooling2d_1 (MaxPooling (None, 45, 45, 32) 0 \n",
|
||
|
" 2D) \n",
|
||
|
" \n",
|
||
|
" conv2d_2 (Conv2D) (None, 45, 45, 64) 18496 \n",
|
||
|
" \n",
|
||
|
" max_pooling2d_2 (MaxPooling (None, 22, 22, 64) 0 \n",
|
||
|
" 2D) \n",
|
||
|
" \n",
|
||
|
" flatten (Flatten) (None, 30976) 0 \n",
|
||
|
" \n",
|
||
|
" dense (Dense) (None, 128) 3965056 \n",
|
||
|
" \n",
|
||
|
" dense_1 (Dense) (None, 4) 516 \n",
|
||
|
" \n",
|
||
|
"=================================================================\n",
|
||
|
"Total params: 3,989,156\n",
|
||
|
"Trainable params: 3,989,156\n",
|
||
|
"Non-trainable params: 0\n",
|
||
|
"_________________________________________________________________\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model.summary()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"id": "ab563098",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Epoch 1/10\n",
|
||
|
"236/236 [==============================] - 101s 426ms/step - loss: 0.9672 - accuracy: 0.5795 - val_loss: 0.8036 - val_accuracy: 0.6656\n",
|
||
|
"Epoch 2/10\n",
|
||
|
"236/236 [==============================] - 118s 499ms/step - loss: 0.6830 - accuracy: 0.7228 - val_loss: 0.6583 - val_accuracy: 0.7466\n",
|
||
|
"Epoch 3/10\n",
|
||
|
"236/236 [==============================] - 103s 433ms/step - loss: 0.5218 - accuracy: 0.7920 - val_loss: 0.5555 - val_accuracy: 0.7884\n",
|
||
|
"Epoch 4/10\n",
|
||
|
"236/236 [==============================] - 98s 415ms/step - loss: 0.3826 - accuracy: 0.8542 - val_loss: 0.6888 - val_accuracy: 0.7688\n",
|
||
|
"Epoch 5/10\n",
|
||
|
"236/236 [==============================] - 97s 413ms/step - loss: 0.2760 - accuracy: 0.8970 - val_loss: 0.5302 - val_accuracy: 0.8295\n",
|
||
|
"Epoch 6/10\n",
|
||
|
"236/236 [==============================] - 97s 410ms/step - loss: 0.1951 - accuracy: 0.9269 - val_loss: 0.6208 - val_accuracy: 0.8184\n",
|
||
|
"Epoch 7/10\n",
|
||
|
"236/236 [==============================] - 96s 408ms/step - loss: 0.1234 - accuracy: 0.9545 - val_loss: 0.6179 - val_accuracy: 0.8354\n",
|
||
|
"Epoch 8/10\n",
|
||
|
"236/236 [==============================] - 98s 414ms/step - loss: 0.0947 - accuracy: 0.9680 - val_loss: 0.6647 - val_accuracy: 0.8406\n",
|
||
|
"Epoch 9/10\n",
|
||
|
"236/236 [==============================] - 103s 436ms/step - loss: 0.0624 - accuracy: 0.9805 - val_loss: 0.7285 - val_accuracy: 0.8321\n",
|
||
|
"Epoch 10/10\n",
|
||
|
"236/236 [==============================] - 99s 421ms/step - loss: 0.0649 - accuracy: 0.9793 - val_loss: 0.8687 - val_accuracy: 0.8374\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"epochs=10\n",
|
||
|
"history = model.fit(\n",
|
||
|
" train_ds,\n",
|
||
|
" validation_data=val_ds,\n",
|
||
|
" epochs=epochs\n",
|
||
|
")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"id": "2456bf81",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAqgAAAKqCAYAAAD2cKxXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAADX/0lEQVR4nOzdd3gU5fbA8e/upickAdIogUAKEAhFQKSDAiEgCqIicqUoeEWwIRaUouiF609FrOC1YUNQRETpIB2U3gOEGloakE7a7vz+mOxCSIAkZDO7m/N5nn12Mjs7czaEyclbzqtTFEVBCCGEEEIIG6HXOgAhhBBCCCGuJQmqEEIIIYSwKZKgCiGEEEIImyIJqhBCCCGEsCmSoAohhBBCCJsiCaoQQgghhLApkqAKIYQQQgibIgmqEEIIIYSwKZKgCiGEEEIImyIJagUZPnw4ISEh5XrvG2+8gU6nq9iAbMypU6fQ6XTMmTOn0q+t0+l44403LF/PmTMHnU7HqVOnbvnekJAQhg8fXqHx3M7PihCi9OS+fHNyX75K7su2x+ETVJ1OV6rHunXrtA61ynv22WfR6XQcO3bshse8/vrr6HQ69u3bV4mRld358+d544032LNnj9ahlCg2NhadToebmxupqalahyOqGLkv2w+5L1uX+Y+E9957T+tQbI6T1gFY2/fff1/k6++++45Vq1YV29+kSZPbus4XX3yByWQq13snTpzIq6++elvXdwRDhgzh448/Zu7cuUyePLnEY3766SeioqJo3rx5ua/z2GOP8cgjj+Dq6lruc9zK+fPnefPNNwkJCaFly5ZFXrudn5WK8sMPPxAUFMTly5dZsGABI0eO1DQeUbXIfdl+yH1ZaMXhE9R//etfRb7++++/WbVqVbH918vOzsbDw6PU13F2di5XfABOTk44OTn8P8UttWvXjrCwMH766acSb4Rbt27l5MmT/Pe//72t6xgMBgwGw22d43bczs9KRVAUhblz5/Loo49y8uRJfvzxR5tNULOysvD09NQ6DFHB5L5sP+S+LLTi8F38pdGtWzeaNWvGzp076dKlCx4eHrz22msA/P777/Tt25fatWvj6upKaGgob731Fkajscg5rh+/cm2z/f/+9z9CQ0NxdXWlbdu2bN++vch7SxrrpNPpGDt2LIsWLaJZs2a4urrStGlTli9fXiz+devW0aZNG9zc3AgNDeXzzz8v9fipjRs38tBDD1GvXj1cXV0JDg7mhRde4MqVK8U+n5eXF+fOnaN///54eXnh7+/P+PHji30vUlNTGT58OD4+Pvj6+jJs2LBSdyMPGTKEw4cPs2vXrmKvzZ07F51Ox+DBg8nLy2Py5Mm0bt0aHx8fPD096dy5M2vXrr3lNUoa66QoCm+//TZ169bFw8OD7t27c/DgwWLvvXTpEuPHjycqKgovLy+8vb2JiYlh7969lmPWrVtH27ZtARgxYoSlu9I8zquksU5ZWVm8+OKLBAcH4+rqSqNGjXjvvfdQFKXIcWX5ubiRzZs3c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uri4AGpX9KVLlygoKKBNmzYldkPdzOrVq8nLy+OZZ54p0v32/PPPFzvW1dUVvV79b2M0Grl48SJeXl40atSozNc1W7p0KQaDgWeffbbI/hdffBFFUVi2bFmR/bf6ubiZZcuWcfHiRQYPHmzZN3jwYPbu3Vuk6+zXX39Fp9MxZcqUYucwf48WLVqEyWRi8uTJlu/J9ceUx6hRo4qNRbv25zQ/P5+LFy8SFhaGr69vke/7r7/+SosWLRgwYMAN4+7Rowe1a9fmxx9/tLx24MAB9u3bd8sxkKJyyX1Z7stV4b5
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x800 with 2 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"acc = history.history['accuracy']\n",
|
||
|
"val_acc = history.history['val_accuracy']\n",
|
||
|
"\n",
|
||
|
"loss = history.history['loss']\n",
|
||
|
"val_loss = history.history['val_loss']\n",
|
||
|
"\n",
|
||
|
"epochs_range = range(epochs)\n",
|
||
|
"\n",
|
||
|
"plt.figure(figsize=(8, 8))\n",
|
||
|
"plt.subplot(1, 2, 1)\n",
|
||
|
"plt.plot(epochs_range, acc, label='Training Accuracy')\n",
|
||
|
"plt.plot(epochs_range, val_acc, label='Validation Accuracy')\n",
|
||
|
"plt.legend(loc='lower right')\n",
|
||
|
"plt.title('Training and Validation Accuracy')\n",
|
||
|
"\n",
|
||
|
"plt.subplot(1, 2, 2)\n",
|
||
|
"plt.plot(epochs_range, loss, label='Training Loss')\n",
|
||
|
"plt.plot(epochs_range, val_loss, label='Validation Loss')\n",
|
||
|
"plt.legend(loc='upper right')\n",
|
||
|
"plt.title('Training and Validation Loss')\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"id": "8b5af9af",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"data_augmentation = keras.Sequential(\n",
|
||
|
" [\n",
|
||
|
" layers.RandomFlip(\"horizontal\",\n",
|
||
|
" input_shape=(img_height,\n",
|
||
|
" img_width,\n",
|
||
|
" 3)),\n",
|
||
|
" layers.RandomRotation(0.1),\n",
|
||
|
" layers.RandomZoom(0.1),\n",
|
||
|
" ]\n",
|
||
|
")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"id": "1873e170",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxkAAAMWCAYAAACdtUsqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9WZNtyXUmiH3Lfe9zIu6QE5AgQBAACYAAC2R1sdStsjaTtfQiPUm/Tw960V/QD+gHWZWp1TWoqruKxQkgCALIAYlM5HCHiHO2+1p6WOM+cQFcmsFMDFlsIDJunLMH3+7L1/rWTCIieDgejofj4Xg4Ho6H4+F4OB6Oh+Ph+B0d7f/XA3g4Ho6H4+F4OB6Oh+PheDgejofj/7+OByXj4Xg4Ho6H4+F4OB6Oh+PheDgejt/p8aBkPBwPx8PxcDwcD8fD8XA8HA/Hw/E7PR6UjIfj4Xg4Ho6H4+F4OB6Oh+PheDh+p8eDkvFwPBwPx8PxcDwcD8fD8XA8HA/H7/R4UDIejofj4Xg4Ho6H4+F4OB6Oh+Ph+J0eD0rGw/FwPBwPx8PxcDwcD8fD8XA8HL/T40HJeDgejofj4Xg4Ho6H4+F4OB6Oh+N3ejwoGQ/Hw/FwPBwPx8PxcDwcD8fD8XD8To/ldU/84c9+ifP5FuexYQrjeLxCaysIAoABmZBxBo8TRKb+LQJAIDwhLCAi9N5BfYHMCQIAMM7nM/7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uqbLgS4h4Pi2joHUvvaLQYqHWmoHu7teQx5PRIaZBzwmiGh5IPmpGDEtTS7v3hAGsv/aoMhlGzsLEQB/W6z3RYe9nJIJD4sxHKfcV
|
||
|
"text/plain": [
|
||
|
"<Figure size 1000x1000 with 9 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.figure(figsize=(10, 10))\n",
|
||
|
"for images, _ in train_ds.take(1):\n",
|
||
|
" for i in range(9):\n",
|
||
|
" augmented_images = data_augmentation(images)\n",
|
||
|
" ax = plt.subplot(3, 3, i + 1)\n",
|
||
|
" plt.imshow(augmented_images[0].numpy().astype(\"uint8\"))\n",
|
||
|
" plt.axis(\"off\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 18,
|
||
|
"id": "17833614",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"model = Sequential([\n",
|
||
|
" data_augmentation,\n",
|
||
|
" layers.Rescaling(1./255),\n",
|
||
|
" layers.Conv2D(16, 3, padding='same', activation='relu'),\n",
|
||
|
" layers.MaxPooling2D(),\n",
|
||
|
" layers.Conv2D(32, 3, padding='same', activation='relu'),\n",
|
||
|
" layers.MaxPooling2D(),\n",
|
||
|
" layers.Conv2D(64, 3, padding='same', activation='relu'),\n",
|
||
|
" layers.MaxPooling2D(),\n",
|
||
|
" layers.Dropout(0.2),\n",
|
||
|
" layers.Flatten(),\n",
|
||
|
" layers.Dense(128, activation='relu'),\n",
|
||
|
" layers.Dense(num_classes)\n",
|
||
|
"])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 19,
|
||
|
"id": "ad1fd3fe",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"model.compile(optimizer='adam',\n",
|
||
|
" loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
|
||
|
" metrics=['accuracy'])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"id": "51028e4e",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Model: \"sequential_2\"\n",
|
||
|
"_________________________________________________________________\n",
|
||
|
" Layer (type) Output Shape Param # \n",
|
||
|
"=================================================================\n",
|
||
|
" sequential_1 (Sequential) (None, 180, 180, 3) 0 \n",
|
||
|
" \n",
|
||
|
" rescaling_2 (Rescaling) (None, 180, 180, 3) 0 \n",
|
||
|
" \n",
|
||
|
" conv2d_3 (Conv2D) (None, 180, 180, 16) 448 \n",
|
||
|
" \n",
|
||
|
" max_pooling2d_3 (MaxPooling (None, 90, 90, 16) 0 \n",
|
||
|
" 2D) \n",
|
||
|
" \n",
|
||
|
" conv2d_4 (Conv2D) (None, 90, 90, 32) 4640 \n",
|
||
|
" \n",
|
||
|
" max_pooling2d_4 (MaxPooling (None, 45, 45, 32) 0 \n",
|
||
|
" 2D) \n",
|
||
|
" \n",
|
||
|
" conv2d_5 (Conv2D) (None, 45, 45, 64) 18496 \n",
|
||
|
" \n",
|
||
|
" max_pooling2d_5 (MaxPooling (None, 22, 22, 64) 0 \n",
|
||
|
" 2D) \n",
|
||
|
" \n",
|
||
|
" dropout (Dropout) (None, 22, 22, 64) 0 \n",
|
||
|
" \n",
|
||
|
" flatten_1 (Flatten) (None, 30976) 0 \n",
|
||
|
" \n",
|
||
|
" dense_2 (Dense) (None, 128) 3965056 \n",
|
||
|
" \n",
|
||
|
" dense_3 (Dense) (None, 4) 516 \n",
|
||
|
" \n",
|
||
|
"=================================================================\n",
|
||
|
"Total params: 3,989,156\n",
|
||
|
"Trainable params: 3,989,156\n",
|
||
|
"Non-trainable params: 0\n",
|
||
|
"_________________________________________________________________\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model.summary()\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 21,
|
||
|
"id": "d494a467",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Epoch 1/15\n",
|
||
|
"236/236 [==============================] - 109s 459ms/step - loss: 1.0434 - accuracy: 0.5354 - val_loss: 0.8396 - val_accuracy: 0.6251\n",
|
||
|
"Epoch 2/15\n",
|
||
|
"236/236 [==============================] - 109s 463ms/step - loss: 0.8393 - accuracy: 0.6411 - val_loss: 0.7897 - val_accuracy: 0.6558\n",
|
||
|
"Epoch 3/15\n",
|
||
|
"236/236 [==============================] - 117s 496ms/step - loss: 0.7672 - accuracy: 0.6809 - val_loss: 0.7359 - val_accuracy: 0.7035\n",
|
||
|
"Epoch 4/15\n",
|
||
|
"236/236 [==============================] - 120s 508ms/step - loss: 0.7099 - accuracy: 0.7027 - val_loss: 0.7405 - val_accuracy: 0.6871\n",
|
||
|
"Epoch 5/15\n",
|
||
|
"236/236 [==============================] - 115s 489ms/step - loss: 0.6702 - accuracy: 0.7240 - val_loss: 0.7335 - val_accuracy: 0.7211\n",
|
||
|
"Epoch 6/15\n",
|
||
|
"236/236 [==============================] - 109s 461ms/step - loss: 0.6064 - accuracy: 0.7582 - val_loss: 0.7130 - val_accuracy: 0.7302\n",
|
||
|
"Epoch 7/15\n",
|
||
|
"236/236 [==============================] - 109s 460ms/step - loss: 0.5666 - accuracy: 0.7727 - val_loss: 0.6493 - val_accuracy: 0.7564\n",
|
||
|
"Epoch 8/15\n",
|
||
|
"236/236 [==============================] - 110s 464ms/step - loss: 0.5299 - accuracy: 0.7939 - val_loss: 0.5655 - val_accuracy: 0.7936\n",
|
||
|
"Epoch 9/15\n",
|
||
|
"236/236 [==============================] - 109s 460ms/step - loss: 0.5043 - accuracy: 0.8069 - val_loss: 0.5580 - val_accuracy: 0.7956\n",
|
||
|
"Epoch 10/15\n",
|
||
|
"236/236 [==============================] - 109s 460ms/step - loss: 0.4594 - accuracy: 0.8266 - val_loss: 0.6662 - val_accuracy: 0.7720\n",
|
||
|
"Epoch 11/15\n",
|
||
|
"236/236 [==============================] - 111s 471ms/step - loss: 0.4437 - accuracy: 0.8359 - val_loss: 0.5410 - val_accuracy: 0.8034\n",
|
||
|
"Epoch 12/15\n",
|
||
|
"236/236 [==============================] - 116s 492ms/step - loss: 0.4287 - accuracy: 0.8381 - val_loss: 0.5495 - val_accuracy: 0.8106\n",
|
||
|
"Epoch 13/15\n",
|
||
|
"236/236 [==============================] - 126s 534ms/step - loss: 0.3930 - accuracy: 0.8511 - val_loss: 0.5732 - val_accuracy: 0.8027\n",
|
||
|
"Epoch 14/15\n",
|
||
|
"236/236 [==============================] - 114s 481ms/step - loss: 0.3790 - accuracy: 0.8624 - val_loss: 0.5392 - val_accuracy: 0.8197\n",
|
||
|
"Epoch 15/15\n",
|
||
|
"236/236 [==============================] - 111s 468ms/step - loss: 0.3558 - accuracy: 0.8650 - val_loss: 0.5117 - val_accuracy: 0.8217\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"epochs = 15\n",
|
||
|
"history = model.fit(\n",
|
||
|
" train_ds,\n",
|
||
|
" validation_data=val_ds,\n",
|
||
|
" epochs=epochs\n",
|
||
|
")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 23,
|
||
|
"id": "5f971072",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAqgAAAKqCAYAAAD2cKxXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGo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w/jnP/9pBAUFFbp/nqtz587GFVdcccGYqgt18efz9vZm/PjxhV739fV1bKelpZGYmEi/fv3IzMxk586dJZ73pptuol69eo6v7X+17du3r8RjBw4cSGxsrOPrTp06ERQU5DjWarWyePFiRo4cSVRUlGO/Fi1aMGzYsBLPDwU/X0ZGBomJifTp0wfDMNiwYUOh/e+8884CX/fr16/AZ1mwYAEeHh6Ov9zBHFt07733lioeMMenHTlyhN9++83x2qxZs/Dy8uKGG25wnNPLywswu6JPnTpFXl4e3bt3L7Ib6kIWL15MTk4O9957b4Hut/vvv7/Qvt7e3ri5mf9trFYrSUlJBAQE0Lp16zJf127BggW4u7tz3333FXj9wQcfxDAMfvrppwKvl/RzcSE//fQTSUlJjBo1yvHaqFGj2LRpU4Gus2+++QaLxcJTTz1V6Bz279G8efOw2WxMnTrV8T05f5/yuP322wuNRTv35zQ3N5ekpCRatGhBSEhIge/7N998Q+fOnbnmmmuKjXvgwIFERUXx+eefO97bunUrmzdvLnEMpDiX7su6L9eF+3JpYom
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x800 with 2 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"acc = history.history['accuracy']\n",
|
||
|
"val_acc = history.history['val_accuracy']\n",
|
||
|
"\n",
|
||
|
"loss = history.history['loss']\n",
|
||
|
"val_loss = history.history['val_loss']\n",
|
||
|
"\n",
|
||
|
"epochs_range = range(epochs)\n",
|
||
|
"\n",
|
||
|
"plt.figure(figsize=(8, 8))\n",
|
||
|
"plt.subplot(1, 2, 1)\n",
|
||
|
"plt.plot(epochs_range, acc, label='Training Accuracy')\n",
|
||
|
"plt.plot(epochs_range, val_acc, label='Validation Accuracy')\n",
|
||
|
"plt.legend(loc='lower right')\n",
|
||
|
"plt.title('Training and Validation Accuracy')\n",
|
||
|
"\n",
|
||
|
"plt.subplot(1, 2, 2)\n",
|
||
|
"plt.plot(epochs_range, loss, label='Training Loss')\n",
|
||
|
"plt.plot(epochs_range, val_loss, label='Validation Loss')\n",
|
||
|
"plt.legend(loc='upper right')\n",
|
||
|
"plt.title('Training and Validation Loss')\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"id": "1c65bca1",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"81/81 [==============================] - 9s 110ms/step - loss: 0.6138 - accuracy: 0.8009\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[0.6138237714767456, 0.800936758518219]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model.evaluate(test_ds)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"id": "3b3a2363",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"model.save(\"m_with_data_augmentation.h5\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 34,
|
||
|
"id": "9013d852",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"1/1 [==============================] - 0s 22ms/step\n",
|
||
|
"This image most likely belongs to biological with a 90.06 percent confidence.\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"sunflower_path = \"trash/test/biological/biological476.jpg\"\n",
|
||
|
"\n",
|
||
|
"img = tf.keras.utils.load_img(\n",
|
||
|
" sunflower_path, target_size=(img_height, img_width)\n",
|
||
|
")\n",
|
||
|
"img_array = tf.keras.utils.img_to_array(img)\n",
|
||
|
"img_array = tf.expand_dims(img_array, 0) # Create a batch\n",
|
||
|
"\n",
|
||
|
"predictions = model.predict(img_array)\n",
|
||
|
"score = tf.nn.softmax(predictions[0])\n",
|
||
|
"\n",
|
||
|
"print(\n",
|
||
|
" \"This image most likely belongs to {} with a {:.2f} percent confidence.\"\n",
|
||
|
" .format(class_names[np.argmax(score)], 100 * np.max(score))\n",
|
||
|
")"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "myenv",
|
||
|
"language": "python",
|
||
|
"name": "myenv"
|
||
|
},
|
||
|
"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.13"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|