{"id":942,"date":"2022-12-18T14:13:00","date_gmt":"2022-12-18T08:43:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=942"},"modified":"2023-08-15T15:29:08","modified_gmt":"2023-08-15T09:59:08","slug":"using-transfer-learning-for-deep-learning-model","status":"publish","type":"post","link":"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model","title":{"rendered":"Build A Custom Deep Learning Model Using Transfer Learning"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"heading-introduction\">Introduction<\/h1>\n\n\n\n<p>Transfer learning is used in machine learning and is a method in which already-trained or pre-trained neural networks are present and these pre-trained neural networks are trained using millions of data points.<\/p>\n\n\n\n<p>This technique is currently most famous for training deep neural networks because its performance is great when training deep neural networks with less data. Actually, it&#8217;s instrumental in the data science field because most of the real-world data typically do not have millions of data points for training a robust deep learning model.<\/p>\n\n\n\n<p>Numerous models are already trained with millions of data points and can be used for training complex deep-learning neural networks with maximum accuracy.<\/p>\n\n\n\n<p>Ahead in this tutorial, you&#8217;ll learn to train deep neural networks using the transfer learning technique.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-keras-applications-for-transfer-learning\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-keras-applications-for-transfer-learning\"><\/a>Keras applications for transfer learning<\/h1>\n\n\n\n<p>Before building or training a deep neural network, you must know what are options available for transfer learning and which one you must use to train a complex deep neural network for your project.<\/p>\n\n\n\n<p>Keras applications are deep learning models that have pre-trained weights that are used for predictions, feature extraction, and fine-tuning. There are numerous models present in the&nbsp;<strong>Keras<\/strong>&nbsp;library and some of the popular models are<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Xception<\/strong><\/li>\n\n\n\n<li><strong>VGG16 and VGG19<\/strong><\/li>\n\n\n\n<li><strong>ResNet Series<\/strong><\/li>\n\n\n\n<li><strong>MobileNet<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Discover More on <a href=\"https:\/\/keras.io\/api\/applications\/\" target=\"_blank\" rel=\"noopener\">Keras Application<\/a>.<\/p>\n\n\n\n<p>In this article, you&#8217;ll see the use of the&nbsp;<a target=\"_blank\" href=\"https:\/\/keras.io\/api\/applications\/mobilenet\/\" rel=\"noreferrer noopener\">MobileNet<\/a>&nbsp;model for transfer learning.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-training-a-deep-learning-model\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-training-a-deep-learning-model\"><\/a>Training a deep learning model<\/h1>\n\n\n\n<p>In this section, you&#8217;ll learn to build a custom deep-learning model for image recognition in a few steps without writing any series of layers of convolution neural networks (CNN), you just need to fine-tune the pre-trained model and your model will be ready to train on the training data.<\/p>\n\n\n\n<p>In this article, the deep learning model will recognize the images of the&nbsp;<strong>hand sign language digits<\/strong>. Let&#8217;s start building a custom deep-learning model.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-grab-the-dataset\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-grab-the-dataset\"><\/a>Grab the dataset<\/h1>\n\n\n\n<p>To start the process of building a deep learning model, you will need the data first and you can easily choose the right dataset from millions of datasets by visiting a website called Kaggle. Though there are other websites also that provide datasets for building deep learning or machine learning model.<\/p>\n\n\n\n<p>But the dataset that will be used in this article is taken from the&nbsp;<strong>Kaggle<\/strong>&nbsp;named&nbsp;<a target=\"_blank\" href=\"https:\/\/www.kaggle.com\/datasets\/rayeed045\/american-sign-language-digit-dataset\" rel=\"noreferrer noopener\">American Sign Language Digit Dataset<\/a>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-data-preprocessing\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-data-preprocessing\"><\/a>Data preprocessing<\/h1>\n\n\n\n<p>After downloading and saving the dataset into the local storage, now it&#8217;s time to perform some preprocessing on the dataset like preparing the data, splitting the data into train, valid, and test directories, defining their path, and creating batches for training purposes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-preparing-the-data\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-preparing-the-data\"><\/a>Preparing the data<\/h3>\n\n\n\n<p>When you download the dataset, it contains directories from&nbsp;<strong>0<\/strong>&nbsp;to&nbsp;<strong>9<\/strong>&nbsp;in which there are three sub-folders&nbsp;<strong>input images<\/strong>,&nbsp;<strong>output images,<\/strong>&nbsp;and a&nbsp;<strong>CSV<\/strong>&nbsp;folder for each directory.<\/p>\n\n\n\n<p>Go ahead and remove the&nbsp;<strong>output images<\/strong>&nbsp;and&nbsp;<strong>CSV<\/strong>&nbsp;folder from each directory and move the content of the&nbsp;<strong>input images<\/strong>&nbsp;to the&nbsp;<strong>main directory<\/strong>&nbsp;and then remove the&nbsp;<strong>input image<\/strong>&nbsp;folder.<\/p>\n\n\n\n<p>Each main directory of the dataset now have 500 images, it&#8217;s your choice to keep all the image but for demonstration purposes, only&nbsp;<strong>200<\/strong>&nbsp;images will be used in each directory in this article.<\/p>\n\n\n\n<p>The structure of the dataset will look as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"264\" height=\"485\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-6.png\" alt=\"Dataset folder structure\" class=\"wp-image-949\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-6.png 264w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-6-163x300.png 163w\" sizes=\"auto, (max-width: 264px) 100vw, 264px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-splitting-dataset\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-splitting-dataset\"><\/a>Splitting dataset<\/h3>\n\n\n\n<p>Now let&#8217;s start by splitting the dataset into the train, valid, and test directories.<\/p>\n\n\n\n<p>The&nbsp;<code>train<\/code>&nbsp;directory will contain training data which will be the input data that we feed into the model for learning the pattern and irregularities.<\/p>\n\n\n\n<p>The&nbsp;<code>valid<\/code>&nbsp;directory will contain the validation data that will be infused into the model and will be the first unseen data that the model has seen and it will help in getting maximum accuracy.<\/p>\n\n\n\n<p>The&nbsp;<code>test<\/code>&nbsp;directory will contain the testing data that will be used for testing the model.<\/p>\n\n\n\n<p>Import the libraries that will be used further in the code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing required libraries\nimport os\nimport shutil\nimport random<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>Here&#8217;s the code that will make the required directories and move the data into the specific directories.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating dirs train, valid and test and organize data into them\nos.chdir('D:\\SACHIN\\Jupyter\\Hand Sign Language\\Hand_Sign_Language_DL_Project\\American-Sign-Language-Digits-Dataset')\n\n# If dir doesn't exist then make specified dirs\nif os.path.isdir('train\/0\/') is False:\n    os.mkdir('train')\n    os.mkdir('valid')\n    os.mkdir('test')\n\n    for i in range(0, 10):\n        # Moving 0-9 dirs into train dir\n        shutil.move(f'{i}', 'train')\n        os.mkdir(f'valid\/{i}')\n        os.mkdir(f'test\/{i}')\n\n        # Take 90 sample imgs for valid dir\n        valid_samples = random.sample(os.listdir(f'train\/{i}'), 90)\n        for j in valid_samples:\n            # Moving the sample imgs from train dir to valid sub-dirs\n            shutil.move(f'train\/{i}\/{j}', f'valid\/{i}')\n\n        # Take 10 sample imgs for test dir\n        test_samples = random.sample(os.listdir(f'train\/{i}'), 10)\n        for k in test_samples:\n            # Moving the sample imgs from train dir to test sub-dirs\n            shutil.move(f'train\/{i}\/{k}', f'test\/{i}')\n\nos.chdir('..\/..')<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>In the above code, first, we changed the directory where the dataset is present in the local storage, and then we checked if the&nbsp;<code>train\/0<\/code>&nbsp;directory is already present if not then we created the&nbsp;<strong>train<\/strong>,&nbsp;<strong>valid,<\/strong>&nbsp;and&nbsp;<strong>test<\/strong>&nbsp;directories.<\/p>\n\n\n\n<p>Then we created the sub-directories&nbsp;<code>0 to 9<\/code>&nbsp;and moved all the data inside the&nbsp;<code>train<\/code>&nbsp;directory and along with it created the sub-directories&nbsp;<code>0 to 9<\/code>&nbsp;for&nbsp;<code>valid<\/code>&nbsp;and&nbsp;<code>test<\/code>&nbsp;directories also.<\/p>\n\n\n\n<p>Then we iterated over the sub-directories&nbsp;<code>0 to 9<\/code>&nbsp;inside the&nbsp;<code>train<\/code>&nbsp;directory and taking the&nbsp;<strong>90<\/strong>&nbsp;image data randomly from each sub-directory and moving them to the corresponding sub-directories inside the&nbsp;<code>valid<\/code>&nbsp;directory.<\/p>\n\n\n\n<p>And the same was done for the&nbsp;<code>test<\/code>&nbsp;directory also.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython wp-block-embed-geekpython\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"mNbuOXCJq8\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python\">Perform High-Level File Operations In Python &#8211; shutil Module<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Perform High-Level File Operations In Python &#8211; shutil Module&#8221; &#8212; GeekPython\" src=\"https:\/\/geekpython.in\/shutil-module-in-python\/embed#?secret=p8RYHsuN9W#?secret=mNbuOXCJq8\" data-secret=\"mNbuOXCJq8\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-defining-the-path-to-the-directories\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-defining-the-path-to-the-directories\"><\/a>Defining the path to the directories<\/h3>\n\n\n\n<p>After creating the required directories, now the path to the&nbsp;<code>train<\/code>,&nbsp;<code>valid<\/code>&nbsp;and&nbsp;<code>test<\/code>&nbsp;directories need to be defined.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Specifying path for train, valid and test dirs\ntrain_path = 'D:\/SACHIN\/Jupyter\/Hand Sign Language\/Hand_Sign_Language_DL_Project\/American-Sign-Language-Digits-Dataset\/train'\nvalid_path = 'D:\/SACHIN\/Jupyter\/Hand Sign Language\/Hand_Sign_Language_DL_Project\/American-Sign-Language-Digits-Dataset\/valid'\ntest_path = 'D:\/SACHIN\/Jupyter\/Hand Sign Language\/Hand_Sign_Language_DL_Project\/American-Sign-Language-Digits-Dataset\/test'<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-applying-preprocessing\">Applying preprocessing<\/h3>\n\n\n\n<p>Pre-trained deep learning model needs some preprocessed data that would fit perfectly for training. So, the data needs to be in the format that the pre-trained model wants it.<\/p>\n\n\n\n<p>Before applying any preprocessing, let&#8217;s import the TensorFlow and its utilities that will be used further in the code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing tensorflow and its utilities\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Dense, Activation\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.metrics import categorical_crossentropy\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import load_model<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating batches of the train, valid and test images and Pre-processing using mobilenet preprocess model\ntrain_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet.preprocess_input).flow_from_directory(\n    directory=train_path, target_size=(224,224), batch_size=10, shuffle=True)\n\nvalid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet.preprocess_input).flow_from_directory(\n    directory=valid_path, target_size=(224,224), batch_size=10, shuffle=True)\n\ntest_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.mobilenet.preprocess_input).flow_from_directory(\n    directory=test_path, target_size=(224,224), batch_size=10, shuffle=False)<\/pre><\/div>\n\n\n\n<p>We used the&nbsp;<code>ImageDatagenerator<\/code>&nbsp;that takes an argument&nbsp;<code>preprocessing_function<\/code>&nbsp;in which we applied preprocessing to the images which are provided by the&nbsp;<strong>MobileNet<\/strong>&nbsp;model.<\/p>\n\n\n\n<p>Then using the&nbsp;<code>flow_from_directory<\/code>&nbsp;function, we provided the path of the directories and dimensions in which our image should be trained because the&nbsp;<strong>MobileNet<\/strong>&nbsp;model was trained for the images having dimensions&nbsp;<code>224x224<\/code>.<\/p>\n\n\n\n<p>Then defined the batch size and it can be defined as how many images can go in one&nbsp;<strong>iteration<\/strong>&nbsp;and then we&nbsp;<strong>shuffled the images<\/strong>. Image shuffling for test data was not applied because testing data will not be going for training.<\/p>\n\n\n\n<p>After running the above cell in Jupyter Notebook or Google Colab, you will see the result like the following.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"93\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/2-7.png\" alt=\"Output after running the above cell\" class=\"wp-image-950\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/2-7.png 537w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/2-7-300x52.png 300w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/figure>\n\n\n\n<p>The general use case of the&nbsp;<code>ImageDataGenerator<\/code>&nbsp;is for augmenting the data and below is the guide for performing data augmentation using Keras&nbsp;<code>ImageDataGenerator<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython wp-block-embed-geekpython\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"nheGPmK38m\"><a href=\"https:\/\/geekpython.in\/data-augmentation-in-deep-learning\">An Intuitive Guide On Data Augmentation In Deep Learning &#8211; Techniques With Examples<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;An Intuitive Guide On Data Augmentation In Deep Learning &#8211; Techniques With Examples&#8221; &#8212; GeekPython\" src=\"https:\/\/geekpython.in\/data-augmentation-in-deep-learning\/embed#?secret=gjxIqPc8Aj#?secret=nheGPmK38m\" data-secret=\"nheGPmK38m\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-model\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-creating-model\"><\/a>Creating model<\/h1>\n\n\n\n<p>Before fitting the training and validation data into the model, the deep learning model&nbsp;<strong>MobileNet<\/strong>&nbsp;needs to be fine-tuned by adding the output layer, removing the unnecessary layers, and making some layers non-trainable for better accuracy.<\/p>\n\n\n\n<p>The following code will download the&nbsp;<strong>MobileNet<\/strong>&nbsp;model from Keras and store it in the&nbsp;<code>mobile<\/code>&nbsp;variable. You need to connect to the internet the first time running the following cell.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">mobile = tf.keras.applications.mobilenet.MobileNet()<\/pre><\/div>\n\n\n\n<p>If you run the following code, then you&#8217;ll see the summary of the model in which you can see the series of layers of neural networks.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">mobile.summary()<\/pre><\/div>\n\n\n\n<p>Now, we&#8217;ll add the dense output layer with a unit of&nbsp;<strong>10<\/strong>&nbsp;into the model because there will be 10 outputs&nbsp;<strong>from 0 to 9<\/strong>. Additionally, we removed the last six layers from the&nbsp;<strong>MobileNet<\/strong>&nbsp;model.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Removing last 6 layers and adding our output layer\nx = mobile.layers[-6].output\noutput = Dense(units=10, activation='softmax')(x)<\/pre><\/div>\n\n\n\n<p>Then we added all the input and output layers into the model.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">model = Model(inputs=mobile.input, outputs=output)<\/pre><\/div>\n\n\n\n<p>Now, we are making the last&nbsp;<strong>23 layers<\/strong>&nbsp;to be non-trainable and it&#8217;s an arbitrary number. This specific number has been achieved through many trials and errors. The sole purpose of this code is to better the accuracy by making some layers non-trainable.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># We are not going to train last 23 layers and it's an arbitrary number\nfor layer in mobile.layers[:-23]:\n    layer.trainable=False<\/pre><\/div>\n\n\n\n<p>If you see the summary of the fine-tuned model, then you&#8217;ll notice some differences in the number of non-trainable params and layers as compared to the original summary that you&#8217;ve seen earlier.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">model.summary()<\/pre><\/div>\n\n\n\n<p>Now we are compiling the optimizer named&nbsp;<code>Adam<\/code>&nbsp;with learning rate&nbsp;<code>0.0001<\/code>&nbsp;along with&nbsp;<strong>loss function<\/strong>&nbsp;and&nbsp;<strong>metrics<\/strong>&nbsp;to measure the accuracy of the model.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])<\/pre><\/div>\n\n\n\n<p>It&#8217;s time when the model is ready to train on the training and validation data. In the following code, we fed the training and validation data along with the number of epochs. The&nbsp;<code>verbose<\/code>&nbsp;is nothing but to display the accuracy progress and you can specify the numbers 0, 1, and 2.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Running 10 epochs\nmodel.fit(x=train_batches, validation_data=valid_batches, epochs=10, verbose=2)<\/pre><\/div>\n\n\n\n<p>If you will run the above cell, then you&#8217;ll see every step of the epoch with training data&#8217;s loss and accuracy. You&#8217;ll see the same for validation data.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"329\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/3-4.png\" alt=\"Steps of the epochs showing accuracy and validation accuracy\" class=\"wp-image-951\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/3-4.png 737w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/3-4-300x134.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-saving-the-model\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-saving-the-model\"><\/a>Saving the model<\/h1>\n\n\n\n<p>The model is now ready with an accuracy score of&nbsp;<strong>99 percentile<\/strong>. Now keep one thing in mind this model is probably overfitted and might not perform well for images other than images of the dataset.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Checking if the model exist otherwise save the model\nif os.path.isfile(\"D:\/SACHIN\/Models\/Hand-Sign-Digit-Language\/digit_model.h5\") is False:\n    model.save(\"D:\/SACHIN\/Models\/Hand-Sign-Digit-Language\/digit_model.h5\")<\/pre><\/div>\n\n\n\n<p>The above code will check if there is already a copy of the model and if not then the&nbsp;<code>save<\/code>&nbsp;function will save the model in the specified path.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-testing-the-model\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-testing-the-model\"><\/a>Testing the model<\/h1>\n\n\n\n<p>The model is trained and ready for recognizing images. This segment will cover&nbsp;<strong>loading the model<\/strong>&nbsp;and making functions for&nbsp;<strong>image preparation<\/strong>,&nbsp;<strong>predicting results,<\/strong>&nbsp;and&nbsp;<strong>displaying and printing the predicted result<\/strong>.<\/p>\n\n\n\n<p>Before writing any code, some libraries that will be used further in the code need to be imported.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-loading-the-custom-model\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-loading-the-custom-model\"><\/a>Loading the custom model<\/h3>\n\n\n\n<p>The prediction on images will be made using the model that was created above using the transfer learning technique, so first, it needs to be loaded for usage.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">my_model = load_model(\"D:\/SACHIN\/Models\/Hand-Sign-Digit-Language\/digit_model.h5\")<\/pre><\/div>\n\n\n\n<p>Using the&nbsp;<code>load_model<\/code>&nbsp;function, the model has been loaded from the specified path and stored inside the&nbsp;<code>my_model<\/code>&nbsp;variable for further use ahead in the code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-preparing-input-image\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-preparing-input-image\"><\/a>Preparing input image<\/h3>\n\n\n\n<p>Before giving any image for predicting or recognizing to the model, it needs to be in the format that the model wants it.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">def preprocess_img(img_path):\n    open_img = image.load_img(img_path, target_size=(224, 224))\n    img_arr = image.img_to_array(open_img)\/255.0\n    img_reshape = img_arr.reshape(1, 224,224,3)\n    return img_reshape<\/pre><\/div>\n\n\n\n<p>First, we defined a function&nbsp;<code>preprocess_img<\/code>&nbsp;that takes the path of the image and then we loaded that image using the&nbsp;<code>load_img<\/code>&nbsp;function from the&nbsp;<code>image<\/code>&nbsp;utility and set the target size of&nbsp;<code>224x224<\/code>, then converted that image into an array and divided that array by&nbsp;<code>255.0<\/code>&nbsp;which converted the pixels of the image between&nbsp;<code>0 and 1<\/code>&nbsp;and then reshaped that image array into the shape (<code>224, 224, 3<\/code>) and finally returned that reshaped image.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-predicting-function\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-predicting-function\"><\/a>Predicting function<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">def predict_result(predict):\n    pred = my_model.predict(predict)\n    return np.argmax(pred[0], axis=-1)<\/pre><\/div>\n\n\n\n<p>Here, we defined a function&nbsp;<code>predict_result<\/code>&nbsp;that takes predict argument which is basically a preprocessed image and then used the&nbsp;<code>predict<\/code>&nbsp;function to predict the result and then returned the maximum value from the predicted result.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython wp-block-embed-geekpython\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"nfmE2tcVx2\"><a href=\"https:\/\/geekpython.in\/numpy-argmax-function-in-python\">Understanding numpy.argmax() Function In Python<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Understanding numpy.argmax() Function In Python&#8221; &#8212; GeekPython\" src=\"https:\/\/geekpython.in\/numpy-argmax-function-in-python\/embed#?secret=vEqHEJXYji#?secret=nfmE2tcVx2\" data-secret=\"nfmE2tcVx2\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-display-and-predict-image\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-display-and-predict-image\"><\/a>Display and predict image<\/h3>\n\n\n\n<p>First, there&#8217;ll be a function that will take a path of the image and then display the image and the predicted result.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Function to display and predicting the Image\ndef display_and_predict(img_path_input):\n    display_img = Image.open(img_path_input)\n    plt.imshow(display_img)\n    plt.show()\n    img = preprocess_img(img_path_input)\n    pred = predict_result(img)\n    print(\"Prediction: \", pred)<\/pre><\/div>\n\n\n\n<p>The above function&nbsp;<code>display_and_predict<\/code>&nbsp;takes the path of an image and used the&nbsp;<code>Image.open<\/code>&nbsp;function from the&nbsp;<code>PIL<\/code>&nbsp;library to open the image and then used the&nbsp;<code>matplotlib<\/code>&nbsp;library to display the image, and then for printing the predicted result the image is passed to the&nbsp;<code>preprocess_img<\/code>&nbsp;function and then used the&nbsp;<code>predict_result<\/code>&nbsp;function to obtain the result and finally printed it.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">img_input = input(\"Enter the path of an image: \")\ndisplay_and_predict(img_input)<\/pre><\/div>\n\n\n\n<p>If you will run the above cell and enter the path of the image from the dataset, then you&#8217;ll get the desired output.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"373\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-4-1024x373.png\" alt=\"Prediction result\" class=\"wp-image-952\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-4-1024x373.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-4-300x109.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-4-768x280.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-4.png 1181w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The model is successfully created using the transfer learning technique without writing any series of layers of neural networks.<\/p>\n\n\n\n<p>The model can be used for making the web app for recognizing images and here&#8217;s how you can implement the model into the Flask app.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython wp-block-embed-geekpython\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"iOSywRQHCj\"><a href=\"https:\/\/geekpython.in\/flask-app-for-image-recognition\">Build Flask App For Image Recognition Using Deep Learning Model<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Build Flask App For Image Recognition Using Deep Learning Model&#8221; &#8212; GeekPython\" src=\"https:\/\/geekpython.in\/flask-app-for-image-recognition\/embed#?secret=4GnxJLYJ2l#?secret=iOSywRQHCj\" data-secret=\"iOSywRQHCj\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>The article covers the making of a custom deep learning model using the pre-trained model or transfer learning.<\/p>\n\n\n\n<p>You&#8217;ve learned each step involved in creating a full-fledged deep-learning model. The steps you&#8217;ve seen are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Preparing the dataset<\/strong><\/li>\n\n\n\n<li><strong>Preprocessing the data<\/strong><\/li>\n\n\n\n<li><strong>Creating the model<\/strong><\/li>\n\n\n\n<li><strong>Saving the custom model<\/strong><\/li>\n\n\n\n<li><strong>Testing the custom model<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Get the complete source code on&nbsp;<a target=\"_blank\" href=\"https:\/\/github.com\/Sachin-crypto\/Image-Recognition-DL-Model\" rel=\"noreferrer noopener\"><strong>GitHub<\/strong><\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might like if you like this article<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/render-images-from-flask\" rel=\"noreferrer noopener\">Upload and display static and dynamic images using Flask<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow\" rel=\"noreferrer noopener\">NumPy and TensorFlow have a common function<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/contact-form-for-your-blog-get-emails-and-save-in-db-dynamically\" rel=\"noreferrer noopener\">Create a dynamic contact form that will save a message in database and will send you a mail<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit\" rel=\"noreferrer noopener\">Build a Covid-19 EDA and Visualization Streamlit app<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/asyncio-how-to-use-asyncawait-in-python\" rel=\"noreferrer noopener\">Use asynchronous programming in Python using asyncio module<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Transfer learning is used in machine learning and is a method in which already-trained or pre-trained neural networks are present and these pre-trained neural networks are trained using millions of data points. This technique is currently most famous for training deep neural networks because its performance is great when training deep neural networks with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":954,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,4],"tags":[54,12,31,55],"class_list":["post-942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-dl","tag-keras","tag-python","tag-python3","tag-tensorflow","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=942"}],"version-history":[{"count":8,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":1331,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/942\/revisions\/1331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/954"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}