Giter Club home page Giter Club logo

p1_facial_keypoints's Introduction

Facial Keypoint Detection

Project Overview

In this project, you’ll combine your knowledge of computer vision techniques and deep learning architectures to build a facial keypoint detection system. Facial keypoints include points around the eyes, nose, and mouth on a face and are used in many applications. These applications include: facial tracking, facial pose recognition, facial filters, and emotion recognition. Your completed code should be able to look at any image, detect faces, and predict the locations of facial keypoints on each face; examples of these keypoints are displayed below.

Facial Keypoint Detection

The project will be broken up into a few main parts in four Python notebooks, only Notebooks 2 and 3 (and the models.py file) will be graded:

Notebook 1 : Loading and Visualizing the Facial Keypoint Data

Notebook 2 : Defining and Training a Convolutional Neural Network (CNN) to Predict Facial Keypoints

Notebook 3 : Facial Keypoint Detection Using Haar Cascades and your Trained CNN

Notebook 4 : Fun Filters and Keypoint Uses

Project Instructions

All of the starting code and resources you'll need to complete this project are in this Github repository. Before you can get started coding, you'll have to make sure that you have all the libraries and dependencies required to support this project. If you have already created a cv-nd environment for exercise code, then you can use that environment! If not, instructions for creation and activation are below.

Note that this project does not require the use of GPU, so this repo does not include instructions for GPU setup.

Local Environment Instructions

  1. Clone the repository, and navigate to the downloaded folder. This may take a minute or two to clone due to the included image data.
git clone https://github.com/udacity/P1_Facial_Keypoints.git
cd P1_Facial_Keypoints
  1. Create (and activate) a new environment, named cv-nd with Python 3.6. If prompted to proceed with the install (Proceed [y]/n) type y.

    • Linux or Mac:
    conda create -n cv-nd python=3.6
    source activate cv-nd
    
    • Windows:
    conda create --name cv-nd python=3.6
    activate cv-nd
    

    At this point your command line should look something like: (cv-nd) <User>:P1_Facial_Keypoints <user>$. The (cv-nd) indicates that your environment has been activated, and you can proceed with further package installations.

  2. Install PyTorch and torchvision; this should install the latest version of PyTorch.

    • Linux or Mac:
    conda install pytorch torchvision -c pytorch
    
    • Windows:
    conda install pytorch-cpu -c pytorch
    pip install torchvision
    
  3. Install a few required pip packages, which are specified in the requirements text file (including OpenCV).

pip install -r requirements.txt

Data

All of the data you'll need to train a neural network is in the P1_Facial_Keypoints repo, in the subdirectory data. In this folder are training and tests set of image/keypoint data, and their respective csv files. This will be further explored in Notebook 1: Loading and Visualizing Data, and you're encouraged to look through these folders on your own, too.

Notebooks

  1. Navigate back to the repo. (Also, your source environment should still be activated at this point.)
cd
cd P1_Facial_Keypoints
  1. Open the directory of notebooks, using the below command. You'll see all of the project files appear in your local environment; open the first notebook and follow the instructions.
jupyter notebook
  1. Once you open any of the project notebooks, make sure you are in the correct cv-nd environment by clicking Kernel > Change Kernel > cv-nd.

NOTE: While some code has already been implemented to get you started, you will need to implement additional functionality and answer all of the questions included in the notebook. Unless requested, it's suggested that you do not modify code that has already been included.

Evaluation

Your project will be reviewed against the project rubric. Review this rubric thoroughly, and self-evaluate your project before submission. All criteria found in the rubric must meet specifications for you to pass.

Project Submission

When you are ready to submit your project, collect all of your project files -- all executed notebooks, and python files -- and compress them into a single zip archive for upload.

Alternatively, your submission could consist of only the GitHub link to your repository with all of the completed files.

Project Rubric

models.py

Specify the CNN architecture

Criteria Meets Specifications
Define a CNN in models.py. Define a convolutional neural network with at least one convolutional layer, i.e. self.conv1 = nn.Conv2d(1, 32, 5). The network should take in a grayscale, square image.

Notebook 2

Define the data transform for training and test data

Criteria Meets Specifications
Define a data_transform and apply it whenever you instantiate a DataLoader. The composed transform should include: rescaling/cropping, normalization, and turning input images into torch Tensors. The transform should turn any input image into a normalized, square, grayscale image and then a Tensor for your model to take it as input.

Define the loss and optimization functions

Criteria Meets Specifications
Select a loss function and optimizer for training the model. The loss and optimization functions should be appropriate for keypoint detection, which is a regression problem.

Train the CNN

Criteria Meets Specifications
Train your model. Train your CNN after defining its loss and optimization functions. You are encouraged, but not required, to visualize the loss over time/epochs by printing it out occasionally and/or plotting the loss over time. Save your best trained model.

Answer questions about model architecture

