"This notebook provides basic example code to create, build, and run [MoViNets (Mobile Video Networks)](https://arxiv.org/pdf/2103.11511.pdf). Models use TF Keras and support inference in TF 1 and TF 2. Pretrained models are provided by [TensorFlow Hub](https://tfhub.dev/google/collections/movinet/), trained on [Kinetics 600](https://deepmind.com/research/open-source/kinetics) for video action classification."
"This notebook provides basic example code to build, run, and fine-tune [MoViNets (Mobile Video Networks)](https://arxiv.org/pdf/2103.11511.pdf).\n",
"\n",
"Pretrained models are provided by [TensorFlow Hub](https://tfhub.dev/google/collections/movinet/) and the [TensorFlow Model Garden](https://github.com/tensorflow/models/tree/master/official/projects/movinet), trained on [Kinetics 600](https://deepmind.com/research/open-source/kinetics) for video action classification. All Models use TensorFlow 2 with Keras for inference and training.\n",
"\n",
"The following steps will be performed:\n",
"\n",
"1. [Running base model inference with TensorFlow Hub](#scrollTo=6g0tuFvf71S9\u0026line=8\u0026uniqifier=1)\n",
"2. [Running streaming model inference with TensorFlow Hub and plotting predictions](#scrollTo=ADrHPmwGcBZ5\u0026line=4\u0026uniqifier=1)\n",
"3. [Exporting a streaming model to TensorFlow Lite for mobile](#scrollTo=W3CLHvubvdSI\u0026line=3\u0026uniqifier=1)\n",
"4. [Fine-Tuning a base Model with the TensorFlow Model Garden](#scrollTo=_s-7bEoa3f8g\u0026line=11\u0026uniqifier=1)\n",
"# To run on a video, pass in one frame at a time\n",
"states = init_states\n",
"for clip in clips:\n",
" # Input shape: [1, 1, 172, 172, 3]\n",
" outputs = runner(**states, image=clip)\n",
" logits = outputs.pop('logits')[0]\n",
" states = outputs\n",
"\n",
"probs = tf.nn.softmax(logits)\n",
"top_k = get_top_k(probs)\n",
"print()\n",
"for label, prob in top_k:\n",
" print(label, prob)"
]
]
},
},
{
{
...
@@ -175,17 +679,17 @@
...
@@ -175,17 +679,17 @@
"id": "_s-7bEoa3f8g"
"id": "_s-7bEoa3f8g"
},
},
"source": [
"source": [
"## Example Usage with the TensorFlow Model Garden\n",
"## Fine-Tune a Base Model with the TensorFlow Model Garden\n",
"\n",
"\n",
"Fine-tune MoViNet-A0-Base on [UCF-101](https://www.crcv.ucf.edu/research/data-sets/ucf101/).\n",
"We will Fine-tune MoViNet-A0-Base on [UCF-101](https://www.crcv.ucf.edu/research/data-sets/ucf101/).\n",
"\n",
"\n",
"The following code will:\n",
"The following code will:\n",
"\n",
"\n",
"- Load the UCF-101 dataset with [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/ucf101).\n",
"- Load the UCF-101 dataset with [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/ucf101).\n",
"- Create a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) pipeline for training and evaluation.\n",
"- Create a simple [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) pipeline for training and evaluation.\n",
"- Display some example videos from the dataset.\n",
"- Display some example videos from the dataset.\n",
"- Build a MoViNet model and load pretrained weights.\n",
"- Build a MoViNet model and load pretrained weights.\n",
"- Fine-tune the final classifier layers on UCF-101."
"- Fine-tune the final classifier layers on UCF-101 and evaluate accuracy on the validation set."
]
]
},
},
{
{
...
@@ -196,7 +700,25 @@
...
@@ -196,7 +700,25 @@
"source": [
"source": [
"### Load the UCF-101 Dataset with TensorFlow Datasets\n",
"### Load the UCF-101 Dataset with TensorFlow Datasets\n",
"\n",
"\n",
"Calling `download_and_prepare()` will automatically download the dataset. After downloading, this cell will output information about the dataset."
"Calling `download_and_prepare()` will automatically download the dataset. This step may take up to 1 hour depending on the download and extraction speed. After downloading, the next cell will output information about the dataset."
"# Build the training and evaluation datasets.\n",
"\n",
"batch_size = 8\n",
"batch_size = 8\n",
"num_frames = 8\n",
"num_frames = 8\n",
"frame_stride = 10\n",
"frame_stride = 10\n",
...
@@ -392,16 +907,9 @@
...
@@ -392,16 +907,9 @@
"id": "R3RHeuHdsd_3"
"id": "R3RHeuHdsd_3"
},
},
"source": [
"source": [
"### Build MoViNet-A0-Base and Load Pretrained Weights"
"### Build MoViNet-A0-Base and Load Pretrained Weights\n",
]
"\n",
},
"Here we create a MoViNet model using the open source code provided in [official/projects/movinet](https://github.com/tensorflow/models/tree/master/official/projects/movinet) and load the pretrained weights. Here we freeze the all layers except the final classifier head to speed up fine-tuning."
{
"cell_type": "markdown",
"metadata": {
"id": "JXVQOP9Rqk0I"
},
"source": [
"Here we create a MoViNet model using the open source code provided in [tensorflow/models](https://github.com/tensorflow/models) and load the pretrained weights. Here we freeze the all layers except the final classifier head to speed up fine-tuning."