def store_prediction(self, sess, batch_x, batch_y, name): prediction = sess.run(self.net.predicter, feed_dict={ self.net.x: batch_x, self.net.y: batch_y, self.net.keep_prob: 1., self.net.block_size: 1 }) pred_shape = prediction.shape loss = sess.run(self.net.cost, feed_dict={ self.net.x: batch_x, self.net.y: util.crop_to_shape(batch_y, pred_shape), self.net.keep_prob: 1., self.net.block_size: 1 }) logging.info("Verification error= {:.1f}%, loss= {:.4f}".format( error_rate(prediction, util.crop_to_shape(batch_y, prediction.shape)), loss)) img = util.combine_img_prediction(batch_x, batch_y, prediction) util.save_image(img, "%s/%s.jpg" % (self.prediction_path, name)) return pred_shape
from tf_SDUnet import unet, util, image_util, metrics import numpy as np data_provider = image_util.ImageDataProvider("DRIVE700/test/*", data_suffix="_test.tif", mask_suffix='_manual1.png', n_class=2) net = unet.Unet(layers=4, features_root=8, channels=3, n_class=2) test_x, test_y = data_provider(1) prediction = net.predict("mode/drive100_0.92_700/model.ckpt", test_x) prediction = util.crop_to_shape(prediction, (20, 584, 565, 2)) AUC_ROC = metrics.roc_Auc(prediction, util.crop_to_shape(test_y, prediction.shape)) print("auc", AUC_ROC) acc = metrics.acc(prediction, util.crop_to_shape(test_y, prediction.shape)) print("acc:", acc) precision = metrics.precision(prediction, util.crop_to_shape(test_y, prediction.shape)) print("ppv:", precision) sen = metrics.sen(prediction, util.crop_to_shape(test_y, prediction.shape)) print("TPR:", sen) TNR = metrics.TNR(prediction, util.crop_to_shape(test_y, prediction.shape)) print("tnr:", TNR) f1 = metrics.f1score2(prediction, util.crop_to_shape(test_y, prediction.shape)) print("f1:", f1) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "19.jpg")
import os data_provider2 = image_util.ImageDataProvider("TNBC/test/*.png", data_suffix=".png", mask_suffix='_mask.png', n_class=2) data_provider = image_util.ImageDataProvider("Model_zoo/train_aug/train/*.png", data_suffix=".png", mask_suffix='_mask.png', n_class=2) output_path = "TNBC2_CKPT" net = unet.Unet(layers=4, features_root=6, channels=3, n_class=2) trainer = unet.Trainer(net, batch_size=8, verification_batch_size=4, optimizer="adam") path = trainer.train(data_provider, output_path, keep_prob=0.8, training_iters=32, epochs=100, display_step=2, restore=True) test_x, test_y = data_provider2(1) prediction = net.predict(path, test_x) error = unet.error_rate(prediction, util.crop_to_shape(test_y, prediction.shape)) print(error) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "tnbc2.jpg")
import numpy as np import os # data_provider2 = image_util.ImageDataProvider("CHASE_NEW/test/*",data_suffix="R.jpg", mask_suffix='R_1stHO.png', n_class=2) data_provider2 = image_util.ImageDataProvider("DRIVE700/test/*",data_suffix="_test.tif", mask_suffix='_manual1.png', n_class=2) #data_provider2 = image_util.ImageDataProvider("DriveTest1/*",data_suffix="_test.tif", mask_suffix='_manual1.gif', n_class=2) data_provider = image_util.ImageDataProvider("DRIVE700/train/*",data_suffix="_training.tif", mask_suffix='_manual1.png', n_class=2) output_path="mode/drive100_0.92_700" net = unet.Unet(layers=4, features_root=8, channels=3, n_class=2) trainer = unet.Trainer(net,batch_size=4,verification_batch_size=4,optimizer="adam") path = trainer.train(data_provider, output_path,keep_prob=0.92,training_iters=64, epochs=80,display_step=2,restore=False) test_x, test_y = data_provider2(20) prediction = net.predict(path, test_x) prediction=util.crop_to_shape(prediction, (20,584,565,2)) AUC_ROC = metrics.roc_Auc(prediction, util.crop_to_shape(test_y, prediction.shape)) print("auc:",AUC_ROC) acc=metrics.acc(prediction,util.crop_to_shape(test_y, prediction.shape)) print("acc:",acc) precision=metrics.precision(prediction,util.crop_to_shape(test_y, prediction.shape)) print("ppv:",precision) sen=metrics.sen(prediction,util.crop_to_shape(test_y, prediction.shape)) print("TPR:",sen) TNR=metrics.TNR(prediction,util.crop_to_shape(test_y, prediction.shape)) print("tnr:",TNR) f1=metrics.f1score2(prediction,util.crop_to_shape(test_y, prediction.shape)) print("f1:",f1) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "Drive_deep.jpg")
def train(self, data_provider, output_path, training_iters=10, epochs=100, keep_prob=0.9, block_size=7, display_step=1, restore=False, write_graph=False, prediction_path='prediction'): """ Lauches the training process :param data_provider: callable returning training and verification data :param output_path: path where to store checkpoints :param training_iters: number of training mini batch iteration :param epochs: number of epochs :param dropout: dropout probability :param display_step: number of steps till outputting stats :param restore: Flag if previous model should be restored :param write_graph: Flag if the computation graph should be written as protobuf file to the output path :param prediction_path: path where to save predictions on each epoch """ save_path = os.path.join(output_path, "model.ckpt") if epochs == 0: return save_path init = self._initialize(training_iters, output_path, restore, prediction_path) with tf.Session() as sess: if write_graph: tf.train.write_graph(sess.graph_def, output_path, "graph.pb", False) sess.run(init) if restore: ckpt = tf.train.get_checkpoint_state(output_path) if ckpt and ckpt.model_checkpoint_path: self.net.restore(sess, ckpt.model_checkpoint_path) test_x, test_y = data_provider(self.verification_batch_size) pred_shape = self.store_prediction(sess, test_x, test_y, "_init") summary_writer = tf.summary.FileWriter(output_path, graph=sess.graph) logging.info("Start optimization") avg_gradients = None for epoch in range(epochs): total_loss = 0 for step in range((epoch * training_iters), ((epoch + 1) * training_iters)): batch_x, batch_y = data_provider(self.batch_size) # Run optimization op (backprop) _, loss, lr, gradients = sess.run( (self.optimizer, self.net.cost, self.learning_rate_node, self.net.gradients_node), feed_dict={ self.net.x: batch_x, self.net.y: util.crop_to_shape(batch_y, pred_shape), self.net.keep_prob: keep_prob, self.net.block_size: block_size }) if self.net.summaries and self.norm_grads: avg_gradients = _update_avg_gradients( avg_gradients, gradients, step) norm_gradients = [ np.linalg.norm(gradient) for gradient in avg_gradients ] self.norm_gradients_node.assign(norm_gradients).eval() if step % display_step == 0: self.output_minibatch_stats( sess, summary_writer, step, batch_x, util.crop_to_shape(batch_y, pred_shape)) total_loss += loss self.output_epoch_stats(epoch, total_loss, training_iters, lr) self.store_prediction(sess, test_x, test_y, "epoch_%s" % epoch) save_path = self.net.save(sess, save_path) logging.info("Optimization Finished!") return save_path
data_suffix=".tif", mask_suffix='_binary.tif', n_class=2) #data_provider = image_util.ImageDataProvider("Tissue_images/*.tif",data_suffix=".tif", mask_suffix='_binary.tif', n_class=2) data_provider = image_util.ImageDataProvider("tissue_aug/train/*.tif", data_suffix=".tif", mask_suffix='_binary.tif', n_class=2) output_path = "out_put_skip_aug3_160" #setup & training net = unet2.Unet(layers=4, features_root=8, channels=3, n_class=2) trainer = unet2.Trainer(net, batch_size=4, verification_batch_size=4, optimizer="adam") path = trainer.train(data_provider, output_path, keep_prob=0.8, training_iters=124, epochs=1, display_step=2, restore=True) test_x, test_y = data_provider2(14) prediction = net.predict(path, test_x) print(test_y) error = unet2.error_rate(prediction, util.crop_to_shape(test_y, prediction.shape)) print("test error:", error) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "test_aug_5.jpg")
shuffle_data=False) net = unet.Unet(layers=4, features_root=8, channels=3, n_class=2) test_x, test_y = DT(6) model = "model/model_f8_0.88_flip_160/model.ckpt" prediction = net.predict(model, test_x) # # pred=tf.cast(prediction, tf.float64) # loss=stats_utils.get_dice(util.crop_to_shape(test_y, prediction.shape),prediction) # init = tf.global_variables_initializer() # with tf.Session() as sess: # # Initialize variables # sess.run(init) # print("loss",sess.run(loss)) print( "dice2:", stats_utils.get_dice_2(util.crop_to_shape(test_y, prediction.shape), prediction)) print( "dice1", stats_utils.get_dice_1(util.crop_to_shape(test_y, prediction.shape), prediction)) print("f1:", unet.f1score2(prediction, util.crop_to_shape(test_y, prediction.shape))) print( "aji:", stats_utils.get_aji(util.crop_to_shape(test_y, prediction.shape), prediction)) print( "aji+:", stats_utils.get_fast_aji(util.crop_to_shape(test_y, prediction.shape), prediction))
from tf_SDUnet import unet, util, image_util,stats_utils,layers import tensorflow as tf import numpy as np Single = image_util.ImageDataProvider("test2/single/*.tif",data_suffix=".tif", mask_suffix='_binary.tif', n_class=2,shuffle_data=False) net = unet.Unet(layers=4, features_root=8, channels=3, n_class=2) test_x, test_y =Single(1) model="model/model_f8_0.88_flip_160/model.ckpt" prediction = net.predict(model, test_x) print("dice2:",stats_utils.get_dice_2(util.crop_to_shape(test_y, prediction.shape),prediction)) print("dice1",stats_utils.get_dice_1(util.crop_to_shape(test_y, prediction.shape),prediction)) print("f1:",unet.f1score2(prediction, util.crop_to_shape(test_y, prediction.shape))) print("aji:",stats_utils.get_aji(util.crop_to_shape(test_y, prediction.shape),prediction)) print("aji+:",stats_utils.get_fast_aji(util.crop_to_shape(test_y, prediction.shape),prediction)) error=unet.error_rate(prediction, util.crop_to_shape(test_y, prediction.shape)) print("error:",error) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "result/5698.jpg")
cost="dice_coefficient") trainer = unet.Trainer(net, batch_size=4, verification_batch_size=4, optimizer="adam") path = trainer.train(data_provider, output_path, keep_prob=0.88, block_size=7, training_iters=64, epochs=100, display_step=2, restore=False) test_x, test_y = DT(6) prediction = net.predict(path, test_x) error = unet.error_rate(prediction, util.crop_to_shape(test_y, prediction.shape)) print("DT error:", error) f1 = unet.f1score2(prediction, util.crop_to_shape(test_y, prediction.shape)) print("DTf1:", f1) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "DTtest.jpg") test_x, test_y = ST(8) prediction = net.predict(path, test_x) error = unet.error_rate(prediction, util.crop_to_shape(test_y, prediction.shape)) print("ST error:", error) f1 = unet.f1score2(prediction, util.crop_to_shape(test_y, prediction.shape)) print("STf1:", f1) img = util.combine_img_prediction(test_x, test_y, prediction) util.save_image(img, "STtest.jpg")