Criteria Meets Specifications
All questions about model, training, and loss choices are answered. After training, all 3 questions in notebook 2 about model architecture, choice of loss function, and choice of batch_size and epoch parameters are answered.

Visualize one or more learned feature maps

Criteria Meets Specifications
Apply a learned convolutional kernel to an image and see its effects. Your CNN "learns" (updates the weights in its convolutional layers) to recognize features and this step requires that you extract at least one convolutional filter from the trained model, apply it to an image, and see what effect this filter has on the image.

Answer question about feature visualization

Criteria Meets Specifications
After visualizing a feature map, answer: what do you think it detects? This answer should be informed by how the filtered image (from the step above) looks.

Notebook 3

Detect faces in a given image

Criteria Meets Specifications
Use a haar cascade face detector to detect faces in a given image. The submission successfully employs OpenCV's face detection to detect all faces in a selected image.

Transform each detected face into an input Tensor

Criteria Meets Specifications
Turn each detected image of a face into an appropriate input Tensor. You should transform any face into a normalized, square, grayscale image and then a Tensor for your model to take in as input (similar to what the data_transform did in Notebook 2).

Predict and display the keypoints

Criteria Meets Specifications
Predict and display the keypoints on each detected face. After face detection with a Haar cascade and face pre-processing, apply your trained model to each detected face, and display the predicted keypoints on each face in the image.

LICENSE: This project is licensed under the terms of the MIT license.

p1_facial_keypoints's People

Contributors

bjarten avatar cezannec avatar kurbakov avatar ronny-udacity avatar sudkul avatar ypwhs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

p1_facial_keypoints's Issues

visualize output is not diplaying ground truth

Also - ground truth is not fed back at any time in the notebook (even when data is available - and it is available in both cases in the 2. notebook).

The correct code should be:

def show_keypoints(image, key_pts, ground_pts):
    """Show image with keypoints"""
    # image is grayscale
    plt.imshow(image, cmap='gray')
    plt.scatter(key_pts[:, 0], key_pts[:, 1], s=20, marker='.', c='m')
    if ground_pts is not None:
        plt.scatter(ground_pts[:, 0], ground_pts[:, 1], s=20, marker='.', c='g')

# visualize the output
def visualize_output(test_images, test_outputs, gt_pts=None, batch_size=10):

    for i in range(batch_size):
        plt.figure(figsize=(50,50))
        ax = plt.subplot(1, batch_size, i+1)

        # un-transform the image data
        image = test_images[i].data              # get the image from it's Variable wrapper
        image = image.numpy()
        image = np.transpose(image, (1, 2, 0))   # transpose to go from torch to numpy image

        # un-transform the key_pts data
        key_pts = test_outputs[i].data
        key_pts = key_pts.numpy()
        key_pts = key_pts*50.0+100               # undo normalization of keypoints 

        # un-transform the gt_pts data
        ground_pts = None
        if gt_pts is not None:
            ground_pts = gt_pts[i].data.numpy()            
            ground_pts = ground_pts*50.0+100

        show_keypoints(np.squeeze(image), key_pts, ground_pts)

        plt.axis('off')

    plt.show()

# call it
visualize_output(test_images, test_outputs, gt_pts)

Missing plt.show() in 1. Load and Visualize Data.ipynb

In the block

# Display a few of the images from the dataset
num_to_display = 3

for i in range(num_to_display):
    
    # define the size of images
    fig = plt.figure(figsize=(20,10))
    
    # randomly select a sample
    rand_i = np.random.randint(0, len(face_dataset))
    sample = face_dataset[rand_i]

    # print the shape of the image and keypoints
    print(i, sample['image'].shape, sample['keypoints'].shape)

    ax = plt.subplot(1, num_to_display, i + 1)
    ax.set_title('Sample #{}'.format(i))
    
    # Using the same display function, defined earlier
    show_keypoints(sample['image'], sample['keypoints'])

we need to add plt.show(). Otherwise these samples show later on with the transforms examples.

RuntimeError: DataLoader worker (pid 4134) is killed by signal: Unknown signal: 0.

From Notebook:
~/Applications/anaconda3/envs/ai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in handler(signum, frame)
173 # This following call uses waitid with WNOHANG from C side. Therefore,
174 # Python can still get and update the process status successfully.
--> 175 _error_if_any_worker_fails()
176 if previous_handler is not None:
177 previous_handler(signum, frame)

RuntimeError: DataLoader worker (pid 4134) is killed by signal: Unknown signal: 0.

In the console:

[I 22:49:58.099 NotebookApp] Saving file at /2. Define the Network Architecture.ipynb
ERROR: Unexpected segmentation fault encountered in worker.
ERROR: Unexpected segmentation fault encountered in worker.
ERROR: Unexpected segmentation fault encountered in worker.
ERROR: Unexpected segmentation fault encountered in worker.
[I 23:05:58.134 NotebookApp] Saving file at /2. Define the Network Architecture.ipynb

Minor issues: list of typos

(1) Section: "Load and Visualize Data" notebook 1. last sentence says "fraes", should be "frames"
(2) Section: "Dataset class" section notebook 1. "mormalizing images...", should be "normalizing"..
(3) Section: first paragraph of notebook 2. "..data your working with...", should be "you're"
(4) Section: Apply the model on a test sample notebook 2, "1. Extract the image and groud truth keypoints from a sample", should be 'ground'
(5) Section: Training and Initial Observation notebook 2, "..you're encouraged to start of with", should be "start off"
(6) Section: Feature maps notebook 2 "...each of these ernesl detects...", should be "kernels"
(7) Section: Face and Facial Keypoint detection notebook 3 "The neural network expectes a Tensor ", should be "expects"

These issues are obviously minor, but I think they are easy to fix and make the project look more professional and gives the student a sense that the Udacity team really went through this project thoroughly before giving it to them.

Zero grad

Instead of:
optimizer.zero_grad()

I think it's way better to use:
net.zero_grad()

Because they are only the same, if optimizer has all params of net, just to be on the safe side.

Missing eval()

The notebook 'Define the Network Architecture' does not call eval() before passing test data to the model. Evalution mode should be enabled to disable dropout effects.

Update Training/Testing Dataset

The training/testing images are incomplete. Can you, please, update the training/testing dataset or provide any other alternative sources?

Incomplete training dataset

Hello, the dataset is incomplete, in the training folder there are, file count: 2308 images, but in the training_frames_keypoints.csv file there are references, Number of images: 3462, you can share the group of correct images for training and testing. thank you.

Jupiter notebook.- ImportError: No module named cv2

Hello everyone

I´m trying to import open cv using Jupyter Notebook, from Anaconda 3 but when i run the command "import cv2" i get this error "ImportError: No module named cv2".

The strange thing is that when i run the same comand on Spyder(From Anaconda 3 too) dont have any problem. And I need use specifically Jupyter notebook

What should I do in order to make this work?

environment not installing on Ubuntu22.4

when following your installation instructions I am failing at the point where I shoud do
(cv-nd) xxxxx@yyyyy:/zzzzzzzzzzzzzz/P1_Facial_Keypoints$ pip install -r requirements.txt
ERROR: Could not find a version that satisfies the requirement opencv-python==3.2.0.6 (from versions: 3.2.0.8, 3.3.0.10, 3.3.1.11, 3.4.0.12, 3.4.0.14, 3.4.1.15, 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27, 3.4.7.28, 3.4.8.29, 3.4.9.31, 3.4.9.33, 3.4.10.35, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 3.4.13.47, 3.4.14.51, 3.4.14.53, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25, 4.1.1.26, 4.1.2.30, 4.2.0.32, 4.2.0.34, 4.3.0.36, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.2.52, 4.5.2.54, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66, 4.7.0.68, 4.7.0.72, 4.8.0.74, 4.8.0.76)
ERROR: No matching distribution found for opencv-python==3.2.0.6

can you please help?

Incorrect visualization

In def visualize_output(test_images, test_outputs, gt_pts=None, batch_size=10):

    # un-transform the key_pts data
    key_pts = test_outputs.data

should be correctly:

    # un-transform the key_pts data
    key_pts = test_outputs[i].data

Suggestion: add load model in test data

Notebook 2:

Sometimes we can override the test output and wants to recover a trained model. Maybe this line could be commented in the cell:

# net = torch.load('saved_models/keypoints_model_2.pt')

image

Python style

Notebook 3, 2nd code block:

screenshot from 2018-04-08 17-56-21

should be (take out spaces around optional argument label):
fig = plt.figure(figsize=(9,9))

ResolvePackageNotFound:

@sunsided Hi, I have a question about your repository facial key point detection, when I was trying to follow your steps
conda env create -f environment.yaml
I got an error

ResolvePackageNotFound:

  • nbconvert==5.3.1=py_1
    i'm using ubuntu 18
    python3.6

Why chosen mean=100 and std=50

mean = 100, sqrt = 50, so, pts should be (pts - 100)/50
key_pts_copy = (key_pts_copy - 100)/50.0

Why mean is taken as 100 and std is taken as 50 .What is the reason behind scaling?

Flattening of key points

In train_net, flattening of key points are done like this:
key_pts = key_pts.view(1, -1)

I think it would be more correct to do it like that:
key_pts = key_pts.view(key_pts.size(0), -1)

Notebook 2 Test Data: Confusing section / incorrectly named variable

In the notebook 2 'Test Data' section:
screenshot from 2018-04-08 13-39-57

Couple things :
(1) Confusing that the visualize_output() method is commented out with no guidance on whether the student should uncomment this out or what to do with this. Honestly looks odd just hanging there with no instruction or even not having that method and just having something like 'TODO - visualize output'
(2) In the second box (in the commented out code) you see second argument to visualize_output if test_outputs_new but there is no variable called that. So, either the above box we need to rename test_outputs to test_outputs_new or visa versa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.