Exemple #1
0
def train(dataset_csv, working_dir, appendum='', force=False):

    # create folder name
    foldername = os.path.join(
        working_dir, s.stages[4], '__'.join([
            os.path.splitext(os.path.basename(dataset_csv))[0],
            'ly' + str(s.network['layers']) + 'ftr' + str(s.network['features_root']) + appendum, ''
            ]))

    # only retrain if necessary
    if not os.path.exists(foldername) or force:
        generator = image_util.ImageDataProvider(
            dataset_path=dataset_csv,
            roles=['train'],
            shuffle_data=True,
            a_min=None,
            a_max=None,
            n_class=s.network['classes'],
            n_channels=s.network['channels'])  # add all options and put shuffle data = True

        net = unet.Unet(
            channels=s.network['channels'],
            n_class=s.network['classes'],
            layers=s.network['layers'],
            features_root=s.network['features_root'],
            cost_kwargs=dict(class_weights=s.network['class_weights'])
        )
        trainer = unet.Trainer(net, optimizer=s.train['optimizer'], batch_size=s.train['batch_size'])

        trainer.train(generator, foldername, training_iters=s.train['training_iters'], epochs=s.train['epochs'],
                      display_step=s.train['display_step'], dropout=s.train['dropout'],
                      restore=False, write_graph=True)
    else:
        print(os.path.basename(foldername), ' already exists. Skipping.')
Exemple #2
0
def train(args):
    # preparing data loading
    data_provider = image_util.ImageDataProvider(args.data_dir,
                                                 n_class=args.classes,
                                                 class_colors=[0, 255, 127])

    # setup & training
    net = unet.Unet(layers=args.layers,
                    features_root=args.features_root,
                    channels=args.channels,
                    n_class=args.classes)
    trainer = unet.Trainer(net)
    total_parameters = 0
    for variable in tf.trainable_variables():
        # shape is an array of tf.Dimension
        shape = variable.get_shape()
        variable_parameters = 1
        for dim in shape:
            variable_parameters *= dim.value
        total_parameters += variable_parameters
    print("Total number of parameters:{0}".format(total_parameters))
    trainer.train(data_provider,
                  args.output_path,
                  training_iters=args.training_iters,
                  epochs=args.num_epochs,
                  write_graph=args.write_graph,
                  restore=args.restore)
Exemple #3
0
def main():

    # input training and test datasets
    train_data = image_util.ImageDataProvider(
        search_path='RoadDetection_Train_Images', n_class=2)

    # instantiate U-net (best results: layers=5, feature_roots=64, batch_size=2, epochs=50, training_iters=64)
    net = unet.Unet(layers=4,
                    n_class=train_data.n_class,
                    channels=train_data.channels,
                    features_root=48,
                    cost='dice_coefficient',
                    cost_kwargs={'regularizer': 0.01})

    trainer = unet.Trainer(net,
                           batch_size=2,
                           verification_batch_size=4,
                           optimizer="momentum",
                           opt_kwargs=dict(momentum=0.5))

    # path = trainer.train(data_provider=train_data, output_path="./unet_trained", training_iters=32,  epochs=1, display_step=2)
    trainer.train(data_provider=train_data,
                  output_path="./unet_trained",
                  training_iters=64,
                  epochs=50,
                  dropout=0.75,
                  display_step=2)

    print('Process completed.')
def Training(net):

    TrainData = image_util.ImageDataProvider(dir + "*.tif", shuffle_data=True)
    L = int(len(TrainData.data_files) / 10)
    trainer = unet.Trainer(net, optimizer="adam")
    path = trainer.train(TrainData,
                         dir + 'model',
                         training_iters=L,
                         epochs=epoch_Num,
                         display_step=100,
                         GPU_Num=gpuNum)  # , GPU_Num=gpuNum
    return path
Exemple #5
0
    def predictAll(self, model_path, dataset_path, output_dir, roles=['test'], supervisely=False):
        """
        Uses the model to create a prediction for all image files in dataset
        :param roles: which images of the dataset should be used
        :param dataset_path: dataset containing image paths
        :param output_directory: where predictions should be saved
        :param model_path: path to the model checkpoint to restore
        """
        init = tf.global_variables_initializer()
        shuffle = True if supervisely else False  # added from Simon for supervisely data
        n_class = 9
        n_channel = 3
        dataProvider = image_util.ImageDataProvider(dataset_path=dataset_path, roles=roles, n_class=n_class,
                                                    n_channels=n_channel, shuffle_data=shuffle)
        imagecount = dataProvider.data_length

        with tf.Session() as sess:
            # Initialize variables
            sess.run(init)

            # Restore model weights from previously saved model
            self.restore(sess, model_path)

            # loop through files
            for i in range(imagecount):
                x, [y], [name] = dataProvider(1)

                # info = os.path.basename(name).split('_')
                # data['time'].append(datetime.strptime(info[0], '%y%m%d %H%M%S'))
                # data['sensor'].append(float(info[1].split('.')[0]) / 10)
                y_dummy = np.empty((x.shape[0], x.shape[1], x.shape[2], self.n_class))
                prediction = sess.run(self.predicter, feed_dict={self.x: x, self.y: y_dummy, self.keep_prob: 1.})[0]

                class_mapping = {1: ['bg', [0, 0, 0]], 2: ['building', []], 3: ['bg', []],
                                 4: ['vegetation', dict(nr=1, name='bg', rgb=[0, 0, 0)]),
                                 dict(nr=1, name='building', rgb=[]),
                                dict(nr=2, name='person', rgb=[]),
                                dict(nr=3, name='vehicle', rgb=[]),
                                dict(nr=4, name='')
                ]


                img = Image.fromarray((np.argmax(prediction, axis=2)*128/n_class).astype('uint8')).convert('RGB')
                img.save(os.path.join(output_dir, os.path.splitext(name)[0]+'.png'))

                # for thr in thrs:
                #     data['watsen'][str(thr)].append(
                #         np.sum(prediction > 0.9, axis=(1, 2))/(prediction.shape[1]*prediction.shape[0])
                #     )
        return
Exemple #6
0
def ThalamusExtraction(net , Test_Path , Train_Path , subFolders, CropDim , padSize):


    Trained_Model_Path = Train_Path + 'model/model.cpkt'


    trainer = unet.Trainer(net)

    TestData = image_util.ImageDataProvider(  Test_Path + '*.tif',shuffle_data=False)

    L = len(TestData.data_files)
    DiceCoefficient  = np.zeros(L)
    LogLoss  = np.zeros(L)
    BB_Cord = np.zeros(L)

    for BB_ind in range(L):
        Stng = TestData.data_files[BB_ind]
        d = Stng.find('slice')
        BB_Cord[BB_ind] = int(Stng[d+5:].split('.')[0])

    BB_CordArg = np.argsort(BB_Cord)
    Data , Label = TestData(len(BB_Cord))


    szD = Data.shape
    szL = Label.shape

    data  = np.zeros((1,szD[1],szD[2],szD[3]))
    label = np.zeros((1,szL[1],szL[2],szL[3]))

    shiftFlag = 0
    PredictionFull = np.zeros((szD[0],148,148,2))
    for BB_ind in BB_CordArg:

        data[0,:,:,:]  = Data[BB_ind,:,:,:].copy()
        label[0,:,:,:] = Label[BB_ind,:,:,:].copy()

        if shiftFlag == 1:
            shiftX = 0
            shiftY = 0
            data = np.roll(data,[0,shiftX,shiftY,0])
            label = np.roll(label,[0,shiftX,shiftY,0])

        prediction = net.predict( Trained_Model_Path, data)
        PredictionFull[BB_ind,:,:,:] = prediction

    return PredictionFull
from tf_unet import unet, util, image_util
import numpy
import PIL

data_provider = image_util.ImageDataProvider(
    "/Users/anujatike/Documents/sem4/CS256/project/trainData/*",
    data_suffix=".png",
    mask_suffix="_mask.png")

net = unet.Unet(channels=1, n_class=2, layers=3, features_root=64)
trainer = unet.Trainer(net)
output_path = "output"
path = trainer.train(data_provider, output_path, training_iters=10,
                     epochs=10)  #100)

print(path)
from tf_unet import unet, util, image_util
import os
import random
import numpy as np

#preparing data loading
data_provider = image_util.ImageDataProvider("C:\\Users\\Magnus\\Documents\\GitHub\\DD2424\\tf_unet\\train\\*", data_suffix = "t1ce.nii", mask_suffix = "seg.nii")

#setup & training
output_path = "C:\\Users\\Magnus\\Documents\\GitHub\\DD2424\\tf_unet\\unet_trained\\"
net = unet.Unet(layers=3, features_root=64, channels=1, n_class=2, cost = "dice_coefficient")
trainer = unet.Trainer(net)
path = trainer.train(data_provider, output_path, training_iters=5, epochs=2)

#verification
...

##counter = 0
##filelist = os.listdir("C:\\Users\\Magnus\\Documents\\GitHub\\DD2424\\tf_unet\\Folder4\\")
##random.shuffle(filelist)
##testdata = np.empty((1, 240, 240, 1))
##testlabels = np.empty((1, 240, 240, 1))
##
##for f in filelist:
##    if counter == 1:
##        break
##    if f[-11:-7] == "t1ce":
##        testdata[counter] = np.load("C:\\Users\\Magnus\\Documents\\GitHub\\DD2424\\tf_unet\\Folder4\\" + f)
##        try:
##            s = "C:\\Users\\Magnus\\Documents\\GitHub\\DD2424\\tf_unet\\Folder4\\" + f
##            s.replace("t1ce", "seg")
Exemple #9
0
from tf_unet import unet, util, image_util
import os
import random
import numpy as np

train_iters = 20
num_epochs = 150
feat_roots = 16
num_layers = 1
learning_rate = 1e-3
batch_size = 1
optimizer = "adam"

reg_lambda = 1e-6
n_classes = 2

#preparing data loading
data_provider = image_util.ImageDataProvider("/home/Kenneth/DL-Lung-Segmentation/img/train/*", data_suffix = "t1ce.jpg", mask_suffix = "seg.jpg")

output_path = "./output"
net = unet.Unet(layers=num_layers, features_root=feat_roots, channels=1, n_class=n_classes,  cost = "dice_coefficient", cost_kwargs=dict(regularizer=reg_lambda))
trainer = unet.Trainer(net, optimizer=optimizer, batch_size = batch_size)
trainer.train(data_provider, output_path, training_iters=train_iters, epochs=num_epochs)


Exemple #10
0
from tf_unet import image_util, unet

output_path = "/data/Cell/unet/model3/"
data_provider = image_util.ImageDataProvider("/data/Cell/unet/*.jpg")

net = unet.Unet(layers=3, features_root=64, channels=3, n_class=2)
trainer = unet.Trainer(net, optimizer="adam")
path = trainer.train(data_provider, output_path, training_iters=32, epochs=100)
Exemple #11
0
    V = tf.transpose(V, (2, 0, 1))
    V = tf.reshape(V, tf.stack((-1, img_w, img_h, 1)))
    return V


# In[8]:

net = Unet(channels=1, n_class=2, layers=3, features_root=16)

# In[9]:

trainer = Trainer(net, optimizer="momentum", opt_kwargs=dict(momentum=0.2))

# In[8]:

data_provider = image_util.ImageDataProvider(
    "C:/Users/orange/Desktop/data/*.tif")

# In[9]:

path = trainer.train(data_provider,
                     "C:/Users/orange/Desktop/out",
                     training_iters=4,
                     epochs=4,
                     display_step=2)

# In[10]:

x_test, y_test = data_provider(13)

# In[11]:
Exemple #12
0
# -*- coding: utf-8 -*-

import numpy as np

from tf_unet import image_util
from tf_unet import unet
from tf_unet import util
import glob

search_path = "D:\\pythonworkspace\\tf_unet\\tf_unet\\demo\\IRholder\\ImageResize\\*.png"

data_provider = image_util.ImageDataProvider(search_path,
                                             data_suffix=".png",
                                             mask_suffix='_label.png')

net = unet.Unet(channels=data_provider.channels,
                n_class=data_provider.n_class,
                layers=4,
                features_root=32)

#trainer = unet.Trainer(net, optimizer="momentum", opt_kwargs=dict(momentum=0.2))
trainer = unet.Trainer(net,
                       batch_size=8,
                       optimizer="adam",
                       opt_kwargs=dict(learning_rate=0.00001))
path = trainer.train(data_provider,
                     "./unet_trained",
                     training_iters=32,
                     epochs=100,
                     dropout=0.5,
                     display_step=2,
Exemple #13
0
                 resnet_kwargs=resnet_kwargs)
 # # data_provider = image_util.ImageDataProvider("Potsdam/RGB/*.tif", "Potsdam/Labels", patch_size=1000, border_size=20,
 # #                                              data_suffix="_RGB.tif", mask_suffix='_label.tif',
 # #                                              channels=3, n_class=6, load_saved=False)
 # # data_provider = image_util.ImageDataProvider('Potsdam/train_RGB/', patch_size=1000, border_size=20,
 # #                                              data_suffix="_RGB.tif", mask_suffix='_label.tif',
 # #                                              channels=3, n_class=6, load_saved=True)
 # # data_provider.save_patches('Potsdam/train_RGB/')
 # print(net.offset)
 # data_provider = \
 #     image_util.ImageDataProvider("Potsdam/resized2/train/*.tif", "Potsdam/bin_labels_resized/", patch_size=1000,
 #                                              channels=3, n_class=2, border_size=net.offset//2+6, data_suffix="_RGB.tif",
 #                                              mask_suffix='_label_mask_mask.tif')
 eval_data_provider = \
     image_util.ImageDataProvider("Potsdam/resized2/eval/*.tif", "Potsdam/bin_labels_resized/", patch_size=1000,
                                              channels=3, n_class=2, border_size=net.offset//2+6, data_suffix="_RGB.tif",
                                              mask_suffix='_label_mask_mask.tif', shuffle_data=False)
 #
 # d_learning_opts={}
 # g_learning_opts = {}
 # trainer = unet.Trainer(net, batch_size=1, optimizer='adam', d_opt_kwargs=d_learning_opts, g_opt_kwargs=g_learning_opts)
 # # # #                      opt_kwargs={'momentum': 0.9, "learning_rate": 0.2, "decay_rate": 0.9})
 # path = trainer.train(data_provider, eval_data_provider, "summaries/", cut_off = 1.0, check_discriminator=10, dropout=1.0,
 #                         training_iters=18, eval_iters=6, epochs=1, display_step=1, predict_step=50,  restore=True)
 # x_test = a._load_file("images/00000000.png")
 # x_test = a._process_data([x_test])
 net.predict('summaries/model/model.cpkt-18',
             eval_data_provider,
             test_iters=6,
             border_size=net.offset // 2 + 6,
             patch_size=1000,
Exemple #14
0
    def get_mask(self, image):
        """
			image : a variable resolution RGB image in the form of a numpy array

			return: A list of lists with the same 2d size as of the input image with either 0 or 1 as each entry

			"""
        sha = image.shape
        # img = color.rgb2gray(image)
        # img = np.asarray(img)
        # row_pad = 572 - (sha[0] % 572)
        # col_pad = 572 - (sha[1] % 572)
        # img1 = np.pad(img, ((0, row_pad), (0, col_pad)), 'constant', constant_values=(0))
        # sha1 = img1.shape
        # final_image = np.zeros((572,572), dtype=int)
        # rows = sha1[0] / 572
        # cols = sha1[1] / 572
        # fin_image = np.zeros((1, sha1[1]))
        # data_provider = image_util.ImageDataProvider(search_path = "test_images/*", data_suffix="train.jpg", mask_suffix="mask.jpg")
        # for i in range(0, int(rows)):
        # 	final_image = np.zeros((572, 572), dtype=int)
        # 	for j in range(0, int(cols)):
        # 		img2 = img1[i*572: (i+1)*572, j*572: (j+1)*572]
        # 		randomPath = "./test_images/" + "1_train.jpg"
        # 		randomPath2 = "./test_images/" + "1_mask.jpg"
        # 		io.imsave(randomPath, img2)
        # 		io.imsave(randomPath2, img2)
        # 		self.count = self.count + 1
        # 		x_test, y_test = data_provider(1)
        # 		prediction = self.net.predict("./temp/model.cpkt", x_test)
        # 		mask = prediction[0,...,1] > 0.5
        # 		mask1 = resize(mask, (572, 572))
        # 		temp = np.asarray(mask1)
        # 		final_image = np.append(final_image, temp, axis=1)
        # 		#print(final_image.shape)
        # 		mask1 = scipy.ndimage.binary_dilation(mask1).astype(dtype=int)
        # 	fin_image = np.append(fin_image, final_image[:, 572:], axis=0)

        # #print(fin_image.shape)
        # mask1 = fin_image[1:sha[0]+1,:sha[1]]
        # mask1 = scipy.ndimage.binary_dilation(mask1).astype(dtype=int)
        # mask1 = scipy.ndimage.binary_dilation(mask1).astype(dtype=int)
        # return mask1
        img = resize(color.rgb2gray(image), (572, 572))
        randomPath = "./test_images/" + "1_train.jpg"
        randomPath2 = "./test_images/" + "1_mask.jpg"
        io.imsave(randomPath, img)
        io.imsave(randomPath2, img)
        self.count = self.count + 1
        data_provider = image_util.ImageDataProvider(
            search_path="test_images/*",
            data_suffix="train.jpg",
            mask_suffix="mask.jpg")
        x_test, y_test = data_provider(1)
        #os.remove(randomPath)
        prediction = self.net.predict("./temp/model.cpkt", x_test)
        print(prediction.shape)
        #prediction = np.resize(prediction, (sha[0], sha[1]))
        #prediction = scipy.ndimage.binary_dilation(prediction).astype(dtype=int)
        mask = prediction[0, ..., 1] > 0.5
        mask1 = resize(mask, (sha[0], sha[1]))
        #for i in range(5):
        mask1 = scipy.ndimage.binary_dilation(mask1).astype(dtype=int)
        #mask1 = scipy.ndimage.binary_dilation(mask1).astype(dtype=int)
        #mask1 = scipy.ndimage.binary_dilation(mask1).astype(dtype=int)
        #mask1 = np.ndarray.astype(mask1, dtype="int")
        #prediction = 1 * (prediction>0.2)
        #prediction = np.resize(prediction, (sha[0], sha[1]))
        return mask1
Exemple #15
0
def TestData(net , Test_Path , Train_Path , padSize):

    TestImageNum = 7

    Trained_Model_Path = Train_Path + 'model/model.cpkt'
    TestResults_Path   = Test_Path  + 'results/'

    try:
        os.stat(TestResults_Path)
    except:
        os.makedirs(TestResults_Path)

    AllImage_logical = np.zeros((1924,1924))
    AllImage = np.zeros((1924,1924))

    trainer = unet.Trainer(net)

    TestData = image_util.ImageDataProvider(  Test_Path + '*.tif' , shuffle_data=False)

    L = len(TestData.data_files)
    DiceCoefficient  = np.zeros(L)
    LogLoss  = np.zeros(L)
    # BB_Cord = np.zeros(L,3)
    BB_Cord = np.zeros((L,2))


    aa = TestData.data_files
    for BB_ind in range(L):
    # BB_ind = 1
        bb = aa[BB_ind]
        d = bb.find('/img')
        cc = bb[d:len(bb)-4]
        dd = cc.split('_')
        # imageName = int(dd[0])
        xdim = int(dd[1])
        ydim = int(dd[2])
        # BB_Cord[ BB_ind , : ] = [xdim,ydim,imageName]
        BB_Cord[ BB_ind , : ] = [xdim,ydim]

    Data , Label = TestData(L)


    szD = Data.shape
    szL = Label.shape

    data  = np.zeros((1,szD[1],szD[2],szD[3]))
    label = np.zeros((1,szL[1],szL[2],szL[3]))

    shiftFlag = 0
    for BB_ind in range(L):

        data[0,:,:,:]  = Data[BB_ind,:,:,:].copy()
        label[0,:,:,:] = Label[BB_ind,:,:,:].copy()

        if shiftFlag == 1:
            shiftX = 0
            shiftY = 0
            data = np.roll(data,[0,shiftX,shiftY,0])
            label = np.roll(label,[0,shiftX,shiftY,0])

        prediction = net.predict( Trained_Model_Path, data)
        PredictedSeg = prediction[0,...,1] > 0.2

        # ix, iy, ImgNum = BB_Cord[ BB_ind , : ]
        ix, iy = BB_Cord[ BB_ind , : ]
        ix = int(148*ix)
        iy = int(148*iy)
        # AllImage[148*ix:148*(ix+1) , 148*iy:148*(iy+1) ,ImgNum] = prediction[0,...,1]
        # AllImage_logical[148*ix:148*(ix+1) , 148*iy:148*(iy+1) ,ImgNum] = PredictedSeg

        AllImage[ix:148+ix , iy:148+iy] = prediction[0,...,1]
        AllImage_logical[ix:148+ix , iy:148+iy] = PredictedSeg

        # unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape))

        sz = label.shape

        A = (padSize/2)
        imgCombined = util.combine_img_prediction(data, label, prediction)
        DiceCoefficient[BB_ind] = DiceCoefficientCalculator(PredictedSeg,label[0,A:sz[1]-A,A:sz[2]-A,1])  # 20 is for zero padding done for input
        util.save_image(imgCombined, TestResults_Path+"prediction_slice"+ str(BB_Cord[BB_ind]) + ".jpg")


        Loss = unet.error_rate(prediction,label[:,A:sz[1]-A,A:sz[2]-A,:])
        LogLoss[BB_ind] = np.log10(Loss+eps)

    np.savetxt(TestResults_Path+'DiceCoefficient.txt',DiceCoefficient)
    np.savetxt(TestResults_Path+'LogLoss.txt',LogLoss)


    im = Image.fromarray(np.uint8(AllImage))
    msk = Image.fromarray(np.uint8(AllImage_logical))

    im.save( TestResults_Path + 'PredictionSeg_'+str(TestImageNum)+'.tif')
    msk.save(TestResults_Path + 'PredictionSeg_'+str(TestImageNum)+'_Logical.tif')


    return AllImage , AllImage_logical

if __name__ == "__main__":

    Parameters = devTest()

# !!! Remove current path in Parameters[output] directory to create a new one !!!
if (os.path.isdir(Parameters['output'] + '/model')):
    shutil.rmtree(Parameters['output'])
os.mkdir(Parameters['output'])
os.mkdir(Parameters['output'] + '/model')
os.mkdir(Parameters['output'] + '/trainPrediction')

#Load images to train UNet
data_provider = image_util.ImageDataProvider(Parameters['datasrc'] + '/*',
                                             data_suffix=".png",
                                             mask_suffix="_mask.png")

# Compute the theoric total number of convolution filters :
# ConvFilter in descending path + ConvFilter in expanding path + output convolution (1x1) + up-convolution (2x2
totConvFilter = (Parameters['layers'] * 2) + (
    Parameters['layers'] - 1) * 2 + 1 + (Parameters['layers'] - 1)
print("Total number of convolution filters (attempted) : " +
      str(totConvFilter))

#Compute the number of iterations necessary to see all train dataset in one batch
if int(Parameters['iterations']) == 0:
    Parameters['iterations'] = round(
        len(data_provider.data_files) / int(Parameters['batchSize']))

net = unet.Unet(layers=int(Parameters['layers']),
from tf_unet import unet, util, image_util
#preparing data loading
data_provider = image_util.ImageDataProvider("../training_pic/*.tif")

#setup & training
net = unet.Unet(layers=3, features_root=64, channels=3, n_class=2)
trainer = unet.Trainer(net)
path = trainer.train(data_provider, "./unet", training_iters=32, epochs=100)
Exemple #18
0
from tf_unet import unet, util, image_util

data_provider = image_util.ImageDataProvider(
    "E:/GitHub/tf_unet/img/train/*.tif")
output_path = "model"
net = unet.Unet(features_root=64, channels=1, n_class=9)
trainer = unet.Trainer(net, 16)
path = trainer.train(data_provider, output_path, training_iters=32, epochs=100)

prediction = net.predict(path, data)
unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape))
img = util.combine_img_prediction(data, label, prediction)
util.save_image(img, "prediction.jpg")
imread()
Exemple #19
0
def predict(model_name, test_data_path, train_path, data_suffix):
	print('Starting prediction for model evaluation...')
	data_provider = image_util.ImageDataProvider(search_path=test_data_path + "*" + data_suffix,
	                                             data_suffix=data_suffix,
	                                             mask_suffix="_mask" + data_suffix, shuffle_data='False', n_class=2)

	# print('Creating Unet')
	net = unet.Unet(channels=data_provider.channels, n_class=data_provider.n_class, layers=3, features_root=16)

	### Prediction ###
	# generator = image_util.ImageDataProvider(search_path=test_data_path+"*.jpg", data_suffix=".jpg",
	#                                          mask_suffix="_mask.jpg", shuffle_data='False')

	modelPerf_df = pd.Series([])
	pred_time_df = pd.Series([])

	# Compute diceCoeff for each image
	for i, j in zip(range(0, len(data_provider.data_files)), tqdm(range(len(data_provider.data_files)))):
		# Get test images and start prediction timer
		start_pred = float(time.time())
		x_test, y_test = data_provider(1)
		y_test = np.array(y_test, dtype='int')
		# Predict on images and end prediction timer
		prediction = net.predict(os.path.join(train_path, "model.ckpt"), x_test)
		end_pred = float(time.time())
		# Change type and shape of prediction to compare with true ground
		# (predicted mask is smaller due to convolutions without padding)
		# Input images must be with dimensions
		pred = np.array(prediction, dtype="float64")
		# print(pred[0, ..., 0].shape[0], pred[0, ..., 0].shape[1])

		a = y_test.shape[1] - pred.shape[1]  # x size difference
		b = y_test.shape[2] - pred.shape[2]  # y size difference

		resized_y_test = y_test[0, int(a / 2):-int(a / 2), int(b / 2):-int(b / 2), :]
		resized_x_test = x_test[0, int(a / 2):-int(a / 2), int(b / 2):-int(b / 2), :]

		##### post processing remove noise #####
		# my_dpi = 96
		# fig = plt.figure(frameon=False)
		# fig.set_size_inches(pred[0, ..., 0].shape[1] / my_dpi, pred[0, ..., 0].shape[0] / my_dpi)
		#
		# img = plt.imshow(pred[0, ..., 0])
		# img.set_cmap('Greys')
		# plt.axis('off')
		# plt.margins(0, 0)
		# plt.subplots_adjust(top=1, bottom=0, right=1, left=0,
		#                     hspace=0, wspace=0)
		# fig.savefig(train_path + "/Predictions/figmask.png", dpi=my_dpi)
		# plt.close('all')
		#
		# figmask = cv2.imread(train_path + "/Predictions/figmask.png")
		#
		# # gray = cv2.cvtColor(figmask, cv2.COLOR_BGR2GRAY)  # convert to grayscale
		# blur = cv2.blur(figmask, (20, 20))  # blur the image
		# ret, thresh = cv2.threshold(blur, 170, 255, cv2.THRESH_BINARY)
		#
		# fig, ax = plt.subplots(1, 3, figsize=(12, 5))
		# ax[0].imshow(figmask, cmap='Greys', aspect="auto")
		# ax[1].imshow(blur, cmap='Greys', aspect="auto")
		# # mask = prediction[0, ..., 0] > 0.6
		# ax[2].imshow(thresh, cmap='Greys', aspect="auto")
		# plt.show()
		# print(pred[0])


		# test_pred = np.zeros(pred.shape)
		# test_pred[:, :, 450:, 1] = 1
		# test_pred[:, :, 450:, 0] = 0

		modelPerf = diceCoeff(resized_y_test, pred[0])  # pred[0]

		modelPerf_df = modelPerf_df.append(pd.Series([modelPerf]), ignore_index=True)
		pred_time_df = pred_time_df.append(pd.Series([end_pred - start_pred]), ignore_index=True)
		# print(data_provider.data_files[0][len(test_data_path):])
		# print(modelPerf_df.idxmax())
		# print(data_provider.data_files[modelPerf_df.idxmax()][len(test_data_path):])

		fig, ax = plt.subplots(1, 3, figsize=(12, 5))
		ax[0].imshow(resized_x_test[...], aspect="auto")
		ax[1].imshow(1 - resized_y_test[..., 1], cmap='Greys', aspect="auto")
		# ax[0].imshow(resized_x_test[...], aspect="auto")
		# ax[1].imshow(resized_y_test[..., -1], aspect="auto")
		mask = pred[0, ..., 1] > 0.6
		ax[2].imshow(1-pred[0, ..., 1], cmap='Greys', aspect="auto", vmin=0.0, vmax=1.0)
		ax[0].set_title("Input", fontsize=20)
		ax[1].set_title("Ground truth", fontsize=20)
		ax[2].set_title("Prediction", fontsize=20)
		ax[0].set_axis_off()
		ax[1].set_axis_off()
		ax[2].set_axis_off()
		fig.tight_layout()
		fig.savefig(train_path + "/Predictions/" + data_provider.data_files[i][len(test_data_path):], bbox_inches=0)
		plt.close()
	# plt.show()

	print('\nPrediction done')

	# Write stats in txt file
	f_stats = open(train_path + "/Predictions/" + model_name + "_stats.txt", "w+")
	f_stats.write(model_name + " stats\n\n")
	f_stats.write("Average Prediction Time (s): %1.3f  +- %2.3f" % (pred_time_df.mean(), pred_time_df.std()))
	f_stats.write("\nMax Dice Score: %1.3f  " % (modelPerf_df.max()))
	f_stats.write(data_provider.data_files[modelPerf_df.idxmax()][len(test_data_path):])
	f_stats.write("\nMin Dice Score: %1.3f  " % (modelPerf_df.min()))
	f_stats.write(data_provider.data_files[modelPerf_df.idxmin()][len(test_data_path):])
	f_stats.write("\nAverage Dice Score: %1.3f  +- %2.3f" % (modelPerf_df.mean(), modelPerf_df.std()))
	f_stats.write("\n\nDetails:\n")
	f_stats.write("Name                  pred_time      dicePerf\n")
	for i in range(len(data_provider.data_files)):
		f_stats.write(data_provider.data_files[i][len(test_data_path):])
		f_stats.write("         %1.3f s        %2.3f\n" % (pred_time_df[i], modelPerf_df[i]))

	f_stats.close()

	modelPerf_df.to_csv('Training_Results/' + model_name + '_stats_df', index=False, header=False)
Exemple #20
0
    else: metrics["Precision"] = 1
    return metrics


#prepare test images
with open("./Data/trainIDX5observer.csv") as f:
    tests_idx = [r for r in csv.reader(f)]
    tests_idx = [int(i) for i in tests_idx[0]]
for imgIDX in tests_idx:
    for f in glob.glob(os.path.join(PRCPATH,
                                    "img_" + str(imgIDX) + "*.nii.gz")):
        shutil.copy(f, TSTPATH)

#network
data_provider = image_util.ImageDataProvider(TSTPATH + "/*",
                                             data_suffix=".nii.gz",
                                             mask_suffix="_label.nii.gz",
                                             n_class=NOCLASS)
net = unet.Unet(layers=UNDEPTH,
                features_root=NOFEATS,
                channels=CHANNEL,
                n_class=NOCLASS,
                cost="dice_coefficient")

#prediction
results = []
noFiles = len(data_provider.data_files)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    net.restore(sess, MDLPATH)
    for img in range(noFiles):
Exemple #21
0
from tf_unet import unet, util, image_util
data_provider = image_util.ImageDataProvider("./train/*",
                                             data_suffix=".jpg",
                                             mask_suffix="_Segmentation.png")

output_path = "./model_val/"
#setup & training
net = unet.Unet(layers=3, features_root=16, channels=3, n_class=2)
trainer = unet.Trainer(net)
path = trainer.train(data_provider,
                     output_path,
                     training_iters=32,
                     epochs=100,
                     restore=True)
Exemple #22
0
#! /usr/bin/env python

import sys
import logging
logging.basicConfig(level=logging.INFO, format='%(message)s')
b_size = int(sys.argv[1])
iters = int(sys.argv[2])
learning_rate = int(sys.argv[3])
epoch_var = int(sys.argv[4])
reg_var = int(sys.argv[5])
reg_lambda = 10**-reg_var

from tf_unet import unet, util, image_util

data_provider = image_util.ImageDataProvider("../Images/*.tif")
result_path = "itr" + str(iters) + "_epo" + str(epoch_var) + "_bat" + str(
    b_size) + "_lrn" + str(learning_rate) + "_reg10e-" + str(reg_var)
output_path = "results_last/" + result_path

net = unet.Unet(layers=3,
                features_root=64,
                channels=1,
                n_class=2,
                cost_kwargs=dict(regularizer=reg_lambda))
trainer = unet.Trainer(net, batch_size=b_size, optimizer="adam")
path = trainer.train(data_provider,
                     output_path,
                     training_iters=iters,
                     epochs=epoch_var,
                     write_graph=True)
logging.info("worked")
Exemple #23
0
from tf_unet import unet, util, image_util
import cv2
from matplotlib import pyplot as plt
data_provider = image_util.ImageDataProvider("Data/*.jpg",
                                             data_suffix=".jpg",
                                             mask_suffix='_mask.jpg',
                                             shuffle_data=True,
                                             n_class=3)
net = unet.Unet(layers=3, features_root=64, channels=3, n_class=3)

data, label = data_provider(1)
print(data.shape)
'''
cv2.imshow('label',label[0,...,1])
cv2.waitKey(0)
cv2.destroyAllWindows()
'''

prediction = net.predict('3.30/model.cpkt', data)
print(prediction.shape)
cv2.imshow('label', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
pred1 = prediction[0, :, :, :]
#pred2 = prediction[1,:,:,:]
#pred3 = prediction[2,:,:,:]

print(unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape)))

#img = util.combine_img_prediction(data, label, prediction)
#util.save_image(img, "prediction.jpg")
Exemple #24
0
from tf_unet import unet, util, image_util
import cv2
import os
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

# data
data_provider = image_util.ImageDataProvider('data/train/*.tif')

# setup and training
net = unet.Unet(layers=3, features_root=64, channels=1, n_class=2)
# trainer = unet.Trainer(net)
# path = trainer.train(data_provider, output_path='train_output', training_iters=32, epochs=100)
# exit()
# verification
val_data_provider = image_util.ImageDataProvider('data/val/*.tif')
val_images, val_masks = val_data_provider(5)
prediction = net.predict('train_output/model.ckpt', val_images)

mask_sample = val_masks[0, :, :, 0]
print(mask_sample[int(len(mask_sample) / 2)])
mask_sample[mask_sample < 0.5] = 255
mask_sample[mask_sample < 1] = 0
print(mask_sample[int(len(mask_sample) / 2)])

