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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"cells": [
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 1,
|
20 |
+
"metadata": {
|
21 |
+
"id": "6B5SMiEcB4KF"
|
22 |
+
},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"import tensorflow as tf\n",
|
26 |
+
"import matplotlib.pyplot as plt\n",
|
27 |
+
"import numpy as np\n",
|
28 |
+
"from tensorflow.keras.datasets import fashion_mnist\n",
|
29 |
+
"from tensorflow.keras.applications.inception_v3 import InceptionV3\n",
|
30 |
+
"from tensorflow.keras.preprocessing import image\n",
|
31 |
+
"from tensorflow.keras.models import Model\n",
|
32 |
+
"from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"source": [
|
38 |
+
"(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
|
39 |
+
],
|
40 |
+
"metadata": {
|
41 |
+
"colab": {
|
42 |
+
"base_uri": "https://localhost:8080/"
|
43 |
+
},
|
44 |
+
"id": "L7gOA-_llaXt",
|
45 |
+
"outputId": "1fa144e0-55e5-420c-a63c-85bb6d4662e9"
|
46 |
+
},
|
47 |
+
"execution_count": 2,
|
48 |
+
"outputs": [
|
49 |
+
{
|
50 |
+
"output_type": "stream",
|
51 |
+
"name": "stdout",
|
52 |
+
"text": [
|
53 |
+
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n",
|
54 |
+
"29515/29515 [==============================] - 0s 0us/step\n",
|
55 |
+
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
|
56 |
+
"26421880/26421880 [==============================] - 1s 0us/step\n",
|
57 |
+
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
|
58 |
+
"5148/5148 [==============================] - 0s 0us/step\n",
|
59 |
+
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
|
60 |
+
"4422102/4422102 [==============================] - 0s 0us/step\n"
|
61 |
+
]
|
62 |
+
}
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"source": [
|
68 |
+
"def format_images(images):\n",
|
69 |
+
" images = images / 255.0\n",
|
70 |
+
" images = np.expand_dims(images, axis=-1)\n",
|
71 |
+
" images = tf.image.resize(images, [80, 80])\n",
|
72 |
+
" images = np.repeat(images[:, :, :, np.newaxis], 3, axis=3)\n",
|
73 |
+
" images = np.squeeze(images)\n",
|
74 |
+
" return images\n"
|
75 |
+
],
|
76 |
+
"metadata": {
|
77 |
+
"id": "8VRLEaQoETtq"
|
78 |
+
},
|
79 |
+
"execution_count": 3,
|
80 |
+
"outputs": []
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"source": [
|
85 |
+
"train_images = format_images(train_images)\n",
|
86 |
+
"test_images = format_images(test_images)\n"
|
87 |
+
],
|
88 |
+
"metadata": {
|
89 |
+
"id": "54MG0ww_Bwtg"
|
90 |
+
},
|
91 |
+
"execution_count": 4,
|
92 |
+
"outputs": []
|
93 |
+
},
|
94 |
+
{
|
95 |
+
"cell_type": "code",
|
96 |
+
"source": [
|
97 |
+
"input_tensor = Input(shape=(80, 80, 3))\n",
|
98 |
+
"base_model = InceptionV3(input_tensor = input_tensor, weights ='imagenet', include_top = False)\n",
|
99 |
+
"# add a global spatial average pooling layer\n",
|
100 |
+
"x = base_model.output\n",
|
101 |
+
"x = GlobalAveragePooling2D()(x)\n",
|
102 |
+
"# let's add a fully-connected layer\n",
|
103 |
+
"x = Dense(1024, activation='relu')(x)\n",
|
104 |
+
"# and a logistic layer -- let's say we have 200 classes\n",
|
105 |
+
"predictions = Dense(10, activation='softmax')(x)\n",
|
106 |
+
"model = Model(inputs=base_model.input, outputs=predictions)\n",
|
107 |
+
"for layer in base_model.layers:\n",
|
108 |
+
" layer.trainable = False\n",
|
109 |
+
"model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
|
110 |
+
"model.fit(train_images, train_labels, epochs=5, )"
|
111 |
+
],
|
112 |
+
"metadata": {
|
113 |
+
"colab": {
|
114 |
+
"base_uri": "https://localhost:8080/"
|
115 |
+
},
|
116 |
+
"id": "jnkQxkQPCI1N",
|
117 |
+
"outputId": "234349dd-6463-4be9-d22a-f9119187b56e"
|
118 |
+
},
|
119 |
+
"execution_count": 6,
|
120 |
+
"outputs": [
|
121 |
+
{
|
122 |
+
"output_type": "stream",
|
123 |
+
"name": "stdout",
|
124 |
+
"text": [
|
125 |
+
"Epoch 1/5\n",
|
126 |
+
"1875/1875 [==============================] - 563s 297ms/step - loss: 0.6177 - accuracy: 0.8018\n",
|
127 |
+
"Epoch 2/5\n",
|
128 |
+
"1875/1875 [==============================] - 551s 294ms/step - loss: 0.4777 - accuracy: 0.8495\n",
|
129 |
+
"Epoch 3/5\n",
|
130 |
+
"1875/1875 [==============================] - 546s 291ms/step - loss: 0.4332 - accuracy: 0.8702\n",
|
131 |
+
"Epoch 4/5\n",
|
132 |
+
"1875/1875 [==============================] - 543s 290ms/step - loss: 0.4008 - accuracy: 0.8863\n",
|
133 |
+
"Epoch 5/5\n",
|
134 |
+
"1875/1875 [==============================] - 542s 289ms/step - loss: 0.3582 - accuracy: 0.9005\n"
|
135 |
+
]
|
136 |
+
},
|
137 |
+
{
|
138 |
+
"output_type": "execute_result",
|
139 |
+
"data": {
|
140 |
+
"text/plain": [
|
141 |
+
"<keras.callbacks.History at 0x7f8ecac538e0>"
|
142 |
+
]
|
143 |
+
},
|
144 |
+
"metadata": {},
|
145 |
+
"execution_count": 6
|
146 |
+
}
|
147 |
+
]
|
148 |
+
},
|
149 |
+
{
|
150 |
+
"cell_type": "code",
|
151 |
+
"source": [
|
152 |
+
"model.save('modelo.h5')"
|
153 |
+
],
|
154 |
+
"metadata": {
|
155 |
+
"id": "IlTi2-WJSfUy"
|
156 |
+
},
|
157 |
+
"execution_count": 7,
|
158 |
+
"outputs": []
|
159 |
+
},
|
160 |
+
{
|
161 |
+
"cell_type": "code",
|
162 |
+
"source": [
|
163 |
+
"from google.colab import files\n",
|
164 |
+
"files.download('modelo.h5')"
|
165 |
+
],
|
166 |
+
"metadata": {
|
167 |
+
"colab": {
|
168 |
+
"base_uri": "https://localhost:8080/",
|
169 |
+
"height": 17
|
170 |
+
},
|
171 |
+
"id": "hoRhJsjhSoGx",
|
172 |
+
"outputId": "59ba12d5-b56a-4499-e132-13cdc71bf13b"
|
173 |
+
},
|
174 |
+
"execution_count": 8,
|
175 |
+
"outputs": [
|
176 |
+
{
|
177 |
+
"output_type": "display_data",
|
178 |
+
"data": {
|
179 |
+
"text/plain": [
|
180 |
+
"<IPython.core.display.Javascript object>"
|
181 |
+
],
|
182 |
+
"application/javascript": [
|
183 |
+
"\n",
|
184 |
+
" async function download(id, filename, size) {\n",
|
185 |
+
" if (!google.colab.kernel.accessAllowed) {\n",
|
186 |
+
" return;\n",
|
187 |
+
" }\n",
|
188 |
+
" const div = document.createElement('div');\n",
|
189 |
+
" const label = document.createElement('label');\n",
|
190 |
+
" label.textContent = `Downloading \"${filename}\": `;\n",
|
191 |
+
" div.appendChild(label);\n",
|
192 |
+
" const progress = document.createElement('progress');\n",
|
193 |
+
" progress.max = size;\n",
|
194 |
+
" div.appendChild(progress);\n",
|
195 |
+
" document.body.appendChild(div);\n",
|
196 |
+
"\n",
|
197 |
+
" const buffers = [];\n",
|
198 |
+
" let downloaded = 0;\n",
|
199 |
+
"\n",
|
200 |
+
" const channel = await google.colab.kernel.comms.open(id);\n",
|
201 |
+
" // Send a message to notify the kernel that we're ready.\n",
|
202 |
+
" channel.send({})\n",
|
203 |
+
"\n",
|
204 |
+
" for await (const message of channel.messages) {\n",
|
205 |
+
" // Send a message to notify the kernel that we're ready.\n",
|
206 |
+
" channel.send({})\n",
|
207 |
+
" if (message.buffers) {\n",
|
208 |
+
" for (const buffer of message.buffers) {\n",
|
209 |
+
" buffers.push(buffer);\n",
|
210 |
+
" downloaded += buffer.byteLength;\n",
|
211 |
+
" progress.value = downloaded;\n",
|
212 |
+
" }\n",
|
213 |
+
" }\n",
|
214 |
+
" }\n",
|
215 |
+
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
|
216 |
+
" const a = document.createElement('a');\n",
|
217 |
+
" a.href = window.URL.createObjectURL(blob);\n",
|
218 |
+
" a.download = filename;\n",
|
219 |
+
" div.appendChild(a);\n",
|
220 |
+
" a.click();\n",
|
221 |
+
" div.remove();\n",
|
222 |
+
" }\n",
|
223 |
+
" "
|
224 |
+
]
|
225 |
+
},
|
226 |
+
"metadata": {}
|
227 |
+
},
|
228 |
+
{
|
229 |
+
"output_type": "display_data",
|
230 |
+
"data": {
|
231 |
+
"text/plain": [
|
232 |
+
"<IPython.core.display.Javascript object>"
|
233 |
+
],
|
234 |
+
"application/javascript": [
|
235 |
+
"download(\"download_7a4bda3f-a31b-46c1-a859-1873b79d883b\", \"modelo.h5\", 104970160)"
|
236 |
+
]
|
237 |
+
},
|
238 |
+
"metadata": {}
|
239 |
+
}
|
240 |
+
]
|
241 |
+
},
|
242 |
+
{
|
243 |
+
"cell_type": "code",
|
244 |
+
"source": [
|
245 |
+
"test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)\n",
|
246 |
+
"\n",
|
247 |
+
"print('\\ntest_accuracy:', test_acc)"
|
248 |
+
],
|
249 |
+
"metadata": {
|
250 |
+
"colab": {
|
251 |
+
"base_uri": "https://localhost:8080/"
|
252 |
+
},
|
253 |
+
"id": "GnWxbi9BTa-a",
|
254 |
+
"outputId": "afb104dd-0560-417a-9238-ae83ed3a68e2"
|
255 |
+
},
|
256 |
+
"execution_count": 9,
|
257 |
+
"outputs": [
|
258 |
+
{
|
259 |
+
"output_type": "stream",
|
260 |
+
"name": "stdout",
|
261 |
+
"text": [
|
262 |
+
"313/313 - 84s - loss: 0.8012 - accuracy: 0.8377 - 84s/epoch - 270ms/step\n",
|
263 |
+
"\n",
|
264 |
+
"test_accuracy: 0.8377000093460083\n"
|
265 |
+
]
|
266 |
+
}
|
267 |
+
]
|
268 |
+
}
|
269 |
+
]
|
270 |
+
}
|