Spaces:
Runtime error
Runtime error
File size: 9,383 Bytes
6682c77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
{
"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": [
"<keras.callbacks.History at 0x7f8ecac538e0>"
]
},
"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": [
"<IPython.core.display.Javascript object>"
],
"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": [
"<IPython.core.display.Javascript object>"
],
"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"
]
}
]
}
]
} |