Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- Image_features_ecoder_decoder.pickle +3 -0
- cnn-rnn.py +125 -0
- encoder_decoder_epoch_5.h5 +3 -0
- my_embedding_matrix.npy +3 -0
Image_features_ecoder_decoder.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1770e1e63a3c67820b988ce259774f255a3e96e599d41e09fa9d5219a9bc51fe
|
| 3 |
+
size 30734447
|
cnn-rnn.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow.keras.applications import densenet
|
| 3 |
+
from tensorflow.keras.applications.densenet import preprocess_input
|
| 4 |
+
from tensorflow.keras.layers import Dense, Dropout, Input, Conv2D
|
| 5 |
+
from tensorflow.keras.models import Model
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
from tqdm import tqdm
|
| 11 |
+
import os
|
| 12 |
+
import cv2
|
| 13 |
+
import tensorflow as tf
|
| 14 |
+
import re
|
| 15 |
+
import pickle
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from skimage.transform import resize
|
| 18 |
+
import warnings
|
| 19 |
+
warnings.filterwarnings('ignore')
|
| 20 |
+
import seaborn as sns
|
| 21 |
+
from tqdm import tqdm
|
| 22 |
+
import tensorflow as tf
|
| 23 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 24 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 25 |
+
from sklearn.model_selection import train_test_split
|
| 26 |
+
import time
|
| 27 |
+
from tensorflow.keras.models import Model
|
| 28 |
+
from tensorflow.keras.layers import Dense, LSTM, Input, Embedding, Conv2D, Concatenate, Flatten, Add, Dropout, GRU
|
| 29 |
+
import random
|
| 30 |
+
import datetime
|
| 31 |
+
from nltk.translate.bleu_score import sentence_bleu
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def getModel():
|
| 36 |
+
input1 = Input(shape=(2048), name='Image_input')
|
| 37 |
+
dense1 = Dense(256, kernel_initializer=tf.keras.initializers.glorot_uniform(seed = 56), name='dense_encoder')(input1)
|
| 38 |
+
|
| 39 |
+
input2 = Input(shape=(153), name='Text_Input')
|
| 40 |
+
embedding_layer = Embedding(input_dim = 1427, output_dim = 300, input_length=153, mask_zero=True, trainable=False,
|
| 41 |
+
weights=[embedding_matrix_vocab], name="Embedding_layer")
|
| 42 |
+
emb = embedding_layer(input2)
|
| 43 |
+
|
| 44 |
+
LSTM1 = LSTM(units=256, activation='tanh', recurrent_activation='sigmoid', use_bias=True,
|
| 45 |
+
kernel_initializer=tf.keras.initializers.glorot_uniform(seed=23),
|
| 46 |
+
recurrent_initializer=tf.keras.initializers.orthogonal(seed=7),
|
| 47 |
+
bias_initializer=tf.keras.initializers.zeros(), return_sequences=True, name="LSTM1")(emb)
|
| 48 |
+
#LSTM1_output = LSTM1(emb)
|
| 49 |
+
|
| 50 |
+
LSTM2 = LSTM(units=256, activation='tanh', recurrent_activation='sigmoid', use_bias=True,
|
| 51 |
+
kernel_initializer=tf.keras.initializers.glorot_uniform(seed=23),
|
| 52 |
+
recurrent_initializer=tf.keras.initializers.orthogonal(seed=7),
|
| 53 |
+
bias_initializer=tf.keras.initializers.zeros(), name="LSTM2")
|
| 54 |
+
LSTM2_output = LSTM2(LSTM1)
|
| 55 |
+
|
| 56 |
+
dropout1 = Dropout(0.5, name='dropout1')(LSTM2_output)
|
| 57 |
+
|
| 58 |
+
dec = tf.keras.layers.Add()([dense1, dropout1])
|
| 59 |
+
|
| 60 |
+
fc1 = Dense(256, activation='relu', kernel_initializer=tf.keras.initializers.he_normal(seed = 63), name='fc1')
|
| 61 |
+
fc1_output = fc1(dec)
|
| 62 |
+
|
| 63 |
+
dropout2 = Dropout(0.4, name='dropout2')(fc1_output)
|
| 64 |
+
|
| 65 |
+
output_layer = Dense(1427, activation='softmax', name='Output_layer')
|
| 66 |
+
output = output_layer(dropout2)
|
| 67 |
+
|
| 68 |
+
encoder_decoder = Model(inputs = [input1, input2], outputs = output)
|
| 69 |
+
|
| 70 |
+
encoder_decoder.load_weights("/content/encoder_decoder_epoch_5.h5")
|
| 71 |
+
# encoder
|
| 72 |
+
|
| 73 |
+
encoder_input = encoder_decoder.input[0]
|
| 74 |
+
encoder_output = encoder_decoder.get_layer('dense_encoder').output
|
| 75 |
+
encoder_model = Model(encoder_input, encoder_output)
|
| 76 |
+
|
| 77 |
+
# decoder#
|
| 78 |
+
text_input = encoder_decoder.input[1]
|
| 79 |
+
enc_output = Input(shape=(256,), name='Enc_Output')
|
| 80 |
+
text_output = encoder_decoder.get_layer('LSTM2').output
|
| 81 |
+
add1 = tf.keras.layers.Add()([text_output, enc_output])
|
| 82 |
+
fc_1 = fc1(add1)
|
| 83 |
+
decoder_output = output_layer(fc_1)
|
| 84 |
+
|
| 85 |
+
decoder_model = Model(inputs = [text_input, enc_output], outputs = decoder_output)
|
| 86 |
+
|
| 87 |
+
return encoder_model,decoder_model
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def greedysearch(image):
|
| 91 |
+
# Open the pickle file for reading
|
| 92 |
+
with open('/content/Image_features_ecoder_decoder.pickle', 'rb') as f:
|
| 93 |
+
Xnet_features = pickle.load(f)
|
| 94 |
+
|
| 95 |
+
encoder_model, decoder_model = getModel()
|
| 96 |
+
input_ = 'startseq'
|
| 97 |
+
image_features = encoder_model.predict(image)
|
| 98 |
+
result = []
|
| 99 |
+
tokenizer = Tokenizer(filters='!"#$%&()*+,-/:;<=>?@[\]^_`{|}~\t\n')
|
| 100 |
+
tokenizer.fit_on_texts(y_train.values)
|
| 101 |
+
for i in range(153):
|
| 102 |
+
input_tok = [tokenizer.word_index[w] for w in input_.split()]
|
| 103 |
+
input_padded = pad_sequences([input_tok], 153, padding='post')
|
| 104 |
+
predictions = decoder_model.predict([input_padded, image_features])
|
| 105 |
+
arg = np.argmax(predictions)
|
| 106 |
+
if arg != 7: # endseq
|
| 107 |
+
result.append(tokenizer.index_word[arg])
|
| 108 |
+
input_ = input_ + ' ' + tokenizer.index_word[arg]
|
| 109 |
+
else:
|
| 110 |
+
break
|
| 111 |
+
rep = ' '.join(e for e in result)
|
| 112 |
+
return rep
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def get_result(img):
|
| 116 |
+
pre_Report = greedysearch(img)
|
| 117 |
+
print('------------------------------------------------------------------------------------------------------')
|
| 118 |
+
print("Predicted Report : ",pre_Report)
|
| 119 |
+
print('------------------------------------------------------------------------------------------------------')
|
| 120 |
+
return pre_Report
|
| 121 |
+
|
| 122 |
+
# get_result("/content/drive/MyDrive/cnn-rnn/NLMCXR_png/CXR192_IM-0598-1001")
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
encoder_decoder_epoch_5.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ebfd374f6b911ee367b3ebec6f32333e748e2434907173b9bcbf4e725073813c
|
| 3 |
+
size 9953276
|
my_embedding_matrix.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:91e0fb9ea63d6d1c851f90b379dac110c5d7418334f13a36cc214fde62121575
|
| 3 |
+
size 2572928
|