Kaggle competition: RSNA Pneumonia Detection Challenge
<SOS> metric: RSNAObjectDetectionAP Kaggle data source: rsna-pneumonia-detection-challenge<categorify>
import os
import sys
import random
import math
import numpy as np
import cv2
import matplotlib.pyplot as plt
import json
import pydicom
from imgaug import augmenters as iaa
from tqdm import tqdm
import pandas as pd
import glob
tweets.keyword.fillna('None', inplace=True)
 def decontraction(phrase):
phrase = re.sub(r"won't", "will not", phrase)
 phrase = re.sub(r"can't", "can not", phrase)
 phrase = re.sub(r"n't", " not", phrase)
 phrase = re.sub(r"'re", " are", phrase)
 phrase = re.sub(r"'s", " is", phrase)
 phrase = re.sub(r"'d", " would", phrase)
 phrase = re.sub(r"'ll", " will", phrase)
 phrase = re.sub(r"'t", " not", phrase)
 phrase = re.sub(r"'ve", " have", phrase)
 phrase = re.sub(r"'m", " am", phrase)
 return phrase
tweets.text = [decontraction(tweet)for tweet in tweets.text]
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 print(remove_emoji("OMG there is a volcano eruption!!! 😭😱😷"))
tweets.text = tweets.text.apply(lambda x: remove_emoji(x))<categorify>
DATA_DIR = '/kaggle/input/rsna-pneumonia-detection-challenge/'
ROOT_DIR = '/kaggle/working'
tweets.text = tweets.text.apply(lambda x: remove_url(x))
def remove_punct(text):
new_punct = re.sub('\ |\!|\?', '', punctuation)
 table=str.maketrans('','',new_punct)
 return text.translate(table)
 tweets.text = tweets.text.apply(lambda x: remove_punct(x))
def replace_amp(text):
text = re.sub(r" amp ", " and ", text)
 return text
tweets.text = tweets.text.apply(lambda x: replace_amp(x)) 
<load_from_csv>
!git clone https://www.github.com/matterport/Mask_RCNN.git
os.chdir('Mask_RCNN')
tweets = pd.read_csv('.. /input/twitter-logo/tweets_segmented.csv' )<string_transform>
train_dicom_dir = os.path.join(DATA_DIR, 'stage_2_train_images')
 test_dicom_dir = os.path.join(DATA_DIR, 'stage_2_test_images' )
lemmatizer = WordNetLemmatizer()
def lemma(text):
words = word_tokenize(text)
 return ' '.join([lemmatizer.lemmatize(w.lower() , pos='v')for w in words])
 tweets.text = tweets.text.apply(lambda x: lemma(x)) 
<string_transform>
def get_dicom_fps(dicom_dir):
dicom_fps = glob.glob(dicom_dir+'/'+'*.dcm')
 return list(set(dicom_fps))
def parse_dataset(dicom_dir, anns):
image_fps = get_dicom_fps(dicom_dir)
 image_annotations = {fp: [] for fp in image_fps}
for index, row in anns.iterrows() :
fp = os.path.join(dicom_dir, row['patientId']+'.dcm')
 image_annotations[fp].append(row)
 return image_fps, image_annotations
def generate_ngrams(text, n):
words = word_tokenize(text)
 return [' '.join(ngram)for ngram in list(get_data(ngrams(words, n)))if not all(w in stop for w in ngram)]
def get_data(gen):
try:
for elem in gen:
yield elem
except(RuntimeError, StopIteration):
return<categorify>
class DetectorConfig(Config):

 NAME = 'pneumonia'
GPU_COUNT = 1
IMAGES_PER_GPU = 8
BACKBONE = 'resnet50'
NUM_CLASSES = 2
IMAGE_MIN_DIM = 256
IMAGE_MAX_DIM = 256
RPN_ANCHOR_SCALES =(32, 64, 128, 256)
 TRAIN_ROIS_PER_IMAGE = 32
MAX_GT_INSTANCES = 3
DETECTION_MAX_INSTANCES = 3
DETECTION_MIN_CONFIDENCE = 0.7
DETECTION_NMS_THRESHOLD = 0.1
STEPS_PER_EPOCH = 200
config = DetectorConfig()
config.display()
bigrams_disaster = tweets[tweets.target==1].text.apply(lambda x: generate_ngrams(x, 2))
bigrams_ndisaster = tweets[tweets.target==0].text.apply(lambda x: generate_ngrams(x, 2))
bigrams_d_dict = {}
for bgs in bigrams_disaster:
for bg in bgs:
if bg in bigrams_d_dict:
bigrams_d_dict[bg] += 1
else:
bigrams_d_dict[bg] = 1
bigrams_d_df = pd.DataFrame(bigrams_d_dict.items() , columns=['Bigrams','Count'])
 bigrams_nd_dict = {}
