{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":112070,"databundleVersionId":13354591,"sourceType":"competition"}],"dockerImageVersionId":31090,"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, optimizers\nfrom tensorflow.keras.applications import EfficientNetB0\nimport numpy as np\nimport glob\nimport random","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.444342Z","iopub.execute_input":"2025-08-11T23:43:31.444717Z","iopub.status.idle":"2025-08-11T23:43:31.449335Z","shell.execute_reply.started":"2025-08-11T23:43:31.444685Z","shell.execute_reply":"2025-08-11T23:43:31.448611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = '/kaggle/input/facial-recognition-7th-gen/Fotos Grupo/'\nstudent_folders = sorted(glob.glob(DATA_PATH + '*/'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.450500Z","iopub.execute_input":"2025-08-11T23:43:31.450732Z","iopub.status.idle":"2025-08-11T23:43:31.479631Z","shell.execute_reply.started":"2025-08-11T23:43:31.450716Z","shell.execute_reply":"2025-08-11T23:43:31.478971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = [p.split('/')[-2] for p in student_folders]\nlabel_map = {name: i for i, name in enumerate(class_names)}\nnum_classes = len(class_names)\nprint(f\"Se encontraron {num_classes} clases (alumnos): {class_names}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.480408Z","iopub.execute_input":"2025-08-11T23:43:31.481003Z","iopub.status.idle":"2025-08-11T23:43:31.485690Z","shell.execute_reply.started":"2025-08-11T23:43:31.480979Z","shell.execute_reply":"2025-08-11T23:43:31.485077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_paths, train_labels, test_paths, test_labels = [], [], [], []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.487100Z","iopub.execute_input":"2025-08-11T23:43:31.487261Z","iopub.status.idle":"2025-08-11T23:43:31.502777Z","shell.execute_reply.started":"2025-08-11T23:43:31.487249Z","shell.execute_reply":"2025-08-11T23:43:31.502107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for student_folder in student_folders:\n    student_name = student_folder.split('/')[-2]\n    image_paths = glob.glob(student_folder + '*.jpg')\n    random.shuffle(image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.503407Z","iopub.execute_input":"2025-08-11T23:43:31.503891Z","iopub.status.idle":"2025-08-11T23:43:31.523513Z","shell.execute_reply.started":"2025-08-11T23:43:31.503875Z","shell.execute_reply":"2025-08-11T23:43:31.522916Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"current_train_paths = image_paths[:20]\ncurrent_test_paths = image_paths[20:]\n    \nnum_train_images = len(current_train_paths)\nnum_test_images = len(current_test_paths)\n\ntrain_paths.extend(current_train_paths)\ntest_paths.extend(current_test_paths)\n    \ntrain_labels.extend([label_map[student_name]] * num_train_images)\ntest_labels.extend([label_map[student_name]] * num_test_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.524333Z","iopub.execute_input":"2025-08-11T23:43:31.524678Z","iopub.status.idle":"2025-08-11T23:43:31.528824Z","shell.execute_reply.started":"2025-08-11T23:43:31.524656Z","shell.execute_reply":"2025-08-11T23:43:31.528297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SIZE = 224\nBATCH_SIZE = 32","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.529484Z","iopub.execute_input":"2025-08-11T23:43:31.529705Z","iopub.status.idle":"2025-08-11T23:43:31.548742Z","shell.execute_reply.started":"2025-08-11T23:43:31.529690Z","shell.execute_reply":"2025-08-11T23:43:31.548029Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_and_preprocess_image(path, label):\n    image = tf.io.read_file(path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [IMG_SIZE, IMG_SIZE])\n    image = image / 255.0\n    return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.549453Z","iopub.execute_input":"2025-08-11T23:43:31.549726Z","iopub.status.idle":"2025-08-11T23:43:31.564431Z","shell.execute_reply.started":"2025-08-11T23:43:31.549707Z","shell.execute_reply":"2025-08-11T23:43:31.563697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.from_tensor_slices((train_paths, train_labels))\ntrain_dataset = train_dataset.shuffle(len(train_paths)).map(load_and_preprocess_image).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n\ntest_dataset = tf.data.Dataset.from_tensor_slices((test_paths, test_labels))\ntest_dataset = test_dataset.map(load_and_preprocess_image).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.565151Z","iopub.execute_input":"2025-08-11T23:43:31.565381Z","iopub.status.idle":"2025-08-11T23:43:31.648429Z","shell.execute_reply.started":"2025-08-11T23:43:31.565366Z","shell.execute_reply":"2025-08-11T23:43:31.647907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = EfficientNetB0(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3))\nbase_model.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:31.650324Z","iopub.execute_input":"2025-08-11T23:43:31.650524Z","iopub.status.idle":"2025-08-11T23:43:32.588098Z","shell.execute_reply.started":"2025-08-11T23:43:31.650508Z","shell.execute_reply":"2025-08-11T23:43:32.587312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))\nx = base_model(inputs, training=False)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.2)(x)\noutputs = layers.Dense(num_classes, activation='softmax')(x)\nmodel = models.Model(inputs, outputs)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:32.588919Z","iopub.execute_input":"2025-08-11T23:43:32.589134Z","iopub.status.idle":"2025-08-11T23:43:32.607271Z","shell.execute_reply.started":"2025-08-11T23:43:32.589118Z","shell.execute_reply":"2025-08-11T23:43:32.606470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:32.608036Z","iopub.execute_input":"2025-08-11T23:43:32.608269Z","iopub.status.idle":"2025-08-11T23:43:32.630529Z","shell.execute_reply.started":"2025-08-11T23:43:32.608249Z","shell.execute_reply":"2025-08-11T23:43:32.629999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    train_dataset,\n    epochs=10,\n    validation_data=test_dataset\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:43:32.631074Z","iopub.execute_input":"2025-08-11T23:43:32.631237Z","iopub.status.idle":"2025-08-11T23:44:12.045099Z","shell.execute_reply.started":"2025-08-11T23:43:32.631224Z","shell.execute_reply":"2025-08-11T23:44:12.044506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('/kaggle/working/test/Fotos Grupo/modelo_amaury.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T23:46:42.732041Z","iopub.execute_input":"2025-08-11T23:46:42.732355Z","iopub.status.idle":"2025-08-11T23:46:43.026170Z","shell.execute_reply.started":"2025-08-11T23:46:42.732334Z","shell.execute_reply":"2025-08-11T23:46:43.025623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}