├── .gitignore
├── Code
└── 014_trainable_parameter.py
├── Datasets
└── .gitkeep
├── LICENSE
├── Notebooks
├── .gitkeep
├── 000_channel_teaser.ipynb
├── 001_First_Net.ipynb
├── 002_Style_Transfer.ipynb
├── 003_Cat_vs_Dog.ipynb
├── 004_Visualize_Model.ipynb
├── 005_Energy_Efficiency.ipynb
├── 006_Tensorboard.ipynb
├── 007_Airfoil_Self_Noise.ipynb
├── 008_Custom_Layer.ipynb
├── 009_Custom_Loss.ipynb
├── 010_Lambda_Layer.ipynb
├── 011_Dtw_Loss_Function.ipynb
├── 012_pythonflow.ipynb
├── 013_tf2tflite.ipynb
└── 015_TftWatcherIntro.ipynb
├── README.md
└── yt_thumb_1.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------
/Code/014_trainable_parameter.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | """013_trainable_parameter
3 |
4 | Automatically generated by Colaboratory.
5 |
6 | ## Setup
7 | """
8 |
9 | import numpy as np
10 | import tensorflow as tf
11 | from tensorflow import keras
12 | from tensorflow.keras import layers
13 | import math
14 |
15 | """## Create a sampling layer"""
16 |
17 | class Sampling(layers.Layer):
18 | """Uses (z_mean, z_log_var) to sample z, the vector encoding a digit."""
19 |
20 | def call(self, inputs):
21 | z_mean, z_log_var = inputs
22 | batch = tf.shape(z_mean)[0]
23 | dim = tf.shape(z_mean)[1]
24 | epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
25 | return z_mean + tf.exp(0.5 * z_log_var) * epsilon
26 |
27 |
28 | """## Define the VAE as a `Model` with a custom `train_step`"""
29 |
30 | class VAE(keras.Model):
31 | def __init__(self, encoder, decoder, **kwargs):
32 | super(VAE, self).__init__(**kwargs)
33 | self.encoder = encoder
34 | self.decoder = decoder
35 | self.total_loss_tracker = keras.metrics.Mean(name="total_loss")
36 | self.reconstruction_loss_tracker = keras.metrics.Mean(
37 | name="reconstruction_loss"
38 | )
39 | self.kl_loss_tracker = keras.metrics.Mean(name="kl_loss")
40 |
41 | # Adding a custom learnable parameter to balance KL and Reconstruction loss
42 | self.gamma = tf.Variable(initial_value=1.0, dtype=float, trainable=True)
43 |
44 | @property
45 | def metrics(self):
46 | return [
47 | self.total_loss_tracker,
48 | self.reconstruction_loss_tracker,
49 | self.kl_loss_tracker,
50 | ]
51 |
52 | def train_step(self, data):
53 | with tf.GradientTape() as tape:
54 | z_mean, z_log_var, z = self.encoder(data)
55 | reconstruction = self.decoder(z)
56 | reconstruction_loss = tf.reduce_mean(
57 | tf.reduce_sum(
58 | keras.losses.binary_crossentropy(data, reconstruction), axis=(1, 2)
59 | )
60 | )
61 | kl_loss = -0.5 * (1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var))
62 | kl_loss = tf.reduce_mean(tf.reduce_sum(kl_loss, axis=1))
63 |
64 | mse = reconstruction_loss
65 |
66 | generative_loss = mse / (2 * self.gamma**2) + tf.math.log(self.gamma) + tf.math.log(2 * math.pi) / 2
67 |
68 | total_loss = generative_loss + kl_loss
69 |
70 | grads = tape.gradient(total_loss, self.trainable_weights)
71 | self.optimizer.apply_gradients(zip(grads, self.trainable_weights))
72 | self.total_loss_tracker.update_state(total_loss)
73 | self.reconstruction_loss_tracker.update_state(reconstruction_loss)
74 | self.kl_loss_tracker.update_state(kl_loss)
75 | return {
76 | "loss": self.total_loss_tracker.result(),
77 | "reconstruction_loss": self.reconstruction_loss_tracker.result(),
78 | "kl_loss": self.kl_loss_tracker.result(),
79 | }
80 |
81 |
82 | def plot_latent_space(vae, n=30, figsize=15):
83 | # display a n*n 2D manifold of digits
84 | digit_size = 28
85 | scale = 1.0
86 | figure = np.zeros((digit_size * n, digit_size * n))
87 | # linearly spaced coordinates corresponding to the 2D plot
88 | # of digit classes in the latent space
89 | grid_x = np.linspace(-scale, scale, n)
90 | grid_y = np.linspace(-scale, scale, n)[::-1]
91 |
92 | for i, yi in enumerate(grid_y):
93 | for j, xi in enumerate(grid_x):
94 | z_sample = np.array([[xi, yi]])
95 | x_decoded = vae.decoder.predict(z_sample)
96 | digit = x_decoded[0].reshape(digit_size, digit_size)
97 | figure[
98 | i * digit_size : (i + 1) * digit_size,
99 | j * digit_size : (j + 1) * digit_size,
100 | ] = digit
101 |
102 | plt.figure(figsize=(figsize, figsize))
103 | start_range = digit_size // 2
104 | end_range = n * digit_size + start_range
105 | pixel_range = np.arange(start_range, end_range, digit_size)
106 | sample_range_x = np.round(grid_x, 1)
107 | sample_range_y = np.round(grid_y, 1)
108 | plt.xticks(pixel_range, sample_range_x)
109 | plt.yticks(pixel_range, sample_range_y)
110 | plt.xlabel("z[0]")
111 | plt.ylabel("z[1]")
112 | plt.imshow(figure, cmap="Greys_r")
113 | plt.show()
114 |
115 | def plot_label_clusters(vae, data, labels):
116 | # display a 2D plot of the digit classes in the latent space
117 | z_mean, _, _ = vae.encoder.predict(data)
118 | plt.figure(figsize=(12, 10))
119 | plt.scatter(z_mean[:, 0], z_mean[:, 1], c=labels)
120 | plt.colorbar()
121 | plt.xlabel("z[0]")
122 | plt.ylabel("z[1]")
123 | plt.show()
124 |
125 |
126 | if __name__ == '__main__':
127 |
128 |
129 | latent_dim = 2
130 | epochs = 1
131 |
132 | """## Build the encoder"""
133 |
134 | encoder_inputs = keras.Input(shape=(28, 28, 1))
135 | x = layers.Conv2D(32, 3, activation="relu", strides=2, padding="same")(encoder_inputs)
136 | x = layers.Conv2D(64, 3, activation="relu", strides=2, padding="same")(x)
137 | x = layers.Flatten()(x)
138 | x = layers.Dense(16, activation="relu")(x)
139 | z_mean = layers.Dense(latent_dim, name="z_mean")(x)
140 | z_log_var = layers.Dense(latent_dim, name="z_log_var")(x)
141 | z = Sampling()([z_mean, z_log_var])
142 | encoder = keras.Model(encoder_inputs, [z_mean, z_log_var, z], name="encoder")
143 | print(encoder.summary())
144 |
145 | """## Build the decoder"""
146 |
147 | latent_inputs = keras.Input(shape=(latent_dim,))
148 | x = layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs)
149 | x = layers.Reshape((7, 7, 64))(x)
150 | x = layers.Conv2DTranspose(64, 3, activation="relu", strides=2, padding="same")(x)
151 | x = layers.Conv2DTranspose(32, 3, activation="relu", strides=2, padding="same")(x)
152 | decoder_outputs = layers.Conv2DTranspose(1, 3, activation="sigmoid", padding="same")(x)
153 | decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder")
154 | print(decoder.summary())
155 |
156 | import time
157 | # time.sleep(20)
158 |
159 | """## Train the VAE"""
160 |
161 | (x_train, _), (x_test, _) = keras.datasets.mnist.load_data()
162 | mnist_digits = np.concatenate([x_train, x_test], axis=0)
163 | mnist_digits = np.expand_dims(mnist_digits, -1).astype("float32") / 255
164 |
165 | vae = VAE(encoder, decoder)
166 | vae.compile(optimizer=keras.optimizers.Adam())
167 |
168 | print(f'Starting value of gamma: {vae.gamma}')
169 | vae.fit(mnist_digits, epochs=epochs, batch_size=128)
170 | print(f'Final value of gamma: {vae.gamma}')
171 |
172 | input('Press enter to continue...')
173 |
174 | """## Display a grid of sampled digits"""
175 |
176 | import matplotlib.pyplot as plt
177 |
178 | plot_latent_space(vae)
179 |
180 | """## Display how the latent space clusters different digit classes"""
181 |
182 | (x_train, y_train), _ = keras.datasets.mnist.load_data()
183 | x_train = np.expand_dims(x_train, -1).astype("float32") / 255
184 |
185 | plot_label_clusters(vae, x_train, y_train)
--------------------------------------------------------------------------------
/Datasets/.gitkeep:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 GitMarco27
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Notebooks/.gitkeep:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Notebooks/010_Lambda_Layer.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "accelerator": "GPU",
6 | "colab": {
7 | "name": "010_Lambda_Layer.ipynb",
8 | "provenance": [],
9 | "collapsed_sections": [],
10 | "authorship_tag": "ABX9TyMSvvrfmw2hZTQtJziPimuh",
11 | "include_colab_link": true
12 | },
13 | "kernelspec": {
14 | "display_name": "Python 3",
15 | "name": "python3"
16 | },
17 | "language_info": {
18 | "name": "python"
19 | }
20 | },
21 | "cells": [
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {
25 | "id": "view-in-github",
26 | "colab_type": "text"
27 | },
28 | "source": [
29 | "
"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {
35 | "id": "cG_fLgb6C1tf"
36 | },
37 | "source": [
38 | "# 3 Minutes Machine Learning\n",
39 | "## Episode 10: Lambda Layer\n",
40 | "\n",
41 | "#### Marco Sanguineti, 2021\n",
42 | "---\n",
43 | "Welcome to 3 minutes Machine Learning!"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "metadata": {
49 | "id": "mSoTlTKwZwnL"
50 | },
51 | "source": [
52 | "import tensorflow as tf\n",
53 | "import pandas as pd\n",
54 | "import seaborn as sns\n",
55 | "import matplotlib.pyplot as plt"
56 | ],
57 | "execution_count": 1,
58 | "outputs": []
59 | },
60 | {
61 | "cell_type": "code",
62 | "metadata": {
63 | "id": "UDz7BaSUakRm"
64 | },
65 | "source": [
66 | "print(tf.__version__)"
67 | ],
68 | "execution_count": null,
69 | "outputs": []
70 | },
71 | {
72 | "cell_type": "code",
73 | "metadata": {
74 | "id": "VksvMLmpC7n9"
75 | },
76 | "source": [
77 | "import os\n",
78 | "def loadThumb(path):\n",
79 | " # Let's import this video thumbnail!\n",
80 | " if os.path.exists(path):\n",
81 | " myThumb = plt.imread(path)\n",
82 | " fig, ax = plt.subplots(figsize=(15, 10))\n",
83 | " plt.axis('off')\n",
84 | " ax.imshow(myThumb)\n",
85 | " plt.show()\n",
86 | "\n",
87 | "loadThumb('/tmp/yt_thumb_010.png')\n"
88 | ],
89 | "execution_count": 3,
90 | "outputs": []
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {
95 | "id": "Y5HhwZm0DETT"
96 | },
97 | "source": [
98 | "#### Video Topics\n",
99 | "> 1. Load the fashion MNIST dataset\n",
100 | "> 2. Create a model with the keras API with a lambda layer for normalization\n",
101 | "> 3. Train the model and check the results\n",
102 | "> 4. See you on next video! "
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {
108 | "id": "p1XK0dMeD_RI"
109 | },
110 | "source": [
111 | "# Load the dataset\n",
112 | "___"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "metadata": {
118 | "id": "Tuwrg8aoauey"
119 | },
120 | "source": [
121 | "fashion_mnist = tf.keras.datasets.fashion_mnist\n",
122 | "(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
123 | ],
124 | "execution_count": null,
125 | "outputs": []
126 | },
127 | {
128 | "cell_type": "code",
129 | "metadata": {
130 | "id": "JabO52u-HbGW"
131 | },
132 | "source": [
133 | "class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',\n",
134 | " 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']\n",
135 | "class_names"
136 | ],
137 | "execution_count": null,
138 | "outputs": []
139 | },
140 | {
141 | "cell_type": "code",
142 | "metadata": {
143 | "id": "FvLif9BIGuG-"
144 | },
145 | "source": [
146 | "def plot_my_image(image):\n",
147 | " plt.figure()\n",
148 | " plt.imshow(image)\n",
149 | " plt.colorbar()\n",
150 | " plt.grid(False)\n",
151 | " plt.show()\n",
152 | "\n",
153 | "def plot_images(images):\n",
154 | " plt.figure(figsize=(10,10))\n",
155 | " for i in range(25):\n",
156 | " plt.subplot(5,5,i+1)\n",
157 | " plt.xticks([])\n",
158 | " plt.yticks([])\n",
159 | " plt.grid(False)\n",
160 | " plt.imshow(images[i], cmap=plt.cm.binary)\n",
161 | " plt.xlabel(class_names[train_labels[i]])\n",
162 | " plt.show()"
163 | ],
164 | "execution_count": 6,
165 | "outputs": []
166 | },
167 | {
168 | "cell_type": "code",
169 | "metadata": {
170 | "id": "iOBgjgHnGh1Q"
171 | },
172 | "source": [
173 | "plot_my_image(train_images[22])"
174 | ],
175 | "execution_count": null,
176 | "outputs": []
177 | },
178 | {
179 | "cell_type": "code",
180 | "metadata": {
181 | "id": "DlTkhMSbHBlg"
182 | },
183 | "source": [
184 | "plot_images(train_images)"
185 | ],
186 | "execution_count": null,
187 | "outputs": []
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {
192 | "id": "5ik1Qq0WED2r"
193 | },
194 | "source": [
195 | "# Create the model\n",
196 | "___"
197 | ]
198 | },
199 | {
200 | "cell_type": "code",
201 | "metadata": {
202 | "id": "qrDJbNoZltFg"
203 | },
204 | "source": [
205 | "from tensorflow.keras.layers import Dense, Input, Lambda, Flatten, Conv2D, MaxPool2D\n",
206 | "from tensorflow.keras.models import Model\n",
207 | "from tensorflow.keras.optimizers import Adam\n",
208 | "from tensorflow.keras.layers import Layer"
209 | ],
210 | "execution_count": 9,
211 | "outputs": []
212 | },
213 | {
214 | "cell_type": "code",
215 | "metadata": {
216 | "id": "dvZim7NkIarK"
217 | },
218 | "source": [
219 | "print(f'Images Shape: {train_images[0].shape}')\n",
220 | "print(f'Num. of classes: {len(set(train_labels))}')"
221 | ],
222 | "execution_count": null,
223 | "outputs": []
224 | },
225 | {
226 | "cell_type": "code",
227 | "metadata": {
228 | "id": "pvMYxPnpmUik"
229 | },
230 | "source": [
231 | "input_data = Input(shape=(train_images[0].shape), name='Input')\n",
232 | "\n",
233 | "normalization = Lambda(lambda x: x / 255.0)(input_data)\n",
234 | "\n",
235 | "flatten = Flatten()(normalization)\n",
236 | "dense = Dense(64, activation='relu')(flatten)\n",
237 | "output = Dense(len(set(train_labels)), name='output', activation='softmax')(dense)\n",
238 | "model = Model(input_data, output)\n",
239 | "model.compile(optimizer=Adam(learning_rate=0.001),\n",
240 | " loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n",
241 | " metrics=['accuracy'])\n",
242 | "model.summary()"
243 | ],
244 | "execution_count": null,
245 | "outputs": []
246 | },
247 | {
248 | "cell_type": "code",
249 | "metadata": {
250 | "id": "MsP5J3gGEM9l"
251 | },
252 | "source": [
253 | "tf.keras.utils.plot_model(\n",
254 | " model, to_file='model.png', show_shapes=True, show_dtype=True,\n",
255 | " show_layer_names=True, rankdir='TB', expand_nested=False, dpi=96\n",
256 | ")"
257 | ],
258 | "execution_count": null,
259 | "outputs": []
260 | },
261 | {
262 | "cell_type": "markdown",
263 | "metadata": {
264 | "id": "wNItDf_LESUC"
265 | },
266 | "source": [
267 | "# Train and check the results\n",
268 | "___"
269 | ]
270 | },
271 | {
272 | "cell_type": "code",
273 | "metadata": {
274 | "id": "RCAgZCI1UOig"
275 | },
276 | "source": [
277 | "model.layers"
278 | ],
279 | "execution_count": null,
280 | "outputs": []
281 | },
282 | {
283 | "cell_type": "code",
284 | "metadata": {
285 | "id": "0dTCj5tupTZ-"
286 | },
287 | "source": [
288 | "history = model.fit(\n",
289 | " train_images,\n",
290 | " train_labels,\n",
291 | " batch_size = 256,\n",
292 | " epochs=40,\n",
293 | " validation_data=(test_images,\n",
294 | " test_labels))"
295 | ],
296 | "execution_count": null,
297 | "outputs": []
298 | },
299 | {
300 | "cell_type": "code",
301 | "metadata": {
302 | "id": "MBttLuuBsLWO"
303 | },
304 | "source": [
305 | "loss = history.history['loss']\n",
306 | "val_loss = history.history['val_loss']\n",
307 | "accuracy = history.history['accuracy']\n",
308 | "val_accuracy = history.history['val_accuracy']\n",
309 | "fig, ax = plt.subplots(figsize=(8, 6))\n",
310 | "plt.plot(loss, label='loss')\n",
311 | "plt.plot(val_loss, label = 'val loss')\n",
312 | "plt.plot(accuracy, label='accuracy')\n",
313 | "plt.plot(val_accuracy, label='val accuracy')\n",
314 | "plt.grid('both')\n",
315 | "plt.xlabel('Epochs')\n",
316 | "plt.ylabel('Loss Function')\n",
317 | "plt.title('Loss Function trend')\n",
318 | "plt.legend()\n",
319 | "plt.show()"
320 | ],
321 | "execution_count": null,
322 | "outputs": []
323 | },
324 | {
325 | "cell_type": "code",
326 | "metadata": {
327 | "id": "dnzCljI5MVAX"
328 | },
329 | "source": [
330 | "input_data = Input(shape=(*train_images[0].shape,), name='Input')\n",
331 | "\n",
332 | "normalization = Lambda(lambda x: x / 255.0)(input_data)\n",
333 | "\n",
334 | "expand = Lambda(lambda x: tf.expand_dims(x, -1))(normalization)\n",
335 | "\n",
336 | "conv_1 = Conv2D(64, (3, 3), activation='relu')(expand)\n",
337 | "max_pool_1 = MaxPool2D(2, 2)(conv_1)\n",
338 | "conv_2 = Conv2D(64, (3, 3), activation='relu')(max_pool_1)\n",
339 | "max_pool_2 = MaxPool2D(2, 2)(conv_2)\n",
340 | "flatten = Flatten()(max_pool_2)\n",
341 | "output = Dense(len(set(train_labels)), name='output', activation='softmax')(flatten)\n",
342 | "model = Model(input_data, output)\n",
343 | "model.compile(optimizer=Adam(learning_rate=0.001),\n",
344 | " loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n",
345 | " metrics=['accuracy'])\n",
346 | "model.summary()"
347 | ],
348 | "execution_count": null,
349 | "outputs": []
350 | },
351 | {
352 | "cell_type": "code",
353 | "metadata": {
354 | "id": "nYK1fWF9NnI5"
355 | },
356 | "source": [
357 | "tf.keras.utils.plot_model(\n",
358 | " model, to_file='model.png', show_shapes=True, show_dtype=True,\n",
359 | " show_layer_names=True, rankdir='TB', expand_nested=False, dpi=96\n",
360 | ")"
361 | ],
362 | "execution_count": null,
363 | "outputs": []
364 | },
365 | {
366 | "cell_type": "code",
367 | "metadata": {
368 | "id": "fYVKz4GbNqFk"
369 | },
370 | "source": [
371 | "history = model.fit(\n",
372 | " train_images,\n",
373 | " train_labels,\n",
374 | " batch_size = 256,\n",
375 | " epochs=40,\n",
376 | " validation_data=(test_images,\n",
377 | " test_labels))"
378 | ],
379 | "execution_count": null,
380 | "outputs": []
381 | },
382 | {
383 | "cell_type": "code",
384 | "metadata": {
385 | "id": "d1ZC6VLEPMJo"
386 | },
387 | "source": [
388 | "loss = history.history['loss']\n",
389 | "val_loss = history.history['val_loss']\n",
390 | "accuracy = history.history['accuracy']\n",
391 | "val_accuracy = history.history['val_accuracy']\n",
392 | "fig, ax = plt.subplots(figsize=(8, 6))\n",
393 | "plt.plot(loss, label='loss')\n",
394 | "plt.plot(val_loss, label = 'val loss')\n",
395 | "plt.plot(accuracy, label='accuracy')\n",
396 | "plt.plot(val_accuracy, label='val accuracy')\n",
397 | "plt.grid('both')\n",
398 | "plt.xlabel('Epochs')\n",
399 | "plt.ylabel('Loss Function')\n",
400 | "plt.title('Loss Function trend')\n",
401 | "plt.legend()\n",
402 | "plt.show()\n"
403 | ],
404 | "execution_count": null,
405 | "outputs": []
406 | },
407 | {
408 | "cell_type": "code",
409 | "metadata": {
410 | "id": "7xbQUzwTQ_zK"
411 | },
412 | "source": [
413 | "model.predict((tf.expand_dims(train_images[0], axis=0)))"
414 | ],
415 | "execution_count": null,
416 | "outputs": []
417 | },
418 | {
419 | "cell_type": "code",
420 | "metadata": {
421 | "id": "pB0CB0PcSWJG"
422 | },
423 | "source": [
424 | "def plot_image(i, predictions_array, true_label, img):\n",
425 | " true_label, img = true_label[i], img[i]\n",
426 | " plt.grid(False)\n",
427 | " plt.xticks([])\n",
428 | " plt.yticks([])\n",
429 | "\n",
430 | " plt.imshow(img, cmap=plt.cm.binary)\n",
431 | "\n",
432 | " predicted_label = np.argmax(predictions_array)\n",
433 | " if predicted_label == true_label:\n",
434 | " color = 'blue'\n",
435 | " else:\n",
436 | " color = 'red'\n",
437 | "\n",
438 | " plt.xlabel(\"{} {:2.0f}% ({})\".format(class_names[predicted_label],\n",
439 | " 100*np.max(predictions_array),\n",
440 | " class_names[true_label]),\n",
441 | " color=color)\n",
442 | "\n",
443 | "def plot_value_array(i, predictions_array, true_label):\n",
444 | " true_label = true_label[i]\n",
445 | " plt.grid(False)\n",
446 | " plt.xticks(range(10))\n",
447 | " plt.yticks([])\n",
448 | " thisplot = plt.bar(range(10), predictions_array, color=\"#777777\")\n",
449 | " plt.ylim([0, 1])\n",
450 | " predicted_label = np.argmax(predictions_array)\n",
451 | "\n",
452 | " thisplot[predicted_label].set_color('red')\n",
453 | " thisplot[true_label].set_color('blue')"
454 | ],
455 | "execution_count": 21,
456 | "outputs": []
457 | },
458 | {
459 | "cell_type": "code",
460 | "metadata": {
461 | "id": "YF5Ho6NvSXGs"
462 | },
463 | "source": [
464 | "import numpy as np\n",
465 | "predictions = model.predict(test_images)\n",
466 | "num_rows = 5\n",
467 | "num_cols = 3\n",
468 | "num_images = num_rows*num_cols\n",
469 | "plt.figure(figsize=(2*2*num_cols, 2*num_rows))\n",
470 | "for i in range(num_images):\n",
471 | " plt.subplot(num_rows, 2*num_cols, 2*i+1)\n",
472 | " plot_image(i, predictions[i], test_labels, test_images)\n",
473 | " plt.subplot(num_rows, 2*num_cols, 2*i+2)\n",
474 | " plot_value_array(i, predictions[i], test_labels)\n",
475 | "plt.tight_layout()\n",
476 | "plt.show()"
477 | ],
478 | "execution_count": null,
479 | "outputs": []
480 | },
481 | {
482 | "cell_type": "markdown",
483 | "metadata": {
484 | "id": "x12mGu0LF-AH"
485 | },
486 | "source": [
487 | "# Greetings\n",
488 | "---"
489 | ]
490 | },
491 | {
492 | "cell_type": "code",
493 | "metadata": {
494 | "id": "xgWUckLSF7wj"
495 | },
496 | "source": [
497 | "!pip install art\n",
498 | "from art import tprint, aprint\n",
499 | "tprint('See you on next videos!')\n",
500 | "def subscribe():\n",
501 | " \"\"\"\n",
502 | " Attractive subscription form\n",
503 | " \"\"\"\n",
504 | " aprint(\"giveme\", number=5)\n",
505 | " print(f'\\n\\tLike and subscribe to support this work!\\n')\n",
506 | " aprint(\"giveme\", number=5)\n",
507 | "subscribe()"
508 | ],
509 | "execution_count": null,
510 | "outputs": []
511 | }
512 | ]
513 | }
--------------------------------------------------------------------------------
/Notebooks/012_pythonflow.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "012_pythonflow.ipynb",
7 | "provenance": [],
8 | "collapsed_sections": [],
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "source": [
33 | "# 3 Minutes Machine Learning\n",
34 | "## Episode 12: Pythonflow: from eager to graph python programming\n",
35 | "\n",
36 | "#### Marco Sanguineti, 2021\n",
37 | "---\n",
38 | "Welcome to 3 minutes Machine Learning!"
39 | ],
40 | "metadata": {
41 | "id": "mcd_NbOtQtRY"
42 | }
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": 32,
47 | "metadata": {
48 | "id": "rIElMyxhsafK"
49 | },
50 | "outputs": [],
51 | "source": [
52 | "! pip install pythonflow -U -q"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "source": [
58 | "import tensorflow as tf\n",
59 | "import pythonflow as pf\n",
60 | "import numpy as np"
61 | ],
62 | "metadata": {
63 | "id": "rmGdY_S4207k"
64 | },
65 | "execution_count": 33,
66 | "outputs": []
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "source": [
71 | "# TensorflowGraph"
72 | ],
73 | "metadata": {
74 | "id": "dKCKJF9N274Z"
75 | }
76 | },
77 | {
78 | "cell_type": "code",
79 | "source": [
80 | "class AddLayerOperation(pf.Operation):\n",
81 | " def __init__(self, model, type_layers, num_nodes, activation):\n",
82 | " super(AddLayerOperation, self).__init__(model, type_layers, num_nodes, activation)\n",
83 | "\n",
84 | "\n",
85 | " def _evaluate(self, model, type_layers, num_nodes, activation):\n",
86 | " if type_layers=='Dense':\n",
87 | " new_layer = tf.keras.layers.Dense(num_nodes, activation=activation)\n",
88 | "\n",
89 | " model.add(new_layer)\n",
90 | " return model\n",
91 | "\n",
92 | "class CompileOperation(pf.Operation):\n",
93 | " def __init__(self, model, number_name):\n",
94 | " super(CompileOperation, self).__init__(model, name='CompileOperation'+number_name)\n",
95 | "\n",
96 | " def _evaluate(self, model):\n",
97 | " model.compile(optimizer='adam', loss='mse')\n",
98 | " return model\n",
99 | "\n",
100 | "class TrainOperation(pf.Operation):\n",
101 | " def __init__(self, model, epochs, batch_size, X, y):\n",
102 | " super(TrainOperation, self).__init__(model, epochs, batch_size, X, y, name='TrainModel')\n",
103 | "\n",
104 | " def _evaluate(self, model,X, y, epochs,batch_size):\n",
105 | " history = model.fit(X, y, epochs=epochs, batch_size=batch_size)\n",
106 | " return model, history\n",
107 | "\n",
108 | "with pf.Graph() as graphadd:\n",
109 | " a = pf.constant(4)\n",
110 | " b = pf.constant(38)\n",
111 | " x = (a + b).set_name('x')\n",
112 | "\n",
113 | "with pf.Graph() as graph:\n",
114 | " num_nodes = pf.placeholder(name=\"num_nodes\")\n",
115 | " activation = pf.placeholder(name=\"activation\")\n",
116 | " type_layers = pf.placeholder(name=\"type_layers\")\n",
117 | " epochs = pf.placeholder(name=\"epochs\")\n",
118 | " batch_size = pf.placeholder(name=\"batch_size\")\n",
119 | "\n",
120 | " input_layer = pf.func_op(tf.keras.Input, [graphadd('x')])\n",
121 | " model = pf.func_op(tf.keras.models.Sequential, [input_layer], name='model')\n",
122 | "\n",
123 | " model_graph = AddLayerOperation(model, type_layers, num_nodes, activation)\n",
124 | " model_graph1 = AddLayerOperation(model_graph, type_layers, num_nodes, activation)\n",
125 | " model_graph2 = AddLayerOperation(model_graph1, type_layers, num_nodes, activation)\n",
126 | " model_graph_compile1 = CompileOperation(model_graph2, number_name='1')\n",
127 | "\n",
128 | " X = pf.func_op(np.random.rand, *[100, graphadd('x')])\n",
129 | " y = pf.func_op(np.random.rand, *(100, 10))\n",
130 | "\n",
131 | " model_trained = TrainOperation(model_graph_compile1, X, y, epochs, batch_size)\n",
132 | "\n",
133 | "\n",
134 | "model = graph(model_graph_compile1 , num_nodes=10, activation='relu', type_layers='Dense', \n",
135 | " batch_size= 16, epochs=5)\n",
136 | "\n",
137 | "model.summary()"
138 | ],
139 | "metadata": {
140 | "colab": {
141 | "base_uri": "https://localhost:8080/"
142 | },
143 | "id": "dpUZu9ea27px",
144 | "outputId": "d1e46344-e121-41dc-fb3b-2591b5c42d7e"
145 | },
146 | "execution_count": 34,
147 | "outputs": [
148 | {
149 | "output_type": "stream",
150 | "name": "stdout",
151 | "text": [
152 | "Model: \"sequential_9\"\n",
153 | "_________________________________________________________________\n",
154 | " Layer (type) Output Shape Param # \n",
155 | "=================================================================\n",
156 | " dense_27 (Dense) (None, 10) 430 \n",
157 | " \n",
158 | " dense_28 (Dense) (None, 10) 110 \n",
159 | " \n",
160 | " dense_29 (Dense) (None, 10) 110 \n",
161 | " \n",
162 | "=================================================================\n",
163 | "Total params: 650\n",
164 | "Trainable params: 650\n",
165 | "Non-trainable params: 0\n",
166 | "_________________________________________________________________\n"
167 | ]
168 | }
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "source": [
174 | "model, history = graph(model_trained , num_nodes=10, activation='relu', type_layers='Dense',\n",
175 | " batch_size= 16, epochs=5)"
176 | ],
177 | "metadata": {
178 | "colab": {
179 | "base_uri": "https://localhost:8080/"
180 | },
181 | "id": "gIMsilE4RupB",
182 | "outputId": "31fa1f53-ee25-4f06-9ddb-aa7482db339f"
183 | },
184 | "execution_count": 35,
185 | "outputs": [
186 | {
187 | "output_type": "stream",
188 | "name": "stdout",
189 | "text": [
190 | "Epoch 1/5\n",
191 | "7/7 [==============================] - 0s 5ms/step - loss: 0.2173\n",
192 | "Epoch 2/5\n",
193 | "7/7 [==============================] - 0s 3ms/step - loss: 0.2011\n",
194 | "Epoch 3/5\n",
195 | "7/7 [==============================] - 0s 3ms/step - loss: 0.1940\n",
196 | "Epoch 4/5\n",
197 | "7/7 [==============================] - 0s 3ms/step - loss: 0.1886\n",
198 | "Epoch 5/5\n",
199 | "7/7 [==============================] - 0s 2ms/step - loss: 0.1836\n"
200 | ]
201 | }
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "source": [
207 | "import time\n",
208 | "def create_graph(num_nodes=10, activation='relu', type_layers='Dense',batch_size= 16, epochs=5 ): \n",
209 | " models = graph(model_trained, num_nodes=num_nodes, activation=activation, type_layers=type_layers, \n",
210 | " batch_size= batch_size, epochs=epochs)\n",
211 | " return models"
212 | ],
213 | "metadata": {
214 | "id": "E08jOB_8xE8p"
215 | },
216 | "execution_count": 36,
217 | "outputs": []
218 | },
219 | {
220 | "cell_type": "code",
221 | "source": [
222 | "import pickle \n",
223 | "\n",
224 | "with open(\"custom_graph\", 'wb') as f:\n",
225 | " pickle.dump(graph, f)"
226 | ],
227 | "metadata": {
228 | "id": "ruxitAMf81JW"
229 | },
230 | "execution_count": 37,
231 | "outputs": []
232 | },
233 | {
234 | "cell_type": "code",
235 | "source": [
236 | "with open(\"custom_graph\", 'rb') as f:\n",
237 | " new_graph = pickle.load(f)\n",
238 | "\n",
239 | "del model\n",
240 | "model, history = new_graph('TrainModel', num_nodes=10, activation='selu', type_layers='Dense',\n",
241 | " batch_size= 16, epochs=5)\n",
242 | "model.summary()\n"
243 | ],
244 | "metadata": {
245 | "colab": {
246 | "base_uri": "https://localhost:8080/"
247 | },
248 | "id": "1RyVy4ol89mj",
249 | "outputId": "5773428e-036a-43e7-bafa-b2f08f07ec43"
250 | },
251 | "execution_count": 38,
252 | "outputs": [
253 | {
254 | "output_type": "stream",
255 | "name": "stdout",
256 | "text": [
257 | "Epoch 1/5\n",
258 | "7/7 [==============================] - 0s 2ms/step - loss: 0.8081\n",
259 | "Epoch 2/5\n",
260 | "7/7 [==============================] - 0s 2ms/step - loss: 0.5746\n",
261 | "Epoch 3/5\n",
262 | "7/7 [==============================] - 0s 2ms/step - loss: 0.4853\n",
263 | "Epoch 4/5\n",
264 | "7/7 [==============================] - 0s 5ms/step - loss: 0.4442\n",
265 | "Epoch 5/5\n",
266 | "7/7 [==============================] - 0s 3ms/step - loss: 0.4065\n",
267 | "Model: \"sequential_11\"\n",
268 | "_________________________________________________________________\n",
269 | " Layer (type) Output Shape Param # \n",
270 | "=================================================================\n",
271 | " dense_33 (Dense) (None, 10) 430 \n",
272 | " \n",
273 | " dense_34 (Dense) (None, 10) 110 \n",
274 | " \n",
275 | " dense_35 (Dense) (None, 10) 110 \n",
276 | " \n",
277 | "=================================================================\n",
278 | "Total params: 650\n",
279 | "Trainable params: 650\n",
280 | "Non-trainable params: 0\n",
281 | "_________________________________________________________________\n"
282 | ]
283 | }
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "source": [
289 | "vars(new_graph).keys()"
290 | ],
291 | "metadata": {
292 | "colab": {
293 | "base_uri": "https://localhost:8080/"
294 | },
295 | "id": "LOaNh399_kb9",
296 | "outputId": "ece0dd1d-ac9a-4b3f-f785-08110346b8d3"
297 | },
298 | "execution_count": 39,
299 | "outputs": [
300 | {
301 | "output_type": "execute_result",
302 | "data": {
303 | "text/plain": [
304 | "dict_keys(['operations', 'dependencies'])"
305 | ]
306 | },
307 | "metadata": {},
308 | "execution_count": 39
309 | }
310 | ]
311 | },
312 | {
313 | "cell_type": "code",
314 | "source": [
315 | "print(new_graph.operations)\n",
316 | "print(new_graph.dependencies)"
317 | ],
318 | "metadata": {
319 | "colab": {
320 | "base_uri": "https://localhost:8080/"
321 | },
322 | "id": "Wr9RcmWzGIu-",
323 | "outputId": "4e5a3dc7-8bdc-4947-aae2-51141deb900e"
324 | },
325 | "execution_count": 40,
326 | "outputs": [
327 | {
328 | "output_type": "stream",
329 | "name": "stdout",
330 | "text": [
331 | "{'num_nodes': , 'activation': , 'type_layers': , 'epochs': , 'batch_size': , '9b632894402c44d8a350599df56d457c': args=<1 items> kwargs=<0 items>>, 'model': args=<1 items> kwargs=<0 items>>, '303900c3a5ee474d9bd5cfc26105225c': <__main__.AddLayerOperation object at 0x7f2b51db8c90>, 'e77a8e676e754bdab36b4325b92b7c2c': <__main__.AddLayerOperation object at 0x7f2b51db8d50>, '427382c0a3aa44268e35c6455e9803f9': <__main__.AddLayerOperation object at 0x7f2b51db8e10>, 'CompileOperation1': <__main__.CompileOperation object at 0x7f2b51db8ed0>, 'd0e3e4b1659d43c5950f0a80db57f846': args=<2 items> kwargs=<0 items>>, '27d988c031754a8aa7f49abb489931c4': args=<2 items> kwargs=<0 items>>, 'TrainModel': <__main__.TrainOperation object at 0x7f2b51f39950>}\n",
332 | "[]\n"
333 | ]
334 | }
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "source": [
340 | "import threading\n",
341 | "import time\n",
342 | "import sys\n",
343 | "import trace\n",
344 | "import threading\n",
345 | "import time\n",
346 | "\n",
347 | "class thread_with_trace(threading.Thread):\n",
348 | " def __init__(self, *args, **keywords):\n",
349 | " threading.Thread.__init__(self, *args, **keywords)\n",
350 | " self.killed = False\n",
351 | " \n",
352 | " def start(self):\n",
353 | " self.__run_backup = self.run\n",
354 | " self.run = self.__run \n",
355 | " threading.Thread.start(self)\n",
356 | " \n",
357 | " def __run(self):\n",
358 | " sys.settrace(self.globaltrace)\n",
359 | " self.__run_backup()\n",
360 | " self.run = self.__run_backup\n",
361 | " \n",
362 | " def globaltrace(self, frame, event, arg):\n",
363 | " if event == 'call':\n",
364 | " return self.localtrace\n",
365 | " else:\n",
366 | " return None\n",
367 | " \n",
368 | " def localtrace(self, frame, event, arg):\n",
369 | " if self.killed:\n",
370 | " if event == 'line':\n",
371 | " raise SystemExit()\n",
372 | " return self.localtrace\n",
373 | " \n",
374 | " def kill(self):\n",
375 | " self.killed = True\n",
376 | "\n",
377 | "print('Actual count: ', threading.active_count())\n",
378 | "th = thread_with_trace(target=create_graph, args=[10,'relu', 'Dense', 16, 10])\n",
379 | "th.start()\n",
380 | "print('Actual count: ', threading.active_count(), '\\n')\n",
381 | "time.sleep(10)\n",
382 | "th.kill()\n",
383 | "th.join()\n",
384 | "print('\\n', 'Actual count: ', threading.active_count())"
385 | ],
386 | "metadata": {
387 | "colab": {
388 | "base_uri": "https://localhost:8080/"
389 | },
390 | "id": "HDHr-q5oGuf0",
391 | "outputId": "a490a01d-7bc4-46a5-e4a4-6c2e55c42d5d"
392 | },
393 | "execution_count": 43,
394 | "outputs": [
395 | {
396 | "output_type": "stream",
397 | "name": "stderr",
398 | "text": [
399 | "\n",
400 | "PYDEV DEBUGGER WARNING:\n",
401 | "sys.settrace() should not be used when the debugger is being used.\n",
402 | "This may cause the debugger to stop working correctly.\n",
403 | "If this is needed, please check: \n",
404 | "http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html\n",
405 | "to see how to restore the debug tracing back correctly.\n",
406 | "Call Location:\n",
407 | " File \"\", line 18, in __run\n",
408 | " sys.settrace(self.globaltrace)\n",
409 | "\n"
410 | ]
411 | },
412 | {
413 | "output_type": "stream",
414 | "name": "stdout",
415 | "text": [
416 | "Actual count: 10\n",
417 | "Actual count: 11 \n",
418 | "\n",
419 | "Epoch 1/10\n",
420 | "7/7 [==============================] - 1s 4ms/step - loss: 0.2853\n",
421 | "Epoch 2/10\n",
422 | "7/7 [==============================] - 0s 4ms/step - loss: 0.2683\n",
423 | "Epoch 3/10\n",
424 | "7/7 [==============================] - 0s 4ms/step - loss: 0.2440\n",
425 | "Epoch 4/10\n",
426 | "7/7 [==============================] - 0s 4ms/step - loss: 0.2191\n",
427 | "Epoch 5/10\n",
428 | "7/7 [==============================] - 0s 4ms/step - loss: 0.2026\n",
429 | "Epoch 6/10\n",
430 | "7/7 [==============================] - 0s 4ms/step - loss: 0.1966\n",
431 | "Epoch 7/10\n",
432 | "7/7 [==============================] - 0s 4ms/step - loss: 0.1879\n",
433 | "Epoch 8/10\n",
434 | "7/7 [==============================] - 0s 4ms/step - loss: 0.1792\n",
435 | "Epoch 9/10\n",
436 | "7/7 [==============================] - 0s 4ms/step - loss: 0.1731\n",
437 | "Epoch 10/10\n",
438 | "7/7 [==============================] - 0s 4ms/step - loss: 0.1693\n",
439 | "\n",
440 | " Actual count: 10\n"
441 | ]
442 | }
443 | ]
444 | }
445 | ]
446 | }
--------------------------------------------------------------------------------
/Notebooks/013_tf2tflite.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "013_tf2tflite.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyOCGVfpYpIn4YHkl+VmZ5GK",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "source": [
33 | "## Converting a Keras model to a TF Lite Model\n",
34 | "---\n",
35 | "Marco Sanguineti, 2022"
36 | ],
37 | "metadata": {
38 | "id": "ixbTs8XHMikP"
39 | }
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 1,
44 | "metadata": {
45 | "colab": {
46 | "base_uri": "https://localhost:8080/",
47 | "height": 35
48 | },
49 | "id": "d9ump0qPK_cv",
50 | "outputId": "a555bd94-09c3-4a4e-f11d-84bbdf49acb5"
51 | },
52 | "outputs": [
53 | {
54 | "output_type": "execute_result",
55 | "data": {
56 | "text/plain": [
57 | "'2.8.0'"
58 | ],
59 | "application/vnd.google.colaboratory.intrinsic+json": {
60 | "type": "string"
61 | }
62 | },
63 | "metadata": {},
64 | "execution_count": 1
65 | }
66 | ],
67 | "source": [
68 | "import tensorflow as tf\n",
69 | "tf.__version__"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "source": [
75 | "input_lay = tf.keras.layers.Input(shape=[1])\n",
76 | "\n",
77 | "lay_1 = tf.keras.layers.Dense(units=16, activation='selu')(input_lay)\n",
78 | "\n",
79 | "out_lay = tf.keras.layers.Dense(units=1, activation='linear')(lay_1)"
80 | ],
81 | "metadata": {
82 | "id": "NTaJtpfqMpaL"
83 | },
84 | "execution_count": 2,
85 | "outputs": []
86 | },
87 | {
88 | "cell_type": "code",
89 | "source": [
90 | "model = tf.keras.models.Model(inputs= input_lay, outputs=out_lay)\n",
91 | "model.summary()"
92 | ],
93 | "metadata": {
94 | "colab": {
95 | "base_uri": "https://localhost:8080/"
96 | },
97 | "id": "CackfP3bNSGX",
98 | "outputId": "3bed9f7d-e432-492c-9c79-f348ad1e7f08"
99 | },
100 | "execution_count": 3,
101 | "outputs": [
102 | {
103 | "output_type": "stream",
104 | "name": "stdout",
105 | "text": [
106 | "Model: \"model\"\n",
107 | "_________________________________________________________________\n",
108 | " Layer (type) Output Shape Param # \n",
109 | "=================================================================\n",
110 | " input_1 (InputLayer) [(None, 1)] 0 \n",
111 | " \n",
112 | " dense (Dense) (None, 16) 32 \n",
113 | " \n",
114 | " dense_1 (Dense) (None, 1) 17 \n",
115 | " \n",
116 | "=================================================================\n",
117 | "Total params: 49\n",
118 | "Trainable params: 49\n",
119 | "Non-trainable params: 0\n",
120 | "_________________________________________________________________\n"
121 | ]
122 | }
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "source": [
128 | "optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)\n",
129 | "\n",
130 | "model.compile(optimizer=optimizer, loss=tf.keras.losses.mean_squared_error)"
131 | ],
132 | "metadata": {
133 | "id": "lPe-A6-4NzaH"
134 | },
135 | "execution_count": 4,
136 | "outputs": []
137 | },
138 | {
139 | "cell_type": "code",
140 | "source": [
141 | "model.fit(x=[-1, 0, 1], y=[2, 3, 4], epochs=10)"
142 | ],
143 | "metadata": {
144 | "colab": {
145 | "base_uri": "https://localhost:8080/"
146 | },
147 | "id": "pCOTCGaxOAPp",
148 | "outputId": "4325cd2b-1862-40df-ba4e-81b417f5f8db"
149 | },
150 | "execution_count": 7,
151 | "outputs": [
152 | {
153 | "output_type": "stream",
154 | "name": "stdout",
155 | "text": [
156 | "Epoch 1/10\n",
157 | "1/1 [==============================] - 0s 10ms/step - loss: 8.2770\n",
158 | "Epoch 2/10\n",
159 | "1/1 [==============================] - 0s 15ms/step - loss: 8.2715\n",
160 | "Epoch 3/10\n",
161 | "1/1 [==============================] - 0s 11ms/step - loss: 8.2660\n",
162 | "Epoch 4/10\n",
163 | "1/1 [==============================] - 0s 14ms/step - loss: 8.2604\n",
164 | "Epoch 5/10\n",
165 | "1/1 [==============================] - 0s 10ms/step - loss: 8.2549\n",
166 | "Epoch 6/10\n",
167 | "1/1 [==============================] - 0s 10ms/step - loss: 8.2494\n",
168 | "Epoch 7/10\n",
169 | "1/1 [==============================] - 0s 11ms/step - loss: 8.2439\n",
170 | "Epoch 8/10\n",
171 | "1/1 [==============================] - 0s 19ms/step - loss: 8.2384\n",
172 | "Epoch 9/10\n",
173 | "1/1 [==============================] - 0s 11ms/step - loss: 8.2329\n",
174 | "Epoch 10/10\n",
175 | "1/1 [==============================] - 0s 10ms/step - loss: 8.2274\n"
176 | ]
177 | },
178 | {
179 | "output_type": "execute_result",
180 | "data": {
181 | "text/plain": [
182 | ""
183 | ]
184 | },
185 | "metadata": {},
186 | "execution_count": 7
187 | }
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "source": [
193 | "converter = tf.lite.TFLiteConverter.from_keras_model(model)"
194 | ],
195 | "metadata": {
196 | "id": "HQGbF6H_OGm4"
197 | },
198 | "execution_count": 8,
199 | "outputs": []
200 | },
201 | {
202 | "cell_type": "code",
203 | "source": [
204 | "tflite_model = converter.convert()"
205 | ],
206 | "metadata": {
207 | "colab": {
208 | "base_uri": "https://localhost:8080/"
209 | },
210 | "id": "ZKOjLpESOTm3",
211 | "outputId": "9d7fb020-e894-47c1-f0a8-81605e5ee1dd"
212 | },
213 | "execution_count": 9,
214 | "outputs": [
215 | {
216 | "output_type": "stream",
217 | "name": "stdout",
218 | "text": [
219 | "INFO:tensorflow:Assets written to: /tmp/tmp5lesccyy/assets\n"
220 | ]
221 | },
222 | {
223 | "output_type": "stream",
224 | "name": "stderr",
225 | "text": [
226 | "WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded\n"
227 | ]
228 | }
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "source": [
234 | "tflite_model"
235 | ],
236 | "metadata": {
237 | "colab": {
238 | "base_uri": "https://localhost:8080/"
239 | },
240 | "id": "XeA2LAfEOWLu",
241 | "outputId": "b3a1d56d-6db8-4b1d-c2b2-5509927c037c"
242 | },
243 | "execution_count": 10,
244 | "outputs": [
245 | {
246 | "output_type": "execute_result",
247 | "data": {
248 | "text/plain": [
249 | "b\"\\x1c\\x00\\x00\\x00TFL3\\x14\\x00 \\x00\\x04\\x00\\x08\\x00\\x0c\\x00\\x10\\x00\\x14\\x00\\x00\\x00\\x18\\x00\\x1c\\x00\\x14\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x18\\x00\\x00\\x000\\x00\\x00\\x00T\\x01\\x00\\x000\\x00\\x00\\x00\\xfc\\x00\\x00\\x00|\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\\\\\x06\\x00\\x00\\x98\\x05\\x00\\x00h\\x04\\x00\\x00\\xcc\\x03\\x00\\x00<\\x03\\x00\\x00\\xac\\x02\\x00\\x00\\x01\\x00\\x00\\x00H\\x01\\x00\\x00\\x14\\x00\\x00\\x00\\xdc\\n\\x00\\x00\\xd8\\n\\x00\\x00\\x14\\n\\x00\\x00\\x88\\t\\x00\\x00\\xfc\\x08\\x00\\x00l\\x08\\x00\\x00$\\x08\\x00\\x00\\xdc\\x07\\x00\\x00\\x94\\x07\\x00\\x00\\x08\\x07\\x00\\x00\\x80\\x06\\x00\\x00\\xb0\\n\\x00\\x00\\xac\\n\\x00\\x00\\xa8\\n\\x00\\x00\\xa4\\n\\x00\\x00\\xa0\\n\\x00\\x00\\x9c\\n\\x00\\x00\\x98\\n\\x00\\x00\\x94\\n\\x00\\x00\\xb4\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x00\\x00\\n\\x00\\x10\\x00\\x0c\\x00\\x08\\x00\\x04\\x00\\n\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x1c\\x00\\x00\\x00 \\x00\\x00\\x00\\x0f\\x00\\x00\\x00serving_default\\x00\\x01\\x00\\x00\\x00\\x14\\x00\\x00\\x00\\x01\\x00\\x00\\x00$\\x00\\x00\\x00\\x08\\x00\\x0c\\x00\\x08\\x00\\x04\\x00\\x08\\x00\\x00\\x00\\x11\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x07\\x00\\x00\\x00dense_1\\x00\\xa2\\xf6\\xff\\xff\\x04\\x00\\x00\\x00\\x07\\x00\\x00\\x00input_1\\x00\\x01\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x08\\x00\\x0c\\x00\\x04\\x00\\x08\\x00\\x08\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x13\\x00\\x00\\x00\\x13\\x00\\x00\\x00min_runtime_version\\x00\\xea\\xf6\\xff\\xff\\x04\\x00\\x00\\x00\\x10\\x00\\x00\\x002.2.0\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x0f\\x00\\x00\\x00MLIR Converted.\\x00\\x00\\x00\\x0e\\x00\\x18\\x00\\x04\\x00\\x08\\x00\\x0c\\x00\\x10\\x00\\x14\\x00\\x0e\\x00\\x00\\x00\\x14\\x00\\x00\\x00\\\\\\x00\\x00\\x00`\\x00\\x00\\x00d\\x00\\x00\\x00\\x84\\x00\\x00\\x00\\x12\\x00\\x00\\x00(\\t\\x00\\x00\\xdc\\x08\\x00\\x00|\\x08\\x00\\x00\\xf0\\x07\\x00\\x00`\\x07\\x00\\x00\\xdc\\x06\\x00\\x00\\x94\\x06\\x00\\x00L\\x06\\x00\\x00\\xfc\\x05\\x00\\x00t\\x05\\x00\\x00\\xc4\\x04\\x00\\x00\\x14\\x04\\x00\\x00L\\x03\\x00\\x00\\xcc\\x02\\x00\\x000\\x02\\x00\\x00\\xa0\\x01\\x00\\x00\\x10\\x01\\x00\\x00t\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x11\\x00\\x00\\x00\\x08\\x00\\x00\\x00D\\x04\\x00\\x00\\x98\\x03\\x00\\x00\\xe8\\x02\\x00\\x00\\\\\\x02\\x00\\x00\\xbc\\x01\\x00\\x00,\\x01\\x00\\x00\\x98\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x04\\x00\\x00\\x00main\\x00\\x00\\x00\\x00\\xf6\\xfb\\xff\\xff\\x00\\x00\\x00\\x08\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x00 \\xf7\\xff\\xff\\x01\\x00\\x00\\x00\\x11\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\xa4\\xf7\\xff\\xff\\x14\\x00\\x00\\x00\\x12\\x00\\x00\\x00\\x18\\x00\\x00\\x00@\\x00\\x00\\x000\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x19\\x00\\x00\\x00StatefulPartitionedCall:0\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x01\\x00\\x00\\x00\\x8c\\xf7\\xff\\xffV\\xfe\\xff\\xff\\x00\\x00\\x00b\\x05\\x00\\x00\\x00\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\xa8\\xf7\\xff\\xff\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x0f\\x00\\x00\\x00p\\xfc\\xff\\xff{\\x00\\x00\\x00\\x00\\x00\\x00{\\x01\\x00\\x00\\x00<\\xf8\\xff\\xff\\x14\\x00\\x00\\x00\\x11\\x00\\x00\\x00\\x18\\x00\\x00\\x008\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu9\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\x1c\\xf8\\xff\\xff\\xe6\\xfe\\xff\\xff\\x00\\x00\\x00\\x15\\x04\\x00\\x00\\x00\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x008\\xf8\\xff\\xff\\x01\\x00\\x00\\x00\\x0f\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\xfc\\xfc\\xff\\xff\\x12\\x00\\x00\\x00\\x00\\x00\\x00\\x12\\x01\\x00\\x00\\x00\\xc8\\xf8\\xff\\xff\\x14\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x18\\x00\\x00\\x008\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu8\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\xa8\\xf8\\xff\\xffr\\xff\\xff\\xff\\x00\\x00\\x00\\x1c\\x03\\x00\\x00\\x00\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\xc4\\xf8\\xff\\xff\\x01\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\r\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x88\\xfd\\xff\\xff)\\x00\\x00\\x00\\x00\\x00\\x00)\\x01\\x00\\x00\\x00T\\xf9\\xff\\xff\\x14\\x00\\x00\\x00\\x0f\\x00\\x00\\x00\\x18\\x00\\x00\\x008\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu7\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\x00\\xfa\\xff\\xff\\x00\\x00\\x0e\\x00\\x18\\x00\\x08\\x00\\x0c\\x00\\x10\\x00\\x07\\x00\\x14\\x00\\x0e\\x00\\x00\\x00\\x00\\x00\\x00!\\x02\\x00\\x00\\x00\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x00`\\xf9\\xff\\xff\\x01\\x00\\x00\\x00\\r\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\n\\x00\\x00\\x00 \\xfe\\xff\\xff/\\x00\\x00\\x00\\x00\\x00\\x00/\\x01\\x00\\x00\\x00\\xec\\xf9\\xff\\xff\\x14\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x18\\x00\\x00\\x008\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu6\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\xcc\\xf9\\xff\\xff\\xba\\xfe\\xff\\xff\\x00\\x00\\x00\\x08\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\xe4\\xf9\\xff\\xff\\x01\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\t\\x00\\x00\\x00h\\xfa\\xff\\xff\\x14\\x00\\x00\\x00\\r\\x00\\x00\\x00\\x18\\x00\\x00\\x00\\\\\\x00\\x00\\x00L\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x007\\x00\\x00\\x00model/dense/MatMul;model/dense/BiasAdd;model/dense/Selu\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x008\\xfb\\xff\\xff\\x00\\x00\\n\\x00\\x10\\x00\\x04\\x00\\x08\\x00\\x0c\\x00\\n\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\n\\x00\\x00\\x00\\x05\\x00\\x00\\x00L\\xff\\xff\\xff=\\x00\\x00\\x00\\x00\\x00\\x00=\\x01\\x00\\x00\\x00\\x14\\x00\\x1c\\x00\\x08\\x00\\x07\\x00\\x0c\\x00\\x10\\x00\\x14\\x00\\x00\\x00\\x00\\x00\\x18\\x00\\x14\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x14\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x18\\x00\\x00\\x008\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu5\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\xdc\\xfb\\xff\\xff\\x00\\x00\\x0e\\x00\\x14\\x00\\x00\\x00\\x08\\x00\\x0c\\x00\\x07\\x00\\x10\\x00\\x0e\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x18\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x04\\x00\\x00\\x008\\xfb\\xff\\xff\\x01\\x00\\x00\\x00\\n\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x0c\\x00\\x10\\x00\\x0b\\x00\\x00\\x00\\x0c\\x00\\x04\\x00\\x0c\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x00\\x00\\t\\x01\\x00\\x00\\x00\\xd8\\xfb\\xff\\xff\\x14\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x18\\x00\\x00\\x00L\\x00\\x00\\x00<\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00&\\x00\\x00\\x00model/dense/MatMul;model/dense/BiasAdd\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\xcc\\xfb\\xff\\xff\\x92\\xfc\\xff\\xff\\x04\\x00\\x00\\x00@\\x00\\x00\\x00p\\xe5\\x06=\\xce\\x0b&=J]\\n\\xbd0\\x9e\\x06\\xbdg\\x1b\\x05=\\xeeO\\x08=gY\\xe7<}\\x1c\\x0b\\xbd\\x8c\\xb5\\x15\\xbd\\xe1\\xb7\\x1c\\xbd\\x84\\xbe\\n\\xbd\\xce\\xad\\x13=\\xc0\\xef\\x04\\xbdZ\\xa8\\t\\xbd\\x18\\x8a\\t\\xbd\\xb0c\\x02=\\xc6\\xfc\\xff\\xff\\x10\\x00\\x00\\x00\\n\\x00\\x00\\x00\\x10\\x00\\x00\\x00$\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu4\\x00\\x00\\x00P\\xfc\\xff\\xff\\x16\\xfd\\xff\\xff\\x04\\x00\\x00\\x00@\\x00\\x00\\x004D\\xaa\\xbe\\xca\\x86%>E\\xec\\x92=\\x9cH\\xad>B\\x98\\xbf\\xbe\\xc4\\x91\\xc1\\xbe\\nD\\xa5\\xbe\\xba\\xcd&\\xbf4\\xfd\\x89\\xbc\\x1f\\x84\\x01\\xbf\\x98l\\x86\\xbd\\x14\\xf2\\x13?7\\xb7\\x14?X\\\\\\xde\\xbd\\xedK\\x90\\xbe<\\xf8\\x9f\\xbcJ\\xfd\\xff\\xff\\x10\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x14\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu3\\x00\\x00\\x00\\xd8\\xfc\\xff\\xff\\x9e\\xfd\\xff\\xff\\x04\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x80?\\x96\\xfd\\xff\\xff\\x10\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x0c\\x00\\x00\\x00 \\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu2\\x00\\x00\\x00\\x1c\\xfd\\xff\\xff\\xe2\\xfd\\xff\\xff\\x04\\x00\\x00\\x00\\x04\\x00\\x00\\x00f\\t\\xe1?\\xda\\xfd\\xff\\xff\\x10\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x0c\\x00\\x00\\x00 \\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x11\\x00\\x00\\x00model/dense/Selu1\\x00\\x00\\x00`\\xfd\\xff\\xff&\\xfe\\xff\\xff\\x04\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x1e\\xfe\\xff\\xff\\x10\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x0c\\x00\\x00\\x00 \\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x10\\x00\\x00\\x00model/dense/Selu\\x00\\x00\\x00\\x00\\xa4\\xfd\\xff\\xffj\\xfe\\xff\\xff\\x04\\x00\\x00\\x00@\\x00\\x00\\x001\\xe1\\x8f>9\\xf8\\x80=\\xa8\\x9c\\x88\\xbe^i\\x9d\\xbe\\xc4+->sK\\x0e?i\\x8e\\xbc<\\xdc\\xbf\\xad\\xbe\\xb3\\xdf\\xc5\\xbe\\r\\xf8\\x85\\xbd\\x86p\\xcf\\xbe\\xac\\xb9\\xde=\\xce\\xceN\\xbe\\xf9\\x9f\\x0f\\xbf1b\\x16\\xbf\\x08{\\x04>\\x9e\\xfe\\xff\\xff\\x10\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x14\\x00\\x00\\x00,\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x14\\x00\\x00\\x00model/dense_1/MatMul\\x00\\x00\\x00\\x000\\xfe\\xff\\xff\\xf6\\xfe\\xff\\xff\\x04\\x00\\x00\\x00@\\x00\\x00\\x00\\xe1\\x0c\\xa2\\xbe\\x04\\x8a\\x1d>O\\xd5\\x8b=\\x03\\xec\\xa4>vY\\xb6\\xbe\\x93:\\xb8\\xbe}J\\x9d\\xbe-\\xc1\\x1e\\xbf\\x9bT\\x83\\xbca\\x88\\xf6\\xbe\\x11\\xe0\\x7f\\xbd|\\xce\\x0c?\\x1c\\x8a\\r?}\\xa1\\xd3\\xbdiU\\x89\\xbe\\x1b@\\x98\\xbc*\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x14\\x00\\x00\\x00(\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x12\\x00\\x00\\x00model/dense/MatMul\\x00\\x00\\xb8\\xfe\\xff\\xff~\\xff\\xff\\xff\\x04\\x00\\x00\\x00@\\x00\\x00\\x00\\x0bc\\x00=\\x9d\\x08\\x1e=\\x0e\\xb0\\x03\\xbd<\\x1f\\x00\\xbd9^\\xfd<\\x0c\\xbc\\x01=\\x83/\\xdc<\\x07f\\x04\\xbd+|\\x0e\\xbd\\xea'\\x15\\xbd\\x97\\x0c\\x04\\xbd\\x81\\x8d\\x0c=!\\x0b\\xfd\\xbc\\xd9\\x03\\x03\\xbd\\r\\xe7\\x02\\xbd\\xf01\\xf8<\\xb2\\xff\\xff\\xff\\x10\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x10\\x00\\x00\\x00 \\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\n\\x00\\x00\\x00dense/bias\\x00\\x00\\x04\\x00\\x06\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x08\\x00\\x04\\x00\\x06\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\xd5e\\x02=\\x00\\x00\\x0e\\x00\\x14\\x00\\x04\\x00\\x00\\x00\\x08\\x00\\x0c\\x00\\x10\\x00\\x0e\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x10\\x00\\x00\\x00 \\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x0c\\x00\\x00\\x00dense_1/bias\\x00\\x00\\x00\\x00\\x94\\xff\\xff\\xff\\x14\\x00\\x18\\x00\\x04\\x00\\x00\\x00\\x08\\x00\\x0c\\x00\\x10\\x00\\x00\\x00\\x00\\x00\\x14\\x00\\x14\\x00\\x00\\x00\\x14\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x18\\x00\\x00\\x00@\\x00\\x00\\x000\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x19\\x00\\x00\\x00serving_default_input_1:0\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\x01\\x00\\x00\\x00\\xfc\\xff\\xff\\xff\\x04\\x00\\x04\\x00\\x04\\x00\\x00\\x00\""
250 | ]
251 | },
252 | "metadata": {},
253 | "execution_count": 10
254 | }
255 | ]
256 | },
257 | {
258 | "cell_type": "code",
259 | "source": [
260 | "with open('model.tflite', 'wb') as model_:\n",
261 | " model_.write(tflite_model)"
262 | ],
263 | "metadata": {
264 | "id": "IglVD-K6OY_g"
265 | },
266 | "execution_count": 11,
267 | "outputs": []
268 | },
269 | {
270 | "cell_type": "code",
271 | "source": [
272 | ""
273 | ],
274 | "metadata": {
275 | "id": "eY5-DIj9OhJI"
276 | },
277 | "execution_count": null,
278 | "outputs": []
279 | }
280 | ]
281 | }
--------------------------------------------------------------------------------
/Notebooks/015_TftWatcherIntro.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "from riotwatcher import TftWatcher\n",
10 | "import pandas as pd\n",
11 | "import plotly.graph_objects as go\n",
12 | "import numpy as np"
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": null,
18 | "metadata": {},
19 | "outputs": [],
20 | "source": [
21 | "api_key = ''\n",
22 | "watcher = TftWatcher(api_key)\n",
23 | "my_region = 'euw1'\n",
24 | "summoner_name = ''\n",
25 | "\n",
26 | "me = watcher.summoner.by_name(my_region, summoner_name)"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "for key in me:\n",
36 | " print(key, ':', me[key])"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": null,
42 | "metadata": {},
43 | "outputs": [],
44 | "source": [
45 | "matches_ids = watcher.match.by_puuid(my_region, me['puuid'], count=20)\n",
46 | "matches = [watcher.match.by_id(my_region, item) for item in matches_ids]"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "matches[0].keys()"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": null,
61 | "metadata": {},
62 | "outputs": [],
63 | "source": [
64 | "matches[0]['metadata']"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {},
71 | "outputs": [],
72 | "source": [
73 | "matches[0]['info'].keys()"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {},
80 | "outputs": [],
81 | "source": [
82 | "my_match_data = matches[0]['info']['participants'][matches[0]['metadata']['participants'].index(me['puuid'])]\n",
83 | "my_match_data.keys()"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": null,
89 | "metadata": {},
90 | "outputs": [],
91 | "source": [
92 | "my_match_data['augments']"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": [
101 | "my_match_data['placement']"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": null,
107 | "metadata": {},
108 | "outputs": [],
109 | "source": [
110 | "my_match_data['units'][:2]"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": [
119 | "import json\n",
120 | "with open('units.json', 'w') as file:\n",
121 | " json.dump(my_match_data['units'], file, indent=4)"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {},
128 | "outputs": [],
129 | "source": [
130 | "from __future__ import annotations\n",
131 | "\n",
132 | "def get_placement_trend(matches: list | np.ndarray) -> np.ndarray:\n",
133 | " \"\"\"\n",
134 | " :param matches: list of matches\n",
135 | " :return np.ndarray placement_trend:\n",
136 | " \"\"\"\n",
137 | "\n",
138 | " placement_trend = []\n",
139 | " for match in matches:\n",
140 | " my_match_data = match['info']['participants'][match['metadata']['participants'].index(me['puuid'])]\n",
141 | " placement_trend.append(int(my_match_data['placement']))\n",
142 | " \n",
143 | " return np.array(placement_trend, dtype=np.int32)"
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": null,
149 | "metadata": {},
150 | "outputs": [],
151 | "source": [
152 | "placements = get_placement_trend(matches=matches)\n",
153 | "x = np.arange(len(placements))\n",
154 | "\n",
155 | "fig = go.Figure()\n",
156 | "\n",
157 | "fig.add_trace(go.Scatter(\n",
158 | " x=x, y=placements,\n",
159 | " name='sin',\n",
160 | " mode='markers',\n",
161 | " marker_color=placements\n",
162 | "))\n",
163 | "\n",
164 | "\n",
165 | "fig.update_traces(mode='markers', marker_line_width=2, marker_size=10)\n",
166 | "fig.update_layout(title='Positioning trend', width=800)\n"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": null,
172 | "metadata": {},
173 | "outputs": [],
174 | "source": []
175 | }
176 | ],
177 | "metadata": {
178 | "kernelspec": {
179 | "display_name": "Python 3.9.15 ('python39')",
180 | "language": "python",
181 | "name": "python3"
182 | },
183 | "language_info": {
184 | "codemirror_mode": {
185 | "name": "ipython",
186 | "version": 3
187 | },
188 | "file_extension": ".py",
189 | "mimetype": "text/x-python",
190 | "name": "python",
191 | "nbconvert_exporter": "python",
192 | "pygments_lexer": "ipython3",
193 | "version": "3.9.15"
194 | },
195 | "orig_nbformat": 4,
196 | "vscode": {
197 | "interpreter": {
198 | "hash": "b1d11ed72a501dea5550e0551559d0470927893b027dce83f7e5d2ff4d36c615"
199 | }
200 | }
201 | },
202 | "nbformat": 4,
203 | "nbformat_minor": 2
204 | }
205 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TMML
2 | ## 3 Minutes Machine Learning
3 | 3 minutes machine learning is a series of tutorials and informative videos related to the world of machine learning. You can find the complete videos on YouTube. This repository contains all the files shown in the videos. This series is in experimental phase and is developed totally free of charge, for fun and culture! Any feedback is welcome. For doubts/questions or collaborate to the project contact me on GitHub/Youtube/LinkedIn.
4 | 
5 |
--------------------------------------------------------------------------------
/yt_thumb_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitMarco27/TMML/93d781fbcc3fdbabda03e650e4cd0375ea2474e5/yt_thumb_1.png
--------------------------------------------------------------------------------