{ "cells": [ { "cell_type": "markdown", "id": "628757b2-052d-459f-a12a-2f099cd10a3d", "metadata": {}, "source": [ "# Building a Facial Recognition system from pre-trained networks\n", "\n", "You have been given extensive examples in the lab webpage, so please refer to those while completing this notebook. Begin by installing the git repo.\n", "\n", "### Understanding your networks\n", "\n", "Take a picture with your phone of somebody in the class, and upload that picture to this notebook. Open the image, and display it with `plt.imshow(img)`." ] }, { "cell_type": "code", "execution_count": null, "id": "6e4bf523-27fd-44e8-bb84-55ab72c19e03", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4a8bae21-b1c5-4bdb-a9b1-31d7db9002b2", "metadata": {}, "source": [ "Now, run it through your facial detector. The output will be a tensor, suppose it's called `img_cropped`. Print out its shape, and then display the image. You can display it with `plt.imshow(img_cropped.permute(1,2,0))`." ] }, { "cell_type": "code", "execution_count": null, "id": "67883ea3-6092-4c47-b301-13d9a14c0519", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "29c20116-620b-49e4-a734-30f568f09f50", "metadata": {}, "source": [ "Now, create an embedding of that face, and print out its shape." ] }, { "cell_type": "code", "execution_count": null, "id": "88db769b-7635-40e4-a527-88c3243c3c2e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "8f7fa883-3727-4359-999b-c873df527e9f", "metadata": {}, "source": [ "### Getting your gallery\n", "\n", "Using the S3 bucket `s3://mlspace-data-521454461163/project/10FacialRecognition/datasets/midsfaces/`, for all midshipmen currently taking SD312, create a list of all the images, and then a numpy array of all the embeddings of their cropped faces, where `embeddings[0]` is a row vector containing the embedding of `images[0]` from your list of images." ] }, { "cell_type": "code", "execution_count": null, "id": "6e57b50c-af50-4f09-a6f5-04da21ce89a8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9e95e63f-6c38-4cf4-8bb0-bc94cbc8c590", "metadata": {}, "source": [ "`.fit()` a sklearn `NearestNeighbor` object to your trained embeddings. Let's keep the top-5 closest faces." ] }, { "cell_type": "code", "execution_count": null, "id": "ed1cf7ad-9aef-41c3-8814-d95255a9dea1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9d70525f-f4fe-4117-8251-80441c43447f", "metadata": {}, "source": [ "### Building your recognition system\n", "\n", "Now, given your picture of someone in your class, find the five faces from SD312 which are closest in embedding space to your picture.\n", "\n", "To help you visualize how this is working, write a function that displays first the probe image, and then the five gallery images closest to that probe image." ] }, { "cell_type": "code", "execution_count": null, "id": "f823ba10-8500-4dee-a7b5-ec95d0dee225", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "8e536069-f0a2-40ac-bbcf-74e19d76781d", "metadata": {}, "source": [ "Collect more pictures of people in the class. Find some with people wearing a hat, or a Halloween costume, or a covid mask, or a terrible mustache. How well do they work?" ] }, { "cell_type": "code", "execution_count": null, "id": "0f3994ab-24d3-4c8f-8129-525f41fd60e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3990ce53-b1a7-4d20-bf03-c6f1030f314a", "metadata": {}, "source": [ "Expand to the full class of 2025 for your gallery images. Then expand to all midshipmen in the dataset. How does your accuracy change?" ] }, { "cell_type": "code", "execution_count": null, "id": "9088e7ea-175b-43e1-915d-112192e2bd12", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }