{"id":86296,"date":"2022-09-07T09:00:13","date_gmt":"2022-09-07T03:30:13","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=86296"},"modified":"2026-06-03T16:24:46","modified_gmt":"2026-06-03T10:54:46","slug":"deepfake-detection-using-cnn","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/","title":{"rendered":"DeepFake Detection using Convolutional Neural Networks"},"content":{"rendered":"<p>With this Machine Learning Project, we will be building a deepfake detection system. The deepfake detection system is used to detect whether the image is a real image or a deepfake image. This can be used in a lot of places. This can help police to solve crimes. Deepfakes are a very serious issue that needs to be solved.<\/p>\n<p>So, let\u2019s build this deepfake detection system using convolutional neural networks.<\/p>\n<h3>Deepfake Detection System<\/h3>\n<p>So, what is this deepfake Detection System?<\/p>\n<p>A deepfake detection system is a system that will predict whether an image is a real image or it is a deepfake image.<\/p>\n<p><strong>What is a deepfake image?<\/strong><\/p>\n<p>A deepfake image is an image in which a person\u2019s face is swapped with someone else\u2019s using neural networks. Deepfakes can be used by criminals to do crimes by using someone else\u2019s face. It can be used by news media to spread fake news. It can be used by bad people and they can swap your face in a porn video and then viral it which will just take seconds to destroy your life. That\u2019s why a deepfake detection system is really necessary.<\/p>\n<p>So, let&#8217;s build our system with these convolutional neural networks.<\/p>\n<h3>Convolutional Neural Networks<\/h3>\n<p>A neural network is an algorithm of Machine Learning which is at the heart of deep learning algorithms. These algorithms are used in a lot of machine learning algorithms. When working with image classification, these appear to outperform in every parameter. They are very easy to train. With just a few lines of code, you can create neural networks using the inbuilt functions of the Keras library.<\/p>\n<p>Convolutional Neural Networks are composed of node layers, convolution layer, pooling layer, hidden layer, and output layer. Our image passes through all these layers and every step has its own importance. It is in these layers where all the feature extraction of the image occurs. Each layer contains nodes that have weights within them. These weights get updated with each backward pass.<\/p>\n<p>There are two passes in the neural networks.<\/p>\n<ol>\n<li><strong>Forward Pass<\/strong> &#8211; It is the propagation from the input layer to the output layer. It refers to the calculation of variables as we move towards the output layer. At the output layer, the data loss is calculated as an error. With the help of this error, weights on the nodes are updated.<\/li>\n<li><strong>Backward Pass<\/strong> &#8211; It is the propagation from the output layer to the input layer. It is the correcting and updating of the value of the weights based on the error rate that resulted in the forward pass.<\/li>\n<\/ol>\n<p>CNN is highly used in image classification problems and object recognition problems because it is very good in feature extraction leveraging principles of linear algebra, specifically matrix multiplication to identify patterns in the image but requires a lot of computation power to do the tasks.<\/p>\n<h3>The Model Architecture<\/h3>\n<p>We are using the Convolutional Neural Networks to build the model. They have superior performance in comparison to other neural networks in image, speech, and audio inputs. CNN&#8217;s are formed using 3 types of layers.<\/p>\n<p>With each layer, the complexity of the neural networks increases. The initial layer of the neural network is useful for identifying the color and edges in the image. But as the network goes deeper into the layers, it starts identifying more portions of the image and starts identifying objects, shapes, etc.<\/p>\n<p><strong>Three Layers of the Convolutional Neural Networks:<\/strong><\/p>\n<ol>\n<li><strong>Convolution Layer<\/strong>: This layer is the building block of CNN. This is the layer where the majority of the computations occur. This layer has 3 inputs. The first is input data. The second is a filter and the third is a feature map. The convolution process occurs on input data that is the image using filters. Filters are nothing but a 3&#215;3 matrix that is iterated over the input data which is also a matrix. As the filter is fed over the area of the input matrix, there is an activation function that calculates the result of the filter and the area of the image. The output is then stored in the feature matrix.<\/li>\n<li><strong>Pooling Layer<\/strong>: This layer is used for downsampling that reduces the number of parameters in our input image. It works in a similar way to the convolution layer. The only difference is that the filters in convolution layers contain some weights on which computation is performed by activation function whereas in the pooling layer we either take the max value in the area of the input matrix or we take the average value in the area of the matrix. There are two types of pooling:\n<ol>\n<li><strong>Max Pooling<\/strong>&#8211; In this pooling, we take the maximum of all the values in the patch of the matrix.<\/li>\n<li><strong>Average Pooling<\/strong>&#8211; In this pooling, we take the average of all the values in the patch of the matrix.<\/li>\n<\/ol>\n<\/li>\n<li><strong>Fully Connected Layer<\/strong>&#8211; In this layer, finally all the calculations we have done in the previous steps will pay off in this step. This layer finally classifies the image. In this layer, our input matrix is flattened and it is passed through hidden layers of the neural networks. The previous Layers use ReLu functions for all computation while this layer uses the Softmax function for multiclassification problems and the Sigmoid function for binary classification problem.<\/li>\n<\/ol>\n<p>So, this is how our model is working.<\/p>\n<h3>Project Prerequisites<\/h3>\n<p>It is necessary that you have Python 3.6 installed on your computer as well as Anaconda.<\/p>\n<p>The required modules for this project are &#8211;<\/p>\n<ol>\n<li>Numpy &#8211; pip install numpy<\/li>\n<li>Tensorflow &#8211; pip install tensorflow<\/li>\n<li>Keras &#8211; pip install keras<\/li>\n<\/ol>\n<p>We only need that for our Convolutional neural networks. The Tensorflow module contains all the functions required for creating an optimized Convolutional Neural Network.<\/p>\n<h3>DeepFake Detection Project<\/h3>\n<p>We have already created the dataset for you. It can be downloaded from the link below. It contains three folders for training, testing, and verifying the model that we have built is working or not. Please download deep fake detection machine learning code and dataset from the following link: <a href=\"https:\/\/drive.google.com\/file\/d\/1wkBzqBeQOit93OfgZMQjHi7w7zwuHXa4\/view?usp=sharing\"><strong>DeepFake Detection Project<\/strong><\/a><\/p>\n<h3>Steps to Implement DeepFake Detection Project<\/h3>\n<p>1. As part of the project, import the modules you need.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\nimport tensorflow as tf\r\nfrom keras.preprocessing.image import ImageDataGenerator\r\n<\/pre>\n<p>2. Using the imageDataGenerator described above, we are reading the training dataset.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">train_datagen = ImageDataGenerator(\r\n    rescale=1.\/255,\r\n    shear_range = 0.2,\r\n    zoom_range = 0.2,\r\n    horizontal_flip=True)\r\ntraining_set = train_datagen.flow_from_directory('train',target_size=(224,224),batch_size=32,shuffle=True,class_mode='binary')\r\n<\/pre>\n<p>3. This is just reading the dataset.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">test_datagen = ImageDataGenerator(rescale=1.\/255)\r\n\r\ntest_set = test_datagen.flow_from_directory('test',target_size=(224,224),batch_size=16,shuffle=False,class_mode='binary')\r\n<\/pre>\n<p>4. Our neural network is being built here. Tensorflow provides all the functions for easily creating neural networks. This is merely a trigger. Clearly, all the functions explained above are called here &amp; nothing is particularly difficult.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cnn = tf.keras.models.Sequential()\r\n \r\ncnn.add(tf.keras.layers.Conv2D(filters=224 , kernel_size=3 , activation='relu' , input_shape=[224,224,3]))\r\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))\r\n \r\ncnn.add(tf.keras.layers.Conv2D(filters=224 , kernel_size=3 , activation='relu' ))\r\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2 , strides=2))\r\n \r\ncnn.add(tf.keras.layers.Dropout(0.5))\r\ncnn.add(tf.keras.layers.Flatten())\r\n\r\ncnn.add(tf.keras.layers.Dense(units=128, activation='relu'))\r\ncnn.add(tf.keras.layers.Dense(units=1 , activation='sigmoid'))\r\ncnn.compile(optimizer = 'Adam' , loss = 'binary_crossentropy' , metrics = ['accuracy'])\r\n<\/pre>\n<p>5. As we trigger the fit function, the training dataset will be fitted into our neural networks, and the test dataset will be used to test the network.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cnn.fit(x = training_set , validation_data = test_set , epochs = 50)<\/pre>\n<p>6. As a result of the successful training and testing of our model. Here are some examples to predict. Using this code, we load the image through Keras&#8217; load_img function and then we convert it into an array to be passed through neural networks.<br \/>\nTo predict the outcome, we called cnn.predict.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from keras.preprocessing import image\r\ntest_image = tf.keras.utils.load_img('prediction\/deepfake1.jpg',target_size=(224,224))\r\ntest_image = tf.keras.utils.img_to_array(test_image)\r\ntest_image = np.expand_dims(test_image,axis=0)\r\n \r\nresult = cnn.predict(test_image)\r\ntraining_set.class_indices\r\n\r\nprint(result)\r\nplt.imshow(image)\r\n<\/pre>\n<h3>Summary<\/h3>\n<p>In this Machine Learning project, we built a deepfake detection system. We used Convolutional Neural Networks to build our neural network in our project. We hope you learn how to make a deepfake detection system.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With this Machine Learning Project, we will be building a deepfake detection system. The deepfake detection system is used to detect whether the image is a real image or a deepfake image. This can&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":86297,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210],"tags":[2598,4770,4771,4772,203,204,205],"class_list":["post-86296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-deep-learning-project","tag-deepfake-detection-cnn","tag-deepfake-detection-system","tag-deepfake-machine-learning","tag-machine-learning","tag-machine-learning-project","tag-machine-learning-project-for-beginners"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>DeepFake Detection using Convolutional Neural Networks - TechVidvan<\/title>\n<meta name=\"description\" content=\"DeepFake Detection using Deep Learning libraries like Tensorflow, Keras, NumPy to detect whether image is real or fake\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DeepFake Detection using Convolutional Neural Networks - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"DeepFake Detection using Deep Learning libraries like Tensorflow, Keras, NumPy to detect whether image is real or fake\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-07T03:30:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-03T10:54:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2022\/08\/deepfake-detection-using-convolutional-neural-networks.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DeepFake Detection using Convolutional Neural Networks - TechVidvan","description":"DeepFake Detection using Deep Learning libraries like Tensorflow, Keras, NumPy to detect whether image is real or fake","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:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/","og_locale":"en_US","og_type":"article","og_title":"DeepFake Detection using Convolutional Neural Networks - TechVidvan","og_description":"DeepFake Detection using Deep Learning libraries like Tensorflow, Keras, NumPy to detect whether image is real or fake","og_url":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2022-09-07T03:30:13+00:00","article_modified_time":"2026-06-03T10:54:46+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2022\/08\/deepfake-detection-using-convolutional-neural-networks.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"DeepFake Detection using Convolutional Neural Networks","datePublished":"2022-09-07T03:30:13+00:00","dateModified":"2026-06-03T10:54:46+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/"},"wordCount":1208,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2022\/08\/deepfake-detection-using-convolutional-neural-networks.webp","keywords":["deep learning project","deepfake detection cnn","deepfake detection system","deepfake machine learning","machine learning","machine learning project","machine learning project for beginners"],"articleSection":["Machine Learning Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/","url":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/","name":"DeepFake Detection using Convolutional Neural Networks - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2022\/08\/deepfake-detection-using-convolutional-neural-networks.webp","datePublished":"2022-09-07T03:30:13+00:00","dateModified":"2026-06-03T10:54:46+00:00","description":"DeepFake Detection using Deep Learning libraries like Tensorflow, Keras, NumPy to detect whether image is real or fake","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2022\/08\/deepfake-detection-using-convolutional-neural-networks.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2022\/08\/deepfake-detection-using-convolutional-neural-networks.webp","width":1200,"height":628,"caption":"deepfake detection using convolutional neural networks"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/deepfake-detection-using-cnn\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"DeepFake Detection using Convolutional Neural Networks"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=86296"}],"version-history":[{"count":1,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86296\/revisions"}],"predecessor-version":[{"id":448172,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86296\/revisions\/448172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/86297"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=86296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=86296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=86296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}