<jupyter_start><jupyter_text>CXRlabeler Model  MIMIC-CXR Dataset<jupyter_code>from fastai.basics import *
from fastai.text.all import *
import warnings
warnings.filterwarnings('ignore')
# Read in the train and test sets.
path = Path('/home/jupyter/data/mimic-cxr')
df_lm = pd.read_csv(path/"lm.csv")
df_cl = pd.read_csv(path/"labels.csv")
df_train = pd.read_csv(path/"train.csv")
df_test = pd.read_csv(path/"test.csv")
df_lm.info()
df_cl.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 156790 entries, 0 to 156789
Data columns (total 17 columns):
 #   Column                      Non-Null Count   Dtype 
---  ------                      --------------   ----- 
 0   dicom_id                    156790 non-null  object
 1   reports                     156790 non-null  object
 2   Atelectasis                 156790 non-null  int64 
 3   Cardiomegaly                156790 non-null  int64 
 4   Consolidation               156790 non-null  int64 
 5   Edema                       156790 non-null  int64 
 6   Enlarged Cardiomediastinum  156790 non-null  int64 
 7   Fracture                    156790 non-null  int64 
 8   Lung Lesion                 156790 non-null  int64 
 9   Lung Opacity                156790 non-null  int64 
 10  No Finding                  156790 non-null  int64 
 11  Pleural Effusion            156790 non-null  int64 
 12  Pleural Other               156790 non-null  int64 
 13  Pneumonia                   1[...]<jupyter_text>1. Language Model<jupyter_code>df_text = pd.Series.append(df_train['reports'], df_test['reports'])
df_text = pd.DataFrame(df_text)
df_text.info()
df_text.head()<jupyter_output><empty_output><jupyter_text>1.1 Data Block<jupyter_code>TextBlock.from_df??
bs_lm = (TextBlock.from_df(text_cols='reports', is_lm=True, tok_text_col='text'))
get_x = ColReader('text')
splitter = RandomSplitter(0.1, seed=42)
db_lm = DataBlock(blocks=bs_lm,
                     get_x=get_x,
                     splitter=splitter)<jupyter_output><empty_output><jupyter_text>1.2 Data Loader<jupyter_code>dl_lm = db_lm.dataloaders(df_text, bs=64)<jupyter_output><empty_output><jupyter_text>1.3 Training<jupyter_code>learn_lm = language_model_learner(dl_lm, AWD_LSTM, pretrained=True, metrics=[accuracy, Perplexity()])
learn_lm.to_fp16()
learn_lm.fine_tune(10, 4e-3)<jupyter_output><empty_output><jupyter_text>1.4 Testing<jupyter_code>N_WORDS = 40
N_SENTENCES = 2
TEXT = "Normal heart size. The right"
preds = [learn_lm.predict(TEXT, N_WORDS, temperature=0.75) 
         for _ in range(N_SENTENCES)]
print("\n".join(preds))
TEXT = "Subtle patchy opacity along the"
preds = [learn_lm.predict(TEXT, N_WORDS, temperature=0.75) 
         for _ in range(N_SENTENCES)]
print("\n".join(preds))
TEXT = "Cardiomegaly is severe, unchanged. Pacemaker"
preds = [learn_lm.predict(TEXT, N_WORDS, temperature=0.75) 
         for _ in range(N_SENTENCES)]
print("\n".join(preds))<jupyter_output><empty_output><jupyter_text>1.5 Saving<jupyter_code># save fine-tuned model for classification
learn_lm.save_encoder(path/'lm')<jupyter_output><empty_output><jupyter_text>2. Multi-Label Classifier<jupyter_code># fix result 
def seed_everything(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
SEED = 42
seed_everything(SEED)<jupyter_output><empty_output><jupyter_text>2.1 Data Block<jupyter_code>labels = ["Atelectasis", "Cardiomegaly", "Consolidation",
              "Edema", "Enlarged Cardiomediastinum", "Fracture", "Lung Lesion", 
              "Lung Opacity", "No Finding", "Pleural Effusion", "Pleural Other", 
              "Pneumonia", "Pneumothorax", "Support Devices"]
dl_lm.seq_len
dl_lm.vocab
dl_lm.max_vocab
bs_cl = (TextBlock.from_df('reports', seq_len=dl_lm.seq_len, vocab=dl_lm.vocab), 
          MultiCategoryBlock(encoded=True, vocab=labels))
db_cl = DataBlock(blocks=bs_cl,
                      get_x=ColReader('text'),
                      get_y=ColReader(labels),
                      splitter=ColSplitter('is_valid'))
db_cl.summary(df_cl.iloc[:100])
db_cl.splitter(df_cl)<jupyter_output><empty_output><jupyter_text>2.2 Data Loader<jupyter_code>dl_cl = db_cl.dataloaders(df_cl)
dl_cl.show_batch()<jupyter_output><empty_output><jupyter_text>2.3 Training<jupyter_code>text_classifier_learner??
loss_func = BCEWithLogitsLossFlat(thresh=0.8)
metrics = [partial(accuracy_multi, thresh=0.8), 
           F1ScoreMulti(average='macro'),
           PrecisionMulti (average='macro'),
           RecallMulti (average='macro'),
           RocAucMulti (average='macro')]
learn_cl = text_classifier_learner(dl_cl, AWD_LSTM, metrics=metrics, loss_func=loss_func)
learn_cl.load_encoder(path/'lm');
learn_cl.to_fp16()
learn_cl.fine_tune(10)<jupyter_output><empty_output><jupyter_text>2.4 Testing<jupyter_code>learn_cl.validate()
# labels = ["Atelectasis", "Cardiomegaly", "Consolidation",
#              "Edema", "Enlarged Cardiomediastinum", "Fracture", "Lung Lesion", 
#              "Lung Opacity", "No Finding", "Pleural Effusion", "Pleural Other", 
#              "Pneumonia", "Pneumothorax", "Support Devices"]
# correct: 1, 0, 0, 1, 0, 0, 0 ,0, 0, 1, 0, 0, 0, 0 
learn_cl.predict("""Compared to chest radiographs since ___, most recently ___.
 
 Large right and moderate left pleural effusions and severe bibasilar
 atelectasis are unchanged.  Cardiac silhouette is obscured.  No pneumothorax. 
 Pulmonary edema is mild, obscured radiographically by overlying abnormalities. """)
# correct: 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1
learn_cl.predict("""New mild pulmonary vascular congestion with mild to moderate interstitial
 pulmonary edema and increased mild cardiomegaly.  No focal consolidation.""")
# correct: 0, 1, 0, 1, 0, 0 ..
learn_cl.predict("""The heart remains enlarged.  There is opacity along the medial left
 hemidiaphragm, which is known to correspond to a Bochdalek's hernia containing
 portion of the left kidney.  The right Bochdalek hernia is not as well
 visualized on today's examination.  There is patchy bibasilar opacity with
 likely associated layering effusions.  These findings could reflect
 compressive atelectasis, although aspiration pneumonia should also be
 considered.  There is also an asymmetric airspace process involving the left
 apex, which likely is not significantly changed dating all the way back to
 ___ and therefore would favor a benign process.  No pulmonary edema.  No
 pneumothoraces.  Mediastinal contours are stable.""")
# correct: 1, 0, 0 ... , 1, 1
learn_cl.predict("""Comparison to the most recent preceding radiograph, there is a
 slight reaccumulation of fluid in the right pleural space.  Two chest tubes
 are noted in that space.  A tiny apical pneumothorax is present.  Right
 atelectasis is also present.  The left lung is essentially clear.  Cardiac
 size is normal.""")
# correct: 0, 0, 0, lung lesion, .., 1,0
learn_cl.predict("""1.  Slowly growing peripheral right upper lobe lung nodule is concerning for
 primary lung adenocarcinoma.  Dedicated chest CT may be considered for more
 accurate assessment as well as to evaluate for possible right hilar lymph node
 enlargement warranted clinically.
 2.  Low lung volumes limit assessment of the lung bases for pneumonia.  Given
 clinical suspicion for this entity, this could be further evaluated with
 repeat chest radiograph with improved inspiratory level. Dr. ___ was paged
 with these results at 8:15 a.m. on ___, at the time of discovery.""")
# correct: 0, 0, 0, fracture,0,1, .., 0,0
learn_cl.predict("""Subtle opacity projecting over the lateral right mid lung may be due to
 overlap of structures, but underlying pulmonary opacity or even rib fracture
 is not excluded.  Findings could be further assessed with shallow oblique
 radiographs or chest CT. No displaced rib fracture definitively identified. However, if clinical
 concern persists, dedicated rib series or chest CT is more sensitive.""")<jupyter_output><empty_output><jupyter_text>2.5 Interpretation<jupyter_code>interp = Interpretation.from_learner(learn_cl)
interp.plot_top_losses(3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10190695979594315, 'fraction_numerical': 0.030244139438843676, 'mean_word_length': 3.915820895522388, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '580748', 'license': 'Apache-2.0', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2680, 'n_words': 719, 'path': 'CXRlabeler/withLM-mimic-cxr.ipynb', 'repo_name': 'MaramMonshi/CXRlabeler', 'size': '8233'}