File size: 2,175 Bytes
76fcfbc
ad0d661
76fcfbc
 
 
 
 
 
 
 
ad0d661
 
 
76fcfbc
ad0d661
 
 
76fcfbc
 
 
 
 
 
f81999a
76fcfbc
 
 
 
 
ad0d661
76fcfbc
 
 
 
 
 
 
fc938e9
76fcfbc
 
 
d260a1c
bf5b3c7
1401578
8c6e8ab
76fcfbc
 
fc938e9
76fcfbc
602f825
76fcfbc
 
 
ad0d661
 
adff46a
eee1b26
 
ad0d661
 
76fcfbc
d260a1c
ad0d661
eee1b26
6a7d2d8
76fcfbc
20c5624
a8aea0c
92f6774
ad0d661
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import codecs
import tensorflow as tf
import keras.backend.tensorflow_backend as tb
import numpy as np
import gradio as gr
import cv2
from PIL import Image
from tensorflow.keras.models import load_model

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
tb._SYMBOLIC_SCOPE.value = True

# 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()

# Class label list
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, (256, 256))
    return img_array


# Main inference function
def inference(img_path):
    img = Image.open(img_path).convert("RGB")
    img = np.array(img)
    img = preprocess_image(img)
    img = np.expand_dims(img, 0)
    preds = model.predict(img)
    # Predict
    preds = model.predict(img)[0]
    labels_probs = {labels[i]: float(preds[i]) for i, _ in enumerate(labels)}
    return labels_probs


title = "Melanoma Detection using Adversarial Training and Deep Transfer Learning"
description = codecs.open("description.html", "r", "utf-8").read()
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2004.06824' target='_blank'>Melanoma Detection using Adversarial Training and Deep Transfer Learning</a> | <a href='https://github.com/hasibzunair/adversarial-lesions' target='_blank'>Github</a></p>"


demo = gr.Interface(
    fn=inference,
    title=title,
    description=description,
    article=article,
    inputs=gr.inputs.Image(type="filepath", label="Input"),
    outputs="label",
    examples=[f"examples/{fname}.png" for fname in ["benign", "malignant"]],
    allow_flagging="never",
    analytics_enabled=False,
)


demo.launch(
    # debug=True,
    # enable_queue=True
)