{"id":16928,"date":"2025-02-18T10:08:19","date_gmt":"2025-02-18T15:08:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=16928"},"modified":"2025-02-18T10:08:19","modified_gmt":"2025-02-18T15:08:19","slug":"transfer-learning-for-grayscale-images","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2025\/02\/18\/transfer-learning-for-grayscale-images\/","title":{"rendered":"Transfer Learning for Grayscale Images"},"content":{"rendered":"<h6><\/h6>\r\nIn computer vision, pretrained models are often used and adapted to the task at hand by performing <a href=\"https:\/\/www.mathworks.com\/discovery\/transfer-learning.html\">transfer learning<\/a>. Transfer learning involves modifying and retraining a pretrained network with your data. Most pretrained models are trained on large, colorful image datasets like ImageNet. But how can you use one of these pretrained models when you have grayscale images?\r\n<h6><\/h6>\r\nIn this blog post, I am going to show you a simple way in MATLAB to quickly adapt your grayscale images to be used as an input to a model pretrained with colored images.\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px; color: #c04c0b;\"><strong>What Are Grayscale Images?<\/strong><\/p>\r\nGrayscale images are single-channel images where each pixel represents a shade of gray, ranging from black (intensity = 0) to white (intensity = 255 for 8-bit images). Unlike RGB images, which have three color channels (red, green, and blue), grayscale images encode intensity information only.\r\n<h6><\/h6>\r\nFor simplicity, I am going to demonstrate how to preprocess grayscale images of handwritten digits for retraining a pretrained model. But grayscale images are not only encountered in toy datasets. They are commonly used in medical imaging (e.g., X-rays and MRIs), remote sensing, and document analysis, where color information is less critical.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-16931 \" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/02\/grayscale_images.png\" alt=\"Examples of grayscale images. Handwritten digits on the left. X-ray image on the right.\" width=\"553\" height=\"245\" \/>\r\n<h6><\/h6>\r\n<strong>Figure:<\/strong> Collage of grayscale images of handwritten digits\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px; color: #c04c0b;\"><strong>Transfer Learning<\/strong><\/p>\r\nHere, I am showing you how to prepare your grayscale image dataset, which contains images of handwritten digits, for retraining a network that was trained on colored images.\r\n<p style=\"font-size: 18px;\"><strong>Load Pretrained Network<\/strong><\/p>\r\nLoad the GoogLeNet network and get the input size of the network.\r\n<pre>net = imagePretrainedNetwork(\"googlenet\",NumClasses=numClasses);\r\ninputSize = networkInputSize(net);\r\n<\/pre>\r\n<p style=\"font-size: 18px;\"><strong>Load Training Data<\/strong><\/p>\r\nCreate an image data store.\r\n<pre>dataFolder = fullfile(toolboxdir(\"nnet\"),\"nndemos\",\"nndatasets\",\"DigitDataset\");\r\nimds = imageDatastore(dataFolder,IncludeSubfolders=true,LabelSource=\"foldernames\");\r\n<\/pre>\r\n<h6><\/h6>\r\nGet information on classes.\r\n<pre>classNames = categories(imds.Labels);\r\nnumClasses = numel(classNames);\r\n<\/pre>\r\n<h6><\/h6>\r\n<p style=\"font-size: 18px;\"><strong>Prepare Data for Training<\/strong><\/p>\r\nSplit the data into training and validation datasets.\r\n<pre>[imdsTrain,imdsValidation] = splitEachLabel(imds,0.8,\"randomized\");\r\n<\/pre>\r\n<h6><\/h6>\r\nSpecify the data augmentation options. You might need to change these options, depending on your data. Applying randomized preprocessing operations helps prevent the network from overfitting and memorizing the exact details of the training images.\r\n<pre>imageAugmenter = imageDataAugmenter( ...\r\n    RandXReflection=true, ...\r\n    RandXTranslation=[-30 30], ...\r\n    RandYTranslation=[-30 30]);\r\n<\/pre>\r\n<h6><\/h6>\r\nAugment the training and validation datasets. The augmented image datastores are transformed batches of the training and validation data. The transformations make the input images compatible with the deep neural network.\r\n<pre>augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...\r\n    ColorPreprocessing=\"gray2rgb\",DataAugmentation=imageAugmenter);\r\naugimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation, ...\r\n    ColorPreprocessing=\"gray2rgb\");\r\n<\/pre>\r\n<h6><\/h6>\r\n<table width=\"90%;\">\r\n<tbody>\r\n<tr style=\"background-color: #e2f0ff;\">\r\n<td style=\"padding: 20px; text-align: left;\"><strong>When you created the augmented image datastores, you specified that the input grayscale images need color preprocessing. It\u2019s as easy as that!<\/strong><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>\r\n<p style=\"font-size: 18px;\"><strong>Retrain Network<\/strong><\/p>\r\nFind the last learnable layer of the network. Freeze the weights of the network, keeping the last learnable layer unfrozen.\r\n<pre>[layerName,learnableNames] = networkHead(net);\r\nnet = freezeNetwork(net,LayerNamesToIgnore=layerName);\r\n<\/pre>\r\n<h6><\/h6>\r\nRetrain the network with your input images and training options.\r\n<pre>net = trainnet(augimdsTrain,net,\"crossentropy\",options);\r\n<\/pre>\r\n<h6><\/h6>\r\n<table width=\"90%;\">\r\n<tbody>\r\n<tr style=\"background-color: #e2f0ff;\">\r\n<td style=\"padding: 20px; text-align: left;\"><strong>To learn more about transfer learning with MATLAB , check out the <a href=\"https:\/\/www.mathworks.com\/videos\/deep-learning-with-matlab-transfer-learning-in-10-lines-of-matlab-code-1487714838381.html\">Transfer Learning in 10 Lines of Code<\/a> video and <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/retrain-neural-network-to-classify-new-images.html\">this example<\/a>.<\/strong><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px; color: #c04c0b;\"><strong>Wrapping Up<\/strong><\/p>\r\nLeave a comment below to discuss if you have encountered grayscale images in your workflow, what models you typically use for transfer learning, and what preprocessing techniques you find the most useful.\r\n<h6><\/h6>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/02\/grayscale_images.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nIn computer vision, pretrained models are often used and adapted to the task at hand by performing transfer learning. Transfer learning involves modifying and retraining a pretrained network with... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2025\/02\/18\/transfer-learning-for-grayscale-images\/\">read more >><\/a><\/p>","protected":false},"author":194,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/16928"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/194"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=16928"}],"version-history":[{"count":22,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/16928\/revisions"}],"predecessor-version":[{"id":19014,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/16928\/revisions\/19014"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=16928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=16928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=16928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}