merve HF staff commited on
Commit
4f93e3c
1 Parent(s): 6734239

Upload mnist_convnet.ipynb

Browse files
Files changed (1) hide show
  1. mnist_convnet.ipynb +428 -0
mnist_convnet.ipynb ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "q5DtHwQEzfyR"
7
+ },
8
+ "source": [
9
+ "# Simple MNIST convnet\n",
10
+ "\n",
11
+ "**Author:** [fchollet](https://twitter.com/fchollet)<br>\n",
12
+ "**Date created:** 2015/06/19<br>\n",
13
+ "**Last modified:** 2020/04/21<br>\n",
14
+ "**Description:** A simple convnet that achieves ~99% test accuracy on MNIST."
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "markdown",
19
+ "metadata": {
20
+ "id": "eZlWB3GpzfyT"
21
+ },
22
+ "source": [
23
+ "## Setup"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 2,
29
+ "metadata": {
30
+ "id": "8utAtD_ozfyU"
31
+ },
32
+ "outputs": [],
33
+ "source": [
34
+ "import numpy as np\n",
35
+ "from tensorflow import keras\n",
36
+ "from tensorflow.keras import layers"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "markdown",
41
+ "metadata": {
42
+ "id": "gbQiYBo1zfyV"
43
+ },
44
+ "source": [
45
+ "## Prepare the data"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 3,
51
+ "metadata": {
52
+ "id": "qJZ6R9iFzfyV",
53
+ "outputId": "76eaada0-0f90-41e1-fa22-866d75351911",
54
+ "colab": {
55
+ "base_uri": "https://localhost:8080/"
56
+ }
57
+ },
58
+ "outputs": [
59
+ {
60
+ "output_type": "stream",
61
+ "name": "stdout",
62
+ "text": [
63
+ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
64
+ "11493376/11490434 [==============================] - 0s 0us/step\n",
65
+ "11501568/11490434 [==============================] - 0s 0us/step\n",
66
+ "x_train shape: (60000, 28, 28, 1)\n",
67
+ "60000 train samples\n",
68
+ "10000 test samples\n"
69
+ ]
70
+ }
71
+ ],
72
+ "source": [
73
+ "# Model / data parameters\n",
74
+ "num_classes = 10\n",
75
+ "input_shape = (28, 28, 1)\n",
76
+ "\n",
77
+ "# the data, split between train and test sets\n",
78
+ "(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
79
+ "\n",
80
+ "# Scale images to the [0, 1] range\n",
81
+ "x_train = x_train.astype(\"float32\") / 255\n",
82
+ "x_test = x_test.astype(\"float32\") / 255\n",
83
+ "# Make sure images have shape (28, 28, 1)\n",
84
+ "x_train = np.expand_dims(x_train, -1)\n",
85
+ "x_test = np.expand_dims(x_test, -1)\n",
86
+ "print(\"x_train shape:\", x_train.shape)\n",
87
+ "print(x_train.shape[0], \"train samples\")\n",
88
+ "print(x_test.shape[0], \"test samples\")\n",
89
+ "\n",
90
+ "\n",
91
+ "# convert class vectors to binary class matrices\n",
92
+ "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
93
+ "y_test = keras.utils.to_categorical(y_test, num_classes)"
94
+ ]
95
+ },
96
+ {
97
+ "cell_type": "markdown",
98
+ "metadata": {
99
+ "id": "kqdKUp6tzfyV"
100
+ },
101
+ "source": [
102
+ "## Build the model"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 4,
108
+ "metadata": {
109
+ "id": "GCmWGoyGzfyW",
110
+ "outputId": "85fad2b6-4b43-406c-bab6-4097a0741b6d",
111
+ "colab": {
112
+ "base_uri": "https://localhost:8080/"
113
+ }
114
+ },
115
+ "outputs": [
116
+ {
117
+ "output_type": "stream",
118
+ "name": "stdout",
119
+ "text": [
120
+ "Model: \"sequential\"\n",
121
+ "_________________________________________________________________\n",
122
+ " Layer (type) Output Shape Param # \n",
123
+ "=================================================================\n",
124
+ " conv2d (Conv2D) (None, 26, 26, 32) 320 \n",
125
+ " \n",
126
+ " max_pooling2d (MaxPooling2D (None, 13, 13, 32) 0 \n",
127
+ " ) \n",
128
+ " \n",
129
+ " conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 \n",
130
+ " \n",
131
+ " max_pooling2d_1 (MaxPooling (None, 5, 5, 64) 0 \n",
132
+ " 2D) \n",
133
+ " \n",
134
+ " flatten (Flatten) (None, 1600) 0 \n",
135
+ " \n",
136
+ " dropout (Dropout) (None, 1600) 0 \n",
137
+ " \n",
138
+ " dense (Dense) (None, 10) 16010 \n",
139
+ " \n",
140
+ "=================================================================\n",
141
+ "Total params: 34,826\n",
142
+ "Trainable params: 34,826\n",
143
+ "Non-trainable params: 0\n",
144
+ "_________________________________________________________________\n"
145
+ ]
146
+ }
147
+ ],
148
+ "source": [
149
+ "model = keras.Sequential(\n",
150
+ " [\n",
151
+ " keras.Input(shape=input_shape),\n",
152
+ " layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n",
153
+ " layers.MaxPooling2D(pool_size=(2, 2)),\n",
154
+ " layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n",
155
+ " layers.MaxPooling2D(pool_size=(2, 2)),\n",
156
+ " layers.Flatten(),\n",
157
+ " layers.Dropout(0.5),\n",
158
+ " layers.Dense(num_classes, activation=\"softmax\"),\n",
159
+ " ]\n",
160
+ ")\n",
161
+ "\n",
162
+ "model.summary()"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "markdown",
167
+ "metadata": {
168
+ "id": "lhwcwv48zfyX"
169
+ },
170
+ "source": [
171
+ "## Train the model"
172
+ ]
173
+ },
174
+ {
175
+ "cell_type": "code",
176
+ "execution_count": 5,
177
+ "metadata": {
178
+ "id": "lTElNbSEzfyX",
179
+ "outputId": "22052ddf-9388-4916-84f8-eaecca77d186",
180
+ "colab": {
181
+ "base_uri": "https://localhost:8080/"
182
+ }
183
+ },
184
+ "outputs": [
185
+ {
186
+ "output_type": "stream",
187
+ "name": "stdout",
188
+ "text": [
189
+ "Epoch 1/10\n",
190
+ "422/422 [==============================] - 46s 107ms/step - loss: 0.3677 - accuracy: 0.8880 - val_loss: 0.0825 - val_accuracy: 0.9780\n",
191
+ "Epoch 2/10\n",
192
+ "422/422 [==============================] - 45s 106ms/step - loss: 0.1108 - accuracy: 0.9664 - val_loss: 0.0628 - val_accuracy: 0.9837\n",
193
+ "Epoch 3/10\n",
194
+ "422/422 [==============================] - 45s 106ms/step - loss: 0.0860 - accuracy: 0.9732 - val_loss: 0.0453 - val_accuracy: 0.9877\n",
195
+ "Epoch 4/10\n",
196
+ "422/422 [==============================] - 44s 104ms/step - loss: 0.0703 - accuracy: 0.9786 - val_loss: 0.0435 - val_accuracy: 0.9875\n",
197
+ "Epoch 5/10\n",
198
+ "422/422 [==============================] - 44s 104ms/step - loss: 0.0599 - accuracy: 0.9810 - val_loss: 0.0398 - val_accuracy: 0.9890\n",
199
+ "Epoch 6/10\n",
200
+ "422/422 [==============================] - 44s 104ms/step - loss: 0.0556 - accuracy: 0.9830 - val_loss: 0.0364 - val_accuracy: 0.9898\n",
201
+ "Epoch 7/10\n",
202
+ "422/422 [==============================] - 45s 107ms/step - loss: 0.0509 - accuracy: 0.9838 - val_loss: 0.0333 - val_accuracy: 0.9910\n",
203
+ "Epoch 8/10\n",
204
+ "422/422 [==============================] - 46s 108ms/step - loss: 0.0477 - accuracy: 0.9847 - val_loss: 0.0314 - val_accuracy: 0.9920\n",
205
+ "Epoch 9/10\n",
206
+ "422/422 [==============================] - 44s 104ms/step - loss: 0.0443 - accuracy: 0.9859 - val_loss: 0.0319 - val_accuracy: 0.9930\n",
207
+ "Epoch 10/10\n",
208
+ "422/422 [==============================] - 43s 103ms/step - loss: 0.0409 - accuracy: 0.9869 - val_loss: 0.0299 - val_accuracy: 0.9923\n"
209
+ ]
210
+ },
211
+ {
212
+ "output_type": "execute_result",
213
+ "data": {
214
+ "text/plain": [
215
+ "<keras.callbacks.History at 0x7fd82e27a850>"
216
+ ]
217
+ },
218
+ "metadata": {},
219
+ "execution_count": 5
220
+ }
221
+ ],
222
+ "source": [
223
+ "batch_size = 128\n",
224
+ "epochs = 10\n",
225
+ "\n",
226
+ "model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n",
227
+ "\n",
228
+ "model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)"
229
+ ]
230
+ },
231
+ {
232
+ "cell_type": "markdown",
233
+ "metadata": {
234
+ "id": "YebG6y4izfyY"
235
+ },
236
+ "source": [
237
+ "## Evaluate the trained model"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "code",
242
+ "execution_count": 6,
243
+ "metadata": {
244
+ "id": "J5oNREXjzfyY",
245
+ "outputId": "98337645-eefe-479c-9a2c-9c3cdbf41e2a",
246
+ "colab": {
247
+ "base_uri": "https://localhost:8080/"
248
+ }
249
+ },
250
+ "outputs": [
251
+ {
252
+ "output_type": "stream",
253
+ "name": "stdout",
254
+ "text": [
255
+ "Test loss: 0.027494722977280617\n",
256
+ "Test accuracy: 0.9898999929428101\n"
257
+ ]
258
+ }
259
+ ],
260
+ "source": [
261
+ "score = model.evaluate(x_test, y_test, verbose=0)\n",
262
+ "print(\"Test loss:\", score[0])\n",
263
+ "print(\"Test accuracy:\", score[1])"
264
+ ]
265
+ },
266
+ {
267
+ "cell_type": "code",
268
+ "source": [
269
+ "!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash"
270
+ ],
271
+ "metadata": {
272
+ "id": "BOyNRT863adC",
273
+ "outputId": "659ef954-5a83-49ea-926c-d9f367b38d2b",
274
+ "colab": {
275
+ "base_uri": "https://localhost:8080/"
276
+ }
277
+ },
278
+ "execution_count": 20,
279
+ "outputs": [
280
+ {
281
+ "output_type": "stream",
282
+ "name": "stdout",
283
+ "text": [
284
+ "Detected operating system as Ubuntu/bionic.\n",
285
+ "Checking for curl...\n",
286
+ "Detected curl...\n",
287
+ "Checking for gpg...\n",
288
+ "Detected gpg...\n",
289
+ "Running apt-get update... done.\n",
290
+ "Installing apt-transport-https... done.\n",
291
+ "Installing /etc/apt/sources.list.d/github_git-lfs.list...done.\n",
292
+ "Importing packagecloud gpg key... done.\n",
293
+ "Running apt-get update... done.\n",
294
+ "\n",
295
+ "The repository is setup! You can now install packages.\n"
296
+ ]
297
+ }
298
+ ]
299
+ },
300
+ {
301
+ "cell_type": "code",
302
+ "source": [
303
+ "!pip install huggingface-hub\n",
304
+ "!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash\n",
305
+ "!sudo apt-get install git-lfs\n",
306
+ "!git-lfs install"
307
+ ],
308
+ "metadata": {
309
+ "id": "rMkFpfhk0XOk",
310
+ "outputId": "047a5b9f-c8bd-467d-84d2-a7b4960a26d9",
311
+ "colab": {
312
+ "base_uri": "https://localhost:8080/"
313
+ }
314
+ },
315
+ "execution_count": 21,
316
+ "outputs": [
317
+ {
318
+ "output_type": "stream",
319
+ "name": "stdout",
320
+ "text": [
321
+ "Reading package lists... Done\n",
322
+ "Building dependency tree \n",
323
+ "Reading state information... Done\n",
324
+ "The following NEW packages will be installed:\n",
325
+ " git-lfs\n",
326
+ "0 upgraded, 1 newly installed, 0 to remove and 40 not upgraded.\n",
327
+ "Need to get 6,526 kB of archives.\n",
328
+ "After this operation, 14.7 MB of additional disk space will be used.\n",
329
+ "Get:1 https://packagecloud.io/github/git-lfs/ubuntu bionic/main amd64 git-lfs amd64 3.0.2 [6,526 kB]\n",
330
+ "Fetched 6,526 kB in 1s (5,795 kB/s)\n",
331
+ "debconf: unable to initialize frontend: Dialog\n",
332
+ "debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 1.)\n",
333
+ "debconf: falling back to frontend: Readline\n",
334
+ "debconf: unable to initialize frontend: Readline\n",
335
+ "debconf: (This frontend requires a controlling tty.)\n",
336
+ "debconf: falling back to frontend: Teletype\n",
337
+ "dpkg-preconfigure: unable to re-open stdin: \n",
338
+ "Selecting previously unselected package git-lfs.\n",
339
+ "(Reading database ... 155222 files and directories currently installed.)\n",
340
+ "Preparing to unpack .../git-lfs_3.0.2_amd64.deb ...\n",
341
+ "Unpacking git-lfs (3.0.2) ...\n",
342
+ "Setting up git-lfs (3.0.2) ...\n",
343
+ "Git LFS initialized.\n",
344
+ "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n"
345
+ ]
346
+ }
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "source": [
352
+ "!huggingface-cli login"
353
+ ],
354
+ "metadata": {
355
+ "id": "uZWWvPH82quN",
356
+ "outputId": "f5c83a3b-62de-4ffd-db6a-25eed36bf9b0",
357
+ "colab": {
358
+ "base_uri": "https://localhost:8080/"
359
+ }
360
+ },
361
+ "execution_count": 13,
362
+ "outputs": [
363
+ {
364
+ "output_type": "stream",
365
+ "name": "stdout",
366
+ "text": [
367
+ "\n",
368
+ " _| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|\n",
369
+ " _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|\n",
370
+ " _|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|\n",
371
+ " _| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|\n",
372
+ " _| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|\n",
373
+ "\n",
374
+ " To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/token.\n",
375
+ " (Deprecated, will be removed in v0.3.0) To login with username and password instead, interrupt with Ctrl+C.\n",
376
+ " \n",
377
+ "Token: \n",
378
+ "Login successful\n",
379
+ "Your token has been saved to /root/.huggingface/token\n",
380
+ "\u001b[1m\u001b[31mAuthenticated through git-credential store but this isn't the helper defined on your machine.\n",
381
+ "You might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your terminal in case you want to set this credential helper as the default\n",
382
+ "\n",
383
+ "git config --global credential.helper store\u001b[0m\n"
384
+ ]
385
+ }
386
+ ]
387
+ },
388
+ {
389
+ "cell_type": "code",
390
+ "source": [
391
+ "from huggingface_hub.keras_mixin import push_to_hub_keras\n",
392
+ "push_to_hub_keras(model = model, repo_url = \"https://huggingface.co/keras-io/simple-mnist-convnet\", organization = \"keras-io\")"
393
+ ],
394
+ "metadata": {
395
+ "id": "RhssM1Dy0sl_"
396
+ },
397
+ "execution_count": 11,
398
+ "outputs": []
399
+ }
400
+ ],
401
+ "metadata": {
402
+ "colab": {
403
+ "collapsed_sections": [],
404
+ "name": "mnist_convnet",
405
+ "provenance": [],
406
+ "toc_visible": true
407
+ },
408
+ "kernelspec": {
409
+ "display_name": "Python 3",
410
+ "language": "python",
411
+ "name": "python3"
412
+ },
413
+ "language_info": {
414
+ "codemirror_mode": {
415
+ "name": "ipython",
416
+ "version": 3
417
+ },
418
+ "file_extension": ".py",
419
+ "mimetype": "text/x-python",
420
+ "name": "python",
421
+ "nbconvert_exporter": "python",
422
+ "pygments_lexer": "ipython3",
423
+ "version": "3.7.0"
424
+ }
425
+ },
426
+ "nbformat": 4,
427
+ "nbformat_minor": 0
428
+ }