Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"800px\" src=\"../fidle/img/header.svg\"></img>\n",
"\n",
"# <!-- TITLE --> [K3GTSRB4] - Hight level example (Keras-cv)\n",
"<!-- DESC --> An example of using a pre-trained model with Keras-cv\n",
"<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
"\n",
"## Objectives :\n",
" - Using a pre-trained model\n",
" \n",
"## What we're going to do :\n",
"\n",
" - Load and use a pre-trained model\n",
"\n",
" See : https://keras.io/guides/keras_cv/classification_with_keras_cv/ \n",
" Imagenet classes can be found at : https://gist.githubusercontent.com/LukeWood/62eebcd5c5c4a4d0e0b7845780f76d55/raw/fde63e5e4c09e2fa0a3436680f436bdcb8325aac/ImagenetClassnames.json\n",
"\n",
"## Step 1 - Import and init\n",
"\n",
"**ATTENTION :** A specific environment is required for this example (Which may require 6 GB). \n",
"This python environment required for this notebook is :\n",
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
"```\n",
"python3 -m venv fidle-kcv\n",
"pip install --upgrade keras-cv tensorflow torch torchvision torchaudio Matplotlib Jupyterlab\n",
"pip install --upgrade keras jupyterlab\n",
"```\n",
"Note: Tensorflow is not used for interference, and will no longer be required in later versions of Keras 3.\n",
"\n",
"### 1.1 - Python stuffs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"KERAS_BACKEND\"] = \"torch\" # @param [\"tensorflow\", \"jax\", \"torch\"]\n",
"\n",
"import json\n",
"import numpy as np\n",
"\n",
"import keras\n",
"import keras_cv\n",
"\n",
"from modules.ImagenetClassnames import ImagenetClassnames"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2 - Get some images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"images_url=['https://i.imgur.com/2eOWImx.jpeg', 'https://i.imgur.com/YB8sG8R.jpeg', 'https://i.imgur.com/orZEMlv.jpeg']\n",
"\n",
"images=[]\n",
"for img_url in images_url:\n",
" \n",
" # Get images from urls in ~/.keras cache\n",
" img_path = keras.utils.get_file(origin=img_url)\n",
"\n",
" # Get image\n",
" img = keras.utils.load_img(img_path, target_size=(256,256))\n",
" images.append(img)\n",
"images=np.array(images)\n",
"\n",
"keras_cv.visualization.plot_image_gallery( images, rows=1, cols=3, value_range=(0, 255), show=True, scale=2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3 - Get a nice pretrained classifier (and classes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classifier = keras_cv.models.ImageClassifier.from_preset( \"efficientnetv2_b0_imagenet_classifier\" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4 - Try some predictions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictions = classifier.predict(images)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5 - Show result"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get classes name \n",
"imc = ImagenetClassnames()\n",
"\n",
"for i,img in enumerate(images):\n",
" # Get classes id instead classes probabilities\n",
" classes_id = predictions[i].argsort(axis=-1)\n",
" # Get classes name instead classes id\n",
" classes_name = imc.get(classes_id, top_n=2)\n",
" # Plot it\n",
" keras_cv.visualization.plot_image_gallery( np.array([img]), rows=1, cols=1, value_range=(0, 255), show=True, scale=2)\n",
" print(classes_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"<img width=\"80px\" src=\"../fidle/img/logo-paysage.svg\"></img>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "fidle-kcv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}