//记得自己搞好图库哈
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "cat_dog_image_classifier.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/emilyliublair/Machine-Learning-Projects/blob/main/cat_dog_image_classifier.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "la_Oz6oLlub6"
},
"source": [
"try:\n",
" # This command only in Colab.\n",
" %tensorflow_version 2.x\n",
"except Exception:\n",
" pass\n",
"import tensorflow as tf\n",
"\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"import os\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jaF8r6aOl48C"
},
"source": [
"# Get project files\n",
"!wget https://cdn.freecodecamp.org/project-data/cats-and-dogs/cats_and_dogs.zip\n",
"\n",
"!unzip cats_and_dogs.zip\n",
"\n",
"PATH = 'cats_and_dogs'\n",
"\n",
"train_dir = os.path.join(PATH, 'train')\n",
"validation_dir = os.path.join(PATH, 'validation')\n",
"test_dir = os.path.join(PATH, 'test')\n",
"\n",
"# Get number of files in each directory. The train and validation directories\n",
"# each have the subdirecories \"dogs\" and \"cats\".\n",
"total_train = sum([len(files) for r, d, files in os.walk(train_dir)])\n",
"total_val = sum([len(files) for r, d, files in os.walk(validation_dir)])\n",
"total_test = len(os.listdir(test_dir))\n",
"\n",
"# Variables for pre-processing and training.\n",
"batch_size = 128\n",
"epochs = 15\n",
"IMG_HEIGHT = 150\n",
"IMG_WIDTH = 150"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "EOJFeEfumns6"
},
"source": [
"#image generators from image datasets\n",
"train_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1/255)\n",
"validation_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1/255)\n",
"test_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1/255)\n",
"\n",
"train_data_gen = train_image_generator.flow_from_directory(\n",
" directory = train_dir,\n",
" target_size=(IMG_HEIGHT, IMG_WIDTH),\n",
" batch_size=batch_size,\n",
" class_mode=\"binary\",\n",
")\n",
"val_data_gen = validation_image_generator.flow_from_directory(\n",
" directory = validation_dir,\n",
" target_size=(IMG_HEIGHT, IMG_WIDTH),\n",
" batch_size=batch_size,\n",
" class_mode=\"binary\",\n",
")\n",
"test_data_gen = test_image_generator.flow_from_directory(\n",
" directory = PATH,\n",
" classes=['test'],\n",
" target_size=(IMG_HEIGHT, IMG_WIDTH),\n",
" batch_size=batch_size,\n",
" class_mode=\"binary\",\n",
" shuffle=False\n",
")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "TP0WA8j1mt7Q"
},
"source": [
"#function for plotting images\n",
"def plotImages(images_arr, probabilities = False):\n",
" fig, axes = plt.subplots(len(images_arr), 1, figsize=(5,len(images_arr) * 3))\n",
" if probabilities is False:\n",
" for img, ax in zip( images_arr, axes):\n",
" ax.imshow(img)\n",
" ax.axis('off')\n",
" else:\n",
" for img, probability, ax in zip( images_arr, probabilities, axes):\n",
" ax.imshow(img)\n",
" ax.axis('off')\n",
" if probability > 0.5:\n",
" ax.set_title(\"%.2f\" % (probability*100) + \"% dog\")\n",
" else:\n",
" ax.set_title(\"%.2f\" % ((1-probability)*100) + \"% cat\")\n",
" plt.show()\n",
"\n",
"sample_training_images, _ = next(train_data_gen)\n",
"plotImages(sample_training_images[:5])\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-32RRLY_3voj"
},
"source": [
"#random transformations to training data\n",
"train_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n",
" rescale = 1/255,\n",
" rotation_range=40,\n",
" width_shift_range=0.2,\n",
" height_shift_range=0.2,\n",
" shear_range=0.2,\n",
" zoom_range=0.2,\n",
" horizontal_flip=True,\n",
" )\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "pkwq2LFvqabS"
},
"source": [
"#new image generator with transformations\n",
"train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,\n",
" directory=train_dir,\n",
" target_size=(IMG_HEIGHT, IMG_WIDTH),\n",
" class_mode='binary')\n",
"\n",
"augmented_images = [train_data_gen[0][0][0] for i in range(5)]\n",
"\n",
"plotImages(augmented_images)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "k8aZkwMam4UY"
},
"source": [
"#creating the model\n",
"model = Sequential()\n",
"model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 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(Flatten())\n",
"model.add(Dense(64, activation='relu'))\n",
"model.add(Dense(1,activation='sigmoid'))\n",
"\n",
"model.compile(optimizer='adam',\n",
" loss='binary_crossentropy',\n",
" metrics=['accuracy']\n",
" )\n",
"\n",
"model.summary()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "1niQDz5x6K7y"
},
"source": [
"#train model\n",
"history = model.fit(\n",
" train_data_gen,\n",
" steps_per_epoch=int(total_train/batch_size),\n",
" epochs=epochs,\n",
" validation_data=val_data_gen,\n",
" validation_steps=int(total_train/batch_size)\n",
" )"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "5xS51mB56OAC"
},
"source": [
"#visualize accuracy and loss of model\n",
"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()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vYrSifOit2aK"
},
"source": [
"#predictions\n",
"probabilities = model.predict(test_data_gen)\n",
"prediction = model.predict_classes(test_data_gen)\n",
"plotImages([test_data_gen[0][0][i] for i in range(50)],probabilities=probabilities,)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4IH86Ux_u7TZ"
},
"source": [
"#test with challenge\n",
"answers = [1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0,\n",
" 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0,\n",
" 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1,\n",
" 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, \n",
" 0, 0, 0, 0, 0, 0]\n",
"\n",
"correct = 0\n",
"\n",
"for probability, answer in zip(probabilities, answers):\n",
"\n",
" if np.round(probability) == answer:\n",
" correct +=1\n",
"\n",
"percentage_identified = (correct / len(answers))\n",
"\n",
"passed_challenge = percentage_identified > 0.63\n",
"\n",
"print(f\"Your model correctly identified {round(percentage_identified, 2)}% of the images of cats and dogs.\")\n",
"\n",
"if passed_challenge:\n",
" print(\"You passed the challenge!\")\n",
"else:\n",
" print(\"You haven't passed yet. Your model should identify at least 63% of the images. Keep trying. You will get it!\")"
],
"execution_count": null,
"outputs": []
}
]
}