Instructions to use DataScienceProject/CNN with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use DataScienceProject/CNN with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://DataScienceProject/CNN") - Notebooks
- Google Colab
- Kaggle
Download cnn_SaveInPainting.py from DataScienceProject/CNN: direct link, hf CLI and curl.
- Browser
- Download file 9.68 kB
-
https://huggingface.co/DataScienceProject/CNN/resolve/main/cnn_SaveInPainting.py
- Command line
-
hf download hf://DataScienceProject/CNN/cnn_SaveInPainting.py
-
curl -L -o cnn_SaveInPainting.py https://huggingface.co/DataScienceProject/CNN/resolve/main/cnn_SaveInPainting.py
9.68 kB
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Sat May 18 16:15:32 2024 | |
| @author: litav | |
| """ | |
| import numpy as np | |
| import tensorflow as tf | |
| import random | |
| import os | |
| import pandas as pd | |
| import cv2 | |
| import matplotlib.pyplot as plt | |
| from sklearn.model_selection import KFold | |
| from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten | |
| from tensorflow.keras.optimizers import Adam | |
| from tensorflow.keras.models import Sequential | |
| from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay | |
| from tensorflow.keras.layers import Dropout | |
| from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint | |
| from sklearn.metrics import precision_score, recall_score, f1_score, classification_report | |
| # Suppress iCCP warning | |
| import warnings | |
| warnings.filterwarnings("ignore", category=UserWarning, message=".*iCCP:.*") | |
| # Define data paths | |
| train_real_folder = 'datasets/training_set/real/' | |
| train_fake_folder = 'datasets/training_set/fake/' | |
| test_real_folder = 'datasets/test_set/real/' | |
| test_fake_folder = 'datasets/test_set/fake/' | |
| # Load train image paths and labels | |
| train_image_paths = [] | |
| train_labels = [] | |
| # Load train_real image paths and labels | |
| for filename in os.listdir(train_real_folder): | |
| image_path = os.path.join(train_real_folder, filename) | |
| label = 0 # Real images have label 0 | |
| train_image_paths.append(image_path) | |
| train_labels.append(label) | |
| # Load train_fake image paths and labels | |
| for filename in os.listdir(train_fake_folder): | |
| image_path = os.path.join(train_fake_folder, filename) | |
| label = 1 # Fake images have label 1 | |
| train_image_paths.append(image_path) | |
| train_labels.append(label) | |
| # Load test image paths and labels | |
| test_image_paths = [] | |
| test_labels = [] | |
| # Load test_real image paths and labels | |
| for filename in os.listdir(test_real_folder): | |
| image_path = os.path.join(test_real_folder, filename) | |
| label = 0 # Assuming test real images are all real (label 0) | |
| test_image_paths.append(image_path) | |
| test_labels.append(label) | |
| # Load test_fake image paths and labels | |
| for filename in os.listdir(test_fake_folder): | |
| image_path = os.path.join(test_fake_folder, filename) | |
| label = 1 # Assuming test fake images are all fake (label 1) | |
| test_image_paths.append(image_path) | |
| test_labels.append(label) | |
| # Create DataFrames | |
| train_dataset = pd.DataFrame({'image_path': train_image_paths, 'label': train_labels}) | |
| test_dataset = pd.DataFrame({'image_path': test_image_paths, 'label': test_labels}) | |
| # Function to preprocess images | |
| def preprocess_image(image_path): | |
| """Loads, resizes, and normalizes an image.""" | |
| image = cv2.imread(image_path) | |
| resized_image = cv2.resize(image, (128, 128)) # Target size defined here | |
| normalized_image = resized_image.astype(np.float32) / 255.0 | |
| return normalized_image | |
| # Preprocess all images and convert labels to numpy arrays | |
| X = np.array([preprocess_image(path) for path in train_image_paths]) | |
| Y = np.array(train_labels) | |
| # Define discriminator network | |
| def build_discriminator(input_shape, dropout_rate=0.5): | |
| model = Sequential() | |
| model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape)) | |
| model.add(MaxPooling2D((2, 2))) | |
| model.add(Conv2D(64, (3, 3), activation='relu')) | |
| model.add(MaxPooling2D((2, 2))) | |
| model.add(Conv2D(64, (3, 3), activation='relu')) | |
| model.add(Flatten()) | |
| model.add(Dense(64, activation='relu')) | |
| model.add(Dropout(dropout_rate)) # Adding dropout layer | |
| model.add(Dense(1, activation='sigmoid')) | |
| return model | |
| # Function to check if previous weights exist | |
| def load_previous_weights(model, fold_number): | |
| weights_file = f'model_weights/model_fold_{fold_number}.weights.h5' | |
| if os.path.exists(weights_file): | |
| model.load_weights(weights_file) | |
| print(f"Loaded weights from {weights_file}") | |
| else: | |
| print("No previous weights found.") | |
| # Function to save weights if current accuracy is higher | |
| def save_updated_weights(model, fold_number, val_accuracy, best_accuracy): | |
| weights_file = f'model_weights/model_fold_{fold_number}.weights.h5' | |
| if val_accuracy > best_accuracy: | |
| model.save_weights(weights_file) | |
| print(f"Saved updated weights to {weights_file} with val_accuracy: {val_accuracy:.4f}") | |
| return val_accuracy | |
| else: | |
| print(f"Did not save weights for fold {fold_number} as val_accuracy {val_accuracy:.4f} is not better than {best_accuracy:.4f}") | |
| return best_accuracy | |
| # Set parameters for cross-validation | |
| kf = KFold(n_splits=4, shuffle=True, random_state=42) | |
| batch_size = 32 | |
| epochs = 15 | |
| # Lists to store accuracy and loss for each fold | |
| accuracy_per_fold = [] | |
| loss_per_fold = [] | |
| # Store the best accuracies for each fold | |
| best_accuracies = [0] * kf.get_n_splits() | |
| # Perform K-Fold Cross-Validation | |
| for fold_number, (train_index, val_index) in enumerate(kf.split(X), 1): | |
| X_train, X_val = X[train_index], X[val_index] | |
| Y_train, Y_val = Y[train_index], Y[val_index] | |
| # Create and compile model | |
| input_dim = X_train.shape[1:] # Dimensionality of the input images | |
| model = build_discriminator(input_dim) | |
| model.compile(loss='binary_crossentropy', optimizer=Adam(0.0002, 0.5), metrics=['accuracy']) | |
| # Load previous weights if they exist | |
| load_previous_weights(model, fold_number) | |
| # Define Early Stopping callback | |
| early_stopping = EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True) | |
| # Define ModelCheckpoint callback to save the best weights | |
| checkpoint = ModelCheckpoint(filepath=f'best_model_weights/model_fold_{fold_number}.best.weights.h5.keras', monitor='val_accuracy', save_best_only=True, mode='max') | |
| # Train the model with the callbacks | |
| history = model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=2, | |
| validation_data=(X_val, Y_val), callbacks=[early_stopping, checkpoint]) | |
| # Store the accuracy and loss for this folds | |
| average_val_accuracy = np.mean(history.history['val_accuracy']) | |
| accuracy_per_fold.append(average_val_accuracy) | |
| average_val_loss = np.mean(history.history['val_loss']) | |
| loss_per_fold.append(average_val_loss) | |
| # Save updated weights if accuracy is high | |
| best_accuracies[fold_number - 1] = save_updated_weights(model, fold_number, average_val_accuracy, best_accuracies[fold_number - 1]) | |
| # Print fold accuracy | |
| print(f'Fold {fold_number} average accuracy: {average_val_accuracy*100:.2f}%') | |
| # Print average accuracy across all folds | |
| print(f'Average accuracy across all folds: {np.mean(accuracy_per_fold)*100:.2f}%') | |
| # Load the model weights of the best model | |
| best_model_index = np.argmax(accuracy_per_fold) | |
| best_model_path = f'best_model_weights/model_fold_{best_model_index + 1}.best.weights.h5.keras' | |
| model.load_weights(best_model_path) | |
| # Evaluate the preprocessed test images using the best model | |
| test_loss, test_accuracy = model.evaluate(np.array([preprocess_image(path) for path in test_image_paths]), np.array(test_labels)) | |
| print(f"\nTest Loss: {test_loss}, Test Accuracy: {test_accuracy}") | |
| # Predict labels for the test set using the best model | |
| predictions = model.predict(np.array([preprocess_image(path) for path in test_image_paths])) | |
| predicted_labels = (predictions > 0.5).astype(int).flatten() | |
| # Summarize the classification results | |
| true_real_correct = np.sum((np.array(test_labels) == 0) & (predicted_labels == 0)) | |
| true_real_incorrect = np.sum((np.array(test_labels) == 0) & (predicted_labels == 1)) | |
| true_fake_correct = np.sum((np.array(test_labels) == 1) & (predicted_labels == 1)) | |
| true_fake_incorrect = np.sum((np.array(test_labels) == 1) & (predicted_labels == 0)) | |
| print("\nClassification Summary:") | |
| print(f"Real images correctly classified: {true_real_correct}") | |
| print(f"Real images incorrectly classified: {true_real_incorrect}") | |
| print(f"Fake images correctly classified: {true_fake_correct}") | |
| print(f"Fake images incorrectly classified: {true_fake_incorrect}") | |
| # Print detailed classification report | |
| print("\nClassification Report:") | |
| print(classification_report(test_labels, predicted_labels, target_names=['Real', 'Fake'])) | |
| print(model.summary()) | |
| # Plot confusion matrix | |
| cm = confusion_matrix(test_labels, predicted_labels) | |
| disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Real', 'Fake']) | |
| disp.plot(cmap=plt.cm.Blues) | |
| plt.title("Confusion Matrix") | |
| plt.show() | |
| # Plot training & validation accuracy values | |
| plt.figure(figsize=(12, 4)) | |
| plt.subplot(1, 2, 1) | |
| plt.plot(history.history['accuracy']) | |
| plt.plot(history.history['val_accuracy']) | |
| plt.title('Model accuracy') | |
| plt.ylabel('Accuracy') | |
| plt.xlabel('Epoch') | |
| plt.legend(['Train', 'Validation'], loc='upper left') | |
| plt.xticks(np.arange(0, len(history.history['accuracy']), step=1), np.arange(1, len(history.history['accuracy']) + 1, step=1)) | |
| # Plot training & validation loss values | |
| plt.subplot(1, 2, 2) | |
| plt.plot(history.history['loss']) | |
| plt.plot(history.history['val_loss']) | |
| plt.title('Model loss') | |
| plt.ylabel('Loss') | |
| plt.xlabel('Epoch') | |
| plt.legend(['Train', 'Validation'], loc='upper left') | |
| plt.xticks(np.arange(0, len(history.history['loss']), step=1), np.arange(1, len(history.history['loss']) + 1, step=1)) | |
| plt.tight_layout() | |
| plt.show() | |
| # Plot validation accuracy and loss per fold | |
| plt.figure(figsize=(12, 4)) | |
| plt.subplot(1, 2, 1) | |
| plt.plot(range(1, kf.get_n_splits() + 1), accuracy_per_fold, marker='o') | |
| plt.title('Validation Accuracy per Fold') | |
| plt.xlabel('Fold') | |
| plt.ylabel('Accuracy') | |
| plt.subplot(1, 2, 2) | |
| plt.plot(range(1, kf.get_n_splits() + 1), loss_per_fold, marker='o') | |
| plt.title('Validation Loss per Fold') | |
| plt.xlabel('Fold') | |
| plt.ylabel('Loss') | |
| plt.tight_layout() | |
| plt | |