prediction_sample = prediction[0, :, :, 0]
print(prediction_sample[int(len(prediction_sample) / 2)])
prediction_sample[prediction_sample < 0.5] = 255
prediction_sample[prediction_sample < 1] = 0
print(prediction_sample[int(len(prediction_sample) / 2)])
Exemple #25
0
def main():

    # input training and test datasets
    train_data = image_util.ImageDataProvider(
        search_path='RoadDetection_Train_Images')
    test_data = image_util.ImageDataProvider(
        search_path='RoadDetection_Test_Images')
    #
    # # train u-net
    # net = unet.Unet(layers=5, n_class=train_data.n_class, channels=train_data.channels, features_root=64, cost='dice_coefficient', cost_kwargs=dict(regularizer=0.01))
    net = unet.Unet(layers=4,
                    n_class=train_data.n_class,
                    channels=train_data.channels,
                    features_root=48,
                    cost='dice_coefficient',
                    cost_kwargs={
                        'regularizer': 0.01,
                        'class_weights': [0.1777, 0.8222]
                    })

    x_test, y_test = test_data(10)

    # save prediction masks in TIFF format
    data_files = [d for d in test_data.data_files if d.split('.')[-1] == 'jpg']
    data_files.sort()
    for i, name in enumerate(data_files):
        file_name = name.split('\\')[1].split('.')[0]
        prediction = net.predict(model_path="./unet_trained/model.ckpt",
                                 x_test=x_test[i].reshape(-1, 600, 400, 3))
        ny = prediction.shape[2]
        img = to_rgb(prediction[..., 1])
        im.fromarray(img[0].round().astype(np.uint8)).save(
            r'RoadDetection_Test_Predictions\\{}_mask.tif'.format(file_name),
            'TIFF',
            dpi=[300, 300],
            quality=90)

    # save prediction masks in JPEG format
    data_files = [d for d in test_data.data_files if d.split('.')[-1] == 'jpg']
    data_files.sort()
    for i, name in enumerate(data_files):
        file_name = name.split('\\')[1].split('.')[0]
        prediction = net.predict(model_path="./unet_trained/model.ckpt",
                                 x_test=x_test[i].reshape(-1, 600, 400, 3))
        ny = prediction.shape[2]
        img = to_rgb(prediction[..., 1])
        im.fromarray(img[0].round().astype(np.uint8)).save(
            r'RoadDetection_Test_Predictions\\{}_mask.jpg'.format(file_name),
            'JPEG',
            dpi=[300, 300],
            quality=90)

    # predict mask from training data for presentation
    x_val, y_val = train_data(1)
    prediction = net.predict(model_path="./unet_trained/model.ckpt",
                             x_test=x_val)

    fig, ax = plt.subplots(1, 3, figsize=(12, 5))
    ax[0].imshow(x_val[0, ..., 0], aspect="auto")
    ax[1].imshow(y_val[0, ..., 1], aspect="auto")
    pred = np.squeeze(prediction[0, ..., 1])
    ax[2].imshow(pred, aspect="auto")
    ax[0].set_title("Input")
    ax[1].set_title("Ground truth")
    ax[2].set_title("Prediction")
    fig.tight_layout()

    # predict mask from test data for presentation
    x_val, y_val = test_data(1)
    prediction = net.predict(model_path="./unet_trained/model.ckpt",
                             x_test=x_val)

    fig, ax = plt.subplots(1, 3, figsize=(12, 5))
    ax[0].imshow(x_val[0, ..., 0], aspect="auto")
    ax[1].imshow(y_val[0, ..., 1], aspect="auto")
    pred = np.squeeze(prediction[0, ..., 1])
    ax[2].imshow(pred, aspect="auto")
    ax[0].set_title("Input")
    ax[1].set_title("Ground truth")
    ax[2].set_title("Prediction")
    fig.tight_layout()

    # presentation method 2
    x_test, y_test = test_data(10)
    img = util.combine_img_prediction(x_test, y_test, prediction)
    util.save_image(img, "test_pred_image.jpg")

    util.plot_prediction(x_test=x_train, y_test=x_train, prediction=validation)
    print('Process completed.')
Exemple #26
0
def main(trainPath='traindata',
         testPath='/media/data4TbExt4/neuron/neurofinder.00.00.test/',
         layerNum=4,
         features=64,
         bsize=4,
         opm='adam',
         iter=120,
         ep=220,
         display=60):
    '''
    Driver function. Provides the required inputs for all the modules of the tf_unet package.
    Input:
    trainPath: The path to the training data. This is the path to the directory. All .tif training images must be stored in this directory.
    testPath: The path to the test data. All _mask.tif files must be stored in this directory.
    layerNum: Number of layers in the Unet architecture.
    features: Length of the feature map in the Unet architecture.
    bsize: The batch size for the input.
    opm: The type of optimizer used.
    iter: The number of iterations during training. 
    ep: Number of epochs to be used for training. 
    display: This is used during display. Number of epochs after which the accuracy should be displayed.
    
    '''
    if sys.version_info[0] >= 3:
        raise ("Must be using Python 2.7!")
    if (tf.test.gpu_device_name()):
        print('GPU detected')
    else:
        print('No GPU!')

    # Train using Unet
    data_provider = image_util.ImageDataProvider('{}/*.tif'.format(trainPath))
    net = unet.Unet(channels=1,
                    n_class=2,
                    layers=layerNum,
                    features_root=features)
    trainer = unet.Trainer(net, batch_size=bsize, optimizer=opm)
    path = trainer.train(data_provider,
                         "./unet_trained",
                         training_iters=iter,
                         epochs=ep,
                         display_step=display)

    # Test using the trained result
    path = '{}/images'.format(testPath)
    files = sorted(glob(path + '/*.tiff'))
    testimg = array([imread(f) for f in files])
    concatArray = testimg.sum(axis=0)
    print('The dimension of testing image is {}'.format(concatArray.shape))
    plt.imshow(concatArray)
    concatArray = concatArray.reshape((1, ) + s.shape + (1, ))
    prediction = net.predict("./unet_trained/model.cpkt", concatArray)
    prediction = prediction[0, :, :, 1]
    print('The output dimension is {}'.format(prediction.shape))
    savetxt('predictedArray.txt', prediction)

    # Plot the results
    fig, ax = plt.subplots(1, 2, figsize=(12, 5))
    ax[0].imshow(s[0, ..., 0], cmap='gray')
    ax[1].imshow(prediction, aspect="auto", cmap='gray')
    ax[0].set_title("Input")
    ax[1].set_title("Prediction")
    plt.show()
