{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":62912,"databundleVersionId":6868003,"sourceType":"competition"}],"dockerImageVersionId":30580,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:08:29.059838Z","iopub.execute_input":"2023-11-15T17:08:29.060154Z","iopub.status.idle":"2023-11-15T17:08:47.857011Z","shell.execute_reply.started":"2023-11-15T17:08:29.060126Z","shell.execute_reply":"2023-11-15T17:08:47.855993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# from sklearn.model_selection import train_test_split\n# import shutil\n\n# # Set the path to your train folder\n# train_folder = \"train\"\n\n# # Set the path to create train2 and val2 folders\n# output_folder = \"dataset\"\n\n# # Get the list of subfolders (each corresponding to a person)\n# subfolders = [f.path for f in os.scandir(train_folder) if f.is_dir()]\n\n# # Initialize lists to store file paths and corresponding labels\n# file_paths = []\n# labels = []\n\n# # Iterate through subfolders\n# for subfolder in subfolders:\n#     # Get the label (person's name) from the subfolder name\n#     label = os.path.basename(subfolder)\n    \n#     # Get the list of image files in the subfolder\n#     image_files = [os.path.join(subfolder, f.name) for f in os.scandir(subfolder) if f.is_file() and f.name.endswith(('.jpg', '.jpeg', '.png'))]\n    \n#     # Append file paths and labels to the lists\n#     file_paths.extend(image_files)\n#     labels.extend([label] * len(image_files))\n\n# # Perform train-validation split\n# train_file_paths, val_file_paths, train_labels, val_labels = train_test_split(\n#     file_paths, labels, test_size=0.2, random_state=42, stratify=labels\n# )\n\n# # Create train2 and val2 folders if they don't exist\n# train2_folder = os.path.join(output_folder, \"train2\")\n# val2_folder = os.path.join(output_folder, \"val2\")\n# os.makedirs(train2_folder, exist_ok=True)\n# os.makedirs(val2_folder, exist_ok=True)\n\n# # Function to copy files to destination folder\n# def copy_files(file_paths, labels, destination_folder):\n#     for file_path, label in zip(file_paths, labels):\n#         destination_path = os.path.join(destination_folder, label)\n#         os.makedirs(destination_path, exist_ok=True)\n#         shutil.copy(file_path, destination_path)\n\n# # Copy files to train2 and val2 folders\n# copy_files(train_file_paths, train_labels, train2_folder)\n# copy_files(val_file_paths, val_labels, val2_folder)\n\n# # Print the number of samples in each set\n# print(f\"Number of samples in training set: {len(train_file_paths)}\")\n# print(f\"Number of samples in validation set: {len(val_file_paths)}\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deepface_architecture(input_shape, num_classes):\n    model = models.Sequential()\n\n    # Convolutional layers\n    model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=input_shape, padding='same'))\n    model.add(layers.MaxPooling2D((2, 2)))\n\n    model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))\n    model.add(layers.MaxPooling2D((2, 2)))\n\n    model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))\n    model.add(layers.MaxPooling2D((2, 2)))\n\n    # Flatten before fully connected layers\n    model.add(layers.Flatten())\n\n    # Fully connected layers\n    model.add(layers.Dense(1024, activation='relu'))\n    model.add(layers.Dropout(0.5))  # Adjust dropout rate as needed\n\n    model.add(layers.Dense(1024, activation='relu'))\n    model.add(layers.Dropout(0.5))\n\n    # Output layer\n    model.add(layers.Dense(num_classes, activation='softmax'))\n\n\n    return model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-15T17:08:47.859342Z","iopub.execute_input":"2023-11-15T17:08:47.860277Z","iopub.status.idle":"2023-11-15T17:08:47.872016Z","shell.execute_reply.started":"2023-11-15T17:08:47.860240Z","shell.execute_reply":"2023-11-15T17:08:47.868862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_data_generators(train_data_dir, input_shape, batch_size):\n    train_datagen = ImageDataGenerator(rescale=1./255,\n                                       shear_range=0.2,\n                                       zoom_range=0.2,\n                                       horizontal_flip=True)\n\n    val_datagen = ImageDataGenerator(rescale=1./255)\n\n    train_generator = train_datagen.flow_from_directory(train_data_dir,\n                                                        target_size=input_shape[:2],\n                                                        batch_size=batch_size,\n                                                        class_mode='categorical')\n\n\n    return train_generator","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:08:47.873653Z","iopub.execute_input":"2023-11-15T17:08:47.874089Z","iopub.status.idle":"2023-11-15T17:08:47.904394Z","shell.execute_reply.started":"2023-11-15T17:08:47.874038Z","shell.execute_reply":"2023-11-15T17:08:47.903481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_dir = \"/kaggle/input/face-classification-deep-learning/dataset/train\"\ninput_shape = (224, 224, 3)\nnum_classes = 7000\nbatch_size = 32\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:08:47.906620Z","iopub.execute_input":"2023-11-15T17:08:47.907009Z","iopub.status.idle":"2023-11-15T17:08:47.917973Z","shell.execute_reply.started":"2023-11-15T17:08:47.906977Z","shell.execute_reply":"2023-11-15T17:08:47.917042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deepface_model = deepface_architecture(input_shape, num_classes)\n\ndeepface_model.compile(optimizer='adam',\n                       loss='categorical_crossentropy',\n                       metrics=['accuracy'])\n\ntrain_generator = create_data_generators(train_data_dir, input_shape, batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:08:47.920530Z","iopub.execute_input":"2023-11-15T17:08:47.921031Z","iopub.status.idle":"2023-11-15T17:09:20.635686Z","shell.execute_reply.started":"2023-11-15T17:08:47.920967Z","shell.execute_reply":"2023-11-15T17:09:20.634614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory_epoch = deepface_model.fit(train_generator,\n                                  steps_per_epoch=train_generator.samples // batch_size,\n                                  epochs=20,\n                                  verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:09:20.636906Z","iopub.execute_input":"2023-11-15T17:09:20.637217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}