-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01b2ae2
commit 3874f72
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":67842,"status":"ok","timestamp":1720240920108,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"okgPSvtjUVgH","outputId":"4d7c8280-92a6-4a66-dd4a-a09b60ba44ee"},"outputs":[{"name":"stdout","output_type":"stream","text":["Mounted at /googledrive\n"]}],"source":["from google.colab import drive\n","drive.mount('/googledrive')"]},{"cell_type":"code","execution_count":3,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":448,"status":"ok","timestamp":1720240943032,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"hFp7Un9oWTqm","outputId":"4af1fa13-1fa1-4498-d998-1a529009303f"},"outputs":[{"name":"stdout","output_type":"stream","text":["Celeb_Face_Recognition.ipynb train val\n"]}],"source":["!ls /googledrive/MyDrive/GSSOC24/celeb/"]},{"cell_type":"code","execution_count":19,"metadata":{"executionInfo":{"elapsed":434,"status":"ok","timestamp":1720241305073,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"06AvH5t9WTuB"},"outputs":[],"source":["import os\n","import numpy as np\n","import cv2\n","import random\n","import matplotlib.pyplot as plt\n","from sklearn.preprocessing import LabelEncoder\n","from tensorflow.keras.models import Sequential\n","from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n","from tensorflow.keras.utils import to_categorical\n","from sklearn.metrics import classification_report, accuracy_score\n"]},{"cell_type":"code","execution_count":20,"metadata":{"executionInfo":{"elapsed":3,"status":"ok","timestamp":1720241306599,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"VNJ_qjB3WfW1"},"outputs":[],"source":["train_dir = '/googledrive/MyDrive/GSSOC24/celeb/train'\n","val_dir = '/googledrive/MyDrive/GSSOC24/celeb/val'\n"]},{"cell_type":"code","execution_count":21,"metadata":{"executionInfo":{"elapsed":514,"status":"ok","timestamp":1720241309573,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"nq8XnzBQWpVh"},"outputs":[],"source":["def load_data(data_dir):\n"," images = []\n"," labels = []\n"," for label in os.listdir(data_dir):\n"," if label not in ['madonna', 'mindy_kaling', 'jerry_seinfeld', 'elton_john', 'ben_afflek']:\n"," continue\n"," for file in os.listdir(os.path.join(data_dir, label)):\n"," img_path = os.path.join(data_dir, label, file)\n"," img = cv2.imread(img_path)\n"," img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n"," img = cv2.resize(img, (128, 128))\n"," images.append(img)\n"," labels.append(label)\n"," return np.array(images), np.array(labels)\n"]},{"cell_type":"code","execution_count":22,"metadata":{"executionInfo":{"elapsed":1123,"status":"ok","timestamp":1720241313000,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"s7XqGThZWr-1"},"outputs":[],"source":["X_train, y_train = load_data(train_dir)\n","X_val, y_val = load_data(val_dir)\n"]},{"cell_type":"code","execution_count":23,"metadata":{"executionInfo":{"elapsed":463,"status":"ok","timestamp":1720241315214,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"8iZhS1u6Wwrd"},"outputs":[],"source":["le = LabelEncoder()\n","y_train = le.fit_transform(y_train)\n","y_val = le.transform(y_val)\n"]},{"cell_type":"code","execution_count":24,"metadata":{"executionInfo":{"elapsed":718,"status":"ok","timestamp":1720241317682,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"-O7YNZeHWx4P"},"outputs":[],"source":["X_train = X_train / 255.0\n","X_val = X_val / 255.0\n"]},{"cell_type":"code","execution_count":25,"metadata":{"executionInfo":{"elapsed":2,"status":"ok","timestamp":1720241319610,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"b-QMrolNW0Xb"},"outputs":[],"source":["y_train = to_categorical(y_train, num_classes=len(le.classes_))\n","y_val = to_categorical(y_val, num_classes=len(le.classes_))\n"]},{"cell_type":"code","execution_count":26,"metadata":{"executionInfo":{"elapsed":2,"status":"ok","timestamp":1720241321396,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"7JXrKXnvW4G0"},"outputs":[],"source":["model = Sequential([\n"," Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),\n"," MaxPooling2D((2, 2)),\n"," Conv2D(64, (3, 3), activation='relu'),\n"," MaxPooling2D((2, 2)),\n"," Conv2D(128, (3, 3), activation='relu'),\n"," MaxPooling2D((2, 2)),\n"," Flatten(),\n"," Dense(128, activation='relu'),\n"," Dropout(0.5),\n"," Dense(len(le.classes_), activation='softmax')\n","])\n"]},{"cell_type":"code","execution_count":27,"metadata":{"executionInfo":{"elapsed":4,"status":"ok","timestamp":1720241323392,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"LtvZkIINW6qr"},"outputs":[],"source":["model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n"]},{"cell_type":"code","execution_count":36,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":41710,"status":"ok","timestamp":1720241624697,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"bxVSDVnKW946","outputId":"6f5c74eb-5fde-42ef-a541-679f8d2e598d"},"outputs":[{"name":"stdout","output_type":"stream","text":["Epoch 1/10\n","3/3 [==============================] - 5s 2s/step - loss: 0.0295 - accuracy: 0.9892 - val_loss: 2.9298 - val_accuracy: 0.6000\n","Epoch 2/10\n","3/3 [==============================] - 3s 1s/step - loss: 0.0869 - accuracy: 0.9677 - val_loss: 2.7866 - val_accuracy: 0.5200\n","Epoch 3/10\n","3/3 [==============================] - 3s 1s/step - loss: 0.0289 - accuracy: 1.0000 - val_loss: 2.7979 - val_accuracy: 0.5600\n","Epoch 4/10\n","3/3 [==============================] - 3s 1s/step - loss: 0.0208 - accuracy: 1.0000 - val_loss: 2.7567 - val_accuracy: 0.6000\n","Epoch 5/10\n","3/3 [==============================] - 5s 2s/step - loss: 0.0229 - accuracy: 1.0000 - val_loss: 2.7142 - val_accuracy: 0.6800\n","Epoch 6/10\n","3/3 [==============================] - 4s 1s/step - loss: 0.0273 - accuracy: 0.9892 - val_loss: 2.8622 - val_accuracy: 0.6400\n","Epoch 7/10\n","3/3 [==============================] - 3s 1s/step - loss: 0.0118 - accuracy: 1.0000 - val_loss: 3.2112 - val_accuracy: 0.6400\n","Epoch 8/10\n","3/3 [==============================] - 3s 1s/step - loss: 0.0198 - accuracy: 1.0000 - val_loss: 3.2759 - val_accuracy: 0.6400\n","Epoch 9/10\n","3/3 [==============================] - 5s 2s/step - loss: 0.0264 - accuracy: 0.9892 - val_loss: 2.9470 - val_accuracy: 0.6400\n","Epoch 10/10\n","3/3 [==============================] - 4s 1s/step - loss: 0.0174 - accuracy: 1.0000 - val_loss: 2.7513 - val_accuracy: 0.6400\n"]},{"data":{"text/plain":["<keras.src.callbacks.History at 0x7a1ab349e2f0>"]},"execution_count":36,"metadata":{},"output_type":"execute_result"}],"source":["model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))\n"]},{"cell_type":"code","execution_count":37,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":718,"status":"ok","timestamp":1720241632236,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"HDdw6wIBXDRs","outputId":"27230245-4578-4ea2-d9b9-2751351cc0b1"},"outputs":[{"name":"stdout","output_type":"stream","text":["1/1 [==============================] - 0s 419ms/step - loss: 2.7513 - accuracy: 0.6400\n","Validation Accuracy: 0.64\n"]}],"source":["val_loss, val_accuracy = model.evaluate(X_val, y_val)\n","print(f'Validation Accuracy: {val_accuracy:.2f}')\n"]},{"cell_type":"code","execution_count":38,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":658,"status":"ok","timestamp":1720241635431,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"KTcabS6EXLlu","outputId":"bfbf4348-7115-406d-d376-152bfb0396ce"},"outputs":[{"name":"stdout","output_type":"stream","text":["1/1 [==============================] - 0s 274ms/step\n"]}],"source":["predictions = model.predict(X_val)\n","predicted_classes = np.argmax(predictions, axis=1)\n","true_classes = np.argmax(y_val, axis=1)\n"]},{"cell_type":"code","execution_count":39,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":3,"status":"ok","timestamp":1720241637772,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"QUU-TgxSXOB-","outputId":"e743df86-7dbc-4d7b-d331-f474fc1ebf03"},"outputs":[{"name":"stdout","output_type":"stream","text":[" precision recall f1-score support\n","\n"," ben_afflek 0.50 0.60 0.55 5\n"," elton_john 0.60 0.60 0.60 5\n","jerry_seinfeld 0.50 0.20 0.29 5\n"," madonna 0.57 0.80 0.67 5\n"," mindy_kaling 1.00 1.00 1.00 5\n","\n"," accuracy 0.64 25\n"," macro avg 0.63 0.64 0.62 25\n"," weighted avg 0.63 0.64 0.62 25\n","\n"]}],"source":["print(classification_report(true_classes, predicted_classes, target_names=le.classes_))\n"]},{"cell_type":"code","execution_count":40,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000,"output_embedded_package_id":"1y3ENGDc1YsGJMHPvShg_qLPonFKt9-aN"},"executionInfo":{"elapsed":7226,"status":"ok","timestamp":1720241647576,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"sKBYqCJKXQoB","outputId":"be84a268-3100-4183-fe92-e6a4f1f5f629"},"outputs":[{"data":{"text/plain":["Output hidden; open in https://colab.research.google.com to view."]},"metadata":{},"output_type":"display_data"}],"source":["def display_images(images, true_labels, pred_labels, label_encoder, num_images=25):\n"," plt.figure(figsize=(20, 20))\n"," for i in range(num_images):\n"," plt.subplot(5, 5, i + 1)\n"," plt.imshow(images[i])\n"," plt.title(f\"True: {label_encoder.inverse_transform([true_labels[i]])[0]}\\nPred: {label_encoder.inverse_transform([pred_labels[i]])[0]}\")\n"," plt.axis('off')\n"," plt.show()\n","\n","# Display images\n","display_images(X_val, true_classes, predicted_classes, le)\n"]},{"cell_type":"code","execution_count":41,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":466,"status":"ok","timestamp":1720241667809,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"EX-O6u8LYJS_","outputId":"1926c532-d773-4d91-88cc-4c3d1d7ed159"},"outputs":[{"name":"stdout","output_type":"stream","text":["Model saved successfully.\n"]},{"name":"stderr","output_type":"stream","text":["/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n"," saving_api.save_model(\n"]}],"source":["model.save('/googledrive/MyDrive/GSSOC24/celeb/celebrity_recognition_model.h5')\n","print(\"Model saved successfully.\")\n"]},{"cell_type":"code","execution_count":43,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":1242,"status":"ok","timestamp":1720241692509,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"1aF50wkxYT6i","outputId":"f5702976-bd88-48af-96e0-7d5b450c0dfd"},"outputs":[{"name":"stdout","output_type":"stream","text":["Model loaded successfully.\n"]}],"source":["from tensorflow.keras.models import load_model\n","\n","loaded_model = load_model('/googledrive/MyDrive/GSSOC24/celeb/celebrity_recognition_model.h5')\n","print(\"Model loaded successfully.\")\n"]},{"cell_type":"code","execution_count":46,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000,"output_embedded_package_id":"1BUfu4w_MryY71VB-T-nlQziepyKcLbP_"},"executionInfo":{"elapsed":16031,"status":"ok","timestamp":1720241906136,"user":{"displayName":"Kalyani arwenundomiel","userId":"01329483214219074689"},"user_tz":-330},"id":"tyLpIPQ4aBGR","outputId":"3ac89100-960e-4ea0-9987-5a907729b62e"},"outputs":[{"data":{"text/plain":["Output hidden; open in https://colab.research.google.com to view."]},"metadata":{},"output_type":"display_data"}],"source":["def predict_and_display_all(val_dir, model, label_encoder):\n"," for label in os.listdir(val_dir):\n"," if label not in ['madonna', 'mindy_kaling', 'jerry_seinfeld', 'elton_john', 'ben_afflek']:\n"," continue\n"," class_dir = os.path.join(val_dir, label)\n"," for img_file in os.listdir(class_dir):\n"," img_path = os.path.join(class_dir, img_file)\n"," img = cv2.imread(img_path)\n"," if img is not None:\n"," img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n"," img_resized = cv2.resize(img_rgb, (128, 128))\n"," img_normalized = img_resized / 255.0\n"," img_expanded = np.expand_dims(img_normalized, axis=0)\n","\n"," prediction = model.predict(img_expanded)\n"," predicted_class = np.argmax(prediction, axis=1)\n"," predicted_label = label_encoder.inverse_transform(predicted_class)[0]\n","\n"," plt.imshow(img_rgb)\n"," plt.title(f\"Predicted: {predicted_label}\\nActual: {label}\")\n"," plt.axis('off')\n"," plt.show()\n","\n","# Use the function to predict and display all validation images\n","predict_and_display_all(val_dir, loaded_model, le)\n"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyMT9S96xKdpruIijvdeF5gT","provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Celebrity Face Recognition | ||
|
||
This project involves training a machine learning model to recognize images of five celebrities: Madonna, Mindy Kaling, Jerry Seinfeld, Elton John, and Ben Affleck. The model is trained on a dataset containing images of these celebrities and is capable of predicting the celebrity in new images with a reasonable degree of accuracy. | ||
|
||
## DataSet Preparation | ||
The dataset used in this project is a collection of images of the five celebrities mentioned above. Download images from https://www.kaggle.com/datasets/dansbecker/5-celebrity-faces-dataset. | ||
|
||
## Project Structure | ||
|
||
- `train`: Directory containing training images of the celebrities. | ||
- `val`: Directory containing validation images of the celebrities. | ||
- `celebrity_recognition_model.h5`: The trained Keras model. | ||
- `label_encoder.pkl`: The label encoder used for transforming class labels. | ||
|
||
## Dependencies | ||
|
||
The project requires the following Python packages: | ||
|
||
- `tensorflow` | ||
- `numpy` | ||
- `opencv-python` | ||
- `matplotlib` | ||
- `scikit-learn` | ||
|
||
These can be installed using the provided `requirements.txt` file. | ||
|
||
## Usage | ||
|
||
### Training the Model | ||
|
||
1. Place the training images in the `train` directory with each celebrity's images in their respective folders. | ||
2. Preprocess the images using data generators and train a convolutional neural network (CNN) model. | ||
3. Save the trained model and the label encoder for future use. | ||
|
||
### Predicting on Validation Data | ||
|
||
1. Load the trained model and label encoder. | ||
2. Use the validation data to test the model's performance. | ||
3. Display the predictions for the validation images, showing the predicted celebrity names. | ||
|
||
## Example Workflow | ||
|
||
1. **Prepare the Dataset**: Organize the images into `train` and `val` directories with subdirectories for each celebrity. | ||
2. **Train the Model**: Use a CNN to learn features from the training images and save the model. | ||
3. **Validate the Model**: Load the model and evaluate it on the validation set to check its performance. | ||
4. **Display Predictions**: Visualize the predictions by displaying validation images with the predicted celebrity labels. | ||
|
||
## Requirements | ||
|
||
- TensorFlow | ||
- NumPy | ||
- OpenCV | ||
- Matplotlib | ||
- Scikit-learn | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. |
Binary file not shown.