def main(): filename = '../data/train/image_2473853.jpg' inp = imread(filename).astype('float32') plt.imshow(inp, cmap='gray') plt.show() print(inp) print(len(inp.reshape(-1, inp.shape[0], inp.shape[1], 1))) cnnnet = CNNModel() conv_layer1, conv_layer2, conv_layer3, network = cnnnet.define_network( inp.reshape(-1, inp.shape[0], inp.shape[1], 1), 'visual') model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='nodule-classifier.tfl.ckpt') model.load('nodule-classifier.tfl') labels = model.predict(inp.reshape(-1, 50, 50, 1)) print("This is the labels :", labels) if labels[0][1] == 1: print('The input image is a nodule.') else: print('The input image is not a nodule.') layer_to_be_plotted = [conv_layer1, conv_layer2, conv_layer3] for idx, layer in enumerate(layer_to_be_plotted): m2, yhat = get_layer_output(layer, model, inp) plot_layers(yhat, idx, '../visual_images/conv_layer_') weights = get_weights(m2, conv_layer1) plot_layers(weights, 0, '../visual_images/weight_conv_layer_', 6) plt.show()
def get_predictions(x_test_images, y_test_labels): """ Given the test images, cnn network predicted the labels for images. :param x_test_images: test images :param y_test_labels: response to labels for the images :return: predictions: the predicted probability values of test labels for images label_predictions: the specific class for each image """ cnn_net = CNNModel() network = cnn_net.define_network(x_test_images) model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='nodule-classifier.tfl.ckpt') model.load('nodule-classifier.tfl') predictions = np.vstack(model.predict(x_test_images[:, :, :, :])) score = model.evaluate(x_test_images, y_test_labels) print('The total classification accuracy is : ', score) label_predictions = np.zeros_like(predictions) label_predictions[np.arange(len(predictions)), predictions.argmax(1)] = 1 return predictions, label_predictions
def main(): mask_extra() lungs_ROI() generate_images() height = 50 width = 50 workpath = '../data/visualization/' workpath_1 = '../testing_patient/' file = glob(workpath + '*.npy') target = file[0][33:-4] print(target) file_list = glob('../testing_patient/lungmask_*.npy') print(file_list) for file_l in file_list: if file_l[33:-4] == target: file_name = file_l[:-4] print(file_name) # file_name = '../testing_patient/lungmask_0003_0125' image = cv2.imread(file_name + '.png') lung_mask = np.load(workpath_1 + file_name + '.npy') image_files = np.load(workpath_1 + 'images_' + file_name[28:] + '.npy') SeletiveSearch(image) images_path = '../temptation/' filelist = glob(images_path + '*.png') filelist.sort(key=lambda x: int(x[18:-4])) print(filelist) image_num = len(filelist) data = np.zeros((image_num, height, width, 1)) for idx in range(image_num): img = imread(filelist[idx]).astype('float32') img = img.reshape(-1, img.shape[0], img.shape[1], 1) data[idx, :, :, :] = img print(data.shape) # Read images and corresponding labels csvfile = open('../testing_patient/predicted_nodules.csv', 'r') csvReader = csv.reader(csvfile) images_labels = list(csvReader) csvfile_1 = open('../testing_patient/file_classes.csv', 'r') csvReader_1 = csv.reader(csvfile_1) images_nodules = list(csvReader_1) real_candidates = [] candidates = [] del images_labels[0] del images_nodules[0] for i_l in images_labels: i_l[1] = eval(i_l[1]) i_l[2] = eval(i_l[2]) candidates.append(i_l[2]) print(images_labels) print(candidates) # Get the lung nodule coordinates for j_l in images_nodules: if j_l[0] == file_name[19:]: j_l[1] = eval(j_l[1]) j_l[2] = eval(j_l[2]) real_candidates.append((j_l[1], j_l[2])) # Mapping the real regions that contain lung nodules real_nodules = [] for candidate in candidates: if (candidate[0], candidate[1]) in real_candidates: real_nodules.append(candidate) print(real_nodules) # Feed the data into trained model. cnnnet = CNNModel() network = cnnnet.define_network(data, 'testtrain') model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='nodule-classifier.tfl.ckpt') model.load('nodule-classifier.tfl') predictions = np.vstack(model.predict(data[:, :, :, :])) label_predictions = np.zeros_like(predictions) label_predictions[np.arange(len(predictions)), predictions.argmax(1)] = 1 print(len(label_predictions)) index_list = [] for ind, val in enumerate(label_predictions): if val[1] == 1: index_list.append(ind) print(len(index_list)) print(index_list) nodule_candidate = [] for i in index_list: nodule_candidate.append(candidates[i]) print(nodule_candidate) fig, ax = plt.subplots(2, 2, figsize=[8, 8]) ax[0, 0].imshow(image_files[0], cmap='gray') ax[0, 1].imshow(image_files[0] * lung_mask[0], cmap='gray') ax[1, 0].imshow(image) ax[1, 1].imshow(image) for x, y, w, h in candidates: rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax[1, 0].add_patch(rect) #for x, y, w, h in real_nodules: # rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='yellow', linewidth=1) # ax[1, 1].add_patch(rect) for x, y, w, h in nodule_candidate: rect = mpatches.Rectangle((x - 3, y - 3), w + 5, h + 5, fill=False, edgecolor='red', linewidth=1) ax[1, 1].add_patch(rect) plt.show() shutil.rmtree('../temptation') os.mkdir('../temptation')
Trains a CNN model using tflearn wrapper for tensorflow """ import tflearn import h5py from models.cnn_model import CNNModel # Load HDF5 dataset h5f = h5py.File('../data/train.h5', 'r') X_train_images = h5f['X'] Y_train_labels = h5f['Y'] h5f2 = h5py.File('../data/val.h5', 'r') X_val_images = h5f2['X'] Y_val_labels = h5f2['Y'] ## Model definition convnet = CNNModel() network = convnet.define_network(X_train_images) model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='nodule-classifier.tfl.ckpt') model.fit(X_train_images, Y_train_labels, n_epoch=50, shuffle=True, validation_set=(X_val_images, Y_val_labels), show_metric=True, batch_size=128, snapshot_epoch=False, run_id='nodule-classifier') model.save("nodule-classifier.tfl") print("Network trained and saved as nodule-classifier.tfl!") h5f.close() h5f2.close()
def main(): height = 50 width = 50 # To get the images from the folder and to sort them as the sequence as csv files images_path = '../images/' filelist = glob(images_path + '*.png') print(filelist) filelist.sort(key=lambda x: int(x[29:-4])) # Read images and corresponding labels csvfile = open('../submission_files/regions_labels.csv', 'r') csvReader = csv.reader(csvfile) # images_labels contains the name of images and the labels generated from selective search images_labels = list(csvReader) del images_labels[0] for i_l in images_labels: i_l[1] = eval(i_l[1]) i_l[2] = eval(i_l[2]) print(len(images_labels)) # Pre-processing the data to generate all dataset. image_num = len(filelist) data = np.zeros((image_num, height, width, 1)) for idx in range(image_num): img = imread(filelist[idx]).astype('float32') img = img.reshape(-1, img.shape[0], img.shape[1], 1) data[idx, :, :, :] = img print(data.shape) # Feed the data into trained model. cnnnet = CNNModel() network = cnnnet.define_network(data, 'testtrain') model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='nodule-classifier.tfl.ckpt') model.load('nodule-classifier.tfl') predictions = np.vstack(model.predict(data[:, :, :, :])) label_predictions = np.zeros_like(predictions) label_predictions[np.arange(len(predictions)), predictions.argmax(1)] = 1 print(len(label_predictions)) index_list = [] for ind, val in enumerate(label_predictions): if val[1] == 1: index_list.append(ind) images_name = [] for index in index_list: #print(images_labels[index][0][:18]) images_name.append(images_labels[index][0][:18] + '.png') images_name2 = list(set(images_name)) images_name2.sort(key=images_name.index) print(images_name2) print(len(images_name2)) img_list = glob('../data/test_images/*.png') img_list.sort() allimages = [] for l in img_list: allimages.append(l[20:]) print(allimages) # Writing the results into the files to show whether the patient contains nodules f_submission1 = open('../submission_files/predicted_patient_labels.csv', 'w') writer_1 = csv.writer(f_submission1) writer_1.writerow(('Patient', 'Predict_Labels')) for a_img in allimages: if a_img in images_name2: writer_1.writerow((a_img, 1)) else: writer_1.writerow((a_img, 0)) # Writing the results into the submission csv file f_submission = open('../submission_files/predicted_regions_labels.csv', 'w') writer = csv.writer(f_submission) writer.writerow( ('File_name', 'Labels', 'Rect', 'Actual_Nodule', 'Predict_Nodule')) for i in range(len(images_labels)): #index = index_list[i] #print(images_labels[index][0][:]) writer.writerow( (images_labels[i][0][:], images_labels[i][1], images_labels[i][2], images_labels[i][3], label_predictions[i][1])) actual = [] pre = [] for i in range(len(images_labels)): actual.append(eval(images_labels[i][3])) pre.append(predictions[i][1]) print(actual) print(pre) fpr, tpr, thresholds = roc_curve(actual, pre, pos_label=1) roc_auc = auc(fpr, tpr) print("The false positive rate is: ", fpr) print("The true positive rate is:", tpr) print(thresholds) print("The auc is:", roc_auc) plt.figure() lw = 2 plt.plot(fpr, tpr, color='darkorange', lw=lw, label='(AUC = %0.2f)' % roc_auc) plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') plt.axis('equal') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.legend(loc='lower right') plt.show() csvfile.close() f_submission.close() f_submission1.close()