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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"source": [
|
| 20 |
+
"!pip install --upgrade tensorflow\n",
|
| 21 |
+
"!pip install --upgrade keras\n"
|
| 22 |
+
],
|
| 23 |
+
"metadata": {
|
| 24 |
+
"colab": {
|
| 25 |
+
"base_uri": "https://localhost:8080/"
|
| 26 |
+
},
|
| 27 |
+
"id": "U-cy2aDRljAL",
|
| 28 |
+
"outputId": "e9efe1eb-0135-432f-9d46-3c39a8013f5b"
|
| 29 |
+
},
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"outputs": [
|
| 32 |
+
{
|
| 33 |
+
"output_type": "stream",
|
| 34 |
+
"name": "stdout",
|
| 35 |
+
"text": [
|
| 36 |
+
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
|
| 37 |
+
"Requirement already satisfied: tensorflow in /usr/local/lib/python3.10/dist-packages (2.12.0)\n",
|
| 38 |
+
"Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.4.0)\n",
|
| 39 |
+
"Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.6.3)\n",
|
| 40 |
+
"Requirement already satisfied: flatbuffers>=2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (23.3.3)\n",
|
| 41 |
+
"Requirement already satisfied: gast<=0.4.0,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.4.0)\n",
|
| 42 |
+
"Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)\n",
|
| 43 |
+
"Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.54.0)\n",
|
| 44 |
+
"Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.8.0)\n",
|
| 45 |
+
"Requirement already satisfied: jax>=0.3.15 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.4.10)\n",
|
| 46 |
+
"Requirement already satisfied: keras<2.13,>=2.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.12.0)\n",
|
| 47 |
+
"Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (16.0.0)\n",
|
| 48 |
+
"Requirement already satisfied: numpy<1.24,>=1.22 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.22.4)\n",
|
| 49 |
+
"Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.3.0)\n",
|
| 50 |
+
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow) (23.1)\n",
|
| 51 |
+
"Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.20.3)\n",
|
| 52 |
+
"Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow) (67.7.2)\n",
|
| 53 |
+
"Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0)\n",
|
| 54 |
+
"Requirement already satisfied: tensorboard<2.13,>=2.12 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.12.2)\n",
|
| 55 |
+
"Requirement already satisfied: tensorflow-estimator<2.13,>=2.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.12.0)\n",
|
| 56 |
+
"Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.3.0)\n",
|
| 57 |
+
"Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (4.5.0)\n",
|
| 58 |
+
"Requirement already satisfied: wrapt<1.15,>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.14.1)\n",
|
| 59 |
+
"Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.32.0)\n",
|
| 60 |
+
"Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow) (0.40.0)\n",
|
| 61 |
+
"Requirement already satisfied: ml-dtypes>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from jax>=0.3.15->tensorflow) (0.1.0)\n",
|
| 62 |
+
"Requirement already satisfied: scipy>=1.7 in /usr/local/lib/python3.10/dist-packages (from jax>=0.3.15->tensorflow) (1.10.1)\n",
|
| 63 |
+
"Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (2.17.3)\n",
|
| 64 |
+
"Requirement already satisfied: google-auth-oauthlib<1.1,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (1.0.0)\n",
|
| 65 |
+
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (3.4.3)\n",
|
| 66 |
+
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (2.27.1)\n",
|
| 67 |
+
"Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (0.7.0)\n",
|
| 68 |
+
"Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (1.8.1)\n",
|
| 69 |
+
"Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.13,>=2.12->tensorflow) (2.3.0)\n",
|
| 70 |
+
"Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow) (5.3.0)\n",
|
| 71 |
+
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow) (0.3.0)\n",
|
| 72 |
+
"Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow) (4.9)\n",
|
| 73 |
+
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<1.1,>=0.5->tensorboard<2.13,>=2.12->tensorflow) (1.3.1)\n",
|
| 74 |
+
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow) (1.26.15)\n",
|
| 75 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow) (2022.12.7)\n",
|
| 76 |
+
"Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow) (2.0.12)\n",
|
| 77 |
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow) (3.4)\n",
|
| 78 |
+
"Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.13,>=2.12->tensorflow) (2.1.2)\n",
|
| 79 |
+
"Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow) (0.5.0)\n",
|
| 80 |
+
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<1.1,>=0.5->tensorboard<2.13,>=2.12->tensorflow) (3.2.2)\n",
|
| 81 |
+
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
|
| 82 |
+
"Requirement already satisfied: keras in /usr/local/lib/python3.10/dist-packages (2.12.0)\n"
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
]
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "code",
|
| 89 |
+
"execution_count": null,
|
| 90 |
+
"metadata": {
|
| 91 |
+
"colab": {
|
| 92 |
+
"base_uri": "https://localhost:8080/",
|
| 93 |
+
"height": 371
|
| 94 |
+
},
|
| 95 |
+
"id": "b6wDH2UglAGa",
|
| 96 |
+
"outputId": "4049c353-40c4-4784-f064-e32e1678a309"
|
| 97 |
+
},
|
| 98 |
+
"outputs": [
|
| 99 |
+
{
|
| 100 |
+
"output_type": "error",
|
| 101 |
+
"ename": "AttributeError",
|
| 102 |
+
"evalue": "ignored",
|
| 103 |
+
"traceback": [
|
| 104 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 105 |
+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
| 106 |
+
"\u001b[0;32m<ipython-input-5-80a54cfd4831>\u001b[0m in \u001b[0;36m<cell line: 50>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;31m# Load the model weights from a checkpoint.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mVisionTransformer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_classes\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 51\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload_weights\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"checkpoints/model.ckpt\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 107 |
+
"\u001b[0;32m<ipython-input-5-80a54cfd4831>\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, num_classes)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;31m# The transformer encoder consists of a stack of self-attention layers.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m self.transformer_encoder = layers.TransformerEncoder(\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0mnum_heads\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0mnum_layers\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m12\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 108 |
+
"\u001b[0;31mAttributeError\u001b[0m: module 'keras.api._v2.keras.layers' has no attribute 'TransformerEncoder'"
|
| 109 |
+
]
|
| 110 |
+
}
|
| 111 |
+
],
|
| 112 |
+
"source": [
|
| 113 |
+
"import tensorflow as tf\n",
|
| 114 |
+
"import tensorflow.keras as keras\n",
|
| 115 |
+
"from tensorflow.keras import layers\n",
|
| 116 |
+
"\n",
|
| 117 |
+
"# Define the model architecture.\n",
|
| 118 |
+
"class VisionTransformer(keras.Model):\n",
|
| 119 |
+
"\n",
|
| 120 |
+
" def __init__(self, num_classes):\n",
|
| 121 |
+
" super(VisionTransformer, self).__init__()\n",
|
| 122 |
+
"\n",
|
| 123 |
+
" # The embedding layer converts each image patch into a vector representation.\n",
|
| 124 |
+
" self.embedding = layers.Embedding(\n",
|
| 125 |
+
" input_dim=256,\n",
|
| 126 |
+
" output_dim=512,\n",
|
| 127 |
+
" input_shape=(7, 7, 3),\n",
|
| 128 |
+
" trainable=True,\n",
|
| 129 |
+
" )\n",
|
| 130 |
+
"\n",
|
| 131 |
+
" # The transformer encoder consists of a stack of self-attention layers.\n",
|
| 132 |
+
" self.transformer_encoder = layers.TransformerEncoder(\n",
|
| 133 |
+
" num_heads=8,\n",
|
| 134 |
+
" num_layers=12,\n",
|
| 135 |
+
" dropout=0.1,\n",
|
| 136 |
+
" )\n",
|
| 137 |
+
"\n",
|
| 138 |
+
" # The classification layer outputs the class probabilities for each image.\n",
|
| 139 |
+
" self.classification = layers.Dense(num_classes, activation=\"softmax\")\n",
|
| 140 |
+
"\n",
|
| 141 |
+
" def call(self, inputs):\n",
|
| 142 |
+
" # Extract the image patches from the input image.\n",
|
| 143 |
+
" patches = tf.image.extract_patches(\n",
|
| 144 |
+
" inputs=inputs,\n",
|
| 145 |
+
" size=(7, 7, 3),\n",
|
| 146 |
+
" strides=(2, 2, 1),\n",
|
| 147 |
+
" padding=\"SAME\",\n",
|
| 148 |
+
" )\n",
|
| 149 |
+
"\n",
|
| 150 |
+
" # Convert the image patches into vector representations.\n",
|
| 151 |
+
" embedded_patches = self.embedding(patches)\n",
|
| 152 |
+
"\n",
|
| 153 |
+
" # Encode the image patches using the transformer encoder.\n",
|
| 154 |
+
" encoded_patches = self.transformer_encoder(embedded_patches)\n",
|
| 155 |
+
"\n",
|
| 156 |
+
" # Classify the image using the classification layer.\n",
|
| 157 |
+
" predictions = self.classification(encoded_patches)\n",
|
| 158 |
+
"\n",
|
| 159 |
+
" return predictions\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"# Load the model weights from a checkpoint.\n",
|
| 162 |
+
"model = VisionTransformer(num_classes=100)\n",
|
| 163 |
+
"model.load_weights(\"checkpoints/model.ckpt\")\n",
|
| 164 |
+
"\n",
|
| 165 |
+
"# Create a video capture object.\n",
|
| 166 |
+
"cap = cv2.VideoCapture(0)\n",
|
| 167 |
+
"\n",
|
| 168 |
+
"# Start a loop to capture frames from the camera and classify them.\n",
|
| 169 |
+
"while True:\n",
|
| 170 |
+
"\n",
|
| 171 |
+
" # Capture a frame from the camera.\n",
|
| 172 |
+
" ret, frame = cap.read()\n",
|
| 173 |
+
"\n",
|
| 174 |
+
" # Convert the frame to a NumPy array.\n",
|
| 175 |
+
" frame = np.array(frame)\n",
|
| 176 |
+
"\n",
|
| 177 |
+
" # Classify the frame.\n",
|
| 178 |
+
" predictions = model.predict(frame)\n",
|
| 179 |
+
"\n",
|
| 180 |
+
" # Display the classification results.\n",
|
| 181 |
+
" cv2.putText(frame, \"Prediction: {}\".format(predictions[0]), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)\n",
|
| 182 |
+
"\n",
|
| 183 |
+
" # Display the frame.\n",
|
| 184 |
+
" cv2.imshow(\"Frame\", frame)\n",
|
| 185 |
+
"\n",
|
| 186 |
+
" # Wait for a key press.\n",
|
| 187 |
+
" key = cv2.waitKey(1) & 0xFF\n",
|
| 188 |
+
"\n",
|
| 189 |
+
" # If the key `q` is pressed, break out of the loop.\n",
|
| 190 |
+
" if key == ord(\"q\"):\n",
|
| 191 |
+
" break\n",
|
| 192 |
+
"\n",
|
| 193 |
+
"# Release the video capture object.\n",
|
| 194 |
+
"cap.release()\n",
|
| 195 |
+
"\n",
|
| 196 |
+
"# Close all open windows.\n",
|
| 197 |
+
"cv2.destroyAllWindows()"
|
| 198 |
+
]
|
| 199 |
+
}
|
| 200 |
+
]
|
| 201 |
+
}
|