def Testing(path, net):

    # TestData = image_util.ImageDataProvider(  dir + 'test_padded/*.tif')
    TestData = image_util.ImageDataProvider(dir + '*.tif')
    Data, Label = TestData(len(TestData.data_files))
    filenamesOrig = TestData.data_files

    filenames = []
    for f in range(len(filenamesOrig)):
        fname = filenamesOrig[f]
        filenames.append(fname.split('data/')[1])

    sz = Data.shape
    Dice = np.zeros((Data.shape[0]))
    Dice_Lgc = np.zeros((Data.shape[0]))
    K = int(sz[0] / TestSizeSpan)

    # if K*TestSizeSpan < sz[0]:
    #     K = K + 1

    prediction2 = np.zeros((sz[0], sz[1] - padSize1, sz[2] - padSize1, sz[3]))
    filesnameWrt = []
    for k in range(K - 1):
        fname = filenames[(k * TestSizeSpan):min((k + 1) *
                                                 TestSizeSpan, sz[0])]
        data = Data[(k * TestSizeSpan):min((k + 1) * TestSizeSpan, sz[0]), ...]
        label = Label[(k * TestSizeSpan):min((k + 1) * TestSizeSpan, sz[0]),
                      ...]
        # path = dir+'model/model.ckpt'
        prediction2 = net.predict(path, data, GPU_Num=gpuNum)

        L = label.shape

        for l in range(L[0]):
            lbl = label[l, padSize:L[1] - padSize, padSize:L[2] - padSize, 1]

            prediction = prediction2[l, ..., 0]
            try:
                Thresh_Mult = max(filters.threshold_otsu(prediction), 0.2)
            except:
                Thresh_Mult = 0.2

            prediction_Logical = prediction > Thresh_Mult

            dice = DiceCoefficientCalculator(prediction, lbl)
            dice_Lgc = DiceCoefficientCalculator(prediction_Logical, lbl)
            filesnameWrt.append(fname[l].split('.tif')[0])

            if l % 100 == 0:
                fnm = fname[l].split('.tif')[0]
                imageio.imwrite(dir + 'Results/' + fnm + '_pred.jpg',
                                prediction * 256)

                imageio.imwrite(
                    dir + 'Results/' + fnm + '_predLgc.jpg',
                    np.asarray(prediction_Logical, dtype=float) * 256)

        Dice[(k * TestSizeSpan):min((k + 1) * TestSizeSpan, sz[0])] = dice
        Dice_Lgc[(k * TestSizeSpan):min((k + 1) *
                                        TestSizeSpan, sz[0])] = dice_Lgc

    return Dice_Lgc, Dice, filesnameWrt
Exemple #28
0
import matplotlib.pyplot as plt
import matplotlib 
import matplotlib.pyplot as plt
import matplotlib 
#%matplotlib inline
#from __future__ import division, print_function
from tf_unet import unet, util, image_util
from tf_unet import unet, util, image_util
#from __future__ import division, print_function
#get_ipython().magic('matplotlib inline')


#load training images
data_provider = image_util.ImageDataProvider(search_path = "train_data_pre/*", data_suffix="train.jpg", mask_suffix="mask.jpg")
#x_test, y_test = data_provider(1)

#load model
net = unet.Unet(layers=3, features_root=64, channels=1, n_class=2)

#train the model
trainer = unet.Trainer(net)
path = trainer.train(data_provider, "./temp", training_iters=10, epochs=10)
prediction = net.predict("./predicted/", data)
"""
unet.error_rate(prediction, util.crop_to_shape(label, prediction.shape))
img = util.combine_img_prediction(data, label, prediction)
util.save_image(img, "prediction.jpg")"""


# In[8]:
Exemple #29
0
from tf_unet import image_util
from tf_unet import unet
from tf_unet import util

# In[2]:

net = unet.Unet(channels=3, n_class=2, layers=2, features_root=300)
trainer = unet.Trainer(net,
                       optimizer="momentum",
                       opt_kwargs=dict(momentum=0.2))

# In[5]:

data_provider = image_util.ImageDataProvider(
    "/mnt/ccipd_data/CCF/ccfMaskTmp/training/*.png",
    data_suffix='_img.png',
    mask_suffix='_mask.png')

# In[4]:

path = trainer.train(data_provider,
                     "./unet_trained",
                     training_iters=32,
                     epochs=1,
                     display_step=2)

# In[2]:

validation_provider = image_util.ImageDataProvider(
    "/mnt/ccipd_data/CCF/ccfMaskTmp/test/*.png",
    data_suffix='_img.png',
# Support two classes only.
net = unet.Unet(layers=3, features_root=64, channels=1, n_class=2)
#net = unet.Unet(layers = 3, features_root = 32, channels = 1, n_class = 2)
#net = unet.Unet(layers = 3, features_root = 128, channels = 1, n_class = 2)
#net = unet.Unet(layers = 3, features_root = 256, channels = 1, n_class = 2)
#net = unet.Unet(layers = 3, features_root = 512, channels = 1, n_class = 2)
#net = unet.Unet(layers = 3, features_root = 1024, channels = 1, n_class = 2)  # Error.
#net = unet.Unet(layers = 5, features_root = 256, channels = 1, n_class = 2)
#net = unet.Unet(layers = 7, features_root = 256, channels = 1, n_class = 2)

#--------------------------------------------------------------------
# Train.

#train_data_provider = image_util.ImageDataProvider(train_dataset_search_pattern)
train_data_provider = image_util.ImageDataProvider(
    search_path=train_dataset_search_pattern,
    data_suffix='.tif',
    mask_suffix='_mask.tif')

# VGG: learning_rate=0.01->0.001->0.0001, momentum=0.9, dropout=0.5,
#trainer = unet.Trainer(net)
trainer = unet.Trainer(net,
                       batch_size=1,
                       optimizer="momentum",
                       opt_kwargs={
                           "learning_rate": 0.2,
                           "decay_rate": 0.95,
                           "momentum": 0.2
                       })

#model_filepath = trainer.train(train_data_provider, model_output_dir_path, training_iters = 32, epochs = 100)
model_filepath = trainer.train(train_data_provider,