import streamlit as st import torch import torchvision.transforms as transforms import numpy as np from PIL import Image import segmentation_models_pytorch as smp from unet import Unet import os import io all_weights_path = "weights" device = torch.device("cuda" if torch.cuda.is_available() else "cpu") st.set_page_config( page_title="Fringe Pattern Inference App", page_icon="💻", ) # Set Streamlit page title st.title("Fringe Pattern Inference App") # Model selection with no default selection model_choice = st.selectbox("Select a model:", ["Select a model", "Vanilla Unet", "Unet Resnet18", "Unet++ Resnet18", "Manet Resnet18", "FPN Resnet18"], index=0) # Load model based on selection def load_model(model_name): if model_name == "Vanilla Unet": model = Unet(in_channels=1, out_channels=1, features=[64, 128, 256, 512]).to(device) weights_path = os.path.join(all_weights_path, "output_unet/best_model_unet_scratch.pth") elif model_name == "Unet Resnet18": model = smp.Unet(encoder_name="resnet18", in_channels=1, classes=1).to(device) weights_path = os.path.join(all_weights_path, "output_unetresnet/best_model_unetresnet_imagenet.pth") elif model_name == "Unet++ Resnet18": model = smp.UnetPlusPlus(encoder_name="resnet18", in_channels=1, classes=1).to(device) weights_path = os.path.join(all_weights_path, "output_unet++/best_model_unet++_imagenet.pth") elif model_name == "Manet Resnet18": model = smp.MAnet(encoder_name="resnet18", in_channels=1, classes=1).to(device) weights_path = os.path.join(all_weights_path, "output_manet/best_model_manet_imagenet.pth") elif model_name == "FPN Resnet18": model = smp.FPN(encoder_name="resnet18", in_channels=1, classes=1).to(device) weights_path = os.path.join(all_weights_path, "output_fpn/best_model_fpn_imagenet.pth") else: return None, None return model, weights_path # File uploader for images with size limit uploaded_image = st.file_uploader("Upload an image for segmentation (Max: 2MB)", type=["png", "jpg", "jpeg"]) if model_choice != "Select a model": # Load the selected model and its corresponding weights model, weights_path = load_model(model_choice) if model and os.path.exists(weights_path): model.load_state_dict(torch.load(weights_path, map_location=torch.device(device))) model.to(device) model.eval() st.success(f"Model weights loaded successfully from {weights_path}!") else: st.error("Weights file not found. Please ensure the correct weight files are available.") if uploaded_image is not None: if uploaded_image.size > 2 * 1024 * 1024: # Check if the file size exceeds 2MB st.error("File size exceeds 2MB. Please upload a smaller image.") else: image = Image.open(uploaded_image).convert("L") transform = transforms.Compose([ transforms.Resize((480, 640)), transforms.ToTensor() ]) resized_image = transform(image).permute(1, 2, 0).numpy() # Convert tensor to image format input_tensor = transform(image).unsqueeze(0).to(device) st.image(resized_image, caption="Input Image", use_container_width=True) if model_choice != "Select a model": if st.button("Run Inference"): with torch.no_grad(): output = model(input_tensor) output = torch.sigmoid(output).squeeze().cpu().numpy() output_image = Image.fromarray((output * 255).astype(np.uint8)) # Display results st.image(output, caption="Output image", use_container_width=True, clamp=True) # Create a download button for the segmented output if 'output_image' in locals(): buf = io.BytesIO() output_image.save(buf, format="PNG") byte_im = buf.getvalue() st.download_button(label="Download Image", data=byte_im, file_name="output.png", mime="image/png")