Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gap between theoric and output image size #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ docs/_build
/.hope/
/.ipynb_checkpoints/
/.settings/
*.iml
*.xml
78 changes: 78 additions & 0 deletions GenerateUNetModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import sys, getopt, os
import numpy as np
import shutil
from tf_unet import image_util, unet


"""
Original paper :
https://lmb.informatik.uni-freiburg.de/Publications/2015/RFB15a/
Parameters['layers'] = 5
Parameters['convolutionFilter'] = 3
"""


#Change the following field to configure your UNet
def devTest():
Parameters = dict()
Parameters['layers'] = 3
Parameters['convolutionFilter'] = 7
Parameters['depthConvolutionFilter'] = 16
Parameters['channel'] = 3
Parameters['outputClassNumber'] = 2
Parameters['optimizerType'] = "momentum"
Parameters['optimizerValue'] = 0.90
Parameters['learningRate'] = 0.01
Parameters['batchSize'] = 2
Parameters['decayRate'] = 0.95
Parameters['dropout'] = 0.8
Parameters['epoch'] = 1
Parameters['iterations'] = 0
Parameters['datasrc'] = "C:/Work/Projets/Git - UNets/Data"
Parameters['output'] = "C:/Work/Projets/Git - UNets/Model"
return Parameters

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']),
features_root=int(Parameters['depthConvolutionFilter']),
channels=int(Parameters['channel']),
n_class=int(Parameters['outputClassNumber']),
filter_size=int(Parameters['convolutionFilter']))

print("\tUNet created")
trainer = unet.Trainer(net,
optimizer=str(Parameters['optimizerType']),
batch_size=int(Parameters['batchSize']),
opt_kwargs=dict(momentum=np.float32(Parameters['optimizerValue']),
learning_rate=np.float32(Parameters['learningRate']),
decay_rate=np.float32(Parameters['decayRate'])))
print("\tUNet initialized")
path = trainer.train(data_provider=data_provider,
output_path=str(Parameters['output']),
dropout=np.float32(Parameters['dropout']),
training_iters=int(Parameters['iterations']),
epochs=int(Parameters['epoch']))

print("\n\tEND of learning step !")
40 changes: 40 additions & 0 deletions PredictDirectoryUNet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from tf_unet import util, image_util, unet
import sys
import glob
import os

#Script which predict a flow of images. Can be used with GPU Tensorflow without overload graphic memory


#CHANGE YO YOUR PATH
listOfFile = glob.glob("C:/Work/Projets/Git - UNets/DataTest/*.png")
modelPath = "C:/Work/Projets/Git - UNets/Model/model/model.ckpt"

#GIVE YOUR CONFIGURATION USE TO GENERATE CURRENT model.ckpt
Layers = 3
Features = 16
Channels = 3
nClass = 2
filterSize = 7

print('\n')
print('Path to model : ' + modelPath)
print('Layers = ' + str(Layers))
print('Features = ' + str(Features))
print('Channels = ' + str(Channels))
print('Class number = ' + str(nClass))
print('Convolution size filter = ' + str(filterSize))

net = unet.Unet(layers=int(Layers),
features_root=int(Features),
channels=int(Channels),
n_class=int(nClass),
filter_size=int(filterSize))

for i in range(0, len(listOfFile)):
imagePath = listOfFile[i]
data_provider = image_util.SingleImageDataProvider(imagePath)
predicter = net.predict(modelPath, data_provider.img)
util.PlotSingleImagePrediction(predicter, output_path=imagePath)

print("\nPrediction finished !")
22 changes: 21 additions & 1 deletion tf_unet/image_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,24 @@ def _next_data(self):
img = self._load_file(image_name, np.float32)
label = self._load_file(label_name, np.bool)

return img,label
return img, label


class SingleImageDataProvider(ImageDataProvider):

def __init__(self, singleImagePath):

super(ImageDataProvider, self).__init__(None, None)

self.data_files = list()
self.data_files.append(singleImagePath)
self.data_suffix = '.' + str(singleImagePath.split('/')[-1].split('.')[-1])
self.imgShape = self._load_file(self.data_files[0]).shape
self.channels = self.imgShape[-1]
self.file_idx = -1
self.img = np.zeros((1, self.imgShape[0], self.imgShape[1], self.channels))

data = self._load_file(self.data_files[0], np.float32)
data = self._process_data(data)

self.img[0] = data
87 changes: 52 additions & 35 deletions tf_unet/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,67 @@
import tensorflow as tf

def weight_variable(shape, stddev=0.1):
initial = tf.truncated_normal(shape, stddev=stddev)
return tf.Variable(initial)
with tf.name_scope('weight_variable') as scope:
initial = tf.truncated_normal(shape, stddev=stddev)
return tf.Variable(initial)

def weight_variable_devonc(shape, stddev=0.1):
return tf.Variable(tf.truncated_normal(shape, stddev=stddev))
with tf.name_scope('weight_variable_devonc') as scope:
return tf.Variable(tf.truncated_normal(shape, stddev=stddev))

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def conv2d(x, W,keep_prob_):
conv_2d = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')
return tf.nn.dropout(conv_2d, keep_prob_)

def deconv2d(x, W,stride):
x_shape = tf.shape(x)
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
return tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='VALID')

def max_pool(x,n):
return tf.nn.max_pool(x, ksize=[1, n, n, 1], strides=[1, n, n, 1], padding='VALID')

def crop_and_concat(x1,x2):
x1_shape = tf.shape(x1)
x2_shape = tf.shape(x2)
# offsets for the top left corner of the crop
offsets = [0, (x1_shape[1] - x2_shape[1]) // 2, (x1_shape[2] - x2_shape[2]) // 2, 0]
size = [-1, x2_shape[1], x2_shape[2], -1]
x1_crop = tf.slice(x1, offsets, size)
return tf.concat([x1_crop, x2], 3)
with tf.name_scope('bias_variable') as scope:
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)


def conv2d(x, W, keep_prob_):
with tf.name_scope('convolution') as scope:
# VALID = without padding
# SAME = padding with 0
conv_2d = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID', name="LAST_CONVOLUTION")
return tf.nn.dropout(conv_2d, keep_prob_)


def deconv2d(x, W, stride):
with tf.name_scope('deconvolution') as scope:
x_shape = tf.shape(x)
output_shape = tf.stack([x_shape[0], x_shape[1] * 2, x_shape[2] * 2, x_shape[3] // 2])
#output_shape = tf.pack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
return tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='VALID')


def max_pool(x, n):
with tf.name_scope('max_pooling') as scope:
return tf.nn.max_pool(x, ksize=[1, n, n, 1], strides=[1, n, n, 1], padding='VALID')


def crop_and_concat(x1, x2):
with tf.name_scope('crop_and_concatenate') as scope:
x1_shape = tf.shape(x1)
x2_shape = tf.shape(x2)
# offsets for the top left corner of the crop
offsets = [0, (x1_shape[1] - x2_shape[1]) // 2, (x1_shape[2] - x2_shape[2]) // 2, 0]
size = [-1, x2_shape[1], x2_shape[2], -1]
x1_crop = tf.slice(x1, offsets, size)
return tf.concat(3, [x1_crop, x2])

def pixel_wise_softmax(output_map):
exponential_map = tf.exp(output_map)
evidence = tf.add(exponential_map,tf.reverse(exponential_map,[False,False,False,True]))
return tf.div(exponential_map,evidence, name="pixel_wise_softmax")
with tf.name_scope('pixel_wise_softmax') as scope:
exponential_map = tf.exp(output_map)
evidence = tf.add(exponential_map, tf.reverse(exponential_map,[False,False,False,True]))
return tf.div(exponential_map, evidence, name="pixel_wise_softmax")

def pixel_wise_softmax_2(output_map):
exponential_map = tf.exp(output_map)
sum_exp = tf.reduce_sum(exponential_map, 3, keep_dims=True)
tensor_sum_exp = tf.tile(sum_exp, tf.stack([1, 1, 1, tf.shape(output_map)[3]]))
return tf.div(exponential_map,tensor_sum_exp)
with tf.name_scope('pixel_wise_softmax_2') as scope:
exponential_map = tf.exp(output_map)
sum_exp = tf.reduce_sum(exponential_map, 3, keep_dims=True)
tensor_sum_exp = tf.tile(sum_exp, tf.pack([1, 1, 1, tf.shape(output_map)[3]]))
return tf.div(exponential_map,tensor_sum_exp)



def cross_entropy(y_,output_map):
return -tf.reduce_mean(y_*tf.log(tf.clip_by_value(output_map,1e-10,1.0)), name="cross_entropy")
# return tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(output_map), reduction_indices=[1]))
with tf.name_scope('cross_entropy') as scope:
return -tf.reduce_mean(y_*tf.log(tf.clip_by_value(output_map,1e-10,1.0)), name="cross_entropy")
# return tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(output_map), reduction_indices=[1]))
Loading