for bgs in bigrams_ndisaster:
for bg in bgs:
if bg in bigrams_nd_dict:
bigrams_nd_dict[bg] += 1
else:
bigrams_nd_dict[bg] = 1
bigrams_nd_df = pd.DataFrame(bigrams_nd_dict.items() , columns=['Bigrams','Count'] )<categorify>
class DetectorDataset(utils.Dataset):

 def __init__(self, image_fps, image_annotations, orig_height, orig_width):
super().__init__(self)
 self.add_class('pneumonia', 1, 'Lung Opacity')
 for i, fp in enumerate(image_fps):
annotations = image_annotations[fp]
self.add_image('pneumonia', image_id=i, path=fp,
annotations=annotations, orig_height=orig_height, orig_width=orig_width)
 def image_reference(self, image_id):
info = self.image_info[image_id]
return info['path']
def load_image(self, image_id):
info = self.image_info[image_id]
fp = info['path']
ds = pydicom.read_file(fp)
 image = ds.pixel_array
if len(image.shape)!= 3 or image.shape[2] != 3:
image = np.stack(( image,)* 3, -1)
 return image
def load_mask(self, image_id):
info = self.image_info[image_id]
annotations = info['annotations']
count = len(annotations)
 if count == 0:
mask = np.zeros(( info['orig_height'], info['orig_width'], 1), dtype=np.uint8)
 class_ids = np.zeros(( 1,), dtype=np.int32)
 else:
mask = np.zeros(( info['orig_height'], info['orig_width'], count), dtype=np.uint8)
 class_ids = np.zeros(( count,), dtype=np.int32)
 for i, a in enumerate(annotations):
if a['Target'] == 1:
x = int(a['x'])
 y = int(a['y'])
 w = int(a['width'])
 h = int(a['height'])
 mask_instance = mask[:, :, i].copy()
cv2.rectangle(mask_instance,(x, y),(x+w, y+h), 255, -1)
 mask[:, :, i] = mask_instance
class_ids[i] = 1
return mask.astype(np.bool), class_ids.astype(np.int32 )
trigrams_disaster = tweets[tweets.target==1].text.apply(lambda x: generate_ngrams(x, 3))
trigrams_ndisaster = tweets[tweets.target==0].text.apply(lambda x: generate_ngrams(x, 3))
trigrams_d_dict = {}
for tgs in trigrams_disaster:
for tg in tgs:
if tg in trigrams_d_dict:
trigrams_d_dict[tg] += 1
else:
trigrams_d_dict[tg] = 1
trigrams_d_df = pd.DataFrame(trigrams_d_dict.items() , columns=['Trigrams','Count'])
 trigrams_nd_dict = {}
for tgs in trigrams_ndisaster:
for tg in tgs:
if tg in trigrams_nd_dict:
trigrams_nd_dict[tg] += 1
else:
trigrams_nd_dict[tg] = 1
trigrams_nd_df = pd.DataFrame(trigrams_nd_dict.items() , columns=['Trigrams','Count'] )<feature_engineering>
anns = pd.read_csv(os.path.join(DATA_DIR, 'stage_2_train_labels.csv'))
anns.head()
def remove_stopwords(text):
word_tokens = word_tokenize(text)
 return ' '.join([w.lower() for w in word_tokens if not w.lower() in stop])
 tweets['text_nostopwords'] = tweets.text.apply(lambda x: remove_stopwords(x)) 
<feature_engineering>
image_fps, image_annotations = parse_dataset(train_dicom_dir, anns=anns )
mask = np.array(Image.open('.. /input/twitter-logo/Twitter-Logo_white.png'))
reverse = mask[...,::-1,:]
def wc_words(target, mask=mask):
words = [word.lower() for tweet in tweets[tweets.target == target].text_nostopwords for word in tweet.split() ]
words = list(filter(lambda w: w != 'like', words))
words = list(filter(lambda w: w != 'new', words))
words = list(filter(lambda w: w != 'people', words))
dict = {}
for w in words:
if w in dict:
dict[w] += 1
else:
dict[w] = 1
my_cloud = WordCloud(background_color='white', stopwords=stop, mask=mask, random_state=0 ).generate_from_frequencies(dict)
 plt.subplot(1,2,target+1)
 plt.imshow(my_cloud, interpolation='bilinear')
 plt.axis("off")
 plt.figure(figsize=(15,10))
wc_words(0)
 plt.title('Non-Disaster')
 wc_words(1, reverse)
 plt.title('Disaster')
 plt.show()<drop_column>
ds = pydicom.read_file(image_fps[0])
 image = ds.pixel_array
