BMClogo

In this tutorial, we demonstrate how to use the open source library in Google Colab to build a prototype X-ray judgment tool. By loading pre-trained Densenet models and Gradio with TorchxRayVision’s power to create an interactive user interface, we will show how to handle and classify chest X-ray images with minimal settings. This laptop guides you through image preprocessing, model inference and result interpretation, all designed to run seamlessly without the need for external API keys or logins. Please note that this demonstration is for educational purposes only and should not be used as a substitute for professional clinical diagnosis.

!pip install torchxrayvision gradio

First, we installed the TorchxRayVision library for X-ray analysis and Gradio to create an interactive interface.

import torch
import torchxrayvision as xrv
import torchvision.transforms as transforms
import gradio as gr

We import Pytorch into deep learning operations, TorchxRayvision for X-ray analysis, Torchvision for image preprocessing transformation, and Gradio for building interactive UIs.

model = xrv.models.DenseNet(weights="densenet121-res224-all")
model.eval()  

We then load the pretrained Densenet model with the “Densenet121-Res224-All” weight and set it to evaluation mode for inference.

try:
    pathology_labels = model.meta("labels")
    print("Retrieved pathology labels from model.meta.")
except Exception as e:
    print("Could not retrieve labels from model.meta. Using fallback labels.")
    pathology_labels = (
         "Atelectasis", "Cardiomegaly", "Consolidation", "Edema",
         "Emphysema", "Fibrosis", "Hernia", "Infiltration", "Mass",
         "Nodule", "Pleural Effusion", "Pneumonia", "Pneumothorax", "No Finding"
    )

Now, we try to retrieve the pathological tags from the metadata of the model and pour into the predefined list when the search fails.

def classify_xray(image):
    try:
        transform = transforms.Compose((
            transforms.Resize((224, 224)),
            transforms.Grayscale(num_output_channels=1),
            transforms.ToTensor()
        ))
        input_tensor = transform(image).unsqueeze(0)  # add batch dimension


        with torch.no_grad():
            preds = model(input_tensor)
       
        pathology_scores = preds(0).detach().numpy()
        results = {}
        for idx, label in enumerate(pathology_labels):
            results(label) = float(pathology_scores(idx))
       
        sorted_results = sorted(results.items(), key=lambda x: x(1), reverse=True)
        top_label, top_score = sorted_results(0)
       
        judgement = (
            f"Prediction: {top_label} (score: {top_score:.2f})nn"
            f"Full Scores:n{results}"
        )
        return judgement
    except Exception as e:
        return f"Error during inference: {str(e)}"

Here, using this feature, we can preprocess an input X-ray image, use a pretrained model for inference, extract pathological scores, and return a format summary of the highest predictions and all scores while gracefully handling errors.

iface = gr.Interface(
    fn=classify_xray,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="X-ray Judgement Tool (Prototype)",
    description=(
        "Upload a chest X-ray image to receive a classification judgement. "
        "This demo is for educational purposes only and is not intended for clinical use."
    )
)


iface.launch()

Finally, we built and launched a Gradio interface that allows users to upload chest X-ray images. The Classify_xray function processes images to output diagnostic judgments.

Gradio interface for this tool

Through this tutorial, we explore the development of an interactive X-ray judgment tool that integrates advanced deep learning techniques with user-friendly interfaces. Despite inherent limitations, such as the model not being fine-tuned by clinical diagnosis, the prototype is a valuable starting point for the application of experimental medical imaging. We encourage you to build a foundation on the basis of your foundation, taking into account the importance of strict verification and compliance with real-world standards of traditional Chinese medicine.


This is COLAB notebook. Also, don’t forget to follow us twitter And join us Telegram Channel and LinkedIn GrOUP. Don’t forget to join us 85k+ ml reddit.


Asif Razzaq is CEO of Marktechpost Media Inc. As a visionary entrepreneur and engineer, ASIF is committed to harnessing the potential of artificial intelligence to achieve social benefits. His recent effort is to launch Marktechpost, an artificial intelligence media platform that has an in-depth coverage of machine learning and deep learning news that can sound both technically, both through technical voices and be understood by a wide audience. The platform has over 2 million views per month, demonstrating its popularity among its audience.

Source link