iSushant commited on
Commit
1740ae1
·
verified ·
1 Parent(s): a9fc71d

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +107 -0
  2. model.h5 +3 -0
  3. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import cv2
4
+ import tensorflow as tf
5
+ from PIL import Image
6
+ from keras import backend as K
7
+ from keras.metrics import Precision, Recall
8
+ from vit_keras import vit, utils, layers
9
+
10
+ # Page configuration
11
+ st.set_page_config(
12
+ page_title="Breast Cancer Classification",
13
+ page_icon="🏥",
14
+ layout="centered"
15
+ )
16
+
17
+ # Cache the model loading
18
+ @st.cache_resource
19
+ def load_model():
20
+ try:
21
+ model = tf.keras.models.load_model(
22
+ 'model.h5',
23
+ custom_objects={
24
+ 'ClassToken': layers.ClassToken,
25
+ 'f1_score': f1_score
26
+ },
27
+ compile=False
28
+ )
29
+ return model
30
+ except Exception as e:
31
+ st.error(f"Error loading model: {str(e)}")
32
+ return None
33
+
34
+ def f1_score(y_true, y_pred):
35
+ y_true = K.round(y_true)
36
+ y_pred = K.round(y_pred)
37
+
38
+ tp = K.sum(y_true * y_pred)
39
+ fp = K.sum((1 - y_true) * y_pred)
40
+ fn = K.sum(y_true * (1 - y_pred))
41
+
42
+ precision = tp / (tp + fp + K.epsilon())
43
+ recall = tp / (tp + fn + K.epsilon())
44
+
45
+ return 2 * precision * recall / (precision + recall + K.epsilon())
46
+
47
+ def process_image(image):
48
+ img_array = np.array(image)
49
+ image_pred = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
50
+ resized_image = cv2.resize(image_pred, (224, 224))
51
+ return np.array([resized_image], dtype='float32') / 255.0
52
+
53
+ # App header
54
+ st.header('🔬 Breast Cancer Classification using Ultrasound')
55
+
56
+ # Load model
57
+ model = load_model()
58
+
59
+ if model:
60
+ model.compile(
61
+ optimizer='adam',
62
+ loss='binary_crossentropy',
63
+ metrics=['accuracy', f1_score]
64
+ )
65
+
66
+ # File uploader
67
+ uploaded_file = st.file_uploader(
68
+ "Upload an ultrasound image",
69
+ type=['jpg', 'jpeg', 'png'],
70
+ help="Supported formats: JPG, JPEG, PNG"
71
+ )
72
+
73
+ if uploaded_file:
74
+ try:
75
+ # Display image
76
+ col1, col2 = st.columns(2)
77
+ with col1:
78
+ image = Image.open(uploaded_file)
79
+ st.image(image, caption='Uploaded Ultrasound Image', use_column_width=True)
80
+
81
+ # Process and predict
82
+ with st.spinner('Analyzing image...'):
83
+ processed_image = process_image(image)
84
+ predictions = model.predict(processed_image, verbose=0)
85
+ predicted_label_index = tf.argmax(predictions, axis=-1).numpy()[0]
86
+
87
+ # Display results
88
+ with col2:
89
+ st.subheader("Analysis Results")
90
+ labels = {'Benign': 0, 'Malignant': 1}
91
+ result = list(labels.keys())[list(labels.values()).index(predicted_label_index)]
92
+ confidence = float(predictions[0][predicted_label_index]) * 100
93
+
94
+ st.metric("Prediction", result)
95
+ st.metric("Confidence", f"{confidence:.2f}%")
96
+
97
+ if predicted_label_index == 1:
98
+ st.error("⚠️ Please consult with your doctor as results indicate potential malignancy.")
99
+ else:
100
+ st.success("✅ Results suggest benign characteristics.")
101
+
102
+ except Exception as e:
103
+ st.error(f"Error processing image: {str(e)}")
104
+ else:
105
+ st.error("Failed to load the model. Please check if model.h5 exists in the correct location.")
106
+
107
+
model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:62f040b88a6fcd237762656ef0d993a3f2588925fd1118045fdb1286a6cd2946
3
+ size 630297216
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ tensorflow
2
+ tensorflow-addons
3
+ tensorflow-estimator
4
+ streamlit
5
+ opencv-python
6
+ numpy
7
+ keras
8
+ vit-keras