Upload auto.py
Browse files
auto.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
button_style = """
|
| 10 |
+
<style>
|
| 11 |
+
.center-align {
|
| 12 |
+
display: flex;
|
| 13 |
+
justify-content: center;
|
| 14 |
+
}
|
| 15 |
+
</style>
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
DEVICE = 'cuda'
|
| 19 |
+
|
| 20 |
+
@st.cache_resource
|
| 21 |
+
|
| 22 |
+
class ConvAutoencoder(nn.Module):
|
| 23 |
+
def __init__(self):
|
| 24 |
+
super().__init__()
|
| 25 |
+
# encoder
|
| 26 |
+
self.conv1 = nn.Sequential(
|
| 27 |
+
nn.Conv2d(1, 32, kernel_size=4),
|
| 28 |
+
nn.BatchNorm2d(32),
|
| 29 |
+
nn.SELU()
|
| 30 |
+
)
|
| 31 |
+
self.conv2 = nn.Sequential(
|
| 32 |
+
nn.Conv2d(32, 8, kernel_size=2),
|
| 33 |
+
nn.BatchNorm2d(8),
|
| 34 |
+
nn.SELU()
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
self.pool = nn.MaxPool2d(2, 2, return_indices=True, ceil_mode=True) #<<<<<< Bottleneck
|
| 38 |
+
|
| 39 |
+
#decoder
|
| 40 |
+
# Как работает Conv2dTranspose https://github.com/vdumoulin/conv_arithmetic
|
| 41 |
+
|
| 42 |
+
self.unpool = nn.MaxUnpool2d(2, 2)
|
| 43 |
+
|
| 44 |
+
self.conv1_t = nn.Sequential(
|
| 45 |
+
nn.ConvTranspose2d(8, 32, kernel_size=2),
|
| 46 |
+
nn.BatchNorm2d(32),
|
| 47 |
+
nn.SELU()
|
| 48 |
+
)
|
| 49 |
+
self.conv2_t = nn.Sequential(
|
| 50 |
+
nn.ConvTranspose2d(32, 1, kernel_size=4),
|
| 51 |
+
nn.LazyBatchNorm2d(),
|
| 52 |
+
nn.Sigmoid()
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
def encode(self, x):
|
| 56 |
+
x = self.conv1(x)
|
| 57 |
+
x = self.conv2(x)
|
| 58 |
+
x, indicies = self.pool(x) # ⟸ bottleneck
|
| 59 |
+
return x, indicies
|
| 60 |
+
|
| 61 |
+
def decode(self, x, indicies):
|
| 62 |
+
x = self.unpool(x, indicies)
|
| 63 |
+
x = self.conv1_t(x)
|
| 64 |
+
x = self.conv2_t(x)
|
| 65 |
+
return x
|
| 66 |
+
|
| 67 |
+
def forward(self, x):
|
| 68 |
+
latent, indicies = self.encode(x)
|
| 69 |
+
out = self.decode(latent, indicies)
|
| 70 |
+
return out
|
| 71 |
+
|
| 72 |
+
model = ConvAutoencoder().to(DEVICE)
|
| 73 |
+
|
| 74 |
+
model.load_state_dict(torch.load('D:\Bootcamp\phase_2\streamlit\\autoend.pt'))
|
| 75 |
+
|
| 76 |
+
transform = transforms.Compose([
|
| 77 |
+
transforms.ToTensor(), # Преобразование изображения в тензор
|
| 78 |
+
# Добавьте другие необходимые преобразования, такие как нормализация, если это необходимо
|
| 79 |
+
])
|
| 80 |
+
model.eval()
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
image_source = st.radio("Choose the option of uploading the image of tumor:", ("File", "URL"))
|
| 84 |
+
|
| 85 |
+
if image_source == "File":
|
| 86 |
+
uploaded_file = st.file_uploader("Upload the image", type=["jpg", "png", "jpeg"])
|
| 87 |
+
if uploaded_file:
|
| 88 |
+
image = Image.open(uploaded_file)
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
url = st.text_input("Enter the URL of image...")
|
| 92 |
+
if url:
|
| 93 |
+
response = requests.get(url)
|
| 94 |
+
image = Image.open(BytesIO(response.content))
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
st.markdown(button_style, unsafe_allow_html=True)
|
| 98 |
+
|
| 99 |
+
model.to('cuda')
|
| 100 |
+
|
| 101 |
+
if 'image' in locals():
|
| 102 |
+
st.image(image, caption="Uploaded image", use_column_width=True)
|
| 103 |
+
|
| 104 |
+
bw_image = image.convert('L')
|
| 105 |
+
|
| 106 |
+
image_tensor = transform(bw_image).unsqueeze(0)
|
| 107 |
+
|
| 108 |
+
image_tensor = image_tensor.to('cuda')
|
| 109 |
+
|
| 110 |
+
with torch.no_grad():
|
| 111 |
+
output = model(image_tensor)
|
| 112 |
+
|
| 113 |
+
output = transforms.ToPILImage()(output[0].cpu())
|
| 114 |
+
|
| 115 |
+
if st.button("Detect tumor", type="primary"):
|
| 116 |
+
st.image(output, caption="Annotated Image", use_column_width=True)
|