{"id":7332,"date":"2023-08-29T13:07:30","date_gmt":"2023-08-29T21:07:30","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=7332"},"modified":"2025-04-24T17:14:33","modified_gmt":"2025-04-24T17:14:33","slug":"end-to-end-deep-learning-project-with-pytorch-comet-ml","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/","title":{"rendered":"End-to-End Deep Learning Project with PyTorch &#038; Comet ML"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\">\n\n\n\n<div class=\"fh fi fj fk fl\">\n<div class=\"ab ca\">\n<div class=\"ch bg et eu ev ew\">\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg\" alt=\"\" width=\"700\" height=\"467\"><\/figure><div class=\"mg mh mi\"><picture><\/picture><\/div>\n<\/div><figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.freepik.com\/free-photo\/programming-background-collage_34089153.htm\" target=\"_blank\" rel=\"noopener ugc nofollow\">Image by Freepik<\/a><\/figcaption><\/figure>\n<p id=\"86d6\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">AI tools such as ChatGPT, DALL-E, and Midjourney are increasingly becoming a part of our daily lives. These tools were developed with deep learning techniques. <strong class=\"be nw\">Deep learning <\/strong>is a subfield of AI that aims to extract knowledge from data. Today, I\u2019ll walk you through how to perform an end-to-end deep learning project using PyTorch, Comet ML, and Gradio. Here are the topics we\u2019ll cover in this blog:<\/p>\n<ul class=\"\">\n<li id=\"3fd0\" class=\"nb nc fo be b gm nd ne nf gp ng nh ni nx nk nl nm ny no np nq nz ns nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\">Loading a custom dataset with PyTorch<\/li>\n<li id=\"7333\" class=\"nb nc fo be b gm od ne nf gp oe nh ni nx of nl nm ny og np nq nz oh nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\">Building a CNN-based model from scratch<\/li>\n<li id=\"f1d7\" class=\"nb nc fo be b gm od ne nf gp oe nh ni nx of nl nm ny og np nq nz oh nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\">Tracking an experiment with Comet ML<\/li>\n<li id=\"1e33\" class=\"nb nc fo be b gm od ne nf gp oe nh ni nx of nl nm ny og np nq nz oh nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\">Deploying an app on HuggingFace with Gradio<\/li>\n<\/ul>\n<p id=\"86cc\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">By the end of this article, you\u2019ll learn step-by-step the life cycle of a deep learning project on how to perform image classification using the cat vs. dog dataset. After finishing the project, our app will look like this:<\/p>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*uF37Jln4iYi-EMlirSbngw.gif\" alt=\"\" width=\"700\" height=\"394\"><\/figure><div class=\"mg mh oi\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*uF37Jln4iYi-EMlirSbngw.gif 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*uF37Jln4iYi-EMlirSbngw.gif 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*uF37Jln4iYi-EMlirSbngw.gif 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*uF37Jln4iYi-EMlirSbngw.gif 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*uF37Jln4iYi-EMlirSbngw.gif 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*uF37Jln4iYi-EMlirSbngw.gif 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*uF37Jln4iYi-EMlirSbngw.gif 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*uF37Jln4iYi-EMlirSbngw.gif 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*uF37Jln4iYi-EMlirSbngw.gif 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*uF37Jln4iYi-EMlirSbngw.gif 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*uF37Jln4iYi-EMlirSbngw.gif 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*uF37Jln4iYi-EMlirSbngw.gif 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*uF37Jln4iYi-EMlirSbngw.gif 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*uF37Jln4iYi-EMlirSbngw.gif 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/youtu.be\/A6xLk_5A5Pw\" target=\"_blank\" rel=\"noopener ugc nofollow\">Image Classification App<\/a> (Video by Author)<\/figcaption>\n<\/figure>\n<h1 id=\"e263\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Introduction<\/h1>\n<p id=\"93e1\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">Before diving into the project, let me explain the libraries I\u2019m going to use in this analysis. Let\u2019s start with PyTorch:<\/p>\n<h2 id=\"c791\" class=\"pk ok fo be ol pl pm pn oo po pp pq or nj pr ps pt nn pu pv pw nr px py pz qa bj\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">PyTorch<\/a><\/h2>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*x23RsuL7SBAiWUjv\" alt=\"\" width=\"700\" height=\"140\"><\/figure><div class=\"mg mh qb\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*x23RsuL7SBAiWUjv 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*x23RsuL7SBAiWUjv 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*x23RsuL7SBAiWUjv 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*x23RsuL7SBAiWUjv 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*x23RsuL7SBAiWUjv 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*x23RsuL7SBAiWUjv 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*x23RsuL7SBAiWUjv 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*x23RsuL7SBAiWUjv 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*x23RsuL7SBAiWUjv 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*x23RsuL7SBAiWUjv 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*x23RsuL7SBAiWUjv 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*x23RsuL7SBAiWUjv 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*x23RsuL7SBAiWUjv 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*x23RsuL7SBAiWUjv 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\">PyTorch Features (<a class=\"af na\" href=\"https:\/\/pytorch.org\/features\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Image Source<\/a>)<\/figcaption>\n<\/figure>\n<p id=\"18a1\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Two frameworks are generally used for deep learning: TensorFlow and PyTorch. TensorFlow is mostly used in industry, while PyTorch is used for academic research. I\u2019m going to use PyTorch for this project because of its user-friendliness, flexibility, and robust community support. Let\u2019s move on and take a look at another library.<\/p>\n<h2 id=\"09ab\" class=\"pk ok fo be ol pl pm pn oo po pp pq or nj pr ps pt nn pu pv pw nr px py pz qa bj\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.comet.com\/site\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet ML<\/a><\/h2>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*e-HOiPQNHDUImZT8i6yVuw.png\" alt=\"\" width=\"700\" height=\"140\"><\/figure><div class=\"mg mh qb\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*e-HOiPQNHDUImZT8i6yVuw.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*e-HOiPQNHDUImZT8i6yVuw.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*e-HOiPQNHDUImZT8i6yVuw.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*e-HOiPQNHDUImZT8i6yVuw.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*e-HOiPQNHDUImZT8i6yVuw.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*e-HOiPQNHDUImZT8i6yVuw.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*e-HOiPQNHDUImZT8i6yVuw.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*e-HOiPQNHDUImZT8i6yVuw.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\">Comet ML (Image by Author)<\/figcaption>\n<\/figure>\n<p id=\"06d9\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">When implementing deep learning projects, you\u2019ll need to track your hyperparameters, visualize performance metrics, monitor models, and share experiments with others. This is when Comet ML comes into play.<\/p>\n<p id=\"0d81\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Comet ML is an machine learning platform that allows you to manage, visualize, compare and optimize models. We\u2019re going to use Comet ML to track our hyperparameters and to monitor our model. Let\u2019s move on and have a look at what Gradio is.<\/p>\n<h2 id=\"a264\" class=\"pk ok fo be ol pl pm pn oo po pp pq or nj pr ps pt nn pu pv pw nr px py pz qa bj\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/gradio.app\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Gradio<\/a><\/h2>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*8uLrpAWKCrZCRgZpN5AeZg.png\" alt=\"\" width=\"700\" height=\"140\"><\/figure><div class=\"mg mh qb\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*8uLrpAWKCrZCRgZpN5AeZg.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*8uLrpAWKCrZCRgZpN5AeZg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*8uLrpAWKCrZCRgZpN5AeZg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*8uLrpAWKCrZCRgZpN5AeZg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*8uLrpAWKCrZCRgZpN5AeZg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*8uLrpAWKCrZCRgZpN5AeZg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*8uLrpAWKCrZCRgZpN5AeZg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*8uLrpAWKCrZCRgZpN5AeZg.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\">Gradio (Image by Author)<\/figcaption>\n<\/figure>\n<p id=\"fadf\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Deep learning projects that are not moved to production are dead projects. Gradio is an open-source Python library that helps you build easy-to-use demos for your ML model that you can share with other people.<\/p>\n<p id=\"f4ab\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Beautiful! We briefly talked about the libraries we\u2019ll use. Let\u2019s go ahead and start loading our dataset.<\/p>\n<h1 id=\"631a\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Loading the Dataset<\/h1>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*nG1Ahtu7A4neez6fxonJGA.png\" alt=\"\" width=\"700\" height=\"140\"><\/figure><div class=\"mg mh qb\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*nG1Ahtu7A4neez6fxonJGA.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*nG1Ahtu7A4neez6fxonJGA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*nG1Ahtu7A4neez6fxonJGA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*nG1Ahtu7A4neez6fxonJGA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*nG1Ahtu7A4neez6fxonJGA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*nG1Ahtu7A4neez6fxonJGA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*nG1Ahtu7A4neez6fxonJGA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*nG1Ahtu7A4neez6fxonJGA.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.kaggle.com\/datasets\/tongpython\/cat-and-dog\" target=\"_blank\" rel=\"noopener ugc nofollow\">Cat and Dog Dataset<\/a><\/figcaption>\n<\/figure>\n<p id=\"739e\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Beginners often start with clean datasets like the MNIST dataset to learn deep learning. It\u2019s good to start with these datasets, but real-world datasets aren\u2019t always clean. One of the challenges in deep learning is loading and working with a custom dataset. The dataset we\u2019ll use is <a class=\"af na\" href=\"https:\/\/www.kaggle.com\/datasets\/tongpython\/cat-and-dog\" target=\"_blank\" rel=\"noopener ugc nofollow\">the cat and dog dataset<\/a>, which contains images of cats and dogs.<\/p>\n<p id=\"30ac\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Before loading this dataset, let\u2019s launch an Experiment in Comet ML..<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"f5de\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Installing comet_ml<\/span>\n<span class=\"hljs-comment\"># !pip install comet_ml<\/span>\n\n<span class=\"hljs-comment\"># Importing the comet_ml library<\/span>\n<span class=\"hljs-keyword\">import<\/span> comet_ml\n<span class=\"hljs-keyword\">from<\/span> comet_ml <span class=\"hljs-keyword\">import<\/span> Experiment\n\n<span class=\"hljs-comment\"># Building an experiment with your API key<\/span>\nexperiment = Experiment(\n    api_key= my_api_key,\n    workspace=<span class=\"hljs-string\">\"tirendaz-academy\"<\/span>,\n    project_name=<span class=\"hljs-string\">\"experiment-tracking\"<\/span>)\n\n<span class=\"hljs-comment\"># Setting hyperparameters<\/span>\nhyper_params = {<span class=\"hljs-string\">\"seed\"<\/span>: <span class=\"hljs-number\">42<\/span>, <span class=\"hljs-string\">\"batch_size\"<\/span>: <span class=\"hljs-number\">32<\/span>, <span class=\"hljs-string\">\"num_epochs\"<\/span>: <span class=\"hljs-number\">20<\/span>,\n                <span class=\"hljs-string\">\"learning_rate\"<\/span>: <span class=\"hljs-number\">1e-3<\/span>,<span class=\"hljs-string\">\"image_size\"<\/span>: <span class=\"hljs-number\">224<\/span>}\n\n<span class=\"hljs-comment\"># Logging hyperparamters<\/span>\nexperiment.log_parameters(hyper_params)<\/span><\/pre>\n<p id=\"5c98\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">If you don\u2019t have a Comet ML account yet, you can create a free account <a class=\"af na\" href=\"https:\/\/www.comet.com\/site\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">here<\/a>. To follow along with the code in this blog, you can access the notebook I used in this project <a class=\"af na\" href=\"https:\/\/www.kaggle.com\/code\/tirendazacademy\/experiment-tracking-with-comet-ml-pytorch\/notebook\" target=\"_blank\" rel=\"noopener ugc nofollow\">here<\/a>.<\/p>\n<p id=\"15f6\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Awesome! We started our experiment. Now let&#8217;s take a look at our version of Torch and check if we have access to CUDA (GPU). CUDA is a parallel computing platform developed by NVIDIA that makes calculations faster. You can use it for free (in limited amounts) on Google Colab or Kaggle notebooks.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"7c9a\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> torch\n<span class=\"hljs-keyword\">from<\/span> torch <span class=\"hljs-keyword\">import<\/span> nn\n\n<span class=\"hljs-comment\"># Make sure torch &gt;= 1.10.0<\/span>\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">\"The version of torch:\"<\/span>, torch.__version__)\n\n<span class=\"hljs-comment\"># Setuping cuda<\/span>\ndevice = <span class=\"hljs-string\">\"cuda\"<\/span> <span class=\"hljs-keyword\">if<\/span> torch.cuda.is_available() <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-string\">\"cpu\"<\/span>\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">\"The type of device: \"<\/span>,device)\n\n<span class=\"hljs-comment\"># Output:<\/span>\nThe version of torch: <span class=\"hljs-number\">1.11<\/span><span class=\"hljs-number\">.0<\/span>\nThe <span class=\"hljs-built_in\">type<\/span> of device:  cuda<\/span><\/pre>\n<p id=\"77ad\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Now let\u2019s create our training and test paths that we\u2019ll use while loading data.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"b775\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Creating our paths<\/span>\nmy_train_dir = <span class=\"hljs-string\">\"\/kaggle\/input\/cat-and-dog\/training_set\/training_set\"<\/span>\nmy_test_dir = <span class=\"hljs-string\">\"\/kaggle\/input\/cat-and-dog\/test_set\/test_set\"<\/span><\/span><\/pre>\n<p id=\"4248\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Excellent! Now let\u2019s get a random image and look at the features of this image.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"68bb\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> random\n<span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image\n<span class=\"hljs-keyword\">import<\/span> glob\n<span class=\"hljs-keyword\">from<\/span> pathlib <span class=\"hljs-keyword\">import<\/span> Path\n\n<span class=\"hljs-comment\"># Setting seed<\/span>\nrandom.seed(hyper_params[<span class=\"hljs-string\">\"seed\"<\/span>])\n\n<span class=\"hljs-comment\"># Creating our image path<\/span>\nimage_path= glob.glob(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{image_path}<\/span>\/*\/*\/*\/*.jpg\"<\/span>)\n\n<span class=\"hljs-comment\"># Getting random a path<\/span>\nrandom_image_path = random.choice(image_path)\n\n<span class=\"hljs-comment\"># Creating a variable for the path <\/span>\nimage_class = Path(random_image_path).parent.stem\n\n<span class=\"hljs-comment\"># Let's open the image<\/span>\nimage = Image.<span class=\"hljs-built_in\">open<\/span>(random_image_path)\n\n<span class=\"hljs-comment\"># Let's print our metadata<\/span>\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">\"Random image path: {}\"<\/span>.<span class=\"hljs-built_in\">format<\/span>(random_image_path))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">\"Image class: {}\"<\/span>.<span class=\"hljs-built_in\">format<\/span>(image_class))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">\"Image height: {}\"<\/span>.<span class=\"hljs-built_in\">format<\/span>(image.height))\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">\"Image width: {}\"<\/span>.<span class=\"hljs-built_in\">format<\/span>(image.width))\nimage<\/span><\/pre>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*NcX7Z0_zzZIcLSsvzboAFA.png\" alt=\"\" width=\"700\" height=\"393\"><\/figure><div class=\"mg mh ql\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*NcX7Z0_zzZIcLSsvzboAFA.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*NcX7Z0_zzZIcLSsvzboAFA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*NcX7Z0_zzZIcLSsvzboAFA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*NcX7Z0_zzZIcLSsvzboAFA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*NcX7Z0_zzZIcLSsvzboAFA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*NcX7Z0_zzZIcLSsvzboAFA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*NcX7Z0_zzZIcLSsvzboAFA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*NcX7Z0_zzZIcLSsvzboAFA.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"9ab2\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">This is a cat image and the size of this image is 217*179. What a cute cat, right? I love cats. Let\u2019s go ahead and create the necessary functions to load the dataset.<\/p>\n<h2 id=\"3bd6\" class=\"pk ok fo be ol pl pm pn oo po pp pq or nj pr ps pt nn pu pv pw nr px py pz qa bj\" data-selectable-paragraph=\"\">Transforming data<\/h2>\n<p id=\"c498\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">So far, we created variables for the dataset paths and explored an image from the dataset. Note that images in a dataset may not all be the same size. In this case, we\u2019ll need to do some data preprocessing to standardize the size, shape, and format of th stylee pictures.<\/p>\n<p id=\"de1d\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Transforming data, also known as preprocessing, helps you prepare quality data. With transforming, you can improve the performance of your model and reduce the risk of bias. Let\u2019s transform our dataset with <code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/vision\/stable\/transforms.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">torchvision.transforms<\/a><\/code> in PyTorch.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"d8b9\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">from<\/span> torchvision <span class=\"hljs-keyword\">import<\/span> transforms\n\n<span class=\"hljs-comment\"># Setting our image size<\/span>\nIMAGE_SIZE=(hyper_params[<span class=\"hljs-string\">\"image_size\"<\/span>], hyper_params[<span class=\"hljs-string\">\"image_size\"<\/span>])\n\n<span class=\"hljs-comment\"># Creating a transform for training using TrivialAugment<\/span>\nmy_train_transform = transforms.Compose([\n    transforms.Resize(IMAGE_SIZE),\n    transforms.TrivialAugmentWide(),\n    transforms.ToTensor()])\n\n<span class=\"hljs-comment\"># Creating a transform for testing <\/span>\nmy_test_transform = transforms.Compose([\n    transforms.Resize(IMAGE_SIZE),\n    transforms.ToTensor()])<\/span><\/pre>\n<p id=\"96a8\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Here, we used the <code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/vision\/stable\/generated\/torchvision.transforms.TrivialAugmentWide.html#torchvision.transforms.TrivialAugmentWide\" target=\"_blank\" rel=\"noopener ugc nofollow\">TrivialAugmentWide<\/a><\/code> function, which is a data augmentation technique. Now let\u2019s take a step back and talk about what data augmentation is. <strong class=\"be nw\">Data augmentation<\/strong> is a method used to artificially increase the diversity of your data by modifying your existing data. This technique is often utilized when the dataset is small. I encourage you to examine the examples of the various transforms <a class=\"af na\" href=\"https:\/\/pytorch.org\/vision\/stable\/auto_examples\/plot_transforms.html#sphx-glr-auto-examples-plot-transforms-py\" target=\"_blank\" rel=\"noopener ugc nofollow\">here<\/a>.<\/p>\n<p id=\"81bd\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Nice, we determined how to transform our dataset. Now we\u2019re ready to create our own custom <a class=\"af na\" href=\"https:\/\/pytorch.org\/docs\/stable\/data.html#torch.utils.data.Dataset\" target=\"_blank\" rel=\"noopener ugc nofollow\">torch <\/a><code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/docs\/stable\/data.html#torch.utils.data.Dataset\" target=\"_blank\" rel=\"noopener ugc nofollow\">Dataset<\/a><\/code><a class=\"af na\" href=\"https:\/\/pytorch.org\/docs\/stable\/data.html#torch.utils.data.Dataset\" target=\"_blank\" rel=\"noopener ugc nofollow\"> class<\/a>.<\/p>\n<h2 id=\"ffc4\" class=\"pk ok fo be ol pl pm pn oo po pp pq or nj pr ps pt nn pu pv pw nr px py pz qa bj\" data-selectable-paragraph=\"\">Creating a Custom <code class=\"cw qm qn qo qd b\">Dataset<\/code><\/h2>\n<p id=\"3010\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">You can find many ready-made datasets such as MNIST, and CIFAR100 in the <code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/vision\/stable\/datasets.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">torchvision.datasets<\/a><\/code> module. But in most cases, you need to handle real-world datasets. If you want, you can create your own class to load the dataset in PyTorch. But, the good news is that you can use the <code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/vision\/main\/generated\/torchvision.datasets.ImageFolder.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">ImageFolder<\/a><\/code> function if the format of your dataset is as shown below:<\/p>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V1IinsPrz8SWHMIapw2uaw.png\" alt=\"\" width=\"700\" height=\"203\"><\/figure><div class=\"mg mh qp\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*V1IinsPrz8SWHMIapw2uaw.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*V1IinsPrz8SWHMIapw2uaw.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*V1IinsPrz8SWHMIapw2uaw.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*V1IinsPrz8SWHMIapw2uaw.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*V1IinsPrz8SWHMIapw2uaw.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*V1IinsPrz8SWHMIapw2uaw.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*V1IinsPrz8SWHMIapw2uaw.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*V1IinsPrz8SWHMIapw2uaw.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"e694\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">We can use this function because the format of our dataset is as shown above. Let&#8217;s load images from train and test folders into <code class=\"cw qm qn qo qd b\">Datasets<\/code> with the <code class=\"cw qm qn qo qd b\">ImageFolder<\/code> function.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"5e5f\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Converting our image folders ito Datasets<\/span>\n<span class=\"hljs-keyword\">from<\/span> torchvision <span class=\"hljs-keyword\">import<\/span> datasets\n\n<span class=\"hljs-comment\"># Converting our image folders ito Datasets<\/span>\nmy_train_data = datasets.ImageFolder(my_train_dir, transform=my_train_transform)\nmy_test_data = datasets.ImageFolder(my_test_dir, transform=my_test_transform)<\/span><\/pre>\n<p id=\"420c\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Note that PyTorch has two great functions for loading the dataset: <code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/tutorials\/beginner\/basics\/data_tutorial.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">Dataset<\/a><\/code><a class=\"af na\" href=\"https:\/\/pytorch.org\/tutorials\/beginner\/basics\/data_tutorial.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">and <\/a><code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/pytorch.org\/tutorials\/beginner\/basics\/data_tutorial.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">DataLoader<\/a><\/code>. The samples and their related labels are stored in <code class=\"cw qm qn qo qd b\">Dataset<\/code>. <code class=\"cw qm qn qo qd b\">DataLoader<\/code> iteratively wraps the <code class=\"cw qm qn qo qd b\">Dataset<\/code> for easy access to samples. We built our custom <code class=\"cw qm qn qo qd b\">Dataset<\/code>s. It\u2019s time to turn our custom <code class=\"cw qm qn qo qd b\">Dataset<\/code>s into <code class=\"cw qm qn qo qd b\">DataLoader<\/code>s. Show time!<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"d8ba\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">from<\/span> torch.utils.data <span class=\"hljs-keyword\">import<\/span> DataLoader\n\n<span class=\"hljs-comment\"># Setting some parameters<\/span>\ntorch.manual_seed(hyper_params[<span class=\"hljs-string\">\"seed\"<\/span>])\nNUM_WORKERS = os.cpu_count()\n\n<span class=\"hljs-comment\"># Creating a training DataLoader<\/span>\nmy_train_dataloader = DataLoader(my_train_data,\n                                 batch_size=hyper_params[<span class=\"hljs-string\">\"batch_size\"<\/span>],\n                                 shuffle=<span class=\"hljs-literal\">True<\/span>,\n                                 num_workers=NUM_WORKERS)\n\n<span class=\"hljs-comment\"># Creating a test DataLoader<\/span>\nmy_test_dataloader = DataLoader(my_test_data,\n                               batch_size=hyper_params[<span class=\"hljs-string\">\"batch_size\"<\/span>],\n                               shuffle=<span class=\"hljs-literal\">False<\/span>,\n                               num_workers=NUM_WORKERS)<\/span><\/pre>\n<p id=\"575d\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Awesome! We have prepared the necessary functions to load the dataset. We are now ready to build a CNN-based model.<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fh fi fj fk fl\">\n<div class=\"ab ca\">\n<div class=\"ch bg et eu ev ew\">\n<blockquote class=\"qy\"><p id=\"ebc0\" class=\"qz ra fo be rb rc rd re rf rg rh nv dv\" data-selectable-paragraph=\"\">Want to see the evolution of AI-generated art projects? <a class=\"af na\" href=\"https:\/\/www.comet.com\/team-comet-ml\/clipdraw\/view\/Y4aT3gy6IrPQKBi5wncFXCYLR?utm_campaign=clipdraw-gradio&amp;utm_source=blog&amp;utm_medium=summary%3F\" target=\"_blank\" rel=\"noopener ugc nofollow\">Visit our public project to see time-lapses, experiment evolutions, and more<\/a>!<\/p><\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fh fi fj fk fl\">\n<div class=\"ab ca\">\n<div class=\"ch bg et eu ev ew\">\n<h1 id=\"677b\" class=\"oj ok fo be ol om ri go oo op rj gr or os rk ou ov ow rl oy oz pa rm pc pd pe bj\" data-selectable-paragraph=\"\">Model Building<\/h1>\n<p id=\"d7d1\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">Convolutional neural network (CNN) is a deep learning technique often used to extract patterns in visual data. CNN consists of at least three layer types: the convolutional layer, the pooling layer and then the fully connected layer.<\/p>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif\" alt=\"\" width=\"700\" height=\"580\"><\/figure><div class=\"mg mh rn\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*vn_hdIrYC9fqL-6AiwxoQQ.gif 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\">Various filters are used in the convolution layer (<a class=\"af na\" href=\"https:\/\/cs231n.github.io\/convolutional-networks\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Source<\/a>)<\/figcaption>\n<\/figure>\n<p id=\"00c6\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">The convolutional layer is the fundamental building block of a CNN that is used to extract information such as edges from images. The pooling layer is added between the successive convolution layers and is leveraged to reduce the number of parameters. Images pass through the convolution and pooling layers, and then classification occurs in the final fully connected layer.<\/p>\n<p id=\"67d0\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">You can build a CNN-based model with transfer learning. But I\u2019m going to create a CNN model from scratch:<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"f083\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Creating a CNN-based image classifier.<\/span>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">ImageClassifier<\/span>(nn.Module):\n    <span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">__init__<\/span>(<span class=\"hljs-params\">self<\/span>):\n        <span class=\"hljs-built_in\">super<\/span>().__init__()\n\n        <span class=\"hljs-comment\"># Creating our first convolutional layer<\/span>\n        self.conv_layer_1 = nn.Sequential(\n          nn.Conv2d(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">64<\/span>, <span class=\"hljs-number\">3<\/span>, padding=<span class=\"hljs-number\">1<\/span>),\n          nn.ReLU(),\n          nn.BatchNorm2d(<span class=\"hljs-number\">64<\/span>),\n          nn.MaxPool2d(<span class=\"hljs-number\">2<\/span>))\n\n        <span class=\"hljs-comment\"># Creating our second convolutional layer<\/span>\n        self.conv_layer_2 = nn.Sequential(\n          nn.Conv2d(<span class=\"hljs-number\">64<\/span>, <span class=\"hljs-number\">512<\/span>, <span class=\"hljs-number\">3<\/span>, padding=<span class=\"hljs-number\">1<\/span>),\n          nn.ReLU(),\n          nn.BatchNorm2d(<span class=\"hljs-number\">512<\/span>),\n          nn.MaxPool2d(<span class=\"hljs-number\">2<\/span>))\n\n        <span class=\"hljs-comment\"># Creating our third convolutional layer<\/span>\n        self.conv_layer_3 = nn.Sequential(\n          nn.Conv2d(<span class=\"hljs-number\">512<\/span>, <span class=\"hljs-number\">512<\/span>, kernel_size=<span class=\"hljs-number\">3<\/span>, padding=<span class=\"hljs-number\">1<\/span>),\n          nn.ReLU(),\n          nn.BatchNorm2d(<span class=\"hljs-number\">512<\/span>),\n          nn.MaxPool2d(<span class=\"hljs-number\">2<\/span>))\n\n        <span class=\"hljs-comment\"># Creating our classifier<\/span>\n        self.classifier = nn.Sequential(\n          nn.Flatten(),\n          nn.Linear(in_features=<span class=\"hljs-number\">512<\/span>*<span class=\"hljs-number\">3<\/span>*<span class=\"hljs-number\">3<\/span>, out_features=<span class=\"hljs-number\">2<\/span>))\n\n    <span class=\"hljs-comment\"># Defining the forward function to pass data<\/span>\n    <span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">forward<\/span>(<span class=\"hljs-params\">self, x: torch.Tensor<\/span>):\n        x = self.conv_layer_1(x)\n        x = self.conv_layer_2(x)\n        x = self.conv_layer_3(x)\n        x = self.conv_layer_3(x)\n        x = self.conv_layer_3(x)\n        x = self.conv_layer_3(x)\n        x = self.classifier(x)\n        <span class=\"hljs-keyword\">return<\/span> x\n\n<span class=\"hljs-comment\"># Instantiating an object<\/span>\nmy_model = ImageClassifier().to(device)<\/span><\/pre>\n<p id=\"5693\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Here, we first defined the hyperparameters and then used these hyperparameters in the <code class=\"cw qm qn qo qd b\">forward<\/code> function. After building the model architecture, we instantiated an object from the <code class=\"cw qm qn qo qd b\">ImageClassifier<\/code> class.<\/p>\n<p id=\"9fba\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Nice, we created a CNN-based model. Note that we used some hyperparameters such as filter size, number of neurons, and activation function. You can fine-tune these hyperparameters to build better models.<\/p>\n<p id=\"bd94\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Let\u2019s see the architecture of the model with <code class=\"cw qm qn qo qd b\">torchinfo<\/code> and then pass an image from this architecture for control.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"d463\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Installing torchinfo<\/span>\n<span class=\"hljs-comment\"># !pip install torchinfo<\/span>\n\n<span class=\"hljs-keyword\">import<\/span> torchinfo\n<span class=\"hljs-keyword\">from<\/span> torchinfo <span class=\"hljs-keyword\">import<\/span> summary\n\n<span class=\"hljs-comment\"># Testing with an example input size <\/span>\nsummary(my_model, input_size=[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>, hyper_params[<span class=\"hljs-string\">\"image_size\"<\/span>] ,hyper_params[<span class=\"hljs-string\">\"image_size\"<\/span>]]) <\/span><\/pre>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*yw-iKnnrLufvPR_NQfN63w.png\" alt=\"\" width=\"700\" height=\"1160\"><\/figure><div class=\"mg mh ro\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*yw-iKnnrLufvPR_NQfN63w.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*yw-iKnnrLufvPR_NQfN63w.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*yw-iKnnrLufvPR_NQfN63w.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*yw-iKnnrLufvPR_NQfN63w.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*yw-iKnnrLufvPR_NQfN63w.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*yw-iKnnrLufvPR_NQfN63w.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*yw-iKnnrLufvPR_NQfN63w.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*yw-iKnnrLufvPR_NQfN63w.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"b1ca\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">The output <code class=\"cw qm qn qo qd b\">torchinfo.summary<\/code> shows all the information about our model, such as input size, the total size of parameters, and the estimated total size.<\/p>\n<p id=\"e3b5\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Awesome, our model worked without errors. Let\u2019s move on to creating the train and test steps. Note that we build the model using the training data and evaluate the performance of the model on the test data. First, let\u2019s create a function that we\u2019ll use to train the model.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"3cf7\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">my_train_step<\/span>(<span class=\"hljs-params\">model: torch.nn.Module,\n                 dataloader: torch.utils.data.DataLoader,\n                 loss_fn: torch.nn.Module,\n                 optimizer: torch.optim.Optimizer<\/span>):\n\n    <span class=\"hljs-comment\"># Setting train mode<\/span>\n    my_model.train()\n\n    <span class=\"hljs-comment\"># Initializing train loss &amp; train accuracy values<\/span>\n    train_loss = <span class=\"hljs-number\">0<\/span>\n    train_acc = <span class=\"hljs-number\">0<\/span>\n\n    <span class=\"hljs-comment\"># Looping through each batch data in the dataloader<\/span>\n    <span class=\"hljs-keyword\">for<\/span> batch, (inp, out) <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">enumerate<\/span>(dataloader):\n\n        <span class=\"hljs-comment\"># Moving data to device<\/span>\n        inp, out = inp.to(device), out.to(device)\n\n        <span class=\"hljs-comment\"># Predicting the input<\/span>\n        y_pred = my_model(inp)\n\n        <span class=\"hljs-comment\"># Calculating &amp; accumulating loss<\/span>\n        loss = loss_fn(y_pred, out)\n        train_loss += loss.item()\n\n        <span class=\"hljs-comment\"># Optimizer zero grad<\/span>\n        optimizer.zero_grad()\n\n        <span class=\"hljs-comment\"># Loss backward<\/span>\n        loss.backward()\n\n        <span class=\"hljs-comment\"># Optimizer step<\/span>\n        optimizer.step()\n\n        <span class=\"hljs-comment\"># Calculating &amp; accumulating the accuracy metric<\/span>\n        y_pred_label = torch.argmax(torch.softmax(y_pred, dim=<span class=\"hljs-number\">1<\/span>), dim=<span class=\"hljs-number\">1<\/span>)\n        train_acc += (y_pred_label == out).<span class=\"hljs-built_in\">sum<\/span>().item()\/<span class=\"hljs-built_in\">len<\/span>(y_pred)\n\n    <span class=\"hljs-comment\"># Calculating metrics <\/span>\n    train_loss = train_loss \/ <span class=\"hljs-built_in\">len<\/span>(dataloader)\n    train_acc = train_acc \/ <span class=\"hljs-built_in\">len<\/span>(dataloader)\n\n    <span class=\"hljs-comment\"># Logging train metrics<\/span>\n    experiment.log_metrics({<span class=\"hljs-string\">\"train_accuracy\"<\/span>: train_acc, <span class=\"hljs-string\">\"train_loss\"<\/span>: train_loss}, epoch=hyper_params[<span class=\"hljs-string\">'num_epochs'<\/span>])\n\n    <span class=\"hljs-keyword\">return<\/span> train_loss, train_acc<\/span><\/pre>\n<p id=\"6da4\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Here, we used the <code class=\"cw qm qn qo qd b\"><a class=\"af na\" href=\"https:\/\/www.comet.com\/docs\/v2\/api-and-sdk\/python-sdk\/reference\/Experiment\/#experimentlog_metrics\" target=\"_blank\" rel=\"noopener ugc nofollow\">experiment.log_metrics<\/a><\/code> function to track the model metrics. Note that if you haven\u2019t instantiated a Comet Experiment (as we did at the very beginning of this article), this step will throw an error. So, we can monitor these metrics on the Comet ML dashboard while the model is being trained.<\/p>\n<p id=\"374a\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Beautiful, we created a function for the training step. Let\u2019s go ahead and similarly create the function that we\u2019ll use to test the model.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"7970\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">my_test_step<\/span>(<span class=\"hljs-params\">model: torch.nn.Module,\n                dataloader: torch.utils.data.DataLoader,\n                loss_fn: torch.nn.Module<\/span>):\n\n    <span class=\"hljs-comment\"># Setting eval mode<\/span>\n    my_model.<span class=\"hljs-built_in\">eval<\/span>()\n\n    <span class=\"hljs-comment\"># Initializing test loss &amp; test accuracy values<\/span>\n    test_loss = <span class=\"hljs-number\">0<\/span>\n    test_acc = <span class=\"hljs-number\">0<\/span>\n\n    <span class=\"hljs-comment\"># Starting the inference mode<\/span>\n    <span class=\"hljs-keyword\">with<\/span> torch.inference_mode():\n\n        <span class=\"hljs-comment\"># Looping through each batch data in the dataloader<\/span>\n        <span class=\"hljs-keyword\">for<\/span> batch, (inp, out) <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">enumerate<\/span>(dataloader):\n\n            <span class=\"hljs-comment\"># Moving data to device<\/span>\n            inp, out = inp.to(device), out.to(device)\n\n            <span class=\"hljs-comment\"># Forward pass<\/span>\n            test_pred_logits = my_model(inp)\n\n            <span class=\"hljs-comment\"># Calculating &amp; accumulating loss<\/span>\n            loss = loss_fn(test_pred_logits, out)\n            test_loss += loss.item()\n\n            <span class=\"hljs-comment\"># Calculating &amp; accumulating the accuracy metric<\/span>\n            test_pred_labels = test_pred_logits.argmax(dim=<span class=\"hljs-number\">1<\/span>)\n            test_acc += ((test_pred_labels == out).<span class=\"hljs-built_in\">sum<\/span>().item()\/<span class=\"hljs-built_in\">len<\/span>(test_pred_labels))\n\n    <span class=\"hljs-comment\"># Calculating metrics<\/span>\n    test_loss = test_loss \/ <span class=\"hljs-built_in\">len<\/span>(dataloader)\n    test_acc = test_acc \/ <span class=\"hljs-built_in\">len<\/span>(dataloader)\n\n    <span class=\"hljs-comment\"># Logging test metrics<\/span>\n    experiment.log_metrics({<span class=\"hljs-string\">\"test_accuracy\"<\/span>: test_acc, <span class=\"hljs-string\">\"test_loss\"<\/span>: test_loss}, epoch=hyper_params[<span class=\"hljs-string\">'num_epochs'<\/span>])\n\n    <span class=\"hljs-keyword\">return<\/span> test_loss, test_acc<\/span><\/pre>\n<p id=\"6b4d\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Here, we used <code class=\"cw qm qn qo qd b\">model.eval<\/code> mode, as we\u2019ll only use this function to evaluate the model. Now let\u2019s define a function named train to combine the <code class=\"cw qm qn qo qd b\">train_step<\/code> and <code class=\"cw qm qn qo qd b\">test_step<\/code> functions.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"2a01\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">from<\/span> tqdm.auto <span class=\"hljs-keyword\">import<\/span> tqdm\n\n<span class=\"hljs-comment\"># Setting parameters <\/span>\n<span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">my_train<\/span>(<span class=\"hljs-params\">model: torch.nn.Module,\n            train_dataloader: torch.utils.data.DataLoader,\n            test_dataloader: torch.utils.data.DataLoader,\n            optimizer: torch.optim.Optimizer,\n            loss_fn: torch.nn.Module = nn.CrossEntropyLoss(),\n            epochs: <span class=\"hljs-built_in\">int<\/span> = <span class=\"hljs-number\">5<\/span><\/span>):\n\n    <span class=\"hljs-comment\"># Creating a variable for metrics<\/span>\n    my_results = {<span class=\"hljs-string\">\"train_loss\"<\/span>: [],\n                  <span class=\"hljs-string\">\"train_acc\"<\/span>: [],\n                  <span class=\"hljs-string\">\"test_loss\"<\/span>: [],\n                  <span class=\"hljs-string\">\"test_acc\"<\/span>: []}\n\n    <span class=\"hljs-comment\"># Looping for training and testing steps <\/span>\n    <span class=\"hljs-keyword\">for<\/span> epoch <span class=\"hljs-keyword\">in<\/span> tqdm(<span class=\"hljs-built_in\">range<\/span>(epochs)):\n        train_loss, train_acc = my_train_step(model=my_model,\n                                              dataloader=my_train_dataloader,\n                                              loss_fn=my_loss_fn,\n                                              optimizer=my_optimizer)\n        test_loss, test_acc = my_test_step(model=my_model,\n                                           dataloader=my_test_dataloader,\n                                           loss_fn=my_loss_fn)\n\n        <span class=\"hljs-comment\"># Printing results<\/span>\n        <span class=\"hljs-built_in\">print<\/span>(\n            <span class=\"hljs-string\">f\"Epoch: <span class=\"hljs-subst\">{epoch+<span class=\"hljs-number\">1<\/span>}<\/span> | \"<\/span>\n            <span class=\"hljs-string\">f\"train_loss: <span class=\"hljs-subst\">{train_loss:<span class=\"hljs-number\">.4<\/span>f}<\/span> | \"<\/span>\n            <span class=\"hljs-string\">f\"train_acc: <span class=\"hljs-subst\">{train_acc:<span class=\"hljs-number\">.4<\/span>f}<\/span> | \"<\/span>\n            <span class=\"hljs-string\">f\"test_loss: <span class=\"hljs-subst\">{test_loss:<span class=\"hljs-number\">.4<\/span>f}<\/span> | \"<\/span>\n            <span class=\"hljs-string\">f\"test_acc: <span class=\"hljs-subst\">{test_acc:<span class=\"hljs-number\">.4<\/span>f}<\/span>\"<\/span>)\n\n        <span class=\"hljs-comment\"># Updating results<\/span>\n        my_results[<span class=\"hljs-string\">\"train_loss\"<\/span>].append(train_loss)\n        my_results[<span class=\"hljs-string\">\"train_acc\"<\/span>].append(train_acc)\n        my_results[<span class=\"hljs-string\">\"test_loss\"<\/span>].append(test_loss)\n        my_results[<span class=\"hljs-string\">\"test_acc\"<\/span>].append(test_acc)\n\n    <span class=\"hljs-comment\"># Returning results at the end of the epochs<\/span>\n    <span class=\"hljs-keyword\">return<\/span> my_results<\/span><\/pre>\n<p id=\"e9f4\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Awesome, we have created our functions to train and test the model. Now we can start model training.<\/p>\n<h1 id=\"86f4\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Model Training<\/h1>\n<p id=\"a160\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">So far, we have created training and testing steps, and then we have prepared a function to combine these steps. We are ready to train the model using these steps. Show time!<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"3961\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Setting seeds<\/span>\ntorch.manual_seed(hyper_params[<span class=\"hljs-string\">\"seed\"<\/span>])\ntorch.cuda.manual_seed(hyper_params[<span class=\"hljs-string\">\"seed\"<\/span>])\n\n<span class=\"hljs-comment\"># Creating loss function &amp; optimizer<\/span>\nmy_loss_fn = nn.CrossEntropyLoss()\nmy_optimizer = torch.optim.Adam(params=my_model.parameters(), lr=hyper_params[<span class=\"hljs-string\">\"learning_rate\"<\/span>])\n\n<span class=\"hljs-comment\"># Initializing the timer<\/span>\n<span class=\"hljs-keyword\">from<\/span> timeit <span class=\"hljs-keyword\">import<\/span> default_timer <span class=\"hljs-keyword\">as<\/span> timer\nmy_start_time = timer()\n\n<span class=\"hljs-comment\"># Training our model<\/span>\nmy_model_results = my_train(model=my_model,\n                            train_dataloader=my_train_dataloader,\n                            test_dataloader=my_test_dataloader,\n                            optimizer=my_optimizer,\n                            loss_fn=my_loss_fn,\n                            epochs=hyper_params[<span class=\"hljs-string\">\"num_epochs\"<\/span>])\n\n<span class=\"hljs-comment\"># Ending the timer <\/span>\nmy_end_time = timer()\n\n<span class=\"hljs-comment\"># Printing the time<\/span>\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">f\"Total training time: <span class=\"hljs-subst\">{my_end_time-my_start_time:<span class=\"hljs-number\">.3<\/span>f}<\/span> seconds\"<\/span>)<\/span><\/pre>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*UI8xuagV-io-b1JiH_0nbA.png\" alt=\"\" width=\"700\" height=\"402\"><\/figure><div class=\"mg mh rp\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*UI8xuagV-io-b1JiH_0nbA.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*UI8xuagV-io-b1JiH_0nbA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*UI8xuagV-io-b1JiH_0nbA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*UI8xuagV-io-b1JiH_0nbA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*UI8xuagV-io-b1JiH_0nbA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*UI8xuagV-io-b1JiH_0nbA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*UI8xuagV-io-b1JiH_0nbA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*UI8xuagV-io-b1JiH_0nbA.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"2315\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Nice, our model was trained on the training data and evaluated on the test data. At the end of 20 epochs, the accuracy of our model on the training data is 0.91 and on the test data is 0.91. Note that we want the accuracy of the model on the training and test data to be close to each other so that our model is not prone to overfitting. Now, let\u2019s visualize the accuracy and loss metrics.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"f030\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">my_plot_loss_curves<\/span>(<span class=\"hljs-params\">results<\/span>):\n\n    my_results = <span class=\"hljs-built_in\">dict<\/span>(<span class=\"hljs-built_in\">list<\/span>(my_model_results.items()))\n\n    <span class=\"hljs-comment\"># Getting the train &amp; test loss values<\/span>\n    my_loss = my_results[<span class=\"hljs-string\">'train_loss'<\/span>]\n    my_test_loss = my_results[<span class=\"hljs-string\">'test_loss'<\/span>]\n\n    <span class=\"hljs-comment\"># Getting the train &amp; test accuracy values<\/span>\n    my_accuracy = my_results[<span class=\"hljs-string\">'train_acc'<\/span>]\n    my_test_accuracy = my_results[<span class=\"hljs-string\">'test_acc'<\/span>]\n\n    <span class=\"hljs-comment\"># Calculating epochs<\/span>\n    my_epochs = <span class=\"hljs-built_in\">range<\/span>(<span class=\"hljs-built_in\">len<\/span>(my_results[<span class=\"hljs-string\">'train_loss'<\/span>]))\n\n    <span class=\"hljs-comment\"># Let's setup a graph<\/span>\n    plt.figure(figsize=(<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">7<\/span>))\n\n    <span class=\"hljs-comment\"># Let's plot loss<\/span>\n    plt.subplot(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>)\n    plt.plot(my_epochs, my_loss, label=<span class=\"hljs-string\">'train_loss'<\/span>)\n    plt.plot(my_epochs, my_test_loss, label=<span class=\"hljs-string\">'test_loss'<\/span>)\n    plt.title(<span class=\"hljs-string\">'Loss'<\/span>)\n    plt.xlabel(<span class=\"hljs-string\">'Epochs'<\/span>)\n    plt.legend()\n\n    <span class=\"hljs-comment\"># Let's plot accuracy<\/span>\n    plt.subplot(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>)\n    plt.plot(my_epochs, my_accuracy, label=<span class=\"hljs-string\">'train_accuracy'<\/span>)\n    plt.plot(my_epochs, my_test_accuracy, label=<span class=\"hljs-string\">'test_accuracy'<\/span>)\n    plt.title(<span class=\"hljs-string\">'Accuracy'<\/span>)\n    plt.xlabel(<span class=\"hljs-string\">'Epochs'<\/span>)\n    plt.legend();\n\n<span class=\"hljs-comment\"># Let's plot the results<\/span>\nmy_plot_loss_curves(model_results)<\/span><\/pre>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*zHg17ahiT7FPgrmEmd7qwg.png\" alt=\"\" width=\"700\" height=\"357\"><\/figure><div class=\"mg mh rq\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*zHg17ahiT7FPgrmEmd7qwg.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*zHg17ahiT7FPgrmEmd7qwg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*zHg17ahiT7FPgrmEmd7qwg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*zHg17ahiT7FPgrmEmd7qwg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*zHg17ahiT7FPgrmEmd7qwg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*zHg17ahiT7FPgrmEmd7qwg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*zHg17ahiT7FPgrmEmd7qwg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*zHg17ahiT7FPgrmEmd7qwg.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\">Accuracy and loss metrics on training and test sets (Image by Author)<\/figcaption>\n<\/figure>\n<p id=\"db31\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">The accuracy values of the model on the training and test data are not bad. Keep in mind that you can achieve better scores by fine-tuning the hyperparameters.<\/p>\n<p id=\"978f\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Since you can obtain different versions of models using different hyperparameters, it\u2019s a good idea to track these with Comet. Let me show you.<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"ee4f\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Saving our model<\/span>\n<span class=\"hljs-keyword\">from<\/span> comet_ml.integration.pytorch <span class=\"hljs-keyword\">import<\/span> log_model\nlog_model(experiment, my_model, model_name=<span class=\"hljs-string\">\"My_Image_Classification_Model\"<\/span>)<\/span><\/pre>\n<p id=\"ec5a\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Beautiful, we built a CNN-based model and saw the performance of this model. While performing these steps, we logged the hyperparameters and metrics with Comet ML. Finally, we saved the model for versioning and monitoring. Now let\u2019s end our experiment with the following command:<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"89f0\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Ending our experiment<\/span>\nexperiment.end()<\/span><\/pre>\n<h1 id=\"7c42\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Experiment Tracking With Comet ML<\/h1>\n<p id=\"c035\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">Time to review the results we found in the Comet ML dashboard. Here are the results:<\/p>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif\" alt=\"\" width=\"700\" height=\"394\"><\/figure><div class=\"mg mh rp\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*KtUFsjycIi6-U3b2ZtH2EQ.gif 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.comet.com\/tirendaz-academy\/experiment-tracking\/view\/new\/panels\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet ML Dashboard<\/a> (Video by Author)<\/figcaption>\n<\/figure>\n<h1 id=\"6fad\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Building An App With Gradio<\/h1>\n<p id=\"58a5\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">Projects that remain in the notebooks are dead projects. The ML lifecycle is an ongoing process from data preparation to deployment and monitoring of the model. With Gradio we can create an app and deploy it on Hugging Face for free. Let\u2019s get started:<\/p>\n<pre class=\"mj mk ml mm mn qc qd qe bo qf ba bj\"><span id=\"9e14\" class=\"qg ok fo qd b bf qh qi l qj qk\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Creating a function for prediction<\/span>\n<span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title.function\">predict<\/span>(<span class=\"hljs-params\">inp<\/span>):\n    image_transform = transforms.Compose([ transforms.Resize(size=(<span class=\"hljs-number\">224<\/span>,<span class=\"hljs-number\">224<\/span>)), transforms.ToTensor()])\n    labels = [<span class=\"hljs-string\">'cat'<\/span>, <span class=\"hljs-string\">'dog'<\/span>]\n    inp = image_transform(inp).unsqueeze(dim=<span class=\"hljs-number\">0<\/span>)\n    <span class=\"hljs-keyword\">with<\/span> torch.no_grad():\n        prediction = torch.nn.functional.softmax(model(inp))\n        confidences = {labels[i]: <span class=\"hljs-built_in\">float<\/span>(prediction.squeeze()[i]) <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">range<\/span>(<span class=\"hljs-built_in\">len<\/span>(labels))}\n    <span class=\"hljs-keyword\">return<\/span> confidences\n\n<span class=\"hljs-comment\"># Building an interface<\/span>\ngr.Interface(fn=predict,\n             inputs=gr.Image(<span class=\"hljs-built_in\">type<\/span>=<span class=\"hljs-string\">\"pil\"<\/span>),\n             outputs=gr.Label(num_top_classes=<span class=\"hljs-number\">2<\/span>),\n             title=title,\n             description=description,\n             article=article,\n             examples=[<span class=\"hljs-string\">'cat.jpg'<\/span>, <span class=\"hljs-string\">'dog.jpg'<\/span>]).launch()<\/span><\/pre>\n<p id=\"e17f\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">Here, we first defined a function to predict the label of the image and then created an interface using this function. You can examine this app and access project files <a class=\"af na\" href=\"https:\/\/huggingface.co\/spaces\/Tirendaz\/pytorch_cat_vs_dog\" target=\"_blank\" rel=\"noopener ugc nofollow\">here.<\/a><\/p>\n<figure class=\"mj mk ml mm mn mo mg mh paragraph-image\">\n<div class=\"mp mq eb mr bg ms\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mt mu c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*_-nJTvCoImXgP6qtVm1x9w.png\" alt=\"\" width=\"700\" height=\"394\"><\/figure><div class=\"mg mh rp\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*_-nJTvCoImXgP6qtVm1x9w.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*_-nJTvCoImXgP6qtVm1x9w.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*_-nJTvCoImXgP6qtVm1x9w.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*_-nJTvCoImXgP6qtVm1x9w.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*_-nJTvCoImXgP6qtVm1x9w.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*_-nJTvCoImXgP6qtVm1x9w.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*_-nJTvCoImXgP6qtVm1x9w.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*_-nJTvCoImXgP6qtVm1x9w.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mv mw mx mg mh my mz be b bf z dv\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/huggingface.co\/spaces\/Tirendaz\/pytorch_cat_vs_dog\" target=\"_blank\" rel=\"noopener ugc nofollow\">Cat vs Dog App<\/a> (Image by Author)<\/figcaption>\n<\/figure>\n<h1 id=\"cbb1\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Conclusion<\/h1>\n<p id=\"a87d\" class=\"pw-post-body-paragraph nb nc fo be b gm pf ne nf gp pg nh ni nj ph nl nm nn pi np nq nr pj nt nu nv fh bj\" data-selectable-paragraph=\"\">Congratulations! You learned how to perform an end-to-end deep learning project. Note that deep learning projects are a never-ending cycle. You collect the data, train the model with this data, then turn this model into an app, move this app to production, and finally monitor whether this app is working properly and iterate.<\/p>\n<p id=\"2f2d\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">In this project, we used PyTorch for model building, Comet ML for experiment tracking, and Gradio to convert the model into an app. You can access the GitHub repo of this project <a class=\"af na\" href=\"https:\/\/github.com\/TirendazAcademy\/End-to-End-Image-Classification-Project\" target=\"_blank\" rel=\"noopener ugc nofollow\">here<\/a>.<\/p>\n<p id=\"fbc7\" class=\"pw-post-body-paragraph nb nc fo be b gm nd ne nf gp ng nh ni nj nk nl nm nn no np nq nr ns nt nu nv fh bj\" data-selectable-paragraph=\"\">That\u2019s it! Thanks for reading and I hope you enjoyed it. Please let me know if you have any feedback and feel free to connect with me on <a class=\"af na\" href=\"https:\/\/youtube.com\/tirendazacademy\" target=\"_blank\" rel=\"noopener ugc nofollow\">YouTube<\/a> | <a class=\"af na\" href=\"https:\/\/twitter.com\/TirendazAcademy\" target=\"_blank\" rel=\"noopener ugc nofollow\">Twitter<\/a> | <a class=\"af na\" href=\"https:\/\/www.instagram.com\/tirendazacademy\" target=\"_blank\" rel=\"noopener ugc nofollow\">Instagram<\/a> | <a class=\"af na\" href=\"https:\/\/www.linkedin.com\/in\/tirendaz-academy\" target=\"_blank\" rel=\"noopener ugc nofollow\">Linkedin<\/a> | <a class=\"af na\" href=\"https:\/\/www.kaggle.com\/tirendazacademy\" target=\"_blank\" rel=\"noopener ugc nofollow\">Kaggle<\/a><\/p>\n<div class=\"rr rs rt ru rv rw\">\n<div class=\"rx ab ik\">\n<div class=\"ry ab cn ca rz sa\"><\/div>\n<\/div>\n<\/div>\n<h6><a href=\"https:\/\/www.comet.com\/site\/blog\/7-steps-to-become-a-machine-learning-engineer\/\">7 Steps to Become a Machine Learning Engineer<\/a><\/h6>\n<h1 id=\"2844\" class=\"oj ok fo be ol om on go oo op oq gr or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" data-selectable-paragraph=\"\">Resources<\/h1>\n<ul class=\"\">\n<li id=\"5f5e\" class=\"nb nc fo be b gm pf ne nf gp pg nh ni nx ph nl nm ny pi np nq nz pj nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.packtpub.com\/product\/comet-for-data-science\/9781801814430\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet for Data Science<\/a><\/li>\n<li id=\"779c\" class=\"nb nc fo be b gm od ne nf gp oe nh ni nx of nl nm ny og np nq nz oh nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.learnpytorch.io\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Zero to Mastery Learn PyTorch for Deep Learning<\/a><\/li>\n<li id=\"f3c4\" class=\"nb nc fo be b gm od ne nf gp oe nh ni nx of nl nm ny og np nq nz oh nt nu nv oa ob oc bj\" data-selectable-paragraph=\"\"><a class=\"af na\" href=\"https:\/\/www.amazon.com\/Modern-Computer-Vision-PyTorch-applications\/dp\/1839213477\" target=\"_blank\" rel=\"noopener ugc nofollow\">Modern Computer Vision with PyTorch<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Image by Freepik AI tools such as ChatGPT, DALL-E, and Midjourney are increasingly becoming a part of our daily lives. These tools were developed with deep learning techniques. Deep learning is a subfield of AI that aims to extract knowledge from data. Today, I\u2019ll walk you through how to perform an end-to-end deep learning project [&hellip;]<\/p>\n","protected":false},"author":70,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[6],"tags":[],"coauthors":[168],"class_list":["post-7332","post","type-post","status-publish","format-standard","hentry","category-machine-learning"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>End-to-End Deep Learning Project with PyTorch &amp; Comet ML - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"End-to-End Deep Learning Project with PyTorch &amp; Comet ML\" \/>\n<meta property=\"og:description\" content=\"Image by Freepik AI tools such as ChatGPT, DALL-E, and Midjourney are increasingly becoming a part of our daily lives. These tools were developed with deep learning techniques. Deep learning is a subfield of AI that aims to extract knowledge from data. Today, I\u2019ll walk you through how to perform an end-to-end deep learning project [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-29T21:07:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:14:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg\" \/>\n<meta name=\"author\" content=\"Tirendaz AI\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tirendaz AI\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"End-to-End Deep Learning Project with PyTorch & Comet ML - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/","og_locale":"en_US","og_type":"article","og_title":"End-to-End Deep Learning Project with PyTorch & Comet ML","og_description":"Image by Freepik AI tools such as ChatGPT, DALL-E, and Midjourney are increasingly becoming a part of our daily lives. These tools were developed with deep learning techniques. Deep learning is a subfield of AI that aims to extract knowledge from data. Today, I\u2019ll walk you through how to perform an end-to-end deep learning project [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-08-29T21:07:30+00:00","article_modified_time":"2025-04-24T17:14:33+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg","type":"","width":"","height":""}],"author":"Tirendaz AI","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Tirendaz AI","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/"},"author":{"name":"Tirendaz AI","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/1afb8219a115db20d32be46c5f0de930"},"headline":"End-to-End Deep Learning Project with PyTorch &#038; Comet ML","datePublished":"2023-08-29T21:07:30+00:00","dateModified":"2025-04-24T17:14:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/"},"wordCount":1843,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg","articleSection":["Machine Learning"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/","url":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/","name":"End-to-End Deep Learning Project with PyTorch & Comet ML - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg","datePublished":"2023-08-29T21:07:30+00:00","dateModified":"2025-04-24T17:14:33+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*ckmHcR0yC0LSVkQMJSVafQ.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/end-to-end-deep-learning-project-with-pytorch-comet-ml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"End-to-End Deep Learning Project with PyTorch &#038; Comet ML"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/1afb8219a115db20d32be46c5f0de930","name":"Tirendaz AI","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/8186f103f79fbe423be05bc95bd135bc","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1669361802236-96x96.jpg","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1669361802236-96x96.jpg","caption":"Tirendaz AI"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/tirendazcontactgmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=7332"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7332\/revisions"}],"predecessor-version":[{"id":15569,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7332\/revisions\/15569"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=7332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=7332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=7332"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=7332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}