TensorFlow 101: Introduction to Deep Learning for Python Within TensorFlow

Overview

TensorFlow 101: Introduction to Deep Learning

Stars License

I have worked all my life in Machine Learning, and I've never seen one algorithm knock over its benchmarks like Deep Learning - Andrew Ng

This repository includes deep learning based project implementations I've done from scratch. You can find both the source code and documentation as a step by step tutorial. Model structrues and pre-trained weights are shared as well.

Facial Expression Recognition Code, Tutorial

This is a custom CNN model. Kaggle FER 2013 data set is fed to the model. This model runs fast and produces satisfactory results. It can be also run real time as well.

We can run emotion analysis in real time as well Real Time Code, Video

Face Recognition Code, Tutorial

Face recognition is mainly based on convolutional neural networks. We feed two face images to a CNN model and it returns a multi-dimensional vector representations. We then compare these representations to determine these two face images are same person or not.

You can find the most popular face recognition models below.

Model Creator LFW Score Code Tutorial
VGG-Face The University of Oxford 98.78 Code Tutorial
FaceNet Google 99.65 Code Tutorial
DeepFace Facebook - Code Tutorial
OpenFace Carnegie Mellon University 93.80 Code Tutorial
DeepID The Chinese University of Hong Kong - Code Tutorial
Dlib Davis E. King 99.38 Code Tutorial
OpenCV OpenCV Foundation - Code Tutorial
OpenFace in OpenCV Carnegie Mellon University 92.92 Code Tutorial
SphereFace Georgia Institute of Technology 99.30 Code Tutorial
ArcFace Imperial College London 99.40 Code Tutorial

All of those state-of-the-art face recognition models are wrapped in deepface library for python. You can build and run them with a few lines of code. To have more information, please visit the repo of the library.

Real Time Deep Face Recognition Implementation Code, Video

These are the real time implementations of the common face recognition models we've mentioned in the previous section. VGG-Face has the highest face recognition score but it comes with the high complexity among models. On the other hand, OpenFace is a pretty model and it has a close accuracy to VGG-Face but its simplicity offers high speed than others.

Model Creator Code Demo
VGG-Face Oxford University Code Video
FaceNet Google Code Video
DeepFace Facebook Code Video
OpenFace Carnegie Mellon University Code Video

Large Scale Face Recognition

Face recognition requires to apply face verification several times. It has a O(n) time complexity and it would be problematic for very large scale data sets (millions or billions level data). Herein, if you have a really strong database, then you use relational databases and regular SQL. Besides, you can store facial embeddings in nosql databases. In this way, you can have the power of the map reduce technology. Besides, approximate nearest neighbor (a-nn) algorithm reduces time complexity dramatically. Spotify Annoy, Facebook Faiss and NMSLIB are amazing a-nn libraries. Besides, Elasticsearch wraps NMSLIB and it also offers highly scalablity. You should build and run face recognition models within those a-nn libraries if you have really large scale data sets.

Library Algorithm Tutorial Code Demo
Spotify Annoy a-nn Tutorial - Video
Facebook Faiss a-nn Tutorial - -
NMSLIB a-nn Tutorial Code -
Elasticsearch a-nn Tutorial Code Video
mongoDB k-NN Tutorial Code -
Cassandra k-NN Tutorial Code Video
Redis k-NN Tutorial Code Video
Hadoop k-NN Tutorial Code -
Relational Database k-NN Tutorial Code -
Neo4j Graph k-NN Tutorial Code Video

Apparent Age and Gender Prediction Tutorial, Code for age, Code for gender

We've used VGG-Face model for apparent age prediction this time. We actually applied transfer learning. Locking the early layers' weights enables to have outcomes fast.

We can run age and gender prediction in real time as well Real Time Code, Video

Celebrity You Look-Alike Face Recognition Code, Tutorial

Applying VGG-Face recognition technology for imdb data set will find your celebrity look-alike if you discard the threshold in similarity score.

This can be run in real time as well Real Time Code, Video

Race and Ethnicity Prediction Tutorial, Code, Real Time Code, Video

Ethnicity is a facial attribute as well and we can predict it from facial photos. We customize VGG-Face and we also applied transfer learning to classify 6 different ethnicity groups.

Beauty Score Prediction Tutorial, Code