pd.reset_option('max_colwidth')
 tweets.drop('text_nostopwords', axis=1, inplace=True)
 tweets.head()<split>
ORIG_SIZE = 1024
X_train, X_val, y_train, y_val = train_test_split(tweets.drop(['id','keyword','location','target'],axis=1), tweets[['target']], test_size=0.2, stratify=tweets[['target']], random_state=0)
 X_train_text = X_train['text']
X_val_text = X_val['text']
print('X_train shape: ', X_train.shape)
 print('X_val shape: ', X_val.shape)
 print('y_train shape: ', y_train.shape)
 print('y_val shape: ', y_val.shape )<count_values>
test_preds = pd.read_csv('/kaggle/input/stage-1-ids/stage_1_sample_submission.csv' )
print('Train Class Proportion:
', y_train['target'].value_counts() / len(y_train)* 100)
 print('
Validation Class Proportion:
', y_val['target'].value_counts() / len(y_val)* 100 )<train_model>
test_patient_ids = set(test_preds.patientId )
tokenizer_1 = Tokenizer(num_words=5000, oov_token='<UNK>')
 tokenizer_1.fit_on_texts(X_train_text )<string_transform>
image_fps_list = list(image_fps)
 random.seed(42)
 random.shuffle(image_fps_list)
 val_size = []
val_size = 1500
image_fps_val = image_fps_list[:val_size]
image_fps_train = image_fps_list[val_size:]
print(len(image_fps_train), len(image_fps_val))
X_train_text = tokenizer_1.texts_to_sequences(X_train_text)
 X_val_text = tokenizer_1.texts_to_sequences(X_val_text)
 print(X_train_text[:10])
 print('')
 print(X_val_text[:10] )<data_type_conversions>
dataset_train = DetectorDataset(image_fps_train, image_annotations, ORIG_SIZE, ORIG_SIZE)
 dataset_train.prepare()
tokenizer_1.sequences_to_texts([X_train_text[1]] )<feature_engineering>
dataset_val = DetectorDataset(image_fps_val, image_annotations, ORIG_SIZE, ORIG_SIZE)
 dataset_val.prepare()
vocab_size = len(tokenizer_1.word_index)+ 1
embeddings_index = dict()
f = open('.. /input/glove-global-vectors-for-word-representation/glove.twitter.27B.200d.txt')
 for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Loaded %s word vectors.' % len(embeddings_index))<categorify>
model = modellib.MaskRCNN(mode='training', config=config, model_dir=ROOT_DIR)
 model.load_weights(COCO_WEIGHTS_PATH, by_name=True, exclude=[
"mrcnn_class_logits", "mrcnn_bbox_fc",
"mrcnn_bbox", "mrcnn_mask"] )
embedding_matrix = np.zeros(( vocab_size, 200))
for word, i in tokenizer_1.word_index.items() :
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
print('Embedding Matrix Shape:', embedding_matrix.shape )<count_values>
LEARNING_RATE = 0.005
warnings.filterwarnings("ignore" )
num_epochs=15
dropout=0.2
recurrent_dropout=0.2
lr=0.0005
batch_size=128
class_weight = {0: y_train['target'].value_counts() [1]/len(y_train), 1: y_train['target'].value_counts() [0]/len(y_train)}<choose_model_class>
%%time
model.train(dataset_train, dataset_val,
learning_rate=LEARNING_RATE*2,
epochs=2,
layers='heads',
augmentation=None )
lstm_model = Sequential()
embedding_layer = Embedding(vocab_size, 200, weights=[embedding_matrix], input_length=maxlen, trainable=False)
 lstm_model.add(embedding_layer)
 lstm_model.add(LSTM(128, return_sequences=True, dropout=dropout, recurrent_dropout=recurrent_dropout))
lstm_model.add(LSTM(128))
lstm_model.add(Dense(1, activation='sigmoid'))
adam = optimizers.Adam(lr=lr)
 lstm_model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'])
 print(lstm_model.summary())
<train_model>
%%time
model.train(dataset_train, dataset_val,
learning_rate=LEARNING_RATE,
epochs=7,
layers='all',
augmentation=augmentation )
checkpoint = ModelCheckpoint('lstm_model.h5', monitor='val_acc', save_best_only=True)
 history = lstm_model.fit(X_train_text, y_train, batch_size=batch_size, callbacks=[checkpoint], epochs=num_epochs,
class_weight=class_weight, validation_data=(X_val_text, y_val), verbose=1)
 plot_model_performance(history )<choose_model_class>
%%time
model.train(dataset_train, dataset_val,
learning_rate=LEARNING_RATE/5,
epochs=15,
layers='all',
augmentation=augmentation )
<normalization>
class InferenceConfig(DetectorConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1
inference_config = InferenceConfig()
model = modellib.MaskRCNN(mode='inference',
config=inference_config,
model_dir=ROOT_DIR)
 assert model_path != "", "Provide path to trained weights"
print("Loading weights from ", model_path)
 model.load_weights(model_path, by_name=True )
class attention(Layer):
def __init__(self,**kwargs):
super(attention,self ).__init__(**kwargs)
 def build(self,input_shape):
self.W=self.add_weight(name="att_weight",shape=(input_shape[-1],1),initializer="normal")
 self.b=self.add_weight(name="att_bias",shape=(input_shape[1],1),initializer="zeros")
 super(attention, self ).build(input_shape)
 def call(self,x):
et=K.squeeze(K.tanh(K.dot(x,self.W)+self.b),axis=-1)
 at=K.softmax(et)
 at=K.expand_dims(at,axis=-1)
 output=x*at
return K.sum(output,axis=1)
 def compute_output_shape(self,input_shape):
return(input_shape[0],input_shape[-1])
 def get_config(self):
return super(attention,self ).get_config()<choose_model_class>
test_image_fps = get_dicom_fps(test_dicom_dir )
num_epochs=15
dropout=0.3
recurrent_dropout=0.3
lr=0.0005
batch_size=128
sequence_input = Input(shape=(maxlen,))
embedded_sequences = Embedding(vocab_size, 200, weights=[embedding_matrix], trainable=False )(sequence_input)
 lstm = Bidirectional(LSTM(128, return_sequences = True, dropout=dropout, recurrent_dropout=recurrent_dropout))(embedded_sequences)
(lstm, forward_h, forward_c, backward_h, backward_c)= Bidirectional(LSTM(128, return_sequences=True, return_state=True))(lstm)
 att_out=attention()(lstm)
 outputs=Dense(1,activation='sigmoid' )(att_out)
 model_attn = Model(sequence_input, outputs)
 adam = optimizers.Adam(lr=lr)
 model_attn.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'])
 print(model_attn.summary() )<train_model>
def predict(image_fps, filepath='submission.csv', min_conf=0.95):
resize_factor = ORIG_SIZE / config.IMAGE_SHAPE[0]
with open(filepath, 'w')as file:
file.write("patientId,PredictionString
")
 for image_id in tqdm(image_fps):
ds = pydicom.read_file(image_id)
 image = ds.pixel_array
if len(image.shape)!= 3 or image.shape[2] != 3:
image = np.stack(( image,)* 3, -1)
 image, window, scale, padding, crop = utils.resize_image(
 image,
min_dim=config.IMAGE_MIN_DIM,
min_scale=config.IMAGE_MIN_SCALE,
max_dim=config.IMAGE_MAX_DIM,
mode=config.IMAGE_RESIZE_MODE)
 patient_id = os.path.splitext(os.path.basename(image_id)) [0]
results = model.detect([image])
 r = results[0]
out_str = ""
out_str += patient_id
out_str += ","
assert(len(r['rois'])== len(r['class_ids'])== len(r['scores']))
if len(r['rois'])== 0:
pass
else:
num_instances = len(r['rois'])
 for i in range(num_instances):
if r['scores'][i] > min_conf:
out_str += ' '
out_str += str(round(r['scores'][i], 2))
out_str += ' '
x1 = r['rois'][i][1]
y1 = r['rois'][i][0]
width = r['rois'][i][3] - x1
height = r['rois'][i][2] - y1
bboxes_str = "{} {} {} {}".format(x1*resize_factor, y1*resize_factor, \
width*resize_factor, height*resize_factor)
 out_str += bboxes_str
file.write(out_str+"
" )
checkpoint = ModelCheckpoint('attn_model.h5', monitor='val_acc', save_best_only=True)
 history_attn = model_attn.fit(X_train_text, y_train, batch_size=batch_size, callbacks=[checkpoint], epochs=num_epochs,
class_weight=class_weight, validation_data=(X_val_text, y_val), verbose=1)
 plot_model_performance(history_attn )<init_hyperparams>
submission_fp = os.path.join(ROOT_DIR, 'submission.csv')
 predict(test_image_fps, filepath=submission_fp)
 print(submission_fp )
maxlen = 160
lr = 1e-5
num_epochs = 3
batch_size=16<string_transform>
output = pd.read_csv(submission_fp, names=['patientId', 'PredictionString'])
 output.head(60 )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
submission_val_fp = os.path.join(ROOT_DIR, 'coco-mask-rcnn-with-submission-0.166-retrain-validation.csv')
 predict(image_fps_val, filepath=submission_val_fp)
 print(submission_val_fp )
<feature_engineering><EOS> 
!rm -rf /kaggle/working/Mask_RCNN