haaiderrr commited on
Commit
f213f0c
β€’
1 Parent(s): 8d7073b

Upload Deep Learning Workshop 3.ipynb

Browse files
Files changed (1) hide show
  1. Deep Learning Workshop 3.ipynb +597 -0
Deep Learning Workshop 3.ipynb ADDED
@@ -0,0 +1,597 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 15,
6
+ "id": "02edd069-0381-4537-902e-03ffd273349c",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stderr",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:99: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
14
+ " super().__init__(\n",
15
+ "WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n",
16
+ "WARNING:absl:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.\n"
17
+ ]
18
+ }
19
+ ],
20
+ "source": [
21
+ "from keras.models import load_model\n",
22
+ "import numpy as np\n",
23
+ "\n",
24
+ "# Load the saved model\n",
25
+ "model = load_model('model.h5')\n",
26
+ "\n"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 33,
32
+ "id": "78c9d169-adbe-4588-9a78-fb02b90e3781",
33
+ "metadata": {},
34
+ "outputs": [],
35
+ "source": [
36
+ "from torchvision import transforms"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": 16,
42
+ "id": "f57b1e4e-c171-4233-addf-a5bbdd91896f",
43
+ "metadata": {},
44
+ "outputs": [
45
+ {
46
+ "ename": "NameError",
47
+ "evalue": "name 'input_image' is not defined",
48
+ "output_type": "error",
49
+ "traceback": [
50
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
51
+ "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
52
+ "Cell \u001b[1;32mIn[16], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Perform inference on the input image\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Make sure your input shape matches the input shape of the model\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m predicted_image \u001b[38;5;241m=\u001b[39m model\u001b[38;5;241m.\u001b[39mpredict(np\u001b[38;5;241m.\u001b[39mexpand_dims(\u001b[43minput_image\u001b[49m, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m))\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# The output 'predicted_image' will be the deblurred image generated by the model\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# You can further process or save the output image as needed\u001b[39;00m\n",
53
+ "\u001b[1;31mNameError\u001b[0m: name 'input_image' is not defined"
54
+ ]
55
+ }
56
+ ],
57
+ "source": [
58
+ "# Perform inference on the input image\n",
59
+ "# Make sure your input shape matches the input shape of the model\n",
60
+ "predicted_image = model.predict(np.expand_dims(input_image, axis=0))\n",
61
+ "\n",
62
+ "# The output 'predicted_image' will be the deblurred image generated by the model\n",
63
+ "# You can further process or save the output image as needed\n"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 4,
69
+ "id": "87814748-7c0b-41e2-998b-d3a3eb6d7bbd",
70
+ "metadata": {},
71
+ "outputs": [
72
+ {
73
+ "data": {
74
+ "text/plain": [
75
+ "'2.16.1'"
76
+ ]
77
+ },
78
+ "execution_count": 4,
79
+ "metadata": {},
80
+ "output_type": "execute_result"
81
+ }
82
+ ],
83
+ "source": [
84
+ "import tensorflow as tf\n",
85
+ "tf.__version__"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 5,
91
+ "id": "9fafecb4-54e9-43a5-ac19-143205069848",
92
+ "metadata": {},
93
+ "outputs": [],
94
+ "source": [
95
+ "import tensorflow as tf\n",
96
+ "from tensorflow.keras.datasets import cifar10\n",
97
+ "from tensorflow.keras.models import Sequential\n",
98
+ "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n",
99
+ "from tensorflow.keras.utils import to_categorical\n",
100
+ "from tensorflow.keras.optimizers import Adam\n",
101
+ "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
102
+ "\n",
103
+ "# Load CIFAR-10 dataset\n",
104
+ "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
105
+ "\n",
106
+ "# Normalize pixel values to be between 0 and 1\n",
107
+ "x_train = x_train.astype('float32') / 255.0\n",
108
+ "x_test = x_test.astype('float32') / 255.0\n",
109
+ "\n",
110
+ "# One-hot encode the labels\n",
111
+ "y_train = to_categorical(y_train, num_classes=10)\n",
112
+ "y_test = to_categorical(y_test, num_classes=10)"
113
+ ]
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "execution_count": 47,
118
+ "id": "b8e744fc-d509-49a9-a1d1-9be7f37a6c21",
119
+ "metadata": {},
120
+ "outputs": [
121
+ {
122
+ "data": {
123
+ "text/plain": [
124
+ "array([[0., 0., 0., ..., 0., 0., 0.],\n",
125
+ " [0., 0., 0., ..., 0., 0., 1.],\n",
126
+ " [0., 0., 0., ..., 0., 0., 1.],\n",
127
+ " ...,\n",
128
+ " [0., 0., 0., ..., 0., 0., 1.],\n",
129
+ " [0., 1., 0., ..., 0., 0., 0.],\n",
130
+ " [0., 1., 0., ..., 0., 0., 0.]])"
131
+ ]
132
+ },
133
+ "execution_count": 47,
134
+ "metadata": {},
135
+ "output_type": "execute_result"
136
+ }
137
+ ],
138
+ "source": [
139
+ "y_train"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": 7,
145
+ "id": "0ae22ae2-c40d-4a4a-9c25-00d2c423b53c",
146
+ "metadata": {},
147
+ "outputs": [
148
+ {
149
+ "name": "stdout",
150
+ "output_type": "stream",
151
+ "text": [
152
+ "\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m23s\u001b[0m 72ms/step\n"
153
+ ]
154
+ }
155
+ ],
156
+ "source": [
157
+ "predicted = model.predict(x_test)"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": 9,
163
+ "id": "e80370e5-6753-4b04-afde-ddcfcdb3c148",
164
+ "metadata": {},
165
+ "outputs": [
166
+ {
167
+ "data": {
168
+ "text/plain": [
169
+ "(10000, 32, 32, 3)"
170
+ ]
171
+ },
172
+ "execution_count": 9,
173
+ "metadata": {},
174
+ "output_type": "execute_result"
175
+ }
176
+ ],
177
+ "source": [
178
+ "x_test.shape"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": 13,
184
+ "id": "9bd01caf-8ce5-465a-b734-a5480b96521d",
185
+ "metadata": {},
186
+ "outputs": [
187
+ {
188
+ "data": {
189
+ "text/plain": [
190
+ "array([3, 8, 8, ..., 5, 1, 7], dtype=int64)"
191
+ ]
192
+ },
193
+ "execution_count": 13,
194
+ "metadata": {},
195
+ "output_type": "execute_result"
196
+ }
197
+ ],
198
+ "source": [
199
+ "np.argmax(predicted, axis = 1)"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": null,
205
+ "id": "070f8ef8-6522-4fe7-9d35-67f1861de531",
206
+ "metadata": {},
207
+ "outputs": [],
208
+ "source": [
209
+ "\n",
210
+ "\n",
211
+ "# Define AlexNet architecture\n",
212
+ "model = Sequential([\n",
213
+ " # First convolutional layer\n",
214
+ " Conv2D(96, (11, 11), strides=(1, 1), activation='relu', input_shape=(32, 32, 3)),\n",
215
+ " MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),\n",
216
+ " # Second convolutional layer\n",
217
+ " Conv2D(256, (5, 5), padding='same', activation='relu'),\n",
218
+ " MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),\n",
219
+ " # Third convolutional layer\n",
220
+ " Conv2D(384, (3, 3), padding='same', activation='relu'),\n",
221
+ " # Fourth convolutional layer\n",
222
+ " Conv2D(384, (3, 3), padding='same', activation='relu'),\n",
223
+ " # Fifth convolutional layer\n",
224
+ " Conv2D(256, (3, 3), padding='same', activation='relu'),\n",
225
+ " MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),\n",
226
+ " # Flatten the convolutional layers output for fully connected layers\n",
227
+ " Flatten(),\n",
228
+ " # First fully connected layer\n",
229
+ " Dense(4096, activation='relu'),\n",
230
+ " Dropout(0.5),\n",
231
+ " # Second fully connected layer\n",
232
+ " Dense(4096, activation='relu'),\n",
233
+ " Dropout(0.5),\n",
234
+ " # Output layer\n",
235
+ " Dense(10, activation='softmax')\n",
236
+ "])\n",
237
+ "\n",
238
+ "# Compile the model with a lower learning rate\n",
239
+ "optimizer = Adam(learning_rate=0.0001)\n",
240
+ "model.compile(optimizer=optimizer,\n",
241
+ " loss='categorical_crossentropy',\n",
242
+ " metrics=['accuracy'])\n",
243
+ "\n",
244
+ "# Data augmentation\n",
245
+ "datagen = ImageDataGenerator(\n",
246
+ " rotation_range=15,\n",
247
+ " width_shift_range=0.1,\n",
248
+ " height_shift_range=0.1,\n",
249
+ " horizontal_flip=True,\n",
250
+ ")\n",
251
+ "\n",
252
+ "datagen.fit(x_train)\n",
253
+ "\n",
254
+ "# Train the model with data augmentation\n",
255
+ "model.fit(datagen.flow(x_train, y_train, batch_size=128), epochs=25, validation_data=(x_test, y_test))\n",
256
+ "\n",
257
+ "# Evaluate the model on the test set\n",
258
+ "test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=2)\n",
259
+ "\n",
260
+ "print(\"\\nTest Accuracy:\", test_accuracy)\n",
261
+ "print(\"Test Loss:\", test_loss)"
262
+ ]
263
+ },
264
+ {
265
+ "cell_type": "code",
266
+ "execution_count": 23,
267
+ "id": "82b6c768-2b9d-4633-8e17-96d26d814421",
268
+ "metadata": {},
269
+ "outputs": [],
270
+ "source": [
271
+ "from PIL import Image\n",
272
+ "import numpy as np"
273
+ ]
274
+ },
275
+ {
276
+ "cell_type": "code",
277
+ "execution_count": 40,
278
+ "id": "692a13a1-3483-4dd9-9364-c27e909b89d6",
279
+ "metadata": {},
280
+ "outputs": [
281
+ {
282
+ "name": "stdout",
283
+ "output_type": "stream",
284
+ "text": [
285
+ "Running on local URL: http://127.0.0.1:7896\n",
286
+ "\n",
287
+ "To create a public link, set `share=True` in `launch()`.\n"
288
+ ]
289
+ },
290
+ {
291
+ "data": {
292
+ "text/html": [
293
+ "<div><iframe src=\"http://127.0.0.1:7896/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
294
+ ],
295
+ "text/plain": [
296
+ "<IPython.core.display.HTML object>"
297
+ ]
298
+ },
299
+ "metadata": {},
300
+ "output_type": "display_data"
301
+ },
302
+ {
303
+ "data": {
304
+ "text/plain": []
305
+ },
306
+ "execution_count": 40,
307
+ "metadata": {},
308
+ "output_type": "execute_result"
309
+ },
310
+ {
311
+ "name": "stdout",
312
+ "output_type": "stream",
313
+ "text": [
314
+ "(1280, 717, 3)\n",
315
+ "(1, 32, 32, 3)\n"
316
+ ]
317
+ },
318
+ {
319
+ "name": "stderr",
320
+ "output_type": "stream",
321
+ "text": [
322
+ "Traceback (most recent call last):\n",
323
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\gradio\\queueing.py\", line 501, in call_prediction\n",
324
+ " output = await route_utils.call_process_api(\n",
325
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
326
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\gradio\\route_utils.py\", line 253, in call_process_api\n",
327
+ " output = await app.get_blocks().process_api(\n",
328
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
329
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\gradio\\blocks.py\", line 1695, in process_api\n",
330
+ " result = await self.call_function(\n",
331
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
332
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\gradio\\blocks.py\", line 1235, in call_function\n",
333
+ " prediction = await anyio.to_thread.run_sync(\n",
334
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
335
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
336
+ " return await get_asynclib().run_sync_in_worker_thread(\n",
337
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
338
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
339
+ " return await future\n",
340
+ " ^^^^^^^^^^^^\n",
341
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
342
+ " result = context.run(func, *args)\n",
343
+ " ^^^^^^^^^^^^^^^^^^^^^^^^\n",
344
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\gradio\\utils.py\", line 692, in wrapper\n",
345
+ " response = f(*args, **kwargs)\n",
346
+ " ^^^^^^^^^^^^^^^^^^\n",
347
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Temp\\ipykernel_29808\\1451871443.py\", line 16, in prediction\n",
348
+ " output = model.predict(transformed_image)\n",
349
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
350
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\keras\\src\\utils\\traceback_utils.py\", line 122, in error_handler\n",
351
+ " raise e.with_traceback(filtered_tb) from None\n",
352
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\keras\\src\\models\\functional.py\", line 280, in _adjust_input_rank\n",
353
+ " raise ValueError(\n",
354
+ "ValueError: Exception encountered when calling Sequential.call().\n",
355
+ "\n",
356
+ "\u001b[1mInvalid input shape for input Tensor(\"data:0\", shape=(32, 32, 3), dtype=float32). Expected shape (None, 32, 32, 3), but input has incompatible shape (32, 32, 3)\u001b[0m\n",
357
+ "\n",
358
+ "Arguments received by Sequential.call():\n",
359
+ " β€’ inputs=tf.Tensor(shape=(32, 32, 3), dtype=float32)\n",
360
+ " β€’ training=False\n",
361
+ " β€’ mask=None\n",
362
+ "Exception in thread Thread-81 (_do_normal_analytics_request):\n",
363
+ "Traceback (most recent call last):\n",
364
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_exceptions.py\", line 10, in map_exceptions\n",
365
+ " yield\n",
366
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_backends\\sync.py\", line 168, in start_tls\n",
367
+ " raise exc\n",
368
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_backends\\sync.py\", line 163, in start_tls\n",
369
+ " sock = ssl_context.wrap_socket(\n",
370
+ " ^^^^^^^^^^^^^^^^^^^^^^^^\n",
371
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\ssl.py\", line 455, in wrap_socket\n",
372
+ " return self.sslsocket_class._create(\n",
373
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
374
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\ssl.py\", line 1046, in _create\n",
375
+ " self.do_handshake()\n",
376
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\ssl.py\", line 1317, in do_handshake\n",
377
+ " self._sslobj.do_handshake()\n",
378
+ "TimeoutError: _ssl.c:983: The handshake operation timed out\n",
379
+ "\n",
380
+ "The above exception was the direct cause of the following exception:\n",
381
+ "\n",
382
+ "Traceback (most recent call last):\n",
383
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_transports\\default.py\", line 69, in map_httpcore_exceptions\n",
384
+ " yield\n",
385
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_transports\\default.py\", line 233, in handle_request\n",
386
+ " resp = self._pool.handle_request(req)\n",
387
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
388
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_sync\\connection_pool.py\", line 268, in handle_request\n",
389
+ " raise exc\n",
390
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_sync\\connection_pool.py\", line 251, in handle_request\n",
391
+ " response = connection.handle_request(request)\n",
392
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
393
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_sync\\connection.py\", line 99, in handle_request\n",
394
+ " raise exc\n",
395
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_sync\\connection.py\", line 76, in handle_request\n",
396
+ " stream = self._connect(request)\n",
397
+ " ^^^^^^^^^^^^^^^^^^^^^^\n",
398
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_sync\\connection.py\", line 156, in _connect\n",
399
+ " stream = stream.start_tls(**kwargs)\n",
400
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
401
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_backends\\sync.py\", line 152, in start_tls\n",
402
+ " with map_exceptions(exc_map):\n",
403
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\contextlib.py\", line 155, in __exit__\n",
404
+ " self.gen.throw(value)\n",
405
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpcore\\_exceptions.py\", line 14, in map_exceptions\n",
406
+ " raise to_exc(exc) from exc\n",
407
+ "httpcore.ConnectTimeout: _ssl.c:983: The handshake operation timed out\n",
408
+ "\n",
409
+ "The above exception was the direct cause of the following exception:\n",
410
+ "\n",
411
+ "Traceback (most recent call last):\n",
412
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\threading.py\", line 1052, in _bootstrap_inner\n",
413
+ " self.run()\n",
414
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\threading.py\", line 989, in run\n",
415
+ " self._target(*self._args, **self._kwargs)\n",
416
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\gradio\\analytics.py\", line 63, in _do_normal_analytics_request\n",
417
+ " httpx.post(url, data=data, timeout=5)\n",
418
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_api.py\", line 319, in post\n",
419
+ " return request(\n",
420
+ " ^^^^^^^^\n",
421
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_api.py\", line 106, in request\n",
422
+ " return client.request(\n",
423
+ " ^^^^^^^^^^^^^^^\n",
424
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_client.py\", line 827, in request\n",
425
+ " return self.send(request, auth=auth, follow_redirects=follow_redirects)\n",
426
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
427
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_client.py\", line 914, in send\n",
428
+ " response = self._send_handling_auth(\n",
429
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
430
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_client.py\", line 942, in _send_handling_auth\n",
431
+ " response = self._send_handling_redirects(\n",
432
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
433
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_client.py\", line 979, in _send_handling_redirects\n",
434
+ " response = self._send_single_request(request)\n",
435
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
436
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_client.py\", line 1015, in _send_single_request\n",
437
+ " response = transport.handle_request(request)\n",
438
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
439
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_transports\\default.py\", line 232, in handle_request\n",
440
+ " with map_httpcore_exceptions():\n",
441
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\contextlib.py\", line 155, in __exit__\n",
442
+ " self.gen.throw(value)\n",
443
+ " File \"C:\\Users\\Haider Ali\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\httpx\\_transports\\default.py\", line 86, in map_httpcore_exceptions\n",
444
+ " raise mapped_exc(message) from exc\n",
445
+ "httpx.ConnectTimeout: _ssl.c:983: The handshake operation timed out\n"
446
+ ]
447
+ }
448
+ ],
449
+ "source": [
450
+ "def prediction(input_img):\n",
451
+ " # image = Image.open(\"img1.jpg\")\n",
452
+ " print(input_img.shape)\n",
453
+ " # Define the transformation\n",
454
+ " transform = transforms.Compose([\n",
455
+ " transforms.Resize(32),\n",
456
+ " transforms.CenterCrop(32),\n",
457
+ " transforms.ToTensor(),\n",
458
+ " # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n",
459
+ " ])\n",
460
+ " pil_image = Image.fromarray(input_img.astype('uint8'))\n",
461
+ " # Apply the transformation\n",
462
+ " transformed_image = np.array(transform(pil_image).T)\n",
463
+ " input_image = np.expand_dims(transformed_image, axis=0)\n",
464
+ " print(input_image.shape)\n",
465
+ " output = model.predict(transformed_image)\n",
466
+ " print(output)\n",
467
+ " # print(transformed_image.shape)\n",
468
+ " # print(transformed_image)\n",
469
+ " # plt.imshow(transformed_image)\n",
470
+ " # plt.show()\n",
471
+ " # return transformed_image\n",
472
+ "demo = gr.Interface(prediction, gr.Image(), \"image\")\n",
473
+ "demo.launch()"
474
+ ]
475
+ },
476
+ {
477
+ "cell_type": "code",
478
+ "execution_count": 49,
479
+ "id": "b2b8aaaf-ce9c-4c25-875e-fce01fbf3832",
480
+ "metadata": {},
481
+ "outputs": [],
482
+ "source": [
483
+ "classes = {\n",
484
+ " 0 : 'Airplane',\n",
485
+ " 1 : 'Automobile',\n",
486
+ " 2 : 'Bird',\n",
487
+ " 3 : 'Cat',\n",
488
+ " 4 : 'Deer',\n",
489
+ " 5 : 'Dog',\n",
490
+ " 6 : 'Frog',\n",
491
+ " 7 : 'Horse',\n",
492
+ " 8 : 'Ship',\n",
493
+ " 9 : 'Truck'\n",
494
+ "}"
495
+ ]
496
+ },
497
+ {
498
+ "cell_type": "code",
499
+ "execution_count": 53,
500
+ "id": "d3da2b58-6b86-4e6d-9b7f-9f8ce1fe2339",
501
+ "metadata": {},
502
+ "outputs": [
503
+ {
504
+ "name": "stdout",
505
+ "output_type": "stream",
506
+ "text": [
507
+ "Running on local URL: http://127.0.0.1:7904\n",
508
+ "\n",
509
+ "To create a public link, set `share=True` in `launch()`.\n"
510
+ ]
511
+ },
512
+ {
513
+ "data": {
514
+ "text/html": [
515
+ "<div><iframe src=\"http://127.0.0.1:7904/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
516
+ ],
517
+ "text/plain": [
518
+ "<IPython.core.display.HTML object>"
519
+ ]
520
+ },
521
+ "metadata": {},
522
+ "output_type": "display_data"
523
+ },
524
+ {
525
+ "data": {
526
+ "text/plain": []
527
+ },
528
+ "execution_count": 53,
529
+ "metadata": {},
530
+ "output_type": "execute_result"
531
+ },
532
+ {
533
+ "name": "stdout",
534
+ "output_type": "stream",
535
+ "text": [
536
+ "(1600, 1204, 3)\n",
537
+ "(1, 32, 32, 3)\n",
538
+ "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 52ms/step\n",
539
+ "[[0.00640055 0.17760815 0.04763744 0.09621317 0.05900569 0.09116109\n",
540
+ " 0.02236336 0.09745935 0.01388952 0.38826168]]\n"
541
+ ]
542
+ }
543
+ ],
544
+ "source": [
545
+ "def prediction(input_img):\n",
546
+ " # Define the transformation\n",
547
+ " transform = transforms.Compose([\n",
548
+ " transforms.Resize(32),\n",
549
+ " transforms.CenterCrop(32),\n",
550
+ " transforms.ToTensor(),\n",
551
+ " # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n",
552
+ " ])\n",
553
+ " pil_image = Image.fromarray(input_img.astype('uint8'))\n",
554
+ " # Apply the transformation\n",
555
+ " transformed_image = np.array(transform(pil_image).T)\n",
556
+ " input_image = np.expand_dims(transformed_image, axis=0)\n",
557
+ " output = model.predict(input_image)\n",
558
+ " # print(transformed_image.shape)\n",
559
+ " # print(transformed_image)\n",
560
+ " # plt.imshow(transformed_image)\n",
561
+ " # plt.show()\n",
562
+ " return classes[np.argmax(output)]\n",
563
+ "demo = gr.Interface(prediction, gr.Image(), \"text\")\n",
564
+ "demo.launch()"
565
+ ]
566
+ },
567
+ {
568
+ "cell_type": "code",
569
+ "execution_count": null,
570
+ "id": "2070d29e-b593-490e-b867-6a10ab1b02ff",
571
+ "metadata": {},
572
+ "outputs": [],
573
+ "source": []
574
+ }
575
+ ],
576
+ "metadata": {
577
+ "kernelspec": {
578
+ "display_name": "Python 3 (ipykernel)",
579
+ "language": "python",
580
+ "name": "python3"
581
+ },
582
+ "language_info": {
583
+ "codemirror_mode": {
584
+ "name": "ipython",
585
+ "version": 3
586
+ },
587
+ "file_extension": ".py",
588
+ "mimetype": "text/x-python",
589
+ "name": "python",
590
+ "nbconvert_exporter": "python",
591
+ "pygments_lexer": "ipython3",
592
+ "version": "3.12.0"
593
+ }
594
+ },
595
+ "nbformat": 4,
596
+ "nbformat_minor": 5
597
+ }