South China University of Technology published a research paper about facial beauty prediction. They also open-sourced the data set. 60 labelers scored the beauty of 5500 people. We will build a regressor to find facial beauty score. We will also test the built regressor on a huge imdb data set to find the most beautiful ones.

Attractiveness Score Prediction Tutorial, Code

The University of Chicago open-sourced the Chicago Face Database. The database consists of 1200 facial photos of 600 people. Facial photos are also labeled with attractiveness and babyface scores by hundreds of volunteer markers. So, we've built a machine learning model to generalize attractiveness score based on a facial photo.

Making Arts with Deep Learning: Artistic Style Transfer Code, Tutorial, Video

What if Vincent van Gogh had painted Istanbul Bosporus? Today we can answer this question. A deep learning technique named artistic style transfer enables to transform ordinary images to masterpieces.

Autoencoder and clustering Code, Tutorial

We can use neural networks to represent data. If you design a neural networks model symmetric about the centroid and you can restore a base data with an acceptable loss, then output of the centroid layer can represent the base data. Representations can contribute any field of deep learning such as face recognition, style transfer or just clustering.

Convolutional Autoencoder and clustering Code, Tutorial

We can adapt same representation approach to convolutional neural networks, too.

Transfer Learning: Consuming InceptionV3 to Classify Cat and Dog Images in Keras Code, Tutorial

We can have the outcomes of the other researchers effortlessly. Google researchers compete on Kaggle Imagenet competition. They got 97% accuracy. We will adapt Google's Inception V3 model to classify objects.

Handwritten Digit Classification Using Neural Networks Code, Tutorial

We had to apply feature extraction on data sets to use neural networks. Deep learning enables to skip this step. We just feed the data, and deep neural networks can extract features on the data set. Here, we will feed handwritten digit data (MNIST) to deep neural networks, and expect to learn digits.

Handwritten Digit Recognition Using Convolutional Neural Networks with Keras Code, Tutorial

Convolutional neural networks are close to human brain. People look for some patterns in classifying objects. For example, mouth, nose and ear shape of a cat is enough to classify a cat. We don't look at all pixels, just focus on some area. Herein, CNN applies some filters to detect these kind of shapes. They perform better than conventional neural networks. Herein, we got almost 2% accuracy than fully connected neural networks.

Automated Machine Learning and Auto-Keras for Image Data Code, Model, Tutorial

AutoML concept aims to find the best network structure and hyper-parameters. Here, I've applied AutoML to facial expression recognition data set. My custom design got 57% accuracy whereas AutoML found a better model and got 66% accuracy. This means almost 10% improvement in the accuracy.

Explaining Deep Learning Models with SHAP Code, Tutorial

SHAP explains black box machine learning models and makes them transparent, explainable and provable.

Gradient Vanishing Problem Code Tutorial

Why legacy activation functions such as sigmoid and tanh disappear on the pages of the history?

How single layer perceptron works Code

This is the 1957 model implementation of the perceptron.

Face Alignment for Face Recognition Code, Tutorial

Google declared that face alignment increase its face recognition model accuracy from 98.87% to 99.63%. This is almost 1% accuracy improvement which means a lot for engineering studies.

Requirements

I have tested this repository on the following environments. To avoid environmental issues, confirm your environment is same as below.

>> import tensorflow as tf >>> print(tf.__version__) 1.9.0 >>> >>> import keras Using TensorFlow backend. >>> print(keras.__version__) 2.2.0 >>> >>> import cv2 >>> print(cv2.__version__) 3.4.4">
C:\>python --version
Python 3.6.4 :: Anaconda, Inc.

C:\>activate tensorflow

(tensorflow) C:\>python
Python 3.5.5 |Anaconda, Inc.| (default, Apr  7 2018, 04:52:34) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print(tf.__version__)
1.9.0
>>>
>>> import keras
Using TensorFlow backend.
>>> print(keras.__version__)
2.2.0
>>>
>>> import cv2
>>> print(cv2.__version__)
3.4.4

To get your environment up from zero, you can follow the instructions in the following videos.

Installing TensorFlow and Prerequisites Video

Installing Keras Video

Disclaimer

This repo might use some external sources. Notice that related tutorial links and comments in the code blocks cite references already.

Support

There are many ways to support a project - starring ⭐️ the GitHub repos is one.

Citation

Please cite tensorflow-101 in your publications if it helps your research. Here is an example BibTeX entry:

