File size: 11,285 Bytes
5672777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bK-7g5sizhg5"
      },
      "source": [
        "## Install Tensorflow-Models packages\n",
        "\n",
        "The notebook is tested with Google Colab sandbox.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 9737,
          "status": "ok",
          "timestamp": 1650513863935,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "eTz93_P2dMty",
        "outputId": "d147b4b0-954f-4064-d179-bb82bc3ea4fe"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[K     |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 21.8 MB 1.6 MB/s \n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip3 install -q tf-models-nightly\n",
        "# Fix Colab default opencv problem\n",
        "!pip3 install -q opencv-python-headless==4.1.2.30\n",
        "\n",
        "## Colab environment setup. To use a stable TF release version\n",
        "##  because of the possible breakage in tf-nightly.\n",
        "# !pip3 install -U numpy\u003e=1.20\n",
        "# !pip3 install -q tensorflow==2.8.0"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 2,
          "status": "ok",
          "timestamp": 1650513867685,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "GHvGWdCcdQqG",
        "outputId": "863683b2-6b70-4de9-98bc-5df743619ad5"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "1.21.6\n",
            "2.10.0-dev20220420\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "import tensorflow as tf\n",
        "print(np.__version__)\n",
        "print(tf.__version__)\n",
        "\n",
        "import tensorflow_models as tfm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eYSeQJniztc8"
      },
      "source": [
        "## Check out modules\n",
        "\n",
        "**Note: As the TensorFlow Models (NLP + Vision) 2.9 release which is tested for this notebook, we partially exported selected modules but the APIs are not stable. Also be aware that, the\n",
        "modeling libraries are advancing very fast, so we generally don't guarantee compatability between versions.** "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 206,
          "status": "ok",
          "timestamp": 1650513874596,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "Y1iEMMGTMrQu",
        "outputId": "f3da68d7-ecda-471c-c27b-915b80b55131"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Top-level modules:  ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'core', 'hyperparams', 'nlp', 'optimization', 'utils', 'vision']\n",
            "NLP modules:  ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'encoders', 'layers', 'losses', 'models', 'networks', 'ops', 'serving_modules', 'tasks']\n",
            "Vision modules:  ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'anchor', 'anchor_generator', 'augment', 'backbones', 'box_matcher', 'box_ops', 'classification_model', 'configs', 'decoders', 'factory', 'factory_3d', 'heads', 'iou_similarity', 'layers', 'mask_ops', 'maskrcnn_model', 'nms', 'preprocess_ops', 'preprocess_ops_3d', 'retinanet_model', 'sampling_ops', 'segmentation_model', 'spatial_transform_ops', 'target_gather', 'video_classification_model']\n"
          ]
        }
      ],
      "source": [
        "print(\"Top-level modules: \", dir(tfm))\n",
        "print(\"NLP modules: \", dir(tfm.nlp))\n",
        "print(\"Vision modules: \", dir(tfm.vision))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UMHeJmk_1yUf"
      },
      "source": [
        "## Quick Examples\n",
        "\n",
        "### 1. Use a tfm.nlp Keras layer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 400,
          "status": "ok",
          "timestamp": 1650514040957,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "XVWEUozQ1xQY",
        "outputId": "5de2aa91-8c38-438e-80b6-481617917c08"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "transformer_encoder_block_1\n",
            "tf.Tensor(\n",
            "[[[-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.79198956  0.4180589 ]]\n",
            "\n",
            " [[-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.7919895   0.41805887]]], shape=(2, 3, 4), dtype=float32)\n"
          ]
        }
      ],
      "source": [
        "encoder_block = tfm.nlp.layers.TransformerEncoderBlock(\n",
        "        num_attention_heads=2, inner_dim=10, inner_activation='relu')\n",
        "\n",
        "batch, length, hidden_size = 2, 3, 4\n",
        "qkv_inputs = tf.ones((batch, length, hidden_size), tf.float32)\n",
        "attention_mask = None\n",
        "outputs = encoder_block([qkv_inputs, attention_mask])\n",
        "print(encoder_block.name)\n",
        "print(outputs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AOOrWjKkSYM0"
      },
      "source": [
        "### 2. Use a tfm.vision Backbone models"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 5979,
          "status": "ok",
          "timestamp": 1650514078414,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "xwD0UhUdSzNU",
        "outputId": "770d46c0-8c71-4f59-ee0b-67430c791380"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.\n",
            "WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.\n",
            "WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "spine_net\n",
            "{'4': \u003cKerasTensor: shape=(1, 8, 8, 128) dtype=float32 (created by layer 'spine_net')\u003e, '5': \u003cKerasTensor: shape=(1, 4, 4, 128) dtype=float32 (created by layer 'spine_net')\u003e, '6': \u003cKerasTensor: shape=(1, 2, 2, 128) dtype=float32 (created by layer 'spine_net')\u003e}\n"
          ]
        }
      ],
      "source": [
        "input_size = 128\n",
        "filter_size_scale, block_repeats, resample_alpha, endpoints_num_filters, min_level, max_level = 0.65, 1, 0.5, 128, 4, 6\n",
        "input_specs = tf.keras.layers.InputSpec(\n",
        "    shape=[None, input_size, input_size, 3])\n",
        "model = tfm.vision.backbones.SpineNet(\n",
        "    input_specs=input_specs,\n",
        "    min_level=min_level,\n",
        "    max_level=max_level,\n",
        "    endpoints_num_filters=endpoints_num_filters,\n",
        "    resample_alpha=resample_alpha,\n",
        "    block_repeats=block_repeats,\n",
        "    filter_size_scale=filter_size_scale,\n",
        "    init_stochastic_depth_rate=0.2,\n",
        ")\n",
        "\n",
        "inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)\n",
        "endpoints = model(inputs)\n",
        "print(model.name)\n",
        "print(endpoints)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HHJs4lRlTk8q"
      },
      "source": [
        "### 3. Use Orbit package for advanced training loops"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 215,
          "status": "ok",
          "timestamp": 1650514185283,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "X4ek9IrJTkP_",
        "outputId": "7b31de68-53a0-4b8a-d9ab-109e7c43c933"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Orbit modules:  ['AbstractEvaluator', 'AbstractTrainer', 'Action', 'Controller', 'StandardEvaluator', 'StandardEvaluatorOptions', 'StandardTrainer', 'StandardTrainerOptions', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'actions', 'controller', 'runner', 'standard_runner', 'utils']\n"
          ]
        }
      ],
      "source": [
        "import orbit\n",
        "print(\"Orbit modules: \", dir(orbit))"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "tensorflow_models_pypi",
      "provenance": [
        {
          "file_id": "1dm1dUZ2Bo6S6Zom7GTQrIG78Xz7iFeZY",
          "timestamp": 1650514452505
        }
      ]
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}