import torch from monai.networks.nets import DenseNet121 import gradio as gr #from PIL import Image model = DenseNet121(spatial_dims=2, in_channels=1, out_channels=6) model.load_state_dict(torch.load('weights/mednist_model.pth', map_location=torch.device('cpu'))) from monai.transforms import ( EnsureChannelFirst, Compose, LoadImage, ScaleIntensity, ) test_transforms = Compose( [LoadImage(image_only=True), EnsureChannelFirst(), ScaleIntensity()] ) class_names = [ 'AbdomenCT', 'BreastMRI', 'CXR', 'ChestCT', 'Hand', 'HeadCT' ] import os, glob #examples_dir = './samples' #example_files = glob.glob(os.path.join(examples_dir, '*.jpg')) def classify_image(image_filepath): input = test_transforms(image_filepath) model.eval() with torch.no_grad(): pred = model(input.unsqueeze(dim=0)) prob = torch.nn.functional.softmax(pred[0], dim=0) confidences = {class_names[i]: float(prob[i]) for i in range(6)} print(confidences) return confidences with gr.Blocks(title="Medical Image Classification- ClassCat", css=".gradio-container {background:mintcream;}" ) as demo: gr.HTML("""