@misc{serengil2021tensorflow,
  abstract     = {TensorFlow 101: Introduction to Deep Learning for Python Within TensorFlow},
  author       = {Serengil, Sefik Ilkin},
  title        = {tensorflow-101},
  howpublished = {https://github.com/serengil/tensorflow-101},
  year         = {2021}
}

Licence

This repository is licensed under MIT license - see LICENSE for more details

Comments
  • Wrong cosine similarity results on face recognition

    Wrong cosine similarity results on face recognition

    First of all I would like to thank for the code, very well done and written.

    I used your code for face recognition (and using the model template provided by you, thank you) . And in my tests everything went as planned. But when I used it on my ip camera (640x480 resolution), the results for my face were confusing. Where can I be wrong? Can the results be wrong because the image I want to compare is in grayscale?

    The face I'd like to compare (face 1): Me

    The face of someone else (face 2): Other person

    My face (which I'd like to match w/ the face 1): Me in ip camera

    Other person "cosine similarity": 0.4480170012 Me "cosine similarity": 0.6674099863

    I wish you would answer me even if you could not help me. I do not know what to do anymore.

    Thank youuu!!

    opened by cucabr 10
  • SystemError: unknown opcode while running model_from_json(open(

    SystemError: unknown opcode while running model_from_json(open("facenet_model.json", "r").read())

    Hi Sefik,

    first of all, thanks for your work. Unfortunately, I get this error when I try to run your model, in particular command "model from jason":

    ~/miniconda3/lib/python3.6/site-packages/keras/layers/core.py in scaling(x, scale) 24 from ..utils.generic_utils import has_arg 25 from ..utils import conv_utils ---> 26 from ..legacy import interfaces 27 28

    SystemError: unknown opcode

    I have python 3.6, tensorflow 1.12.0 and keras 2.2.4.

    What could be the issue?

    P.S. I tried to leave a comment on the related post on your site but I keep getting mistaken for a Bot.

    opened by cedoard 9
  • help me in improving accuracy

    help me in improving accuracy

    I followed your ideas to train a model. After training when I test with my own data my predictions are not upto the mark. Please share me your accuracy level. print('Test accuracy:', 100*score[1])

    opened by balajib363 6
  • Does your python file named

    Does your python file named "openface-real-time" uses single image for each person?

    I am not an expert but from the following code what i have concluded is that you can only use 1 image per employee. As you cannot put multiple images of same person under same name. I am trying to built a facial recognition system and i can train the person before hand with 50 images. Kindly tell me about this so i can change code according to my need.

    ` #put your employee pictures in this path as name_of_employee.jpg employee_pictures = "database/"

    employees = dict()

    for file in listdir(employee_pictures): employee, extension = file.split(".") img = preprocess_image('database/%s.jpg' % (employee)) representation = model.predict(img)[0,:]

    employees[employee] = representation
    

    print("employee representations retrieved successfully") `

    opened by HananAhmadHunain 4
  • why only detect the faces? Not recognize.

    why only detect the faces? Not recognize.

    Brother, I used your real-time face recognition of the deep face. Its working fine but it only detects the faces not recognise the faces via my database like your video? Give me a solution brother.

    opened by TasilNimashan 3
  • Adding threshold to the Elasticsearch Query

    Adding threshold to the Elasticsearch Query

    Hi,

    I am little confused about how the l2norm function is executing for face recognition as we haven't given our less than threshold in the query. Can you please explain where it is being done ?

    query = {
        "size": 5,
        "query": {
        "script_score": {
            "query": {
                "match_all": {}
            },
            "script": {
                #"source": "cosineSimilarity(params.queryVector, 'title_vector') + 1.0",
                "source": "1 / (1 + l2norm(params.queryVector, 'title_vector'))", #euclidean distance
                "params": {
                    "queryVector": list(target_embedding)
                }
            }
        }
    }}
    
    question 
    opened by dexception 2
  • Getting Error at 6th Cell of LFW.ipynb

    Getting Error at 6th Cell of LFW.ipynb

    I'm getting a value error at the 6th cell of LFW.ipynb. I'm getting this error both in google colab and my local machine. Please solve this issue.

    The error is:

    ValueError Traceback (most recent call last) in 11 12 #obj = DeepFace.verify(img1, img2, model_name = 'VGG-Face', model = vgg_model) ---> 13 obj = DeepFace.verify(img1, img2, model_name = 'Dlib', model = dlib_model, distance_metric = 'euclidean') 14 prediction = obj["verified"] 15 predictions.append(prediction)

    ~/.local/lib/python3.8/site-packages/deepface/DeepFace.py in verify(img1_path, img2_path, model_name, distance_metric, model, enforce_detection, detector_backend) 152 , detector_backend = detector_backend) 153 --> 154 img2 = functions.preprocess_face(img=img2_path 155 , target_size=(input_shape_y, input_shape_x) 156 , enforce_detection = enforce_detection

    ~/.local/lib/python3.8/site-packages/deepface/commons/functions.py in preprocess_face(img, target_size, grayscale, enforce_detection, detector_backend) 454 455 if enforce_detection == True: --> 456 raise ValueError("Detected face shape is ", img.shape,". Consider to set enforce_detection argument to False.") 457 else: #restore base image 458 img = base_img.copy()

    ValueError: ('Detected face shape is ', (0, 92, 3), '. Consider to set enforce_detection argument to False.')

    question 
    opened by FahimT5 2
  • vgg-face.ipynb how to evaluate on a dataset

    vgg-face.ipynb how to evaluate on a dataset

    Could you share the script to do the evaluation of the model on dataset. My doubt is in order to find the accuracy on a dataset, do we take every pair of embedding in the dataset and check if the model can correctly detect as the same person or not? Or is there some other standard approach to evaluate the face recongition models which are designed in one shot learning fashion?

    opened by dingusagar 2
  • How to train network for custom dataset?

    How to train network for custom dataset?

    I've used your pre-trained weights for face recognition and it seems to work well. Thank you. Now, I am interested to use a similar network to verify if two images match or not (no faces). Do you have any idea about how could I train my network?

    In your code for face recognition, an image is used as input for network and a vector is generated as output. Both images (faces) are fed to network such that -both- generated vectors are then compared with cosine or euclidean similarity.

    But in my case I just have two images that match or not.

    opened by jcgarciaca 2
  • Insufficient resources

    Insufficient resources

    I am trying to run this code for a school project, and am using AWS SageMaker as my laptop does not have enough resources. However, even with 8 cores and 64 GB of RAM, each epoch is estimated to take about an hour to run. Any suggestions to speed this up?

    question 
    opened by spujet314 1
  • Using DeepFace on a Database

    Using DeepFace on a Database

    Wasn't sure where else to ask this so I made an issue here since it has the source code of this article, Is there a way to use your deepface library to get the gender, age, and ethnicity estimations from each person in the database? https://sefiks.com/2021/02/06/deep-face-recognition-with-sql/?unapproved=3034&moderation-hash=f5f6fc54c7e8ebd2af7f1bd3d1283caa

    question 
    opened by CrimsonMiralis 1
  • Pre-trained weights of SphereFace

    Pre-trained weights of SphereFace

    Hi, Could you please provide the pre-trained weights that you used in your SphereFace tutorial? Their link (Weights: https://drive.google.com/open?id=0B_geeR2lTMegb2F6dmlmOXhWaVk) requires permission and they are not responding to the permission request.

    opened by HamDan1999 0
Owner
Sefik Ilkin Serengil
👨‍💻Software Engineer 🎓GSU alumni ⌨️Blogger 🏠Istanbulite 💬Code wins arguments
Sefik Ilkin Serengil
aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)

Bayesian Methods for Hackers Using Python and PyMC The Bayesian method is the natural approach to inference, yet it is hidden from readers behind chap

Cameron Davidson-Pilon 25.1k Jan 2, 2023
Introduction to CPM

CPM CPM is an open-source program on large-scale pre-trained models, which is conducted by Beijing Academy of Artificial Intelligence and Tsinghua Uni

Tsinghua AI 136 Dec 23, 2022
Official code for 'Robust Siamese Object Tracking for Unmanned Aerial Manipulator' and offical introduction to UAMT100 benchmark

SiamSA: Robust Siamese Object Tracking for Unmanned Aerial Manipulator Demo video ?? Our video on Youtube and bilibili demonstrates the evaluation of

Intelligent Vision for Robotics in Complex Environment 12 Dec 18, 2022
Introduction to AI assignment 1 HCM University of Technology, term 211

Sokoban Bot Introduction to AI assignment 1 HCM University of Technology, term 211 Abstract This is basically a solver for Sokoban game using Breadth-

Quang Minh 4 Dec 12, 2022
CS50's Introduction to Artificial Intelligence Test Scripts

CS50's Introduction to Artificial Intelligence Test Scripts ??‍♂️ What's this? ??‍♀️ This repository contains Python scripts to automate tests for mos

Jet Kan 2 Dec 28, 2022
[AI6101] Introduction to AI & AI Ethics is a core course of MSAI, SCSE, NTU, Singapore

[AI6101] Introduction to AI & AI Ethics is a core course of MSAI, SCSE, NTU, Singapore. The repository corresponds to the AI6101 of Semester 1, AY2021-2022, starting from 08/2021. The instructors of this course are Prof. Bo An, Prof. Yu Han, and Dr. Melvin Chen.

AccSrd 1 Sep 22, 2022
This repository is related to an Arabic tutorial, within the tutorial we discuss the common data structure and algorithms and their worst and best case for each, then implement the code using Python.

Data Structure and Algorithms with Python This repository is related to the Arabic tutorial here, within the tutorial we discuss the common data struc

Mohamed Ayman 33 Dec 2, 2022
Implementation of 'lightweight' GAN, proposed in ICLR 2021, in Pytorch. High resolution image generations that can be trained within a day or two

512x512 flowers after 12 hours of training, 1 gpu 256x256 flowers after 12 hours of training, 1 gpu Pizza 'Lightweight' GAN Implementation of 'lightwe

Phil Wang 1.5k Jan 2, 2023
Rayvens makes it possible for data scientists to access hundreds of data services within Ray with little effort.

Rayvens augments Ray with events. With Rayvens, Ray applications can subscribe to event streams, process and produce events. Rayvens leverages Apache

CodeFlare 32 Dec 25, 2022
Implementation for our AAAI2021 paper (Entity Structure Within and Throughout: Modeling Mention Dependencies for Document-Level Relation Extraction).

SSAN Introduction This is the pytorch implementation of the SSAN model (see our AAAI2021 paper: Entity Structure Within and Throughout: Modeling Menti

benfeng 69 Nov 15, 2022
Manage the availability of workspaces within Frappe/ ERPNext (sidebar) based on user-roles

Workspace Permissions Manage the availability of workspaces within Frappe/ ERPNext (sidebar) based on user-roles. Features Configure foreach workspace

Patrick.St. 18 Sep 26, 2022
This project deals with the detection of skin lesions within the ISICs dataset using YOLOv3 Object Detection with Darknet.

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Skin Lesion detection using YOLO This project deal

Lalith Veerabhadrappa Badiger 1 Nov 22, 2021
This application is the basic of automated online-class-joiner(for YıldızEdu) within the right time. Gets the ZOOM link by scheduled date and time.

This application is the basic of automated online-class-joiner(for YıldızEdu) within the right time. Gets the ZOOM link by scheduled date and time.

215355 1 Dec 16, 2021
[SIGIR22] Official PyTorch implementation for "CORE: Simple and Effective Session-based Recommendation within Consistent Representation Space".

CORE This is the official PyTorch implementation for the paper: Yupeng Hou, Binbin Hu, Zhiqiang Zhang, Wayne Xin Zhao. CORE: Simple and Effective Sess

RUCAIBox 26 Dec 19, 2022
FTIR-Deep Learning - FTIR Deep Learning With Python

CANDIY-spectrum Human analyis of chemical spectra such as Mass Spectra (MS), Inf

Wei Mei 1 Jan 3, 2022
Ivy is a templated deep learning framework which maximizes the portability of deep learning codebases.

Ivy is a templated deep learning framework which maximizes the portability of deep learning codebases. Ivy wraps the functional APIs of existing frameworks. Framework-agnostic functions, libraries and layers can then be written using Ivy, with simultaneous support for all frameworks. Ivy currently supports Jax, TensorFlow, PyTorch, MXNet and Numpy. Check out the docs for more info!

Ivy 8.2k Jan 2, 2023
Deep learning (neural network) based remote photoplethysmography: how to extract pulse signal from video using deep learning tools

Deep-rPPG: Camera-based pulse estimation using deep learning tools Deep learning (neural network) based remote photoplethysmography: how to extract pu

Terbe Dániel 138 Dec 17, 2022
deep-table implements various state-of-the-art deep learning and self-supervised learning algorithms for tabular data using PyTorch.

deep-table implements various state-of-the-art deep learning and self-supervised learning algorithms for tabular data using PyTorch.

null 63 Oct 17, 2022