ErnestBeckham commited on
Commit
914808d
·
verified ·
1 Parent(s): 5ef584f

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -12
app.py CHANGED
@@ -7,25 +7,34 @@ 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
13
  hp['num_channels'] = 3
14
- hp['patch_size'] = 64
15
  hp['num_patches'] = (hp['image_size']**2) // (hp["patch_size"]**2)
16
  hp["flat_patches_shape"] = (hp["num_patches"], hp['patch_size']*hp['patch_size']*hp["num_channels"])
17
  hp['batch_size'] = 32
18
  hp['lr'] = 1e-4
19
  hp["num_epochs"] = 30
20
  hp['num_classes'] = 2
21
- hp["num_layers"] = 12
22
- hp["hidden_dim"] = 512
23
- hp["mlp_dim"] = 3072
24
- hp['num_heads'] = 12
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
 
@@ -40,16 +49,38 @@ def main():
40
 
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
 
55
  def convert_to_opencv(uploaded_file):
@@ -57,11 +88,12 @@ def convert_to_opencv(uploaded_file):
57
  image_bytes = uploaded_file.read()
58
  np_arr = np.frombuffer(image_bytes, np.uint8)
59
  image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
60
- return image
 
61
 
62
  def process_image_as_batch(image):
63
  #resize the image
64
- image = cv2.resize(image, [512, 512])
65
  #scale the image
66
  image = image / 255.0
67
  #change the data type of image
 
7
  import matplotlib.pyplot as plt
8
  from tensorflow.keras.models import load_model
9
  from grad_cam import GradCam
10
+ from vit import CNN_ViT
11
 
12
  hp = {}
13
+ hp['image_size'] = 256
14
  hp['num_channels'] = 3
15
+ hp['patch_size'] = 32
16
  hp['num_patches'] = (hp['image_size']**2) // (hp["patch_size"]**2)
17
  hp["flat_patches_shape"] = (hp["num_patches"], hp['patch_size']*hp['patch_size']*hp["num_channels"])
18
  hp['batch_size'] = 32
19
  hp['lr'] = 1e-4
20
  hp["num_epochs"] = 30
21
  hp['num_classes'] = 2
22
+ hp["num_layers"] = 6
23
+ hp["hidden_dim"] = 256
24
+ hp["mlp_dim"] = 256
25
+ hp['num_heads'] = 6
26
  hp['dropout_rate'] = 0.1
27
  hp['class_names'] = ["breast_benign", "breast_malignant"]
28
 
29
+ #model = load_model("model/resnet_for_breast_cancer-v1.h5")
30
+ model = CNN_ViT(hp)
31
+
32
+ model.compile(loss='binary_crossentropy',
33
+ optimizer = tf.keras.optimizers.Adam(hp['lr'], clipvalue=1.0),
34
+ metrics=['acc']
35
+ )
36
+ model.load_weights("model/Breast-ResViT.keras")
37
+
38
  print("Model initiated")
39
  explainer = lime_image.LimeImageExplainer()
40
 
 
49
 
50
  if uploaded_file is not None:
51
  # Convert the uploaded file to OpenCV format
52
+ image, gray_img = convert_to_opencv(uploaded_file)
53
+ gray_img = cv2.resize(gray_img, [256,256])
54
+ #gradCam = GradCam(model, image, last_conv_layer_name='conv5_block3_3_conv')
55
 
56
  # Display the uploaded image
57
  st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True)
58
 
59
  # Display the image shape
60
  image_class = predict_single_image(image, model, hp)
61
+ #gradCam.save_and_display_gradcam()
62
  st.write(f"Image Class: {image_class}")
63
+ explanation = explainer.explain_instance(
64
+ gray_img.astype('double'),
65
+ model.predict,
66
+ top_labels=4,
67
+ hide_color=0,
68
+ num_samples=500
69
+ )
70
+ temp, mask = explanation.get_image_and_mask(
71
+ explanation.top_labels[0],
72
+ positive_only=True,
73
+ num_features=4,
74
+ hide_rest=True
75
+ )
76
+
77
+ temp = (temp / 2 + 0.5)
78
+ xai = mark_boundaries(temp.clip(0, 1), mask)
79
+
80
+ # Save and display LIME explanation
81
+ lime_explanation_path = 'lime_explanation.png'
82
+ cv2.imwrite(lime_explanation_path, (xai * 255).astype(np.uint8))
83
+ st.image(lime_explanation_path, caption="LIME Explanation", use_column_width=True)
84
 
85
 
86
  def convert_to_opencv(uploaded_file):
 
88
  image_bytes = uploaded_file.read()
89
  np_arr = np.frombuffer(image_bytes, np.uint8)
90
  image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
91
+ gray_img = cv2.imdecode(np_arr, cv2.IMREAD_GRAYSCALE)
92
+ return image, gray_img
93
 
94
  def process_image_as_batch(image):
95
  #resize the image
96
+ image = cv2.resize(image, [256, 256])
97
  #scale the image
98
  image = image / 255.0
99
  #change the data type of image