File size: 4,424 Bytes
d70f24c
 
 
 
 
 
25cd769
 
 
 
 
 
 
 
 
 
 
d70f24c
 
 
 
 
 
 
 
 
 
 
 
 
 
25cd769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70f24c
 
 
 
 
 
 
 
25cd769
d70f24c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7a385
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import streamlit as st
from PIL import Image
from modInference import main
import numpy as np
import math

import numpy as np
from skimage import transform
import os
from keras.models import Model
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.layers import Dense, Dropout, Flatten
import numpy as np

import torch
from models.create_fasterrcnn_model import create_model

st.set_page_config(layout="wide") 
st.markdown("")

showImg = Image.open('3dots.jpg')
ogInp = Image.open('3dots.jpg')
showImg = showImg.resize((200, 200))
ogInp = ogInp.resize((200, 200))
cellImgs = []

st.title('MicroScan In Action!')
st.subheader("Enter an image of a thin blood smear. Preview the image and run the application. This program was developed by Anish Pallod =)")

input, outputIm = st.columns(2)

@st.cache_resource
def load_model():
   NUM_CLASSES = 2
   CLASSES = ['__background__', 'Cell']

   DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
   print(DEVICE)
   checkpoint = torch.load("best_model.pth", map_location=DEVICE)

   NUM_CLASSES = checkpoint['config']['NC']
   CLASSES = checkpoint['config']['CLASSES']
   build_model = create_model[checkpoint['model_name']]

   model = build_model(num_classes=NUM_CLASSES, coco_model=False)
   model.load_state_dict(checkpoint['model_state_dict'])
   model.to(DEVICE).eval()

   COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

   conv_base = VGG16(include_top=False,
               weights='imagenet', 
               input_shape=(200,200,3))

   if 2 > 0:
      for layer in conv_base.layers[:-2]:
         layer.trainable = False
   else:
      for layer in conv_base.layers:
         layer.trainable = False


   top_model = conv_base.output
   top_model = Flatten(name="flatten")(top_model)
   top_model = Dense(4096, activation='relu')(top_model)
   top_model = Dense(1048, activation='relu')(top_model)
   top_model = Dense(256, activation='relu')(top_model)
   top_model = Dense(128, activation='relu')(top_model)
   top_model = Dense(64, activation='relu')(top_model)
   top_model = Dropout(0.2)(top_model)
   output_layer = Dense(5, activation='softmax')(top_model)

   CNN = Model(inputs=conv_base.input, outputs=output_layer)
   CNN.load_weights("CNN.hdf5")

   return CNN, model

CNN, model = load_model()



with input:
   st.header("Input")
   imageInput = st.file_uploader("Enter an image of a thin blood smear.")
   if st.button("Run"):
      if imageInput is not None:
            image = Image.open(imageInput)
            ogInp = image
            img_array = np.array(image)
            output, cellImgs = main(CNN, model, img_array)
            showImg = Image.fromarray(output)
   if st.button("Preview"):
      if imageInput is not None:
            image = Image.open(imageInput)
            ogInp = image
            st.write("-" * 34)
            st.header("How it looks:")
            st.image(ogInp)
   else:
      st.write("-" * 34)
      st.header("How it looks:")
      st.image(ogInp)

with outputIm:
   st.header("General Output")
   st.image(showImg)
   
   st.write("-" * 34)

   st.header("Segmented Cell Output")
   st.markdown("""
    <style>
    [data-testid=column] [data-testid=stVerticalBlock]{
        gap: 0.3rem;
    }
    </style>
    """,unsafe_allow_html=True)
   col1,col2,col3,col4,col5,col6,col7 = st.columns(7)

   total = len(cellImgs)
   print(cellImgs)

   barrier = []
   for k in range(1, 8):
      barrier.append(math.floor(total/7) * k)
   leftOver = total % 7

   for k in range(leftOver):
      barrier[k] += 1
   
   print(barrier)

   with col1:
      for x in cellImgs[0:barrier[0]]:
         st.write(x[1])
         st.image(x[0])
   with col2:
      for x in cellImgs[barrier[0]: barrier[1]]:
         st.write(x[1])
         st.image(x[0])
   with col3:
      for x in cellImgs[barrier[1]: barrier[2]]:
         st.write(x[1])
         st.image(x[0])
   with col4:
      for x in cellImgs[barrier [2]: barrier[3]]:
         st.write(x[1])
         st.image(x[0])
   with col5:
      for x in cellImgs[barrier[3]: barrier[4]]:
         st.write(x[1])
         st.image(x[0])
   with col6:
      for x in cellImgs[barrier[4]: barrier[5]]:
         st.write(x[1])
         st.image(x[0])
   with col7:
      for x in cellImgs[barrier[5]: barrier[6]]:
         st.write(x[1])
         st.image(x[0])

       

# with parameters:
#    st.header("Parameters")