#import everything needed
import dicom
import os
import numpy
import csv
from matplotlib import pyplot, cm
from PIL import Image
import pydicom
import scipy.ndimage
import scipy.ndimage
import math
import matplotlib.pyplot as plt
from keras.optimizers import SGD
import random
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
from PIL import Image
from PIL import ImageOps
import cv2
#model = Sequential()
#model.add(Conv2D(64, (3,3), input_shape = x_train.shape[1:]))
#model.add(Activation("relu"))
#model.add(MaxPooling2D(pool_size=(2,2)))
#model.add(Conv2D(64, (3,3)))
#model.add(Activation("relu"))
#model.add(MaxPooling2D(pool_size=(2,2)))
#model.add(Flatten())
#model.add(Dense(64))
#model.add(Dense(1))
#model.add(Activation('sigmoid'))
#model.compile(loss = 'binary_crossentropy', optimizer="adam", metrics=['accuracy'])
#model.fit()
PathDicom = "/Users/armaanjohal/Desktop/Computer Vision Files/CBIS-DDSM-Training/CBIS-DDSM/"
PathCSV = "/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_train_set.csv"
lstFilesDCM = [] # create an empty list for training files
for dirName, subdirList, fileList in os.walk(PathDicom):
for filename in fileList:
if ".dcm" in filename.lower(): # check whether the file's DICOM
lstFilesDCM.append(os.path.join(dirName,filename))
PathDicom_test = "/Users/armaanjohal/Desktop/Computer Vision Files/CBIS-DDSM-Test/CBIS-DDSM"
PathCSV1_test = "/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_test_set.csv"
lstFilesDCM_test = [] # create an empty list for testing files
for dirName, subdirList, fileList in os.walk(PathDicom_test):
for filename in fileList:
if ".dcm" in filename.lower(): # check whether the file's DICOM
lstFilesDCM_test.append(os.path.join(dirName,filename))
RefDs = dicom.read_file(lstFilesDCM[0])
ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM))
def resize (file, x, y):
#x and y are desired dimensions for resizing
#checks if file is DICOM or PNG and reads into array
if ".dcm" in filename.lower():
RefDs = dicom.read_file(file)
elif ".png" in filenam.lower():
RefDs = numpy.asarray(file)
# Finds multiplication factor for resizing
data = RefDs.pixel_array
#if direc == 255.0:
#plot_image = numpy.concatenate((plot_image,numpy.fliplr(resize(lstFilesDCM[f], 299, 299, 0))), axis =1)
#print('The image has {} x {} voxels'.format(data.shape[0],data.shape[1]))
x1 = [math.ceil(data.shape[0]/x)]
y1 = [math.ceil(data.shape[1]/y)]
data_downsampling = data[::x1[0], ::y1[0]]
#print('The downsampled image has {} x {} voxels'.format(data_downsampling.shape[0], data_downsampling.shape[1]))
# copy the data back to the original data set
RefDs.PixelData = data_downsampling.tobytes()
# update the information regarding the shape of the data array
RefDs.Rows, RefDs.Columns = data_downsampling.shape
# print the image information given in the dataset
#print('The information of the data set after downsampling: \n')
#print(RefDs)
#Creating a NaN array so dimensions are always 299x299
retArray = numpy.zeros((x,y))
#newArray = resize(lstFilesDCM[0], x, y)
sh = data_downsampling.shape
retArray[0:sh[0], 0:sh[1]] = RefDs.pixel_array
return retArray
#an example of the retArray of dimensions 299, 299:
#resize(lstFilesDCM[0], 299, 299, getDir(csv_train, dicom.read_file(lstFilesDCM[0])))
getDir(csv_train, dicom.read_file(lstFilesDCM[4]))
resizeFunc = numpy.zeros([299, 299])
import csv
def getDensity (csvfl, RefDs):
lim = 0
with open(csvfl, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if RefDs.StudyInstanceUID in row['image file path'] and RefDs.SeriesInstanceUID in row['image file path'] and RefDs.PatientID in row['image file path'] and lim<1:
#Adding if statement to return 0 (not dense) or 1(dense) for given breast density
#print(row['breast_density'])
#if row['breast_density'] == '1' or row['breast_density'] == '2':
#print('low')
#return '0'
#elif row['breast_density'] == '3' or row['breast_density'] == '4':
#print('high')
#return '1'
if row['breast_density'] == '1':
#print('low')
return '1'
if row['breast_density'] == '2':
#print('low')
return '2'
if row['breast_density'] == '3':
#print('low')
return '3'
if row['breast_density'] == '4':
#print('low')
return '4'
#resizeFunc = numpy.flipud(resize(RefDs, 299, 299))
#elif row['left or right breast'] == 'RIGHT':
#resizeFunc = resize(RefDs, 299, 299)
#im.save("lena_mirror.png")
#return row['breast_density']
lim = lim + 1
def getDir (csvfl, RefDs):
direction = '0'
lim1 = 0
with open(csvfl, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if RefDs.StudyInstanceUID in row['image file path'] and RefDs.SeriesInstanceUID in row['image file path'] and RefDs.PatientID in row['image file path'] and lim1<1:
#Adding if statement to return 0 (not dense) or 1(dense) for given breast density
#print(row['breast_density'])
if row['left or right breast'] == 'LEFT':
direction = '1'
return direction
#resizeFunc = numpy.flipud(resize(RefDs, 299, 299))
#elif row['left or right breast'] == 'RIGHT':
#resizeFunc = resize(RefDs, 299, 299)
#im.save("lena_mirror.png")
#return row['breast_density']
lim1 = lim1 + 1
def Flip (file):
from PIL import Image
from math import sqrt
imag1 = file
#Convert the image te RGB if it is a .gif for example
imag = Image.fromarray(imag1)
imag = imag.convert ('RGB')
#coordinates of the pixel
X,Y = 250,160
#Get RGB
pixelRGB = imag.getpixel((X,Y))
R,G,B = pixelRGB
brightness = sum([R,G,B])/3 ##0 is dark (black) and 255 is bright (white)
return brightness
def Flip_and_Resize (flip, seq, x, y):
if flip == 255.0:
#ret = resize(lstFilesDCM[seq], x, y)
ret = numpy.fliplr(resize(lstFilesDCM[seq], x, y))
#ret1 = BreastIsolation(ret)
#ret = resize(ret, x, y)
#print('flip')
#print(getDir(csv_train, dicom.read_file(lstFilesDCM[f])))
else:
ret = resize(lstFilesDCM[seq], x, y)
#ret1 = BreastIsolation(ret)
#ret = resize(ret, x, y)
#print('noflip')
return ret #ret1
numpyZ = [resize(lstFilesDCM[0], 299, 299), getDensity('/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_train_set.csv', RefDs)]
print(numpyZ)
import keras
import tensorflow as tf
mnist = tf.keras.datasets.mnist
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
#Create empty 3D matricies for training and testing
num = 0
num1 = 0
#Had errors with any more files than 32 for some reason
sz = 1230
sz1 = 360
x_train = numpy.zeros([sz, 89401])#89401, 358801, 808201
y_train = numpy.zeros([sz, 1])
x_test = numpy.zeros([sz, 89401])
y_test = numpy.zeros([sz, 1])
csv_train = '/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_train_set.csv'
csv_test = '/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_test_set.csv'
#Assign values of numpy files to the empty 3D training and testing matricies
while num<sz:
mat_train = Flip_and_Resize(Flip(resize(lstFilesDCM[num], 299, 299)), num, 1, 89401)
density_train = getDensity(csv_train, dicom.read_file(lstFilesDCM[num]))
#mat_test = resize(lstFilesDCM_test[num], 1,89401)
#density_test = getDensity(csv_test, dicom.read_file(lstFilesDCM_test[num]))
x_train[num,:] = mat_train
tmp = x_train[num,:]
x_train[num,:] = numpy.reshape(tmp, 89401)
y_train[num,:] = density_train
#x_test[num,:] = mat_test
#x_test[num,:] = numpy.reshape(tmp, 89401)
#y_test[num,:] = density_test
num = num + 1
while num1<sz1:
mat_test = Flip_and_Resize(Flip(resize(lstFilesDCM_test[num1], 299, 299)), num1, 1, 89401)
density_test = getDensity(csv_test, dicom.read_file(lstFilesDCM_test[num1]))
x_test[num1,:] = mat_test
tmp1 = x_test[num1,:]
x_test[num1,:] = numpy.reshape(tmp1, 89401)
y_test[num1,:] = density_test
num1 = num1 + 1
#Normalize the training and testing values to 1
x_train = tf.keras.utils.normalize(x_train, axis = 1)
x_test = tf.keras.utils.normalize(x_test, axis = 1)
#Create a model with 2 layers and 2 outputs
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(5, activation=tf.nn.softmax)) #2
#Compile and train
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
#model.fit(x_train,y_train, epochs=10)
model.fit(x_train,y_train, batch_size = 32, epochs=10, validation_split=0.05)
val_loss, val_acc = model.evaluate(x_test, y_test, verbose=0)
print("%.2f%% (+/- %.2f%%)" % (numpy.mean(val_acc*100), numpy.std(val_acc*100)))
#save model
tf.keras.models.save_model(model, '/Users/armaanjohal/Desktop/Computer Vision Files/cool_neural_network1', overwrite=True, include_optimizer=True)
#call saved model
new_model = tf.keras.models.load_model('/Users/armaanjohal/Desktop/Computer Vision Files/cool_neural_network1', custom_objects=None, compile=False)
predictions = new_model.predict(x_test)
print(numpy.argmax(predictions[0]))
f = 0
plot_image = x_test[0]
while f<9:
if
plot_image = numpy.concatenate((plot_image,x_test[random.randrange(32)]), axis=1)
f=f+1
plt.figure(figsize = (100,100))
plt.imshow(plot_image, cmap=plt.cm.binary)
plt.show()
plot_image = resize(lstFilesDCM[0], 299, 299)#, getDir(csv_train, dicom.read_file(lstFilesDCM[0])))
f = 1
while f<10:
l_r = Flip(resize(lstFilesDCM[f], 299, 299))
if f<1:
plot_image = numpy.concatenate((plot_image,resize(lstFilesDCM[f], 299, 299)), axis =1)
print('noflip')
else:
if l_r == 255.0:
plot_image = numpy.concatenate((plot_image,numpy.fliplr(resize(lstFilesDCM[f], 299, 299))), axis =1)
print('flip')
#print(getDir(csv_train, dicom.read_file(lstFilesDCM[f])))
else:
plot_image = numpy.concatenate((plot_image,resize(lstFilesDCM[f], 299, 299)), axis =1)
print('noflip')
f=f+1
#plot_image = numpy.concatenate((plot_image,resize(lstFilesDCM[random.randrange(32)], 299, 299)), axis =1)
plt.figure(figsize = (100,100))
plt.imshow(plot_image, cmap=plt.cm.binary)
plt.show()
q = resize(lstFilesDCM[0], 299, 299)
getDensity(csv_train, lstFilesDCM[0], dicom.read_file(lstFilesDCM[0]))
qq = resize(lstFilesDCM[0], 299, 299)#, getDir(csv_train, dicom.read_file(lstFilesDCM[0])))#numpy.flipud(x_train)
qq = tf.keras.utils.normalize(qq, axis = 1)
plt.figure(figsize = (100, 100))
plt.imshow(qq, cmap=plt.cm.binary)
plt.show()
qq.shape
pp = numpy.fliplr(qq)
pp.shape
plt.imshow(pp)#, cmap=plt.cm.binary)
plt.show()
imagg = Image.fromarray(resize(lstFilesDCM[num], 299, 299))
mask=imagg.convert("L")
th=255 # the value has to be adjusted for an image of interest
mask = mask.point(lambda i: i < th and 255)
plt.imshow(mask, cmap=plt.cm.binary)
plt.show()
fl = resize(lstFilesDCM[num], 299, 299)
img = fl
img = img.astype(numpy.uint8)
edges = cv2.Canny(img, 700, 1090)
plt.imshow(edges)
#indices = numpy.where(edges >= [0])
#coordinates = tuple(zip(indices[0], indices[1]))
#print(coordinates)
a = numpy.asarray(mask)
numRows = numpy.size(a,0)
ct = 0
boundry = []
boundry1 = numpy.zeros([299, 299])
while ct < numRows:
hit = 0
hit1 = 0
double = False
pix = 0
vec = a[ct,:]
x1 = numpy.where(vec == [255])
x = x1[0]
while pix < len(x)-2:
if x[pix+1] - x[pix] >1:
hit = pix+1
hit1 = pix
double = True
pix = pix+1
if x[hit] <= 250:
boundry.append([x[hit],ct])
boundry1[x[hit], ct] = 1
if double == True:
boundry.append([x[hit1],ct])
boundry1[x[hit1], ct] = 1
ct = ct+1
boundry = numpy.asarray(boundry)
x,y = boundry.T
plt.axis([0,300, 300, 0])
plt.plot(x,y)
#vec = a[253,:]
#x1 = numpy.where(vec == [255])
#x = x1[0]
#len(x1[0])
#x
ct = 0
boundry = []
while ct < numRows:
leng = 0
vec = a[ct,:]
x1 = numpy.where(vec == [0])
x = x1[0]
while leng < len(x):
boundry.append([x,ct])
leng = leng+1
ct = ct+1
#indices = numpy.where(numpy.asarray(mask) == [0])
#coordinates = tuple(zip(indices[0], indices[1]))
#print(coordinates)
print(len(coordinates))
#i = coordinates[0]
#i[0]
#justBreast = Image.fromarray(resize(lstFilesDCM[0], 299, 299))
#plt.imshow(justBreast)
#plt.show()
bi_x_train = numpy.array
def BreastIsolation (file):
bi_x_train = []
imagg = Image.fromarray(file)
mask=imagg.convert("L")
th=255 # the value has to be adjusted for an image of interest
mask = mask.point(lambda i: i < th and 255)
indices = numpy.where(numpy.asarray(mask) == [0])
coordinates = tuple(zip(indices[0], indices[1]))
c = len(coordinates)
i = 0
while i < c:
coords = coordinates[i]
x = int(coords[0])
y = int(coords[1])
#pixl = mask.load()
#value = pixl[x,y]
fl = file
value = fl[x,y]
#bi_x_train.append(value)
#bi_x_train = numpy.array(bi_x_train)
bi_x_train = numpy.append(bi_x_train, value)
i=i+1
#bi_x_train = numpy.pad(bi_x_train, (0,(360000-c)), 'constant', constant_values=(numpy.nan))
bi_x_train = numpy.pad(bi_x_train, (0,(90000-c)), 'constant', constant_values=(numpy.nan))
#bi_x_train = numpy.pad(bi_x_train, (0,(90000-c)), 'constant')
return bi_x_train
iii = BreastIsolation(resize(lstFilesDCM[0], 599, 599))
#iii.shape
iii
coords = coordinates[i]
x = int(coords[0])
y = int(coords[1])
pixl = mask.load()
value = pixl[x,y]
print(value)
import keras
import tensorflow as tf
mnist = tf.keras.datasets.mnist
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
#Create empty 3D matricies for training and testing
num = 0
num1 = 0
dec = 0
dec1 = 0
#Had errors with any more files than 32 for some reason
sz = 1230
sz1 = 360
csv_train = '/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_train_set.csv'
csv_test = '/Users/armaanjohal/Desktop/Computer Vision Files/mass_case_description_test_set.csv'
x_train = numpy.zeros([sz, 90000])
x_test = numpy.zeros([sz1, 90000])
y_train = []
y_train = numpy.array(y_train)
y_test = []
y_test = numpy.array(y_test)
#Assign values of numpy files to the empty 3D training and testing matricies
while num<sz:
mat_train = Flip_and_Resize(Flip(resize(lstFilesDCM[num], 299, 299)), num, 299, 299)
density_train = getDensity(csv_train, dicom.read_file(lstFilesDCM[num]))
#mat_test = resize(lstFilesDCM_test[num], 1,89401)
#density_test = getDensity(csv_test, dicom.read_file(lstFilesDCM_test[num]))
x_train[num,:] = mat_train
tmp = x_train[num,:]
x_train[num,:] = numpy.reshape(tmp, len(mat_train))
#y_train[num,:] = density_train
y_train = numpy.append(y_train, density_train)
#y_train.append(density_train)
#y_train = int(density_train)
#x_test[num,:] = mat_test
#x_test[num,:] = numpy.reshape(tmp, 89401)
#y_test[num,:] = density_test
num = num + 1
while num1<sz1:
mat_test = Flip_and_Resize(Flip(resize(lstFilesDCM_test[num1], 299, 299)), num1, 299, 299)
density_test = getDensity(csv_test, dicom.read_file(lstFilesDCM_test[num1]))
x_test[num1,:] = mat_test
tmp1 = x_test[num1,:]
x_test[num1,:] = numpy.reshape(tmp1, len(mat_test))
y_test = numpy.append(y_test, density_test)
#y_test.append(density_test)
#y_test = int(density_test)
#y_test[num1,:] = density_test
num1 = num1 + 1
#Normalize the training and testing values to 1
#x_train = tf.keras.utils.normalize(x_train, axis = 1)
#x_test = tf.keras.utils.normalize(x_test, axis = 1)
#Create a model with 2 layers and 2 outputs
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax))
#Compile and train
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
#model.fit(x_train,y_train, epochs=10)
model.fit(x_train,y_train, batch_size = 32, epochs=10, validation_split=0.05)
num = 0
sz = 10
y_train = []
x_train = numpy.zeros([sz, 70000])
y_train = numpy.array(y_train)
while num<10:
mat_train = Flip_and_Resize(Flip(resize(lstFilesDCM[num], 299, 299)), num, 299, 299)
density_train = getDensity(csv_train, dicom.read_file(lstFilesDCM[num]))
#mat_test = resize(lstFilesDCM_test[num], 1,89401)
#density_test = getDensity(csv_test, dicom.read_file(lstFilesDCM_test[num]))
#y_train = numpy.zeros([sz, 1])
x_train[num,:] = mat_train
tmp = x_train[num,:]
x_train[num,:] = numpy.reshape(tmp, len(mat_train))
#y_train[num,:] = density_train
y_train = numpy.append(y_train, density_train)
#y_train.append(density_train)
#y_train = int(density_train)
#x_test[num,:] = mat_test
#x_test[num,:] = numpy.reshape(tmp, 89401)
#y_test[num,:] = density_test
num = num + 1
print(num)
y_train
#ret = resize(lstFilesDCM[0], 299, 299)
#print(ret[100,100])
#ret1 = BreastIsolation(ret)
#print(ret1)
#ret1.shape
#y_test
y_test = numpy.array(y_test)
print(y_test)
numpy.append(y_test, '1')
print(y_test)