diff --git a/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/input_ipcai_data.py b/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/input_ipcai_data.py index fbcae77f3c..a4b53d64e0 100644 --- a/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/input_ipcai_data.py +++ b/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/input_ipcai_data.py @@ -1,103 +1,103 @@ """Functions for downloading and reading ipcai data.""" from __future__ import print_function import os import numpy import pandas as pd from regression.preprocessing import preprocess class DataSet(object): def __init__(self, images, labels, fake_data=False): if fake_data: self._num_examples = 10000 else: assert images.shape[0] == labels.shape[0], ( "images.shape: %s labels.shape: %s" % (images.shape, labels.shape)) self._num_examples = images.shape[0] images = images.astype(numpy.float32) self._images = images self._labels = labels if self._labels.ndim == 1: self._labels = self._labels[:, numpy.newaxis] self._epochs_completed = 0 self._index_in_epoch = 0 @property def images(self): return self._images @property def labels(self): return self._labels @property def num_examples(self): return self._num_examples @property def epochs_completed(self): return self._epochs_completed def next_batch(self, batch_size, fake_data=False): """Return the next `batch_size` examples from this data set.""" if fake_data: fake_image = [1.0 for _ in xrange(784)] fake_label = 0 return [fake_image for _ in xrange(batch_size)], [ fake_label for _ in xrange(batch_size)] start = self._index_in_epoch self._index_in_epoch += batch_size if self._index_in_epoch > self._num_examples: # Finished epoch self._epochs_completed += 1 # Shuffle the data perm = numpy.arange(self._num_examples) numpy.random.shuffle(perm) self._images = self._images[perm] self._labels = self._labels[perm] # Start next epoch start = 0 self._index_in_epoch = batch_size assert batch_size <= self._num_examples end = self._index_in_epoch return self._images[start:end], self._labels[start:end] def read_data_sets(dir, fake_data=False): class DataSets(object): pass data_sets = DataSets() if fake_data: data_sets.train = DataSet([], [], fake_data=True) data_sets.validation = DataSet([], [], fake_data=True) data_sets.test = DataSet([], [], fake_data=True) return data_sets TRAIN_IMAGES = "ipcai_revision_colon_mean_scattering_train_all_virtual_camera.txt" TEST_IMAGES = "ipcai_revision_colon_mean_scattering_test_all_virtual_camera.txt" df_train = pd.read_csv(os.path.join(dir, TRAIN_IMAGES), header=[0, 1]) df_test = pd.read_csv(os.path.join(dir, TEST_IMAGES), header=[0, 1]) - train_images, train_labels = preprocess(df_train, snr=10., magnification=10) + train_images, train_labels = preprocess(df_train, snr=10.) test_images, test_labels = preprocess(df_test, snr=10.) train_labels = train_labels.values test_labels = test_labels.values VALIDATION_SIZE = 1 validation_images = train_images[:VALIDATION_SIZE] validation_labels = train_labels[:VALIDATION_SIZE] train_images = train_images[VALIDATION_SIZE:] train_labels = train_labels[VALIDATION_SIZE:] data_sets.train = DataSet(train_images, train_labels) data_sets.validation = DataSet(validation_images, validation_labels) data_sets.test = DataSet(test_images, test_labels) return data_sets \ No newline at end of file diff --git a/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/script_train_tensorflow_model.py b/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/script_train_tensorflow_model.py index dedb9f3222..d669564584 100644 --- a/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/script_train_tensorflow_model.py +++ b/Modules/Biophotonics/python/iMC/scripts/ipcai_to_tensorflow/script_train_tensorflow_model.py @@ -1,143 +1,164 @@ import os import numpy as np import commons sc = commons.ScriptCommons() sc.set_root("/media/wirkert/data/Data/2016_02_02_IPCAI/") sc.create_folders() ''' A Multilayer Perceptron implementation example using TensorFlow library. This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/) Author: Aymeric Damien Project: https://github.com/aymericdamien/TensorFlow-Examples/ ''' # Import MINST data import input_data #mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) ipcai_dir = os.path.join(sc.get_full_dir("INTERMEDIATES_FOLDER")) import input_ipcai_data ipcai = input_ipcai_data.read_data_sets(ipcai_dir) import tensorflow as tf # Parameters -learning_rate = 0.00001 -training_epochs = 2000 +learning_rate = 0.0001 +training_epochs = 300 batch_size = 100 display_step = 1 # Network Parameters -n_hidden = 100 # hidden layers number of elements +n_hidden = 50 # hidden layers number of elements n_bands = 21 # number of features (wavelengths) n_classes = 1 # number of outputs (one for oxygenation) # tf Graph input -x = tf.placeholder("float", [None, n_bands, 1, 1]) +x = tf.placeholder("float", [None, 21, 1, 1]) y = tf.placeholder("float", [None, n_classes]) keep_prob = tf.placeholder("float") def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def conv2d(x, W, padding='SAME'): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=padding) def max_pool_2x1(x): return tf.nn.max_pool(x, ksize=[1, 2, 1, 1], strides=[1, 2, 1, 1], padding='SAME') -def add_hidden_layer(input, n_inputs, n_outputs, kernel_size, padding='SAME'): +def add_cnn_layer(input, n_inputs, n_outputs, kernel_size, padding='SAME'): #w = weight_variable([n_inputs, n_outputs]) #b = bias_variable([n_outputs]) W = weight_variable([kernel_size, 1, n_inputs, n_outputs]) b = bias_variable([n_outputs]) # Hidden layer with RELU activation #new_layer = tf.nn.relu(tf.add(tf.matmul(input, w), b)) h_conv = tf.nn.relu(conv2d(input, W, padding=padding) + b) # Add dropout regularization #new_layer_with_dropout = tf.nn.dropout(new_layer, keep_prob) h_pool = max_pool_2x1(h_conv) return h_pool -# Create model -def multilayer_perceptron(_X): +def add_fully_connected_layer(_X, n_inputs, n_outputs): + W = weight_variable([n_inputs, n_outputs]) + b = bias_variable([n_outputs]) + # Hidden layer with RELU activation + new_layer = tf.nn.relu(tf.add(tf.matmul(_X, W), b)) + # Add dropout regularization + new_layer_with_dropout = tf.nn.dropout(new_layer, keep_prob) + return new_layer_with_dropout + + +# this is my exemplary convolutional network +def cnn(_X): # two convolutional layers - layer_1 = add_hidden_layer(_X, 1, 32, 6, padding='VALID') - layer_2 = add_hidden_layer(layer_1, 32, 64, 4) + layer_1 = add_cnn_layer(_X, 1, 32, 6, padding='VALID') + layer_2 = add_cnn_layer(layer_1, 32, 64, 4) # flatten last one to be able to apply it to fully connected layer final_number_of_dimensions = 4*64 layer_2_flat = tf.reshape(layer_2, [-1, final_number_of_dimensions]) # fully connected layer to bring information together - W_fc1 = weight_variable([final_number_of_dimensions, 100]) - b_fc1 = bias_variable([100]) - h_fc1 = tf.nn.relu(tf.matmul(layer_2_flat, W_fc1) + b_fc1) - # with dropout regularization - h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) + h_fc1_drop = add_fully_connected_layer(layer_2_flat, + final_number_of_dimensions, + 100) # return linear output layer W_fc2 = weight_variable([100, n_classes]) b_fc2 = bias_variable([n_classes]) return tf.matmul(h_fc1_drop, W_fc2) + b_fc2 -# Construct model + +# and this is the simpler multilayer perceptron +def multilayer_perceptron(_X): + flattend_input = tf.reshape(x, [-1, n_bands]) + layer_1 = add_fully_connected_layer(flattend_input, n_bands, n_hidden) + layer_2 = add_fully_connected_layer(layer_1, n_hidden, n_hidden) + last_hidden_layer = add_fully_connected_layer(layer_2, n_hidden, n_hidden) + + W_out = weight_variable([n_hidden, n_classes]) + b_out = bias_variable([n_classes]) + return tf.matmul(last_hidden_layer, W_out) + b_out + + +# Construct the desired model pred = multilayer_perceptron(x) # Define loss and optimizer cost = tf.reduce_mean(tf.square(pred - y)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Adam Optimizer # Initializing the variables init = tf.initialize_all_variables() # Launch the graph with tf.Session() as sess: sess.run(init) # Training cycle for epoch in range(training_epochs): avg_cost = 0. total_batch = int(ipcai.train.num_examples/batch_size) # Loop over all batches for i in range(total_batch): batch_xs, batch_ys = ipcai.train.next_batch(batch_size) # Fit training using batch data x_image = np.reshape(batch_xs, [-1, n_bands, 1, 1]) sess.run(optimizer, feed_dict={x: x_image, y: batch_ys, keep_prob: 0.5}) # Compute average loss avg_cost += sess.run(cost, feed_dict={x: x_image, y: batch_ys, keep_prob: 1.0})/total_batch # Display logs per epoch step if epoch % display_step == 0: print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost) print "Optimization Finished!" # Test model accuracy = tf.reduce_mean(tf.cast(tf.abs(pred-y), "float")) x_test_image = np.reshape(ipcai.test.images, [-1, n_bands, 1, 1]) print "Median testing error:", accuracy.eval({x: x_test_image, y: ipcai.test.labels, keep_prob:1.0})