{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":30756,"sourceType":"datasetVersion","datasetId":7467}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Intro\n\nYou don't directly choose the numbers to go into your convolutions for deep learning... instead the deep learning technique determines what convolutions will be useful from the data (as part of model-training). We'll come back to how the model does that soon.\n\n![Imgur](https://i.imgur.com/op9Maqr.png)\n\nBut looking closely at convolutions and how they are applied to your image will improve your intuition for these models, how they work, and how to debug them when they don't work.\n\n**Let's get started.**\n\n# Exercises\nWe'll use some small utilty functions to visualize raw images and some results of your code. Execute the next cell to load the utility functions.","metadata":{}},{"cell_type":"code","source":"# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.deep_learning.exercise_1 import *\nprint(\"Setup Complete\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T16:33:45.568562Z","iopub.execute_input":"2025-07-14T16:33:45.568922Z","iopub.status.idle":"2025-07-14T16:33:45.573962Z","shell.execute_reply.started":"2025-07-14T16:33:45.568879Z","shell.execute_reply":"2025-07-14T16:33:45.573135Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Exercise 1\n\nIn the video, you saw a convolution that detected horizontal lines. That convolution shows up again in the code cell below.\n\nRun the cell to see a raw image as well as the output from applying this convolution to the image.\n","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Fix for Image.ANTIALIAS deprecated in Pillow 10.0.0+\ntry:\n    resample = Image.Resampling.LANCZOS\nexcept AttributeError:\n    resample = Image.ANTIALIAS\n\n# Utility function to load and preprocess image\ndef load_my_image(fname='../input/dog-breed-identification/train/0246f44bb123ce3f91c939861eb97fb7.jpg'):\n    '''Returns array containing grayscale values for the supplied file (resized thumbnail).'''\n    image_color = Image.open(fname).resize((135, 188), resample)\n    image_grayscale = image_color.convert('L')  # convert to grayscale\n    image_array = np.asarray(image_grayscale)\n    return image_array\n\n# Example convolution filter (horizontal edge detection)\nhorizontal_line_conv = [[1, 1], \n                        [-1, -1]]\n\n# Simple convolution visualization function\ndef visualize_conv(image_array, conv_filter):\n    conv_filter = np.array(conv_filter)\n    output_shape = (\n        image_array.shape[0] - conv_filter.shape[0] + 1,\n        image_array.shape[1] - conv_filter.shape[1] + 1\n    )\n    output = np.zeros(output_shape)\n\n    for i in range(output_shape[0]):\n        for j in range(output_shape[1]):\n            region = image_array[i:i+conv_filter.shape[0], j:j+conv_filter.shape[1]]\n            output[i, j] = np.sum(region * conv_filter)\n\n    plt.figure(figsize=(12, 5))\n    plt.subplot(1, 2, 1)\n    plt.title(\"Original Image (Grayscale)\")\n    plt.imshow(image_array, cmap='gray')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.title(\"After Convolution\")\n    plt.imshow(output, cmap='gray')\n    plt.axis('off')\n    plt.show()\n\n# Load image and apply convolution\noriginal_image = load_my_image()\nvisualize_conv(original_image, horizontal_line_conv)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T16:41:08.765960Z","iopub.execute_input":"2025-07-14T16:41:08.766242Z","iopub.status.idle":"2025-07-14T16:41:09.382278Z","shell.execute_reply.started":"2025-07-14T16:41:08.766219Z","shell.execute_reply":"2025-07-14T16:41:09.381407Z"}},"outputs":[],"execution_count":null}]}