-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.py
42 lines (33 loc) · 1.35 KB
/
app.py
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
import tensorflow as tf
import numpy as np
from PIL import Image
from weights import download_weights
from model import download_model
import cv2
import gradio as gr
def main():
def load_model():
download_model()
model=tf.keras.models.load_model("model/model.h5")
model.compile(optimizer =tf.keras.optimizers.Adam(learning_rate=0.00001,decay=0.0001),metrics=["accuracy"],
loss= tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1))
download_weights()
model.load_weights("weights/modeldense1.h5")
return model
model=load_model()
def preprocess(image):
kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
im = cv2.filter2D(image, -1, kernel)
return im
image=gr.inputs.Image(shape=(224,224))
label=gr.outputs.Label(num_top_classes=8)
class_name=['Benign with Density=1','Malignant with Density=1','Benign with Density=2','Malignant with Density=2','Benign with Density=3','Malignant with Density=3','Benign with Density=4','Malignant with Density=4']
def predict_img(img):
img=preprocess(img)
img=img/255.0
im=img.reshape(-1,224,224,3)
pred=model.predict(im)[0]
return {class_name[i]:float(pred[i]) for i in range(8)}
gr.Interface(fn=predict_img,inputs=image,outputs=label,capture_session=True).launch(debug='True',share=True)
if __name__=='__main__':
main()