Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/xray report generation #144

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
updated documentation
  • Loading branch information
samarthkeshari committed Apr 23, 2023
commit 0a2e74147d8bc0c6e2b3b0860892d443cc714c67
40 changes: 29 additions & 11 deletions pyhealth/models/wordsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,18 @@ def forward(self, **kwargs):
Returns:
A dictionary with the following keys:
loss: a scalar tensor representing the loss.
y_generated: a dictionary list of text representing the generated caption.
The list contains only one element.
y_true: a list of text representing the true caption.
The list contains only one element.
y_generated: a dictionary with following key
- "patient_id": list of text representing the generated
text.
e.g.
{123: ["generated text"], 456: ["generated text"]}
y_true: a dictionary with following key
- "patient_id": list of text representing the true text.
e.g.
{123: ["true text"], 456: ["true text"]}
"""
# Initialize the output
output = {"loss": None,"y_generated": "","y_true": ""}
output = {"loss": None,"y_generated": [""],"y_true": [""]}

# Get list of patient_ids
patient_ids = kwargs["patient_id"]
Expand Down Expand Up @@ -286,21 +291,34 @@ def _prepare_batch_images(self,kwargs):
return images

def _prepare_batch_captions(self,captions):
"""Prepare caption for input.
"""Prepare caption idx for input.

Args:
captions: list of captions. Each caption is a list of list, where
each list represents a sentence in the caption.
Following is an example of a caption
[
["<start>","first","sentence","."],
[".", "second", "sentence",".", "<end>"]
]
Returns:
captions_idx: an int tensor of size [batch_size,max_caption_length]
masks: a bool tensor of size [batch_size,max_caption_length]
"""
samples = []
# Combine all sentences in each caption to create a single sentence
for caption in captions:
tokens = []
tokens.extend(flatten_list(caption))
text = ' '.join(tokens).replace('. .','.')
samples.append([text.split()])
#print(caption)

x = self.caption_tokenizer.batch_encode_3d(samples)
captions = torch.tensor(x, dtype=torch.long, device=self.device)
masks = torch.sum(captions,dim=1) !=0
captions = captions.squeeze(1)
captions_idx = torch.tensor(x, dtype=torch.long, device=self.device)
masks = torch.sum(captions_idx,dim=1) !=0
captions_idx = captions_idx.squeeze(1)

return captions,masks
return captions_idx,masks

def _forward_inference(self,patient_ids,cnn_features):
"""
Expand Down