Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import tensorflow as tf
- from tqdm.notebook import tqdm_notebook
- from IPython.display import display, Javascript
- from google.colab import files
- import os
- import shutil
- import ast
- from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- import seaborn as sns
- display(Javascript('IPython.OutputArea.auto_scroll_threshold = 9999;'))
- label_colors = {0: [0, 128, 0], 1: [255, 0, 0]}
- label_colors_testing = {0: [0, 128, 0], 1: [255, 0, 0]}
- %matplotlib inline
- def create_image(data, predictions, label_colors):
- num_rows, num_columns = len(data), len(data[0])
- image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
- min_val = np.min(data)
- max_val = np.max(data)
- for i in range(num_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [min_val, max_val], [0, 255]))
- image[i, j] = np.array([pixel_value] * 3)
- image[i, -1] = label_colors[predictions[i]]
- return image
- def create_imageN(data, predictions, label_colors=None):
- num_training_rows = len(data)
- num_columns = len(data[0])
- image_training = np.zeros((num_training_rows, num_columns + 1, 3), dtype=np.uint8)
- for i in range(num_training_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [-3, 3], [0, 255]))
- image_training[i, j] = np.array([pixel_value] * 3)
- if label_colors is not None:
- image_training[i, -1] = label_colors[predictions[i]]
- return image_training
- def create_cnn_model(input_shape):
- model = tf.keras.Sequential([
- tf.keras.layers.InputLayer(input_shape=input_shape),
- tf.keras.layers.Reshape((input_shape[0], 1)),
- tf.keras.layers.Conv1D(filters=32, kernel_size=2, activation='relu'),
- tf.keras.layers.MaxPooling1D(pool_size=2),
- tf.keras.layers.Flatten(),
- tf.keras.layers.Dense(64, activation='relu'),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- return model
- uploaded = files.upload()
- for filename in uploaded.keys():
- original_path = f"/content/{filename}"
- destination_path = os.path.join("/content/", "/content/DATA2")
- shutil.move(original_path, destination_path)
- print(f"Soubor {filename} byl přesunut do {destination_path}")
- file_path = '/content/DATA2'
- with open(file_path, 'r') as file:
- code = file.read()
- A_list = ast.literal_eval(code)
- A = np.array(A_list)
- labels = [results[-1] for results in A]
- data = [results[:-1] for results in A]
- num_training_rows = 50
- num_testing_rows = 50
- X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
- mean_values = np.mean(X_train, axis=0)
- std_values = np.std(X_train, axis=0)
- X_train_normalized = (X_train - mean_values) / std_values
- X_test_normalized = (X_test - mean_values) / std_values
- # DNN Model
- dnn_model = tf.keras.Sequential([
- tf.keras.layers.Dense(128, activation='relu', input_shape=(len(X_train[0]),)),
- tf.keras.layers.Dense(64, activation='relu'),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- dnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- # Training DNN Model
- dnn_accuracy_history = []
- epochs = 600
- for epoch in tqdm_notebook(range(epochs)):
- history_dnn = dnn_model.fit(X_train_normalized, np.array(y_train), epochs=1, verbose=0, shuffle=False)
- dnn_accuracy_history.append(history_dnn.history['accuracy'][0])
- if epoch == 1:
- y_pred_after_2nd_epoch_dnn = dnn_model.predict(X_test_normalized)
- y_pred_binary_after_2nd_epoch_dnn = [1 if pred >= 0.5 else 0 for pred in y_pred_after_2nd_epoch_dnn]
- image_testing_before_2nd_epoch_dnn = create_image(X_test_normalized, y_pred_binary_after_2nd_epoch_dnn, label_colors_testing)
- if epoch >= epochs-1:
- print(f"HERE HERE Epoch: {epoch}, Epochs: {epochs}\n")
- sys.stdout.flush()
- # Iterate through new persons
- for idx, personNEW_results in enumerate(new_persons_results, start=1):
- assert len(personNEW_results) == len(X_train[0]), "Mismatch in the number of features."
- personNEW_results_normalized = (np.array(personNEW_results) - mean_values) / std_values
- personNEW_prediction_dnn = dnn_model.predict(np.array([personNEW_results_normalized]))
- personNEW_label_dnn = 1 if personNEW_prediction_dnn >= 0.5 else 0
- y_pred_after_50_epochs_dnn = dnn_model.predict(X_test_normalized)
- y_pred_binary_after_50_epochs_dnn = [1 if pred >= 0.5 else 0 for pred in y_pred_after_50_epochs_dnn]
- image_testing_after_50_epochs_dnn = create_image(X_test_normalized, y_pred_binary_after_50_epochs_dnn, label_colors_testing)
- image_personNEW_dnn = create_imageN([personNEW_results_normalized], [personNEW_label_dnn], label_colors)
- plt.figure(figsize=(5, 5))
- plt.imshow(image_personNEW_dnn)
- plt.title(f"New Person {idx} - DNN\nLabel: {personNEW_label_dnn}, Prediction: {personNEW_prediction_dnn}")
- plt.axis("off")
- plt.show()
- # CNN Model
- cnn_model = create_cnn_model((len(X_train[0]), 1))
- # Preparing data for CNN
- X_train_normalized_cnn = X_train_normalized.reshape((X_train_normalized.shape[0], X_train_normalized.shape[1], 1))
- X_test_normalized_cnn = X_test_normalized.reshape((X_test_normalized.shape[0], X_test_normalized.shape[1], 1))
- # Training CNN Model
- cnn_accuracy_history = []
- for epoch in tqdm_notebook(range(epochs)):
- history_cnn = cnn_model.fit(X_train_normalized_cnn, np.array(y_train), epochs=1, verbose=0, shuffle=False)
- cnn_accuracy_history.append(history_cnn.history['accuracy'][0])
- if epoch == 1:
- y_pred_after_2nd_epoch_cnn = cnn_model.predict(X_test_normalized_cnn)
- y_pred_binary_after_2nd_epoch_cnn = [1 if pred >= 0.5 else 0 for pred in y_pred_after_2nd_epoch_cnn]
- image_testing_before_2nd_epoch_cnn = create_image(X_test_normalized, y_pred_binary_after_2nd_epoch_cnn, label_colors_testing)
- if epoch >= epochs-1:
- print(f"HERE HERE Epoch: {epoch}, Epochs: {epochs}\n")
- sys.stdout.flush()
- # Iterate through new persons
- for idx, personNEW_results in enumerate(new_persons_results, start=1):
- assert len(personNEW_results) == len(X_train[0]), "Mismatch in the number of features."
- personNEW_results_normalized = (np.array(personNEW_results) - mean_values) / std_values
- personNEW_results_normalized_cnn = personNEW_results_normalized.reshape((len(personNEW_results_normalized), 1))
- personNEW_prediction_cnn = cnn_model.predict(np.array([personNEW_results_normalized_cnn]))
- personNEW_label_cnn = 1 if personNEW_prediction_cnn >= 0.5 else 0
- y_pred_after_50_epochs_cnn = cnn_model.predict(X_test_normalized_cnn)
- y_pred_binary_after_50_epochs_cnn = [1 if pred >= 0.5 else 0 for pred in y_pred_after_50_epochs_cnn]
- image_testing_after_50_epochs_cnn = create_image(X_test_normalized, y_pred_binary_after_50_epochs_cnn, label_colors_testing)
- image_personNEW_cnn = create_imageN([personNEW_results_normalized], [personNEW_label_cnn], label_colors)
- plt.figure(figsize=(5, 5))
- plt.imshow(image_personNEW_cnn)
- plt.title(f"New Person {idx} - CNN\nLabel: {personNEW_label_cnn}, Prediction: {personNEW_prediction_cnn}")
- plt.axis("off")
- plt.show()
- # Display the images
- plt.figure(figsize=(25, 15))
- plt.subplot(2, 2, 1)
- plt.imshow(image_training)
- plt.title("Training Data")
- plt.axis("off")
- plt.subplot(2, 2, 2)
- plt.imshow(image_testing_before_2nd_epoch_dnn)
- plt.title("Testing Data (2nd Epoch) - DNN")
- plt.axis("off")
- plt.subplot(2, 2, 3)
- plt.imshow(image_testing_after_50_epochs_dnn)
- plt.title(f"Testing Data ({epochs} Epochs) - DNN")
- plt.axis("off")
- plt.subplot(2, 2, 4)
- plt.imshow(image_personNEW_dnn)
- plt.title(f"New Person - DNN\nLabel: {personNEW_label_dnn},[{personNEW_prediction_dnn}]")
- plt.axis("off")
- plt.figure(figsize=(12, 5))
- plt.plot(range(1, epochs + 1), dnn_accuracy_history, marker='o')
- plt.title('DNN Accuracy Over Epochs')
- plt.xlabel('Epochs')
- plt.ylabel('Accuracy')
- plt.grid()
- plt.figure(figsize=(25, 15))
- plt.subplot(2, 2, 1)
- plt.imshow(image_training)
- plt.title("Training Data")
- plt.axis("off")
- plt.subplot(2, 2, 2)
- plt.imshow(image_testing_before_2nd_epoch_cnn)
- plt.title("Testing Data (2nd Epoch) - CNN")
- plt.axis("off")
- plt.subplot(2, 2, 3)
- plt.imshow(image_testing_after_50_epochs_cnn)
- plt.title(f"Testing Data ({epochs} Epochs) - CNN")
- plt.axis("off")
- plt.subplot(2, 2, 4)
- plt.imshow(image_personNEW_cnn)
- plt.title(f"New Person - CNN\nLabel: {personNEW_label_cnn},[{personNEW_prediction_cnn}]")
- plt.axis("off")
- plt.figure(figsize=(12, 5))
- plt.plot(range(1, epochs + 1), cnn_accuracy_history, marker='o')
- plt.title('CNN Accuracy Over Epochs')
- plt.xlabel('Epochs')
- plt.ylabel('Accuracy')
- plt.grid()
- # Confusion Matrix and Performance Metrics for DNN
- dnn_predictions = (dnn_model.predict(X_test_normalized) > 0.5).astype(int)
- dnn_conf_matrix = confusion_matrix(y_test, dnn_predictions)
- print(f"Confusion Matrix for DNN:\n{dnn_conf_matrix}")
- dnn_accuracy = accuracy_score(y_test, dnn_predictions)
- dnn_precision = precision_score(y_test, dnn_predictions)
- dnn_recall = recall_score(y_test, dnn_predictions)
- dnn_f1 = f1_score(y_test, dnn_predictions)
- print(f"DNN Accuracy: {dnn_accuracy:.4f}")
- print(f"DNN Precision: {dnn_precision:.4f}")
- print(f"DNN Recall: {dnn_recall:.4f}")
- print(f"DNN F1 Score: {dnn_f1:.4f}")
- # Confusion Matrix and Performance Metrics for CNN
- cnn_predictions = (cnn_model.predict(X_test_normalized_cnn) > 0.5).astype(int)
- cnn_conf_matrix = confusion_matrix(y_test, cnn_predictions)
- print(f"Confusion Matrix for CNN:\n{cnn_conf_matrix}")
- cnn_accuracy = accuracy_score(y_test, cnn_predictions)
- cnn_precision = precision_score(y_test, cnn_predictions)
- cnn_recall = recall_score(y_test, cnn_predictions)
- cnn_f1 = f1_score(y_test, cnn_predictions)
- print(f"CNN Accuracy: {cnn_accuracy:.4f}")
- print(f"CNN Precision: {cnn_precision:.4f}")
- print(f"CNN Recall: {cnn_recall:.4f}")
- print(f"CNN F1 Score: {cnn_f1:.4f}")
- # Display confusion matrices
- plt.figure(figsize=(12, 5))
- plt.subplot(1, 2, 1)
- sns.heatmap(dnn_conf_matrix, annot=True, fmt='d', cmap='Blues')
- plt.xlabel('Predicted')
- plt.ylabel('Actual')
- plt.title('DNN Confusion Matrix')
- plt.subplot(1, 2, 2)
- sns.heatmap(cnn_conf_matrix, annot=True, fmt='d', cmap='Blues')
- plt.xlabel('Predicted')
- plt.ylabel('Actual')
- plt.title('CNN Confusion Matrix')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement