Skip to content

Commit

Permalink
merged lymphnode and mammo pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
HelioStrike committed Mar 14, 2020
1 parent 02b35f0 commit fe4ebe1
Show file tree
Hide file tree
Showing 102 changed files with 600 additions and 66 deletions.
Binary file not shown.
131 changes: 131 additions & 0 deletions pipelines/lymph_node/Testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

from fastai.vision import *
from fastai.metrics import error_rate
import numpy as np
import random
import h5py
import cv2 as cv2
import os

train_dir="./pipelines/lymph_node/data/data_bunch"
base_dir="./pipelines/lymph_node/data" #base directory


l=os.listdir(train_dir)
random.shuffle(l) #shuffle training images





tfms = get_transforms(do_flip=True) #do_flip: if True, a random flip is applied with probability 0.5 to images





bs=64 # also the default batch size
data = ImageDataBunch.from_csv(
base_dir,
ds_tfms=tfms,
size=224,
suffix=".tif",
folder="data_bunch",
#csv_labels="train_labels.csv",
csv_labels="dummy_labels.csv",
bs=bs)
#ImageDataBunch splits out the imnages (in the train sub-folder) into a training set and validation set (defaulting to an 80/20 percent split)





data.normalize(imagenet_stats)
# transform the image values according to the nueral network we are using





learn = cnn_learner(data,models.densenet161, metrics=error_rate, callback_fns=ShowGraph)
#cnn_learner loads the model into learn variable`






f2 = h5py.File("./pipelines/lymph_node_backend/camelyonpatch_level_2_split_test_y.h5", 'r')
set2 = f2['y']





learn=learn.load("./pipelines/lymph_node/model/densenet10epochs.pth")




import matplotlib.pyplot as plt


image=orig
actual=image
preds=learn.predict(actual)
r=int(preds[0])
fig = plt.figure()
fig.set_figheight(3)
fig.set_figwidth(3)
if(set2[i][0][0][0]==0):
actualresult="Normal"
else:
actualresult="Metastatic"
if(r==0):
result="Normal"
else:
result="Metastatic"
plt.imshow(color)
plt.show()
print("Predicted Percentages")
print("Normal:",float(preds[2][0])*100,"%","Metastatic:",float(preds[2][1])*100,"%")
print("\n")
print("Ground Truth:",actualresult)
print("Predicted:",result)


if(result==set2[i][0][0][0]):
c=c+1


# In[16]:


actual=open_image("/home/abhay/environments/histopathologic-cancer-detection/new_histotest/"+"0.tiff")
predict=learn.predict(actual)


# In[17]:


predict


# In[20]:


float(predict[2][0])*100


# In[ ]:


print("Testing Accuracy")
print((c/32768)*100)


# In[ ]:




126 changes: 126 additions & 0 deletions pipelines/lymph_node/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from __future__ import print_function
import numpy as np
from PIL import Image
from fastai.vision import *
from fastai.metrics import error_rate
import cv2
import os
import matplotlib.pyplot as plt
import random
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import h5py
from fastai.vision.transform import get_transforms
from fastai.vision.data import ImageDataBunch
from fastai.vision.image import pil2tensor


MODEL_PATH = './pipelines/lymph_node_backend/data/densenet10epochs.pth'


def preprocess(image_location):
images=[]
for location in image_location:
img=cv2.imread(location)
print("location ",location)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_TRUNC) #applying truncation
rgb = np.repeat(thresh[..., np.newaxis], 3, -1)
print("type ",type(rgb))
images.append((Image(pil2tensor(rgb,dtype=np.float32).div_(255))))
print("tensor ",type(Image(pil2tensor(rgb,dtype=np.float32).div_(255))))
return images

def predict(images):
#loadig model
train_dir="./pipelines/lymph_node/data/data_bunch"
base_dir="./pipelines/lymph_node/data" #base directory
l=os.listdir(train_dir)
#random.shuffle(l)
tfms = get_transforms(do_flip=True)
#do_flip: if True, a random flip is applied with probability 0.5 to images
bs=64 # also the default batch size
print("loaddd")
#ImageDataBunch splits out the imnages (in the train sub-folder) into a training set and validation set (defaulting to an 80/20 percent split)
data = ImageDataBunch.from_csv(base_dir, ds_tfms=tfms, size=224, suffix=".tiff",folder="data_bunch",csv_labels="dummy_labels.csv", bs=bs)
print("valid ",data.valid_ds)
print("train ",data.train_ds)
print("test ",data.test_ds)
# transform the image values according to the nueral network we are using
data.normalize(imagenet_stats)

#cnn_learner loads the model into learn variable`
learn = cnn_learner(data,models.densenet161, metrics=error_rate, callback_fns=ShowGraph)



learn=learn.load("./densenet10epochs")
#predicting labels
print("prediction ",type(images))
print(images)
print("size ",len(images),images[0].shape)
preds=learn.predict(images[0])
#preds=learn.pred_batch(np.array(images)) #TO-DO!!!!!!!!
print(type(preds))
print("prediction ",preds)
return preds






#pass in the original , and get the image to be displayed on the website
def get_display_image(orig):
#preprocessing
print(type(orig))
print("def init LOCCCCCC",orig)
preprocessed=preprocess(orig)
preds=predict(preprocessed)
print(preds)
fh = 30
fw = 15
f2 = h5py.File("./pipelines/lymph_node/data/camelyonpatch_level_2_split_test_y.h5", 'r') #contains actuals test labels
set2 = f2['y']
print(len(set2))
imgs = [orig,preds]
titles = ['Image', 'Normal', 'Metastatic','actual']
f, ax = plt.subplots(len(orig), 4 , figsize=(fh,fw))
print(preds[0])
for i in range(len(orig)):
print(preds[0])
r=int(preds[0])
print("rrrrrrr ",r)
"""fig = plt.figure()
fig.set_figheight(3)
fig.set_figwidth(3)"""
image_name=orig[i].split("/")
val=image_name[1].split(".")
print("val ",val)
print(set2)
ans=""

if(set2[(int)(val[0])][0][0][0]==0):
actualresult="Normal"
else:
actualresult="Metastatic"
if(r==0):
result="Normal"
else:
result="Metastatic"
#plt.imshow(cv2.imread(orig[i]))
#plt.show()
m1=str(round(float(preds[2][1])*100,2))
m2=str(round(float(preds[2][0])*100,2))
ans=actualresult+" "+"Metastatic"+m1+" "+"Normal"+m2
print(ans)
"""print("Predicted Percentages")
print("Normal:",float(preds[2][0])*100,"%","Metastatic:",float(preds[2][1])*100,"%")
print("\n")
print("Ground Truth:",actualresult)
print("Predicted:",result)"""

return ans




Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/0.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/1.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/10.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/11.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/12.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/13.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/14.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/15.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/16.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/17.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/2.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/3.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/4.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/5.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/6.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/69.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/7.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/70.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/71.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/72.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/73.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/74.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/75.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/76.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/77.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/78.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/79.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/8.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/80.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/81.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/82.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/83.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/84.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/85.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/9.tiff
Binary file not shown.
Binary file added pipelines/lymph_node/data/data_bunch/99.tiff
Binary file not shown.
36 changes: 36 additions & 0 deletions pipelines/lymph_node/data/dummy_labels.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
0,0
1,1
2,0
3,1
4,1
5,0
6,1
7,1
8,1
9,0
10,1
11,0
12,1
13,1
14,0
15,1
16,0
17,1
69,1
70,1
71,0
72,0
73,0
74,1
75,1
76,1
77,1
78,1
79,1
80,1
81,0
82,1
83,1
84,1
85,1
99,0
Binary file not shown.
2 changes: 1 addition & 1 deletion pipelines/mammography/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
#pass in the original and mask image, and get the image to be displayed on the website
def get_display_image(orig):
inp = np.array(Image.open(orig))
pred=predictt(inp)
pred=predictt(inp,orig.split("/")[1])
return pred
Binary file modified pipelines/mammography/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file modified pipelines/mammography/__pycache__/frcnn_test_vgg.cpython-37.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions pipelines/mammography/annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mdb012.png,431,526,511,606,B
mdb265.png,533,466,653,586,M
mdb290.png,292,626,382,716,B
mdb314.png,479,794,557,872,B
mdb069.png,418,574,506,662,B
mdb144.png,286,457,340,511,M
mdb075.png,445,284,491,330,M
mdb025.png,595,502,753,660,B
mdb142.png,321,362,373,414,B
27 changes: 22 additions & 5 deletions pipelines/mammography/frcnn_test_vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,10 @@ def get_real_coordinates(ratio, x1, y1, x2, y2):

model_classifier = Model([feature_map_input, roi_input], classifier)

print('Loading weights from {}'.format(C.model_path))
model_rpn.load_weights('pipelines/mammography/model/model_frcnn_vgg.hdf5', by_name=True)
model_classifier.load_weights('pipelines/mammography/model/model_frcnn_vgg.hdf5', by_name=True)
model_path = 'pipelines/mammography/model/model_frcnn_vgg.hdf5'
print('Loading weights from {}'.format(model_path))
model_rpn.load_weights(model_path, by_name=True)
model_classifier.load_weights(model_path, by_name=True)

model_rpn.compile(optimizer='sgd', loss='mse')
model_classifier.compile(optimizer='sgd', loss='mse')
Expand Down Expand Up @@ -1164,9 +1165,10 @@ def get_real_coordinates(ratio, x1, y1, x2, y2):
#print(img_name)
#st = time.time()
#filepath = os.path.join(test_base_path, img_name)
def predictt(img):
def predictt(img,img_name):
#img = cv2.imread(filepath)



X, ratio = format_img(img, C)

X = np.transpose(X, (0, 2, 3, 1))
Expand Down Expand Up @@ -1270,6 +1272,21 @@ def predictt(img):
cv2.rectangle(img, (textOrg[0] - 5,textOrg[1]+baseLine - 5), (textOrg[0]+retval[0] + 5, textOrg[1]-retval[1] - 5), (255, 255, 255), -1)
cv2.putText(img, textLabel, textOrg, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1)

gt_data= pd.read_csv(os.getcwd()+'/pipelines/mammography/annotations.txt',sep=",",header=None)
gt_data=gt_data.set_index(0).T.to_dict('list')
print("**********",gt_data)
original_xmin,original_ymin,original_xmax,original_ymax,original_class=gt_data[img_name]
print(original_xmin,original_ymin,original_xmax,original_ymax,original_class)
print(type(original_xmax))

#original_xmin,original_ymin,original_xmax,original_ymax,original_class=gt_data[test_base_path+img_name]
cv2.rectangle(img,(original_xmin,original_ymin),(original_xmax,original_ymax),(0,255,0),4)
textgt = (original_xmin,original_ymin-0)
(retval,baseLine) = cv2.getTextSize(original_class,cv2.FONT_HERSHEY_COMPLEX,1,1)
cv2.rectangle(img,(textgt[0]-5,textgt[1]+baseLine-5),(textgt[0]+retval[0] + 5, textgt[1]-retval[1] - 5),(0,0,0),1)
cv2.rectangle(img, (textgt[0] - 5,textgt[1]+baseLine - 5), (textgt[0]+retval[0] + 5, textgt[1]-retval[1] - 5), (255, 255, 255), -1)
cv2.putText(img, original_class, textgt, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1)

#print('Elapsed time = {}'.format(time.time() - st))
##print(all_dets)
#plt.figure(figsize=(10,10))
Expand Down
Binary file added sample_inputs/lymph_node/0.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/1.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/10.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/11.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/12.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/13.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/14.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/15.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/16.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/17.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/2.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/3.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/4.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/5.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/6.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/69.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/7.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/70.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/71.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/72.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/73.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/74.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/75.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/76.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/77.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/78.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/79.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/8.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/80.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/81.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/82.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/83.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/84.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/85.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/9.tiff
Binary file not shown.
Binary file added sample_inputs/lymph_node/99.tiff
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading

0 comments on commit fe4ebe1

Please sign in to comment.