Spaces:
Sleeping
Sleeping
crazyscientist
commited on
updated st.cache
Browse files- appTest.py +62 -1
appTest.py
CHANGED
@@ -4,6 +4,17 @@ from modInference import main
|
|
4 |
import numpy as np
|
5 |
import math
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
st.set_page_config(layout="wide")
|
8 |
st.markdown("")
|
9 |
|
@@ -18,6 +29,56 @@ st.subheader("Enter an image of a thin blood smear. Preview the image and run th
|
|
18 |
|
19 |
input, outputIm = st.columns(2)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
with input:
|
22 |
st.header("Input")
|
23 |
imageInput = st.file_uploader("Enter an image of a thin blood smear.")
|
@@ -26,7 +87,7 @@ with input:
|
|
26 |
image = Image.open(imageInput)
|
27 |
ogInp = image
|
28 |
img_array = np.array(image)
|
29 |
-
output, cellImgs = main(
|
30 |
showImg = Image.fromarray(output)
|
31 |
if st.button("Preview"):
|
32 |
if imageInput is not None:
|
|
|
4 |
import numpy as np
|
5 |
import math
|
6 |
|
7 |
+
import numpy as np
|
8 |
+
from skimage import transform
|
9 |
+
import os
|
10 |
+
from keras.models import Model
|
11 |
+
from keras.applications.vgg16 import VGG16, preprocess_input
|
12 |
+
from keras.layers import Dense, Dropout, Flatten
|
13 |
+
import numpy as np
|
14 |
+
|
15 |
+
import torch
|
16 |
+
from models.create_fasterrcnn_model import create_model
|
17 |
+
|
18 |
st.set_page_config(layout="wide")
|
19 |
st.markdown("")
|
20 |
|
|
|
29 |
|
30 |
input, outputIm = st.columns(2)
|
31 |
|
32 |
+
@st.cache_resource
|
33 |
+
def load_model():
|
34 |
+
NUM_CLASSES = 2
|
35 |
+
CLASSES = ['__background__', 'Cell']
|
36 |
+
|
37 |
+
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
38 |
+
print(DEVICE)
|
39 |
+
checkpoint = torch.load("best_model.pth", map_location=DEVICE)
|
40 |
+
|
41 |
+
NUM_CLASSES = checkpoint['config']['NC']
|
42 |
+
CLASSES = checkpoint['config']['CLASSES']
|
43 |
+
build_model = create_model[checkpoint['model_name']]
|
44 |
+
|
45 |
+
model = build_model(num_classes=NUM_CLASSES, coco_model=False)
|
46 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
47 |
+
model.to(DEVICE).eval()
|
48 |
+
|
49 |
+
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
|
50 |
+
|
51 |
+
conv_base = VGG16(include_top=False,
|
52 |
+
weights='imagenet',
|
53 |
+
input_shape=(200,200,3))
|
54 |
+
|
55 |
+
if 2 > 0:
|
56 |
+
for layer in conv_base.layers[:-2]:
|
57 |
+
layer.trainable = False
|
58 |
+
else:
|
59 |
+
for layer in conv_base.layers:
|
60 |
+
layer.trainable = False
|
61 |
+
|
62 |
+
|
63 |
+
top_model = conv_base.output
|
64 |
+
top_model = Flatten(name="flatten")(top_model)
|
65 |
+
top_model = Dense(4096, activation='relu')(top_model)
|
66 |
+
top_model = Dense(1048, activation='relu')(top_model)
|
67 |
+
top_model = Dense(256, activation='relu')(top_model)
|
68 |
+
top_model = Dense(128, activation='relu')(top_model)
|
69 |
+
top_model = Dense(64, activation='relu')(top_model)
|
70 |
+
top_model = Dropout(0.2)(top_model)
|
71 |
+
output_layer = Dense(5, activation='softmax')(top_model)
|
72 |
+
|
73 |
+
CNN = Model(inputs=conv_base.input, outputs=output_layer)
|
74 |
+
CNN.load_weights("CNN.hdf5")
|
75 |
+
|
76 |
+
return CNN, model
|
77 |
+
|
78 |
+
CNN, model = load_model()
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
with input:
|
83 |
st.header("Input")
|
84 |
imageInput = st.file_uploader("Enter an image of a thin blood smear.")
|
|
|
87 |
image = Image.open(imageInput)
|
88 |
ogInp = image
|
89 |
img_array = np.array(image)
|
90 |
+
output, cellImgs = main(CNN, model, img_array)
|
91 |
showImg = Image.fromarray(output)
|
92 |
if st.button("Preview"):
|
93 |
if imageInput is not None:
|