{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":"**This notebook is an exercise in the [Deep Learning for Computer Vision](https://www.kaggle.com/learn/deep-learning-for-computer-vision) course.  You can reference the tutorial at [this link](https://www.kaggle.com/dansbecker/intro-to-deep-learning-and-computer-vision).**\n\n---\n","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-06-03T03:12:47.626662Z","iopub.execute_input":"2024-06-03T03:12:47.627101Z","iopub.status.idle":"2024-06-03T03:12:48.917441Z","shell.execute_reply.started":"2024-06-03T03:12:47.627062Z","shell.execute_reply":"2024-06-03T03:12:48.916316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"horizontal_line_conv = [[1, 1], \n                        [-1, -1]]\n# load_my_image and visualize_conv are utility functions provided for this exercise\noriginal_image = load_my_image() \nvisualize_conv(original_image, horizontal_line_conv)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:15:24.622680Z","iopub.execute_input":"2024-06-03T03:15:24.623264Z","iopub.status.idle":"2024-06-03T03:15:25.066970Z","shell.execute_reply.started":"2024-06-03T03:15:24.623230Z","shell.execute_reply":"2024-06-03T03:15:25.066076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now it's your turn. Instead of a horizontal line detector, you will create a vertical line detector.\n\n**Replace the underscores with numbers to make a vertical line detector and uncomment both lines of code in the cell below. Then run **","metadata":{}},{"cell_type":"code","source":"# vertical_line_conv = [[1, -1], \n#                     [1, -1]]\n\n# # Check your answer\n# q_1.check()\n# visualize_conv(original_image, vertical_line_conv)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you'd like a hint or the solution, uncomment the appropriate line below.","metadata":{}},{"cell_type":"code","source":"#q_1.hint()\n#q_1.solution()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:22:06.176402Z","iopub.execute_input":"2024-06-03T03:22:06.176792Z","iopub.status.idle":"2024-06-03T03:22:06.182002Z","shell.execute_reply.started":"2024-06-03T03:22:06.176762Z","shell.execute_reply":"2024-06-03T03:22:06.180737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Exercise 2\nThe convolutions you've seen are 2x2.  But you could have larger convolutions. They could be 3x3, 4x4, etc.  They don't even have to be square. Nothing prevents using a 4x7 convolution.\n\nCompare the number of visual patterns that can be captured by small convolutions. Which of the following is true?\n\n- There are more visual patterns that can be captured by large convolutions\n- There are fewer visual patterns that can be captured by large convolutions\n- The number of visual patterns that can be captured by large convolutions is the same as the number of visual patterns that can be captured by small convolutions?\n\nOnce you think you know the answer, check it by running the line below.\n\n---\n(Trans:\nCác phép tích chập bạn đã thấy là 2x2. Nhưng bạn có thể có các phép tích chập lớn hơn. Chúng có thể là 3x3, 4x4, v.v. Chúng thậm chí không cần phải là hình vuông. Không có gì ngăn cản việc sử dụng phép tích chập 4x7.\n\nSo sánh số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập nhỏ. Câu nào sau đây là đúng?\n\n- Có nhiều mẫu hình ảnh hơn có thể được chụp bằng các phép tích chập lớn\n- Có ít mẫu hình ảnh hơn có thể được chụp bằng các phép tích chập lớn\n- Số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập lớn bằng số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập nhỏ?\n\nKhi bạn nghĩ rằng mình đã biết câu trả lời, hãy kiểm tra bằng cách chạy dòng bên dưới.)","metadata":{}},{"cell_type":"code","source":"# thử check với ma trận 2x2\nvertical_line_conv = [[1, 1], \n                    [-1, -1]]\n\n# Check your answer\n#q_1.check()\nimg2 = visualize_conv(original_image, vertical_line_conv)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:35:49.443869Z","iopub.execute_input":"2024-06-03T03:35:49.444305Z","iopub.status.idle":"2024-06-03T03:35:49.825029Z","shell.execute_reply.started":"2024-06-03T03:35:49.444272Z","shell.execute_reply":"2024-06-03T03:35:49.823850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# thử check với ma trận 3x3\nvertical_line_conv = [[1, 1, 1], \n                    [-1, -1, -1]]\n\n# Check your answer\n#q_1.check()\nimg3 = visualize_conv(original_image, vertical_line_conv)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:35:43.678529Z","iopub.execute_input":"2024-06-03T03:35:43.678902Z","iopub.status.idle":"2024-06-03T03:35:44.060836Z","shell.execute_reply.started":"2024-06-03T03:35:43.678872Z","shell.execute_reply":"2024-06-03T03:35:44.059591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# thử check với ma trận 4x4\nvertical_line_conv = [[1, 1, 1, 1], \n                    [-1, -1, -1, -1]]\n\n# Check your answer\n#q_1.check()\nimg4 = visualize_conv(original_image, vertical_line_conv)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:35:37.208191Z","iopub.execute_input":"2024-06-03T03:35:37.209420Z","iopub.status.idle":"2024-06-03T03:35:37.601278Z","shell.execute_reply.started":"2024-06-03T03:35:37.209371Z","shell.execute_reply":"2024-06-03T03:35:37.600090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# thử check với ma trận 5x5\nvertical_line_conv = [[1, 1, 1, 1, 1], \n                    [-1, -1, -1, -1, -1]]\n\n# Check your answer\n#q_1.check()\nimg5 = visualize_conv(original_image, vertical_line_conv)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:36:09.802914Z","iopub.execute_input":"2024-06-03T03:36:09.803351Z","iopub.status.idle":"2024-06-03T03:36:10.184330Z","shell.execute_reply.started":"2024-06-03T03:36:09.803317Z","shell.execute_reply":"2024-06-03T03:36:10.183432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check your answer (Run this code cell to receive credit!)\n# q_2.solution()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T03:47:54.319667Z","iopub.execute_input":"2024-06-03T03:47:54.320128Z","iopub.status.idle":"2024-06-03T03:47:54.324866Z","shell.execute_reply.started":"2024-06-03T03:47:54.320091Z","shell.execute_reply":"2024-06-03T03:47:54.323755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Exercise 2: Ans\n\nĐối với câu hỏi: So sánh số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập nhỏ. Câu nào sau đây là đúng?\n\n- Có nhiều mẫu hình ảnh hơn có thể được chụp bằng các phép tích chập lớn\n- Có ít mẫu hình ảnh hơn có thể được chụp bằng các phép tích chập lớn\n- Số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập lớn bằng số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập nhỏ?\n\n---\nSau khi so sánh các kết quả được tạo ra khi sử dụng các phép tích chập khác nhau (2x2, 3x3, 4x4, 5x5), có thể thấy là các kết quả cho ra số lượng mẫu hình ảnh có thể được chụp bằng các phép tích chập này là như nhau. Tuy nhiên ở các kết quả từ phép tích chập 3x3 trở lên, hình ảnh thu được có phần sáng hơn so với kết quả thu được từ phép tích chập 2x2. Điều này là bởi tích chập 3x3 lọc các pixel sáng với một điểm tối ở giữa, trong khi tích chập 2x2 không thể làm tương tự.\n\nVì vậy đối với câu hỏi được đưa ra, tôi chọn phương án thứ 3 (Số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập lớn bằng số lượng các mẫu hình ảnh có thể được chụp bằng các phép tích chập nhỏ).\nCó nhiều kết cấu có thể được tạo ra với kích thước lớn. Vì vậy, cũng có nhiều mẫu hơn có thể được chụp với các kết cấu lớn. Tuy nhiên bất cứ thứ gì có thể được chụp bằng tích chập 2x2 cũng có thể được chụp bằng tích chập 3x3.","metadata":{}},{"cell_type":"markdown","source":"# Keep Going\nNow you are ready to **[combine convolutions into powerful models](https://www.kaggle.com/dansbecker/building-models-from-convolutions).** These models are fun to work with, so keep going.\n","metadata":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [Learn Discussion forum](https://www.kaggle.com/learn-forum/161321) to chat with other Learners.*","metadata":{}}]}