Spaces:
Sleeping
Sleeping
| import cv2 | |
| import os | |
| import numpy as np | |
| from huggingface_hub import push_to_hub_keras | |
| from tensorflow.keras.applications import VGG16 | |
| from tensorflow.keras.layers import Flatten, Dense, Dropout | |
| from tensorflow.keras.models import Model | |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
| from tensorflow.keras.applications.vgg16 import preprocess_input # VGG16 specific pre-processing | |
| from PIL import Image # For grayscale to RGB conversion | |
| # Early Stopping to prevent overfitting | |
| from tensorflow.keras.callbacks import EarlyStopping | |
| # Compile the model with a learning rate scheduler for potential improvement | |
| from tensorflow.keras.callbacks import LearningRateScheduler | |
| sac = os.getenv('accesstoken') | |
| # Define data paths and hyperparameters | |
| train_data_dir = 'datasets' | |
| validation_data_dir = 'first_ten_files' | |
| test_data_dir = 'first_ten_files' | |
| img_size = (224, 224) # Adjust based on pre-trained model input size | |
| img_height, img_width = 224, 224 | |
| batch_size = 32 | |
| num_classes = 10 # Adjust based on your fingerprint classification types | |
| # Data augmentation (optional) | |
| train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) | |
| val_datagen = ImageDataGenerator(rescale=1./255) | |
| # Load training and validation data | |
| train_generator = train_datagen.flow_from_directory( | |
| train_data_dir, | |
| target_size=(img_height, img_width), | |
| batch_size=32, | |
| class_mode='categorical' | |
| ) | |
| val_generator = val_datagen.flow_from_directory( | |
| validation_data_dir, | |
| target_size=(img_height, img_width), | |
| batch_size=32, | |
| class_mode='categorical' | |
| ) | |
| # Load pre-trained model (VGG16 in this example) | |
| base_model = VGG16(weights="imagenet", include_top=False, input_shape=(img_height, img_width, 3)) | |
| base_model.trainable = False # Freeze pre-trained layers | |
| # Add new layers | |
| x = base_model.output | |
| x = Flatten()(x) | |
| x = Dense(1024, activation='relu')(x) # Add a dense layer with 1024 neurons and ReLU activation | |
| x = Dropout(0.5)(x) # Add dropout for regularization | |
| predictions = Dense(10, activation='softmax')(x) # Output layer with 10 units for 10 classes | |
| # Build the final model | |
| model = Model(inputs=base_model.input, outputs=predictions) | |
| # Compile the model | |
| model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) | |
| # Early stopping callback | |
| early_stopping = EarlyStopping(monitor='val_loss', patience=3) | |
| # Train the model | |
| model.fit( | |
| train_generator, | |
| epochs=10, | |
| validation_data=val_generator, | |
| callbacks=[early_stopping] | |
| ) | |
| # # Evaluate the model on unseen data | |
| # test_generator = val_datagen.flow_from_directory( | |
| # test_data_dir, | |
| # target_size=img_size, | |
| # batch_size=batch_size, | |
| # class_mode='categorical', | |
| # color_mode='grayscale' | |
| # ) | |
| # test_loss, test_acc = model.evaluate(test_generator) | |
| # print('Test accuracy:', test_acc) | |
| # Save the model for future use | |
| model.save('fingerprint_classifier.keras') | |
| labels = train_generator.class_indices | |
| labels = dict((v, k) for k, v in labels.items()) | |
| print(labels) | |
| # Save the trained model | |
| # model.save('fingerprint_recognition_model.keras') | |
| # Show the model architecture | |
| model.summary() | |
| push_to_hub_keras( | |
| model, | |
| repo_id="okeowo1014/fingerprintgrecognizer", | |
| commit_message="Initial commit model", | |
| token=sac, | |
| include_optimizer=True, | |
| ) | |
| print(f"Model pushed to Hugging Face Hub") | |