File size: 1,545 Bytes
76fcfbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import tensorflow as tf
import keras.backend.tensorflow_backend as tb
tb._SYMBOLIC_SCOPE.value = True
import numpy as np
import gradio as gr
import cv2
from PIL import Image
from tensorflow.keras.models import load_model

# Get model weights
#os.system("wget https://github.com/hasibzunair/adversarial-lesions/releases/latest/download/MelaNet.h5")

# Load model
model = None
model = load_model("MelaNet.h5", compile=False)
model.summary()

examples = None #["./examples/covid.jpeg", "./examples/normal.jpeg"]
labels = ["Benign", "Malignant"]

# Helpers
def preprocess_image(img_array):
    # Normalize to [0,1]
    img_array = img_array.astype('float32')
    img_array /= 255
    # Check that images are 2D arrays
    if len(img_array.shape) > 2:
        img_array = img_array[:, :, 0]
    # Convert to 3-channel
    img_array = np.stack((img_array, img_array, img_array), axis=-1)
    # Convert to array
    img_array = cv2.resize(img_array, (224, 224))
    return img_array


def inference(image):
    img = preprocess_image(img)
    preds = model.predict(img)
    img = np.expand_dims(img, 0)
    # Predict
    preds = model.predict(image)[0]
    labels_probs = {labels[i]: float(preds[i]) for i, _ in enumerate(labels)}
    return labels_probs

gr.Interface(
    fn=inference,
    title="Benign or Malignant",
    description = "Predict if this image has benign or malignant symptoms",
    inputs="image",
    outputs="label",
    examples=examples,
).launch(debug=True, enable_queue=True)