ErnestBeckham commited on
Commit ·
78cb732
1
Parent(s): cc6af3f
application updated
Browse files
app.py
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from huggingface_hub import from_pretrained_keras
|
| 6 |
+
from lime import lime_image
|
| 7 |
+
from skimage.segmentation import mark_boundaries
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
model = from_pretrained_keras('ErnestBeckham/BreastResViT')
|
| 13 |
+
explainer = lime_image.LimeImageExplainer()
|
| 14 |
+
|
| 15 |
+
hp = {}
|
| 16 |
+
hp['class_names'] = ["breast_benign", "breast_malignant"]
|
| 17 |
+
|
| 18 |
+
def main():
|
| 19 |
+
st.title("Breast Cancer Classification")
|
| 20 |
+
|
| 21 |
+
# Upload image through drag and drop
|
| 22 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 23 |
+
|
| 24 |
+
if uploaded_file is not None:
|
| 25 |
+
# Convert the uploaded file to OpenCV format
|
| 26 |
+
image = convert_to_opencv(uploaded_file)
|
| 27 |
+
|
| 28 |
+
# Display the uploaded image
|
| 29 |
+
st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True)
|
| 30 |
+
|
| 31 |
+
# Display the image shape
|
| 32 |
+
image_class = predict_single_image(image, model, hp)
|
| 33 |
+
st.write(f"Image Class: {image_class}")
|
| 34 |
+
|
| 35 |
+
def convert_to_opencv(uploaded_file):
|
| 36 |
+
# Read the uploaded file using OpenCV
|
| 37 |
+
image_bytes = uploaded_file.read()
|
| 38 |
+
np_arr = np.frombuffer(image_bytes, np.uint8)
|
| 39 |
+
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 40 |
+
return image
|
| 41 |
+
|
| 42 |
+
def process_image_as_batch(image):
|
| 43 |
+
#resize the image
|
| 44 |
+
image = cv2.resize(image, [512, 512])
|
| 45 |
+
#scale the image
|
| 46 |
+
image = image / 255.0
|
| 47 |
+
#change the data type of image
|
| 48 |
+
image = image.astype(np.float32)
|
| 49 |
+
return image
|
| 50 |
+
|
| 51 |
+
def predict_single_image(image, model, hp):
|
| 52 |
+
# Preprocess the image
|
| 53 |
+
preprocessed_image = process_image_as_batch(image)
|
| 54 |
+
# Convert the preprocessed image to a TensorFlow tensor if needed
|
| 55 |
+
preprocessed_image = tf.convert_to_tensor(preprocessed_image)
|
| 56 |
+
# Add an extra batch dimension (required for model.predict)
|
| 57 |
+
preprocessed_image = tf.expand_dims(preprocessed_image, axis=0)
|
| 58 |
+
# Make the prediction
|
| 59 |
+
predictions = model.predict(preprocessed_image)
|
| 60 |
+
|
| 61 |
+
np.around(predictions)
|
| 62 |
+
y_pred_classes = np.argmax(predictions, axis=1)
|
| 63 |
+
class_name = hp['class_names'][y_pred_classes[0]]
|
| 64 |
+
return class_name
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def xai_result(image):
|
| 68 |
+
path = "lime_explanation.png"
|
| 69 |
+
tem = cv2.resize(image, [512,512])
|
| 70 |
+
gray_img = cv2.cvtColor(tem, cv2.COLOR_BGR2GRAY)
|
| 71 |
+
explanation = explainer.explain_instance(gray_img.astype('double'),
|
| 72 |
+
model.predict,
|
| 73 |
+
top_labels=1000, hide_color=0, num_samples=1000)
|
| 74 |
+
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=True)
|
| 75 |
+
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask), interpolation='nearest')
|
| 76 |
+
plt.savefig(path)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|