updated
Browse files
app.py
CHANGED
@@ -5,7 +5,8 @@ import numpy as np
|
|
5 |
from lime import lime_image
|
6 |
from skimage.segmentation import mark_boundaries
|
7 |
import matplotlib.pyplot as plt
|
8 |
-
from
|
|
|
9 |
|
10 |
hp = {}
|
11 |
hp['image_size'] = 512
|
@@ -24,12 +25,13 @@ hp['num_heads'] = 12
|
|
24 |
hp['dropout_rate'] = 0.1
|
25 |
hp['class_names'] = ["breast_benign", "breast_malignant"]
|
26 |
|
27 |
-
model =
|
28 |
-
model.load_weights("model/ResViT_for_breast_cancer_classification.keras")
|
29 |
print("Model initiated")
|
30 |
explainer = lime_image.LimeImageExplainer()
|
31 |
|
32 |
|
|
|
|
|
33 |
def main():
|
34 |
st.title("Breast Cancer Classification")
|
35 |
|
@@ -39,13 +41,14 @@ def main():
|
|
39 |
if uploaded_file is not None:
|
40 |
# Convert the uploaded file to OpenCV format
|
41 |
image = convert_to_opencv(uploaded_file)
|
|
|
42 |
|
43 |
# Display the uploaded image
|
44 |
st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True)
|
45 |
|
46 |
# Display the image shape
|
47 |
image_class = predict_single_image(image, model, hp)
|
48 |
-
|
49 |
st.write(f"Image Class: {image_class}")
|
50 |
|
51 |
|
@@ -81,17 +84,7 @@ def predict_single_image(image, model, hp):
|
|
81 |
return class_name
|
82 |
|
83 |
|
84 |
-
|
85 |
-
path = "lime_explanation.png"
|
86 |
-
tem = cv2.resize(image, [512,512])
|
87 |
-
gray_img = cv2.cvtColor(tem, cv2.COLOR_BGR2GRAY)
|
88 |
-
explanation = explainer.explain_instance(gray_img.astype('double'),
|
89 |
-
model.predict,
|
90 |
-
top_labels=1000, hide_color=0, num_samples=1000)
|
91 |
-
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=True)
|
92 |
-
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask), interpolation='nearest')
|
93 |
-
plt.savefig(path)
|
94 |
-
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
main()
|
|
|
5 |
from lime import lime_image
|
6 |
from skimage.segmentation import mark_boundaries
|
7 |
import matplotlib.pyplot as plt
|
8 |
+
from tensorflow.keras.models import load_model
|
9 |
+
from grad_cam import GradCam
|
10 |
|
11 |
hp = {}
|
12 |
hp['image_size'] = 512
|
|
|
25 |
hp['dropout_rate'] = 0.1
|
26 |
hp['class_names'] = ["breast_benign", "breast_malignant"]
|
27 |
|
28 |
+
model = load_model("model/resnet_for_breast_cancer-v1.h5")
|
|
|
29 |
print("Model initiated")
|
30 |
explainer = lime_image.LimeImageExplainer()
|
31 |
|
32 |
|
33 |
+
|
34 |
+
|
35 |
def main():
|
36 |
st.title("Breast Cancer Classification")
|
37 |
|
|
|
41 |
if uploaded_file is not None:
|
42 |
# Convert the uploaded file to OpenCV format
|
43 |
image = convert_to_opencv(uploaded_file)
|
44 |
+
gradCam = GradCam(model, image, last_conv_layer_name='conv5_block3_3_conv')
|
45 |
|
46 |
# Display the uploaded image
|
47 |
st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True)
|
48 |
|
49 |
# Display the image shape
|
50 |
image_class = predict_single_image(image, model, hp)
|
51 |
+
gradCam.save_and_display_gradcam()
|
52 |
st.write(f"Image Class: {image_class}")
|
53 |
|
54 |
|
|
|
84 |
return class_name
|
85 |
|
86 |
|
87 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
if __name__ == "__main__":
|
90 |
main()
|