|
import streamlit as st |
|
import tensorflow as tf |
|
import cv2 |
|
import numpy as np |
|
from patchify import patchify |
|
from huggingface_hub import from_pretrained_keras |
|
|
|
model = from_pretrained_keras('ErnestBeckham/MulticancerViT') |
|
|
|
hp = {} |
|
hp['image_size'] = 512 |
|
hp['num_channels'] = 3 |
|
hp['patch_size'] = 64 |
|
hp['num_patches'] = (hp['image_size']**2) // (hp["patch_size"]**2) |
|
hp["flat_patches_shape"] = (hp["num_patches"], hp['patch_size']*hp['patch_size']*hp["num_channels"]) |
|
hp['class_names'] = ['cervix_koc', |
|
'cervix_dyk', |
|
'cervix_pab', |
|
'cervix_sfi', |
|
'cervix_mep', |
|
'colon_bnt', |
|
'colon_aca', |
|
'lung_aca', |
|
'lung_bnt', |
|
'lung_scc', |
|
'oral_scc', |
|
'oral_normal', |
|
'kidney_tumor', |
|
'kidney_normal', |
|
'breast_benign', |
|
'breast_malignant', |
|
'lymph_fl', |
|
'lymph_cll', |
|
'lymph_mcl', |
|
'brain_tumor', |
|
'brain_glioma', |
|
'brain_menin'] |
|
|
|
def main(): |
|
st.title("Multi-Cancer Classification") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = convert_to_opencv(uploaded_file) |
|
|
|
|
|
st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
image_class = predict_single_image(image, model, hp) |
|
st.write(f"Image Class: {image_class}") |
|
|
|
def convert_to_opencv(uploaded_file): |
|
|
|
image_bytes = uploaded_file.read() |
|
np_arr = np.frombuffer(image_bytes, np.uint8) |
|
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) |
|
return image |
|
|
|
def detect_image_shape(image): |
|
|
|
return image.shape |
|
|
|
def preprocess_image(image, hp): |
|
|
|
image = cv2.resize(image, (hp['image_size'], hp['image_size'])) |
|
|
|
image = image / 255.0 |
|
|
|
patch_shape = (hp['patch_size'], hp['patch_size'], hp['num_channels']) |
|
patches = patchify(image, patch_shape, hp['patch_size']) |
|
|
|
patches = np.reshape(patches, hp['flat_patches_shape']) |
|
|
|
patches = patches.astype(np.float32) |
|
|
|
return patches |
|
|
|
def predict_single_image(image, model, hp): |
|
|
|
preprocessed_image = preprocess_image(image, hp) |
|
|
|
preprocessed_image = tf.convert_to_tensor(preprocessed_image) |
|
|
|
preprocessed_image = tf.expand_dims(preprocessed_image, axis=0) |
|
|
|
predictions = model.predict(preprocessed_image) |
|
|
|
np.around(predictions) |
|
y_pred_classes = np.argmax(predictions, axis=1) |
|
class_name = hp['class_names'][y_pred_classes[0]] |
|
return class_name |
|
|
|
if __name__ == "__main__": |
|
main() |