# Breast Cancer Detection Model Analysis Validation import streamlit as st import numpy as np import tensorflow as tf # Load the trained model CNN_model.h5 model = tf.keras.models.load_model('CNN_model.h5') # Load the trained model X_test.npy X_test = np.load('X_test.npy') # Load the trained model y_test.npy y_test = np.load('y_test.npy') # Populate the X_test shape st.write('The shape of X_test = ', X_test.shape) # Populate the y_test shape st.write('The shape of y_test = ', y_test.shape) model.summary() # Populate the model summary st.write('The model summary =', model) # The output of y_test equals (55505, 2) 2-Dimensional Array [Reduce the Dimensionality into 1D] # Ensure y_test is a 1D array (Although, the inference drawn from y_test data is that it is a 2D array) if len(y_test.shape) > 1: st.write('The y_test.shape is more than 1 Dimension.') st.write('Reducing Dimensionality in y_test.shape array...') y_test = y_test.flatten() # Flatten the array if it is not 1D st.write('The y_test.shape after Dimensionality Reduction (Flattening): ', y_test.shape) st.write('Dimensionality Reduction on y_test complete!') # Define a mapping of class indices to human-readable labels class_labels = { 0: 'Non-Cancerous', 1: 'Cancerous', } # Define a function make predictions def predict_diagnosis(image_data, image_index): # Preprocess the image data (e.g., reshape, normalize) if len(image_data.shape) == 3: st.write('Implementation fails with: # image_data = image_data.reshape(1, image_data.shape[0], image_data.shape[1], image_data.shape[2])') st.write() st.write('Reshaping 3-Dimensional array into 4-Dimensional array not possible') st.write() st.write('Reshape an array of size 56250000 into a shape of (1,1,50,50)') st.write() st.write('Not possible because the total number of elements in the array (56250000)') st.write() st.write("Does not match the product of the new shapes dimensions (1150*50 = 5000)") st.write() st.write('Adding a new dimension obverse np.expand_dims not reshape') st.write() image_data = np.expand_dims(image_data, axis=0) image_data = image_data.astype('float32') / 255 # Make a prediction prediction = model.predict(image_data) # Get the predicted label index predicted_label_index = np.argmax(prediction) # Returns Highest Probability predicted_label = class_labels[predicted_label_index] # Convert the true label to an integer true_label_index = int(y_test[image_index]) true_label = class_labels[true_label_index] return predicted_label, true_label # Select an image index from the X_test dataset: 89 image_index = 89 image_data = X_test[image_index] # Predict the diagnosis predicted_label, true_label = predict_diagnosis(image_data, image_index=image_index) st.write('Predicted Diagnosis:', predicted_label) st.write('True Diagnosis:', true_label)