|
259 | 259 | } |
260 | 260 | ] |
261 | 261 | }, |
| 262 | + { |
| 263 | + "cell_type": "markdown", |
| 264 | + "source": [ |
| 265 | + "We defined the same architecture using both the Sequential and Functional APIs, which correspond to the symbolic or declarative way of implementing networks, and also a third tiem using an imperative approach. <br> To make clear that, in the end, the three networks are the same, no matter which approach we took, we trained and evaluated them on the ffamous MNIST dataset, obtaining a decent 98% accuracy on the test set." |
| 266 | + ], |
| 267 | + "metadata": { |
| 268 | + "id": "5X27c4Q6gIE6" |
| 269 | + } |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "markdown", |
| 273 | + "source": [ |
| 274 | + "If you are runnning this locally, for the next section of this notebook, you should need to instal `pillow`" |
| 275 | + ], |
| 276 | + "metadata": { |
| 277 | + "id": "yHu72oYzlLfb" |
| 278 | + } |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "source": [ |
| 283 | + "# pip install pillow" |
| 284 | + ], |
| 285 | + "metadata": { |
| 286 | + "colab": { |
| 287 | + "base_uri": "https://localhost:8080/" |
| 288 | + }, |
| 289 | + "id": "E24krM9ik9mf", |
| 290 | + "outputId": "84f1ee4a-5ea9-44a7-d642-49678f7586e1" |
| 291 | + }, |
| 292 | + "execution_count": 11, |
| 293 | + "outputs": [ |
| 294 | + { |
| 295 | + "output_type": "stream", |
| 296 | + "name": "stdout", |
| 297 | + "text": [ |
| 298 | + "Requirement already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (9.4.0)\n" |
| 299 | + ] |
| 300 | + } |
| 301 | + ] |
| 302 | + }, |
| 303 | + { |
| 304 | + "cell_type": "markdown", |
| 305 | + "source": [ |
| 306 | + "### Loading images using the Keras API" |
| 307 | + ], |
| 308 | + "metadata": { |
| 309 | + "id": "iq1Rp1XZlEYT" |
| 310 | + } |
| 311 | + }, |
| 312 | + { |
| 313 | + "cell_type": "code", |
| 314 | + "source": [ |
| 315 | + "# Import the necessary packages\n", |
| 316 | + "\n", |
| 317 | + "import glob\n", |
| 318 | + "import os\n", |
| 319 | + "import tarfile\n", |
| 320 | + "import matplotlib.pyplot as plt\n", |
| 321 | + "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", |
| 322 | + "from tensorflow.keras.preprocessing.image import load_img, img_to_array\n", |
| 323 | + "from tensorflow.keras.utils import get_file" |
| 324 | + ], |
| 325 | + "metadata": { |
| 326 | + "id": "bVQnDzb4lhRJ" |
| 327 | + }, |
| 328 | + "execution_count": 12, |
| 329 | + "outputs": [] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "code", |
| 333 | + "source": [ |
| 334 | + "# Define the URL and destination of the CINIC-10 dataset, an alternative to the famous CIFAR-10 dataset\n", |
| 335 | + "\n", |
| 336 | + "DATASET_URL = 'https://datashare.is.ed.ac.uk/bitstream/handle/10283/3192/CINIC-10.tar.gz?sequence=4&isAllowed=y'\n", |
| 337 | + "DATA_NAME = 'cinic10'\n", |
| 338 | + "FILE_EXTENSION = 'tar.gz'\n", |
| 339 | + "FILE_NAME = '.'.join([DATA_NAME, FILE_EXTENSION])" |
| 340 | + ], |
| 341 | + "metadata": { |
| 342 | + "id": "RZ4DwQfAmtGW" |
| 343 | + }, |
| 344 | + "execution_count": 13, |
| 345 | + "outputs": [] |
| 346 | + }, |
| 347 | + { |
| 348 | + "cell_type": "code", |
| 349 | + "source": [ |
| 350 | + "# Download and decompress the data. By default, it will be stored in ~/.keras/datasets/<FILE_NAME>\n", |
| 351 | + "\n", |
| 352 | + "downloaded_file_location = get_file(origin=DATASET_URL, fname=FILE_NAME, extract=False)\n" |
| 353 | + ], |
| 354 | + "metadata": { |
| 355 | + "colab": { |
| 356 | + "base_uri": "https://localhost:8080/" |
| 357 | + }, |
| 358 | + "id": "7_rOUoZOn2A7", |
| 359 | + "outputId": "958be2e5-4884-4924-8834-e63899467e47" |
| 360 | + }, |
| 361 | + "execution_count": 14, |
| 362 | + "outputs": [ |
| 363 | + { |
| 364 | + "output_type": "stream", |
| 365 | + "name": "stdout", |
| 366 | + "text": [ |
| 367 | + "Downloading data from https://datashare.is.ed.ac.uk/bitstream/handle/10283/3192/CINIC-10.tar.gz?sequence=4&isAllowed=y\n", |
| 368 | + "687544992/687544992 [==============================] - 518s 1us/step\n" |
| 369 | + ] |
| 370 | + } |
| 371 | + ] |
| 372 | + }, |
| 373 | + { |
| 374 | + "cell_type": "code", |
| 375 | + "source": [ |
| 376 | + "# Build the path to the data directory based on the location of the downloaded file\n", |
| 377 | + "data_directory, _ = downloaded_file_location.rsplit(os.path.sep, maxsplit=1)\n", |
| 378 | + "data_directory = os.path.sep.join([data_directory, DATA_NAME])" |
| 379 | + ], |
| 380 | + "metadata": { |
| 381 | + "id": "39rcGo7UqQ7q" |
| 382 | + }, |
| 383 | + "execution_count": 15, |
| 384 | + "outputs": [] |
| 385 | + }, |
| 386 | + { |
| 387 | + "cell_type": "code", |
| 388 | + "source": [ |
| 389 | + "# Only extract the data if it hasn´t been extracted yet.\n", |
| 390 | + "if not os.path.exists(data_directory) :\n", |
| 391 | + " tar = tarfile.open(downloaded_file_location)\n", |
| 392 | + " tar.extractall(data_directory)" |
| 393 | + ], |
| 394 | + "metadata": { |
| 395 | + "id": "yuad3_COtRLW" |
| 396 | + }, |
| 397 | + "execution_count": 16, |
| 398 | + "outputs": [] |
| 399 | + }, |
| 400 | + { |
| 401 | + "cell_type": "code", |
| 402 | + "source": [ |
| 403 | + "# Load all the image paths and print the number of images\n", |
| 404 | + "data_pattern = os.path.sep.join([data_directory, '*/*/*.png'])\n", |
| 405 | + "image_paths = list(glob.glob(data_pattern))\n", |
| 406 | + "print(f'There are {len(image_paths):,} images in the dataset')" |
| 407 | + ], |
| 408 | + "metadata": { |
| 409 | + "colab": { |
| 410 | + "base_uri": "https://localhost:8080/" |
| 411 | + }, |
| 412 | + "id": "9K5XBaYKt7NF", |
| 413 | + "outputId": "54c06705-42e9-4f43-afef-a738c9504346" |
| 414 | + }, |
| 415 | + "execution_count": 17, |
| 416 | + "outputs": [ |
| 417 | + { |
| 418 | + "output_type": "stream", |
| 419 | + "name": "stdout", |
| 420 | + "text": [ |
| 421 | + "There are 270,000 images in the dataset\n" |
| 422 | + ] |
| 423 | + } |
| 424 | + ] |
| 425 | + }, |
| 426 | + { |
| 427 | + "cell_type": "code", |
| 428 | + "source": [ |
| 429 | + "# lOAD A SINGLE IMAGE FROM THE DATA SET AND PRINT ITS METADATA\n", |
| 430 | + "\n", |
| 431 | + "sample_image = load_img(image_paths[0])\n", |
| 432 | + "print(f'Image type: {type(sample_image)}')\n", |
| 433 | + "print(f'Image format: {sample_image.format}')\n", |
| 434 | + "print(f'Image mode: {sample_image.mode}')\n", |
| 435 | + "print(f'Image size: {sample_image.size}')" |
| 436 | + ], |
| 437 | + "metadata": { |
| 438 | + "colab": { |
| 439 | + "base_uri": "https://localhost:8080/" |
| 440 | + }, |
| 441 | + "id": "VTogeqSK3H4C", |
| 442 | + "outputId": "746700fb-73c1-4671-c31c-fdcbb49ee80f" |
| 443 | + }, |
| 444 | + "execution_count": 19, |
| 445 | + "outputs": [ |
| 446 | + { |
| 447 | + "output_type": "stream", |
| 448 | + "name": "stdout", |
| 449 | + "text": [ |
| 450 | + "Image type: <class 'PIL.PngImagePlugin.PngImageFile'>\n", |
| 451 | + "Image format: PNG\n", |
| 452 | + "Image mode: RGB\n", |
| 453 | + "Image size: (32, 32)\n" |
| 454 | + ] |
| 455 | + } |
| 456 | + ] |
| 457 | + }, |
262 | 458 | { |
263 | 459 | "cell_type": "code", |
264 | 460 | "source": [], |
265 | 461 | "metadata": { |
266 | | - "id": "5X27c4Q6gIE6" |
| 462 | + "id": "f55iZIpG8ZGP" |
267 | 463 | }, |
268 | 464 | "execution_count": null, |
269 | 465 | "outputs": [] |
|
0 commit comments