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_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],
- ]
- new_persons_results =[[1000],
- [2000],
- [3000],
- [4000],
- [5000],
- [6000],
- [7000],
- [8000],
- [9000],
- [10000],
- [11000],
- [12000],
- [13000],
- [14000],
- [15000],
- [16000],
- [17000],
- [18000],
- [19000],
- [20000],
- [21000],
- [22000],
- [23000],
- [24000],
- [25000],
- ]
- 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 = 10
- num_testing_rows = 25
- 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]
- # Check image shapes
- print(f"Shape of image_training_resized[0]: {image_training_resized[0].shape}")
- print(f"Shape of image_testing_resized[0]: {image_testing_resized[0].shape}")
- # 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 = 200
- 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))
- np.set_printoptions(threshold=np.inf)
- # 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."
- # Check the new person's data before normalization
- print(f"Person {idx} original results: {personNEW_results}")
- personNEW_results_normalized = (np.array(personNEW_results) - mean_values) / std_values
- print(f"Person {idx} normalized results: {personNEW_results_normalized}")
- 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
- # Check the resized and normalized image
- print(f"Person {idx} resized and normalized image shape: {image_personNEW_resized_normalized.shape}")
- print(f"Person {idx} resized and normalized image: {image_personNEW_resized_normalized}")
- personNEW_prediction_cnn = cnn_model.predict(np.array([image_personNEW_resized_normalized]))
- print(f"Person {idx} prediction: {personNEW_prediction_cnn}")
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement