Spaces:
Build error
Build error
Commit
·
9d65827
1
Parent(s):
3237f99
created app
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from patchify import patchify
|
| 6 |
+
from huggingface_hub import from_pretrained_keras
|
| 7 |
+
|
| 8 |
+
model = from_pretrained_keras('ErnestBeckham/MulticancerViT')
|
| 9 |
+
|
| 10 |
+
hp = {}
|
| 11 |
+
hp['image_size'] = 512
|
| 12 |
+
hp['num_channels'] = 3
|
| 13 |
+
hp['patch_size'] = 64
|
| 14 |
+
hp['num_patches'] = (hp['image_size']**2) // (hp["patch_size"]**2)
|
| 15 |
+
hp["flat_patches_shape"] = (hp["num_patches"], hp['patch_size']*hp['patch_size']*hp["num_channels"])
|
| 16 |
+
hp['class_names'] = ['cervix_koc',
|
| 17 |
+
'cervix_dyk',
|
| 18 |
+
'cervix_pab',
|
| 19 |
+
'cervix_sfi',
|
| 20 |
+
'cervix_mep',
|
| 21 |
+
'colon_bnt',
|
| 22 |
+
'colon_aca',
|
| 23 |
+
'lung_aca',
|
| 24 |
+
'lung_bnt',
|
| 25 |
+
'lung_scc',
|
| 26 |
+
'oral_scc',
|
| 27 |
+
'oral_normal',
|
| 28 |
+
'kidney_tumor',
|
| 29 |
+
'kidney_normal',
|
| 30 |
+
'breast_benign',
|
| 31 |
+
'breast_malignant',
|
| 32 |
+
'lymph_fl',
|
| 33 |
+
'lymph_cll',
|
| 34 |
+
'lymph_mcl',
|
| 35 |
+
'brain_tumor',
|
| 36 |
+
'brain_glioma',
|
| 37 |
+
'brain_menin']
|
| 38 |
+
|
| 39 |
+
def main():
|
| 40 |
+
st.title("Multi-Cancer Classification")
|
| 41 |
+
|
| 42 |
+
# Upload image through drag and drop
|
| 43 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 44 |
+
|
| 45 |
+
if uploaded_file is not None:
|
| 46 |
+
# Convert the uploaded file to OpenCV format
|
| 47 |
+
image = convert_to_opencv(uploaded_file)
|
| 48 |
+
|
| 49 |
+
# Display the uploaded image
|
| 50 |
+
st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True)
|
| 51 |
+
|
| 52 |
+
# Display the image shape
|
| 53 |
+
image_class = predict_single_image(image, model, hp)
|
| 54 |
+
st.write(f"Image Class: {image_class}")
|
| 55 |
+
|
| 56 |
+
def convert_to_opencv(uploaded_file):
|
| 57 |
+
# Read the uploaded file using OpenCV
|
| 58 |
+
image_bytes = uploaded_file.read()
|
| 59 |
+
np_arr = np.frombuffer(image_bytes, np.uint8)
|
| 60 |
+
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 61 |
+
return image
|
| 62 |
+
|
| 63 |
+
def detect_image_shape(image):
|
| 64 |
+
# Get the image shape
|
| 65 |
+
return image.shape
|
| 66 |
+
|
| 67 |
+
def preprocess_image(image, hp):
|
| 68 |
+
# Resize the image to the expected input size
|
| 69 |
+
image = cv2.resize(image, (hp['image_size'], hp['image_size']))
|
| 70 |
+
# Normalize pixel values to be in the range [0, 1]
|
| 71 |
+
image = image / 255.0
|
| 72 |
+
# Extract patches using the same patching mechanism as during training
|
| 73 |
+
patch_shape = (hp['patch_size'], hp['patch_size'], hp['num_channels'])
|
| 74 |
+
patches = patchify(image, patch_shape, hp['patch_size'])
|
| 75 |
+
# Flatten the patches
|
| 76 |
+
patches = np.reshape(patches, hp['flat_patches_shape'])
|
| 77 |
+
# Convert the flattened patches into a format suitable for prediction
|
| 78 |
+
patches = patches.astype(np.float32)
|
| 79 |
+
|
| 80 |
+
return patches
|
| 81 |
+
|
| 82 |
+
def predict_single_image(image, model, hp):
|
| 83 |
+
# Preprocess the image
|
| 84 |
+
preprocessed_image = preprocess_image(image, hp)
|
| 85 |
+
# Convert the preprocessed image to a TensorFlow tensor if needed
|
| 86 |
+
preprocessed_image = tf.convert_to_tensor(preprocessed_image)
|
| 87 |
+
# Add an extra batch dimension (required for model.predict)
|
| 88 |
+
preprocessed_image = tf.expand_dims(preprocessed_image, axis=0)
|
| 89 |
+
# Make the prediction
|
| 90 |
+
predictions = model.predict(preprocessed_image)
|
| 91 |
+
|
| 92 |
+
np.around(predictions)
|
| 93 |
+
y_pred_classes = np.argmax(predictions, axis=1)
|
| 94 |
+
class_name = hp['class_names'][y_pred_classes[0]]
|
| 95 |
+
return class_name
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
main()
|