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
- from skimage.transform import resize
- 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):
- num_training_rows = len(data)
- num_columns = len(data[0])
- image_training = np.zeros((num_training_rows, num_columns + 1, 3), dtype=np.uint8)
- min_val = np.min(data)
- max_val = np.max(data)
- for i in range(num_training_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [min_val, max_val], [0, 255]))
- image_training[i, j] = np.array([pixel_value] * 3)
- image_training[i, -1] = label_colors[int(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.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
- # tf.keras.layers.MaxPooling2D(pool_size=(2, 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
- def create_cnn_model(input_shape):
- model = tf.keras.Sequential([
- tf.keras.layers.InputLayer(input_shape=input_shape),
- tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
- tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
- tf.keras.layers.Dropout(0.25),
- tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
- tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
- tf.keras.layers.Dropout(0.25),
- tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),
- tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
- tf.keras.layers.Dropout(0.25),
- tf.keras.layers.Conv2D(filters=256, kernel_size=(3, 3), activation='relu'),
- tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
- tf.keras.layers.Flatten(),
- tf.keras.layers.Dense(512, activation='relu'),
- tf.keras.layers.Dropout(0.5),
- 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
- def create_dnn_model(input_shape):
- model = tf.keras.Sequential([
- tf.keras.layers.Dense(128, activation='relu', input_shape=(input_shape,)),
- 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
- new_persons_results = [
- [0.030391238492519845, 0.23021081913032299, 0.4743575198860915, 0.639395348276238],
- [0.19790381537769108, 0.37639843860181527, 0.5676528538456297, 0.716530820399044],
- [0.0035245462826666075, 0.23127629815305784, 0.4802171123709532, 0.6591272725083992],
- [0.059230621364548486, 0.24424510845680134, 0.442553808602372, 0.6891856336835676],
- [0.05536813173866345, 0.2538888869331579, 0.47861285542743165, 0.6200559751500355],
- [0.1300359168058454, 0.38443677757577344, 0.5957238735056223, 0.795823160451845],
- [0.1743368240338569, 0.3713129035302336, 0.5640350202165867, 0.7213527928848786],
- [0.09173335232875372, 0.2559096689549753, 0.49527436563146954, 0.6970388573439903],
- [0.015235204378572087, 0.2284904031445293, 0.46613902406934005, 0.6917336579549159],
- [0.0011416656054787145, 0.24567669307188245, 0.4388400949432476, 0.667323193441009],
- [0.11776711, 0.17521301, 0.5074825, 0.8509191],
- [0.12314088, 0.27565651, 0.52214202, 0.77386896],
- ]
- uploaded = files.upload()
- for filename in uploaded.keys():
- original_path = f"/content/{filename}"
- destination_path = os.path.join("/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 = 30
- #X_train, X_test, y_train, y_test = data[:num_training_rows], data[num_training_rows:num_training_rows + num_testing_rows], labels[:num_training_rows], labels[num_training_rows + num_testing_rows]
- 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
- # Verify normalization
- print("Mean of X_train_normalized (should be close to 0):", np.mean(X_train_normalized, axis=0))
- print("Std of X_train_normalized (should be close to 1):", np.std(X_train_normalized, axis=0))
- # Generate images from normalized data for CNN
- image_training = create_imageN(X_train_normalized, y_train, label_colors)
- image_testing = create_imageN(X_test_normalized, y_test, label_colors_testing)
- # Resize images to a fixed size for CNN input
- image_training_resized = [resize(img[:, :-1], (100, 100, 3)) for img in image_training]
- image_testing_resized = [resize(img[:, :-1], (100, 100, 3)) for img in image_testing]
- print(image_training)
- # Reshape images for CNN
- X_train_cnn = np.array(image_training_resized)
- X_test_cnn = np.array(image_testing_resized)
- mean_train_cnn = np.mean(X_train_cnn, axis=(0, 1, 2))
- std_train_cnn = np.std(X_train_cnn, axis=(0, 1, 2))
- X_train_cnn_normalized = (X_train_cnn - mean_train_cnn) / std_train_cnn
- X_test_cnn_normalized = (X_test_cnn - mean_train_cnn) / std_train_cnn
- # DNN Model
- dnn_model = create_dnn_model(len(X_train[0]))
- # Training DNN Model
- dnn_accuracy_history = []
- epochs = 150
- 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")
- # 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((100, 100, 3))
- # Training CNN Model
- cnn_accuracy_history = []
- for epoch in tqdm_notebook(range(epochs)):
- history_cnn = cnn_model.fit(X_train_cnn_normalized, 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_cnn_normalized)
- 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")
- # 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
- image_personNEW = create_imageN([personNEW_results_normalized], [0], label_colors)
- image_personNEW_resized = resize(image_personNEW[:, :-1], (100, 100, 3))
- image_personNEW_resized_normalized = (image_personNEW_resized - mean_train_cnn) / std_train_cnn # Normalize new person image
- personNEW_prediction_cnn = cnn_model.predict(np.array([image_personNEW_resized_normalized]))
- personNEW_label_cnn = 1 if personNEW_prediction_cnn >= 0.5 else 0
- y_pred_after_epochs_cnn = cnn_model.predict(X_test_cnn_normalized)
- y_pred_binary_after_epochs_cnn = [1 if pred >= 0.5 else 0 for pred in y_pred_after_epochs_cnn]
- image_testing_after_epochs_cnn = create_image(X_test_normalized, y_pred_binary_after_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_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_cnn_normalized) > 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()
- # Optimalizace nového vektoru pro predikci co nejblíže 0.52 a 1.00
- target_predictions = [0.52, 1.00]
- input_shape = 4
- results = {}
- def loss_function(target):
- prediction = dnn_model(tf.reshape(new_vector, (1, -1)))
- return tf.abs(prediction - target)
- for target in target_predictions:
- new_vector = tf.Variable(np.random.randn(input_shape), dtype=tf.float32)
- optimizer = tf.optimizers.Adam(learning_rate=0.1)
- optimizer.build([new_vector])
- for _ in range(1000):
- optimizer.minimize(lambda: loss_function(target), [new_vector])
- # Denormalizace výsledného vektoru
- result_vector = new_vector.numpy()
- denormalized_vector = result_vector * std_values + mean_values
- result_prediction = dnn_model.predict(result_vector.reshape(1, -1))
- results[target] = {
- "normalized": result_vector,
- "denormalized": denormalized_vector,
- "prediction": result_prediction
- }
- print(f"Výsledný vektor (normalizovaný) pro target {target}: {result_vector}")
- print(f"Výsledný vektor (denormalizovaný) pro target {target}: {denormalized_vector}")
- print(f"Predikce výsledného vektoru pro target {target}: {result_prediction}")
- # Funkce pro vytvoření vstupu pro CNN s 4 šedo-bílo-černými čtverečky a 5. barevným čtverečkem
- def create_cnn_input_image(vector, target_shape, prediction):
- image = np.ones(target_shape, dtype=np.float32) * 255 # bílé pozadí
- square_size = target_shape[0] // 3 # velikost čtverců
- # Vyplnění 4 čtverečků hodnotami z vektoru (šedo-bílo-černé)
- for i in range(2):
- for j in range(2):
- value = vector[i * 2 + j]
- gray_value = int((value - np.min(vector)) / (np.max(vector) - np.min(vector)) * 255)
- image[i*square_size:(i+1)*square_size, j*square_size:(j+1)*square_size] = gray_value
- # 5. čtvereček na základě predikce
- color = [0, 255, 0] if prediction >= 0.5 else [255, 0, 0]
- image[2*square_size:, :square_size] = color # zelený nebo červený čtvereček
- return image
- # Připravit vstupy pro CNN model
- target_shape = (100, 100, 3)
- cnn_inputs = {target: create_cnn_input_image(result["denormalized"], target_shape, result["prediction"]) for target, result in results.items()}
- # Normalizace vstupů pro CNN model
- cnn_inputs_normalized = {target: (input_image - mean_train_cnn) / std_train_cnn for target, input_image in cnn_inputs.items()}
- # Predikce CNN modelu
- for target, input_image in cnn_inputs_normalized.items():
- input_image_reshaped = input_image.reshape(1, *target_shape)
- cnn_prediction = cnn_model.predict(input_image_reshaped)
- print(f"Predikce CNN modelu pro target {target}: {cnn_prediction}")
- # Zobrazení obrazů
- for target, input_image in cnn_inputs.items():
- plt.figure(figsize=(5, 5))
- plt.imshow(input_image.astype(np.uint8))
- plt.title(f"Target {target} - CNN Input Image")
- plt.axis("off")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement