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
- import sys
- 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, column_min_vals, column_max_vals):
- num_rows, num_columns = len(data), len(data[0])
- image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
- for i in range(num_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [column_min_vals[j], column_max_vals[j]], [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, column_min_vals, column_max_vals):
- 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], [column_min_vals[j], column_max_vals[j]], [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.Dropout(0.25),
- tf.keras.layers.Flatten(),
- tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
- tf.keras.layers.Dropout(0.5),
- 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/", "/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_training_rows:num_training_rows+num_testing_rows], labels[:num_training_rows], labels[num_training_rows: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
- column_min_vals = np.min(X_train_normalized, axis=0)
- column_max_vals = np.max(X_train_normalized, axis=0)
- # 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))
- dnn_model = tf.keras.Sequential([
- tf.keras.layers.Dense(128, activation='relu', input_shape=(len(X_train[0]),)),
- tf.keras.layers.BatchNormalization(),
- tf.keras.layers.Dropout(0.2), # Snížení dropout rate
- tf.keras.layers.Dense(64, activation='relu'),
- tf.keras.layers.BatchNormalization(),
- tf.keras.layers.Dropout(0.2),
- tf.keras.layers.Dense(32, activation='relu'),
- tf.keras.layers.BatchNormalization(),
- tf.keras.layers.Dropout(0.2),
- tf.keras.layers.Dense(16, activation='relu'),
- tf.keras.layers.BatchNormalization(),
- tf.keras.layers.Dropout(0.2),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- dnn_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
- # Generování dummy předpovědí pro tréninková data
- train_predictions = [0] * len(X_train_normalized) # Dummy předpovědi pro tréninkové obrázky
- # Generování skutečných předpovědí pro testovací data
- test_predictions = (dnn_model.predict(X_test_normalized) > 0.5).astype(int).flatten()
- # Generování obrázků z normalizovaných dat
- image_training = create_imageN(X_train_normalized, y_train, label_colors, column_min_vals, column_max_vals)
- image_testing = create_image(X_test_normalized, test_predictions, label_colors_testing, column_min_vals, column_max_vals)
- # Ověření počtu vygenerovaných obrázků
- print("Počet tréninkových obrázků:", len(image_training))
- print("Počet testovacích obrázků:", len(image_testing))
- assert len(image_training) == 50, "Počet tréninkových obrázků by měl být 50."
- assert len(image_testing) == len(X_test_normalized), "Počet testovacích obrázků by měl odpovídat počtu testovacích vzorků."
- # Změna velikosti obrázků na pevnou velikost pro vstup do CNN
- 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]
- # Ověření změny velikosti obrázků
- print("Tvar prvního tréninkového obrázku po změně velikosti:", image_training_resized[0].shape)
- print("Tvar prvního testovacího obrázku po změně velikosti:", image_testing_resized[0].shape)
- # Převod obrázků do formátu numpy pole
- X_train_cnn = np.array(image_training_resized)
- X_test_cnn = np.array(image_testing_resized)
- # Ověření tvaru polí
- print("Tvar X_train_cnn:", X_train_cnn.shape)
- print("Tvar X_test_cnn:", X_test_cnn.shape)
- assert X_train_cnn.shape == (50, 100, 100, 3), "Tvar X_train_cnn by měl být (50, 100, 100, 3)."
- # Funkce pro zobrazení obrázku
- def display_image(image, title="Image"):
- plt.imshow(image)
- plt.title(title)
- plt.axis('off')
- plt.show()
- # Zobrazení několika tréninkových obrázků
- for i in range(5):
- display_image(X_train_cnn[i], title=f"Tréninkový obrázek {i+1}")
- # Trénování modelu DNN
- dnn_accuracy_history = []
- epochs = 500 # Reduced to 500 for quicker convergence
- 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, column_min_vals, column_max_vals)
- 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, column_min_vals, column_max_vals)
- image_personNEW_dnn = create_imageN([personNEW_results_normalized], [personNEW_label_dnn], label_colors, column_min_vals, column_max_vals)
- 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 = tf.keras.Sequential([
- tf.keras.layers.InputLayer(input_shape=(100, 100, 3)),
- 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.Flatten(),
- tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
- tf.keras.layers.Dropout(0.5),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- cnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- # Training CNN Model
- cnn_accuracy_history = []
- for epoch in tqdm_notebook(range(epochs)):
- history_cnn = cnn_model.fit(X_train_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_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, column_min_vals, column_max_vals)
- 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
- image_personNEW = create_imageN([personNEW_results_normalized], [0], label_colors, column_min_vals, column_max_vals)
- image_personNEW_resized = resize(image_personNEW[:, :-1], (100, 100, 3))
- personNEW_prediction_cnn = cnn_model.predict(np.array([image_personNEW_resized]))
- personNEW_label_cnn = 1 if personNEW_prediction_cnn >= 0.5 else 0
- y_pred_after_50_epochs_cnn = cnn_model.predict(X_test_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, column_min_vals, column_max_vals)
- image_personNEW_cnn = create_imageN([personNEW_results_normalized], [personNEW_label_cnn], label_colors, column_min_vals, column_max_vals)
- 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_cnn) > 0.5).astype(int)
- print("PP:",X_test_cnn)
- 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
- target_prediction = 0.52
- input_shape = 4
- new_vector = np.random.randn(input_shape)
- new_vector = tf.Variable(new_vector, dtype=tf.float32)
- optimizer = tf.optimizers.Adam(learning_rate=0.1)
- def loss_function():
- prediction = dnn_model(tf.reshape(new_vector, (1, -1)))
- return tf.abs(prediction - target_prediction)
- # Gradientní sestup
- for _ in range(1000):
- optimizer.minimize(loss_function, [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))
- print("Výsledný vektor (normalizovaný):", result_vector)
- print("Výsledný vektor (denormalizovaný):", denormalized_vector)
- print("Predikce výsledného vektoru:", result_prediction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement