{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "6B5SMiEcB4KF" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from tensorflow.keras.datasets import fashion_mnist\n", "from tensorflow.keras.applications.inception_v3 import InceptionV3\n", "from tensorflow.keras.preprocessing import image\n", "from tensorflow.keras.models import Model\n", "from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input" ] }, { "cell_type": "code", "source": [ "(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "L7gOA-_llaXt", "outputId": "1fa144e0-55e5-420c-a63c-85bb6d4662e9" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n", "29515/29515 [==============================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n", "26421880/26421880 [==============================] - 1s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n", "5148/5148 [==============================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n", "4422102/4422102 [==============================] - 0s 0us/step\n" ] } ] }, { "cell_type": "code", "source": [ "def format_images(images):\n", " images = images / 255.0\n", " images = np.expand_dims(images, axis=-1)\n", " images = tf.image.resize(images, [80, 80])\n", " images = np.repeat(images[:, :, :, np.newaxis], 3, axis=3)\n", " images = np.squeeze(images)\n", " return images\n" ], "metadata": { "id": "8VRLEaQoETtq" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "train_images = format_images(train_images)\n", "test_images = format_images(test_images)\n" ], "metadata": { "id": "54MG0ww_Bwtg" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "input_tensor = Input(shape=(80, 80, 3))\n", "base_model = InceptionV3(input_tensor = input_tensor, weights ='imagenet', include_top = False)\n", "# add a global spatial average pooling layer\n", "x = base_model.output\n", "x = GlobalAveragePooling2D()(x)\n", "# let's add a fully-connected layer\n", "x = Dense(1024, activation='relu')(x)\n", "# and a logistic layer -- let's say we have 200 classes\n", "predictions = Dense(10, activation='softmax')(x)\n", "model = Model(inputs=base_model.input, outputs=predictions)\n", "for layer in base_model.layers:\n", " layer.trainable = False\n", "model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", "model.fit(train_images, train_labels, epochs=5, )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jnkQxkQPCI1N", "outputId": "234349dd-6463-4be9-d22a-f9119187b56e" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "1875/1875 [==============================] - 563s 297ms/step - loss: 0.6177 - accuracy: 0.8018\n", "Epoch 2/5\n", "1875/1875 [==============================] - 551s 294ms/step - loss: 0.4777 - accuracy: 0.8495\n", "Epoch 3/5\n", "1875/1875 [==============================] - 546s 291ms/step - loss: 0.4332 - accuracy: 0.8702\n", "Epoch 4/5\n", "1875/1875 [==============================] - 543s 290ms/step - loss: 0.4008 - accuracy: 0.8863\n", "Epoch 5/5\n", "1875/1875 [==============================] - 542s 289ms/step - loss: 0.3582 - accuracy: 0.9005\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "code", "source": [ "model.save('modelo.h5')" ], "metadata": { "id": "IlTi2-WJSfUy" }, "execution_count": 7, "outputs": [] }, { "cell_type": "code", "source": [ "from google.colab import files\n", "files.download('modelo.h5')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, "id": "hoRhJsjhSoGx", "outputId": "59ba12d5-b56a-4499-e132-13cdc71bf13b" }, "execution_count": 8, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_7a4bda3f-a31b-46c1-a859-1873b79d883b\", \"modelo.h5\", 104970160)" ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)\n", "\n", "print('\\ntest_accuracy:', test_acc)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GnWxbi9BTa-a", "outputId": "afb104dd-0560-417a-9238-ae83ed3a68e2" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "313/313 - 84s - loss: 0.8012 - accuracy: 0.8377 - 84s/epoch - 270ms/step\n", "\n", "test_accuracy: 0.8377000093460083\n" ] } ] } ] }