Skip to main content

EHR generation with prompt learning by language models.

Project description

MedCLIP

Wang, Zifeng and Wu, Zhenbang and Agarwal, Dinesh and Sun, Jimeng. (2022). MedCLIP: Contrastive Learning from Unpaired Medical Images and Texts. EMNLP'22.

Download MedCLIP

Before download MedCLIP, you need to find feasible torch version (with GPU) on https://pytorch.org/get-started/locally/.

Then, download MedCLIP by

pip install git+https://github.com/RyanWangZf/MedCLIP.git

# or

pip install medclip

Three lines to get pretrained MedCLIP models

from medclip import MedCLIPModel, MedCLIPVisionModelViT, MedCLIPVisionModel

# load MedCLIP-ResNet50
model = MedCLIPModel(vision_cls=MedCLIPVisionModel)
model.from_pretrained()

# load MedCLIP-ViT
model = MedCLIPModel(vision_cls=MedCLIPVisionModelViT)
model.from_pretrained()

As simple as using CLIP

from medclip import MedCLIPModel, MedCLIPVisionModelViT
from medclip import MedCLIPProcessor

# prepare for the demo image and texts
processor = MedCLIPProcessor()
image = Image.open('./example_data/view1_frontal.jpg')
inputs = processor(
    text=["lungs remain severely hyperinflated with upper lobe emphysema", 
        "opacity left costophrenic angle is new since prior exam ___ represent some loculated fluid cavitation unlikely"], 
    images=image, 
    return_tensors="pt", 
    padding=True
    )

# pass to MedCLIP model
model = MedCLIPModel(vision_cls=MedCLIPVisionModelViT)
model.from_pretrained()
model.cuda()
outputs = model(**inputs)
print(outputs.keys())
# dict_keys(['img_embeds', 'text_embeds', 'logits', 'loss_value', 'logits_per_text'])

MedCLIP for Prompt-based Classification

from medclip import MedCLIPModel, MedCLIPVisionModelViT
from medclip import MedCLIPProcessor
from medclip import PromptClassifier

processor = MedCLIPProcessor()
model = MedCLIPModel(vision_cls=MedCLIPVisionModelViT)
model.from_pretrained()
clf = PromptClassifier(model, ensemble=True)
clf.cuda()

# prepare input image
from PIL import Image
image = Image.open('./example_data/view1_frontal.jpg')
inputs = processor(images=image, return_tensors="pt")

# prepare input prompt texts
from medclip.prompts import generate_chexpert_class_prompts, process_class_prompts
cls_prompts = process_class_prompts(generate_chexpert_class_prompts(n=10))
inputs['prompt_inputs'] = cls_prompts

# make classification
output = clf(**inputs)
print(output)
# {'logits': tensor([[0.5154, 0.4119, 0.2831, 0.2441, 0.4588]], device='cuda:0',
#       grad_fn=<StackBackward0>), 'class_names': ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Pleural Effusion']}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

MedCLIP-0.0.1.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

MedCLIP-0.0.1-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file MedCLIP-0.0.1.tar.gz.

File metadata

  • Download URL: MedCLIP-0.0.1.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for MedCLIP-0.0.1.tar.gz
Algorithm Hash digest
SHA256 57c69dd947f9606217dea6e355e5aa6ac242b0ee5bb21eb01e521399bf940b93
MD5 39138515bbd684ac0ba693ea42994a41
BLAKE2b-256 a1ba74b5a3b4327e06e0b9d0e5e4b9778f6e53955a15c1ca15483de26772c11c

See more details on using hashes here.

File details

Details for the file MedCLIP-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: MedCLIP-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for MedCLIP-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef88ca1eb3afb5f14c88001ad9da9d59449641ca449d42c21addac10ac4306d5
MD5 b66db35042d87324277f55cbae3951ee
BLAKE2b-256 69fe3526ce97fe3590f02abc5b8836c973768d1f301718c9fb0a724e682837f3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page