def convolve_data_inception(input_data, val, n, dtype): data = tf.reshape(input_data, [-1, 299, 299, 3]) logits, end_points = inception_resnet_v2( data, num_classes=output_sizes[-1] * output_sizes[-1] * layer_elements[-2], is_training=False, reuse=incep_reuse) return logits
def __init__(self, input, training, params=None): net, end_points = inception_resnet_v2( input, num_classes=None, is_training=training, dropout_keep_prob=0.8, reuse=None, scope='InceptionResnetV2', create_aux_logits=False, activation_fn=tf.nn.relu) net = tf.layers.flatten(net) self.outputs = net
def main(dataset_dir, log_dir, tfrecord_filename, convlayer): plt.style.use('ggplot') image_size=299 img_size=image_size*image_size*3 file_pattern=tfrecord_filename + '_%s_*.tfrecord' #State the batch_size to evaluate each time, which can be a lot more than the training batch batch_size = 36 #State the number of epochs to evaluate num_epochs = 1 #Get the latest checkpoint file checkpoint_file = tf.train.latest_checkpoint(log_dir) #Just construct the graph from scratch again with tf.Graph().as_default() as graph: tf.logging.set_verbosity(tf.logging.INFO) #Get the dataset first and load one batch of validation images and labels tensors. Set is_training as False so as to use the evaluation preprocessing dataset = get_split('train', dataset_dir, file_pattern, tfrecord_filename) images, raw_images, labels = load_batch(dataset, batch_size = batch_size, height=image_size, width=image_size, is_training = False) #Create some information about the training steps x = tf.placeholder(tf.float32, shape=[None, img_size], name='x') #Now create the inference model but set is_training=False with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(images, num_classes = dataset.num_classes, is_training = False) # #get all the variables to restore from the checkpoint file and create the saver function to restore variables_to_restore = slim.get_variables_to_restore() saver = tf.train.Saver(variables_to_restore) def restore_fn(sess): return saver.restore(sess, checkpoint_file) #Just define the metrics to track without the loss or whatsoever conv2dx = end_points[convlayer] predictions = tf.argmax(end_points['Predictions'], 1) accuracy, accuracy_update = tf.contrib.metrics.streaming_accuracy(predictions, labels) def plot_conv_layer(layer_name, images): # Create a feed-dict containing just one image. # Calculate and retrieve the output values of the layer # when inputting that image. for j in range(5): image = images[j] values = sess.run(layer_name, feed_dict={x:np.reshape([image], [1, img_size], order='F')}) # Number of filters used in the conv. layer. num_filters = values.shape[3] # Number of grids to plot. # Rounded-up, square-root of the number of filters. grids = math.ceil(math.sqrt(num_filters)) # Create figure with a grid of sub-plots. fig, axes = plt.subplots(grids, grids) # Plot the output images of all the filters. for i, ax in enumerate(axes.flat): # Only plot the images for valid filters. if i<num_filters: # Get the output image of using the i'th filter. # See new_conv_layer() for details on the format # of this 4-dim tensor. img = values[0, :, :, i] # Plot image. ax.imshow(img, interpolation='nearest') # Remove ticks from the plot. ax.set_xticks([]) ax.set_yticks([]) # Ensure the plot is shown correctly with multiple plots # in a single Notebook cell. plt.show() def plot_sample_images(images, labels): for j in range(9): images = images[j] images.append(images) grids = math.ceil(math.sqrt(batch_size)) fig, axes = plt.subplots(grids, grids) fig.subplots_adjust(hspace=0.50, wspace=0.2, top=0.97, bottom=0.06) for i, ax in enumerate(axes.flat): label_name = dataset.labels_to_name[labels[i]] # Plot image. ax.imshow(images[i]) xlabel = 'GroundTruth: ' + label_name # Show the classes as the label on the x-axis. ax.set_xlabel(xlabel) # Remove ticks from the plot. ax.set_xticks([]) ax.set_yticks([]) # Ensure the plot is shown correctly with multiple plots # in a single Notebook cell. plt.show() #Get your supervisor sv = tf.train.Supervisor(logdir = None, summary_op = None, saver = None, init_fn = restore_fn) #Now we are ready to run in one session with sv.managed_session() as sess: #Now we want to visualize the last batch's images just to see what our model has predicted raw_images, labels, predictions = sess.run([raw_images, labels, predictions]) plot_conv_layer(conv2dx, raw_images) logging.info('Model Visualisation completed!.')
def main(dataset_dir, log_dir, log_eval, tfrecord_filename): #State the batch_size to evaluate each time, which can be a lot more than the training batch if not os.path.exists(log_eval): print(log_eval+'is required! Creating!'+log_eval+'...') os.mkdir(log_eval) batch_size = 64 #State the number of epochs to evaluate num_epochs = 1 #Get the latest checkpoint file checkpoint_file = tf.train.latest_checkpoint(log_dir) image_size=299 img_size=image_size*image_size*3 file_pattern=tfrecord_filename + '_%s_*.tfrecord' #Create log_dir for evaluation information #if not os.path.exists(log_eval): # os.mkdir(log_eval) plt.style.use('ggplot') #Just construct the graph from scratch again with tf.Graph().as_default() as graph: tf.logging.set_verbosity(tf.logging.INFO) #Get the dataset first and load one batch of validation images and labels tensors. Set is_training as False so as to use the evaluation preprocessing dataset = get_split('validation', dataset_dir, file_pattern, tfrecord_filename) images, raw_images, labels = load_batch(dataset, batch_size = batch_size, height=image_size, width=image_size, is_training = False) #Create some information about the training steps num_batches_per_epoch = dataset.num_samples / batch_size num_steps_per_epoch = num_batches_per_epoch #Now create the inference model but set is_training=False with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(images, num_classes = dataset.num_classes, is_training = False) # #get all the variables to restore from the checkpoint file and create the saver function to restore variables_to_restore = slim.get_variables_to_restore() saver = tf.train.Saver(variables_to_restore) def restore_fn(sess): return saver.restore(sess, checkpoint_file) #Just define the metrics to track without the loss or whatsoever predictions = tf.argmax(end_points['Predictions'], 1) accuracy, accuracy_update = tf.contrib.metrics.streaming_accuracy(predictions, labels) precision, precision_update=slim.metrics.streaming_precision(predictions, labels) metrics_op = tf.group(accuracy_update, precision_update) #Create the global step and an increment op for monitoring global_step = get_or_create_global_step() global_step_op = tf.assign(global_step, global_step + 1) #no apply_gradient method so manually increasing the global_step def plot_sample_images(images, labels, predictions): grids = math.ceil(math.sqrt(batch_size)-2) fig, axes = plt.subplots(grids, grids) fig.subplots_adjust(hspace=0.50, wspace=0.2, top=0.97, bottom=0.06) for i, ax in enumerate(axes.flat): prediction_name, label_name = dataset.labels_to_name[predictions[i]], dataset.labels_to_name[labels[i]] # Plot image. ax.imshow(images[i]) # Show true and predicted classes. if predictions is None: xlabel = 'GroundTruth: ' + label_name else: xlabel = 'GroundTruth: ' + label_name + '\n' + 'Prediction: ' + prediction_name # Show the classes as the label on the x-axis. ax.set_xlabel(xlabel) # Remove ticks from the plot. ax.set_xticks([]) ax.set_yticks([]) # Ensure the plot is shown correctly with multiple plots # in a single Notebook cell. plt.show() #Create a evaluation step function def eval_step(sess, metrics_op, global_step): ''' Simply takes in a session, runs the metrics op and some logging information. ''' start_time = time.time() _, global_step_count, accuracy_value, precision_value = sess.run([metrics_op, global_step_op, accuracy, precision]) time_elapsed = time.time() - start_time #Log some information logging.info('Global Step %s:- Streaming Accuracy: %.4f : Precision: %2f (%.2f sec/step)', global_step_count, accuracy_value, precision_value, time_elapsed) return accuracy_value, precision_value #Define some scalar quantities to monitor tf.summary.scalar('Validation_Accuracy', accuracy) tf.summary.scalar('precision', precision) my_summary_op = tf.summary.merge_all() #Get your supervisor sv = tf.train.Supervisor(logdir = log_eval, summary_op = None, saver = None, init_fn = restore_fn) #Now we are ready to run in one session with sv.managed_session() as sess: for step in range(int(num_steps_per_epoch * num_epochs)): sess.run(sv.global_step) #print vital information every start of the epoch as always if step % num_batches_per_epoch == 0: logging.info('Epoch: %s/%s', step / num_batches_per_epoch + 1, num_epochs) logging.info('Current Streaming Accuracy: %.4f', sess.run(accuracy)) #Compute summaries every 10 steps and continue evaluating if step % 10 == 0: eval_step(sess, metrics_op = metrics_op, global_step = sv.global_step) summaries = sess.run(my_summary_op) sv.summary_computed(sess, summaries) #Otherwise just run as per normal else: eval_step(sess, metrics_op = metrics_op, global_step = sv.global_step) #At the end of all the evaluation, show the final accuracy logging.info('Final Streaming Accuracy: %.4f', sess.run(accuracy)) logging.info('Precision: %.4f', sess.run(precision)) #Now we want to visualize the last batch's images just to see what our model has predicted raw_images, labels, predictions = sess.run([raw_images, labels, predictions]) plot_sample_images(raw_images, labels, predictions) confusion_matrix_= confusion_matrix(y_true=labels, y_pred=predictions) print('\n Evaluation Confusion Matirx:') print(confusion_matrix_) plt.matshow(confusion_matrix_) plt.colorbar() ticks = labels plt.xticks(ticks) plt.yticks(ticks) plt.xlabel('Prediction') plt.ylabel('Ground_Truth') plt.show() logging.info('Model evaluation completed.')
def main(dataset_dir, log_dir, tfrecord_filename): #State the location of the checkpoint file is checkpoint_file = 'init_ckpt/inception_resnet_v2.ckpt' #State the labels file and read it labels_file = dataset_dir+'/labels.txt' labels = open(labels_file, 'r') #Create a dictionary to refer each label to their string name labels_to_name = {} for line in labels: label, string_name = line.split(':') string_name = string_name[:-1] #Remove newline labels_to_name[int(label)] = string_name #Create the file pattern of your TFRecord files so that it could be recognized later on file_pattern = tfrecord_filename + '_%s_*.tfrecord' #================= TRAINING INFORMATION ================== #State the number of epochs to train num_epochs = 2 #State your batch size batch_size = 4 #Learning rate information and configuration (Up to you to experiment) initial_learning_rate = 0.001 learning_rate_decay_factor = 0.5 num_epochs_before_decay = 1 #Create the log directory here. Must be done here otherwise import will activate this unneededly. if not os.path.exists(log_dir): os.mkdir(log_dir) # session=tf.Session() #Training the model #we start by constructing the graph and then build the model with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) #Set the verbosity to INFO level #First create the dataset and load one batch dataset = get_split('train', dataset_dir, file_pattern=file_pattern, tfrecord_filename=tfrecord_filename) images, _, labels = load_batch(dataset, height=image_size, width=image_size, batch_size=batch_size) #Know the number steps to take before decaying the learning rate and batches per epoch and Because one step is one batch processed step per epoch=step per batch num_batches_per_epoch = dataset.num_samples / batch_size num_steps_per_epoch = num_batches_per_epoch decay_steps = int(num_epochs_before_decay * num_steps_per_epoch) #Create the model inference with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(images, num_classes = dataset.num_classes, is_training = True) #Define the scopes that you want to exclude for restoration exclude = ['InceptionResnetV2/Logits', 'InceptionResnetV2/AuxLogits'] variables_to_restore = slim.get_variables_to_restore(exclude = exclude) #Perform one-hot-encoding of the labels (Try one-hot-encoding within the load_batch function!) one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) """# To make the model better at classifying the input images, we must somehow change the variables for all the network layers. To do this we first need to know how well the model currently performs by comparing the predicted output of the model `y_pred` to the desired output `y_true` The cross-entropy is a performance measure used in classification. The cross-entropy is a continuous function that is always positive and if the predicted output of the model exactly matches the desired output then the cross-entropy equals zero. The goal of optimization is therefore to minimize the cross-entropy so it gets as close to zero as possible by changing the variables of the network layers. Performs the equivalent to tf.nn.sparse_softmax_cross_entropy_with_logits but enhanced with checks this is achieved by computing the loss, applies the gradients in order to update the weight and later return a tensor that when evaluated returns the total loss """ loss = tf.losses.softmax_cross_entropy(onehot_labels = one_hot_labels, logits = logits) total_loss = tf.losses.get_total_loss() #obtain the regularization losses as well #Create the global step for monitoring the learning_rate and training. global_step = get_or_create_global_step() #Define your exponentially decaying learning rate lr = tf.train.exponential_decay( learning_rate = initial_learning_rate, global_step = global_step, decay_steps = decay_steps, decay_rate = learning_rate_decay_factor, staircase = True) #Now we can define the optimizer that takes on the learning rate optimizer = tf.train.AdamOptimizer(learning_rate = lr) """ Create the train_op, Computation of the loss and gradient """ train_op = slim.learning.create_train_op(total_loss, optimizer) #State the metrics that you want to predict. We get a predictions that is not one_hot_encoded. predictions = tf.argmax(end_points['Predictions'], 1) probabilities = end_points['Predictions'] accuracy, accuracy_update = tf.contrib.metrics.streaming_accuracy(predictions, labels) metrics_op = tf.group(accuracy_update, probabilities) my_summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) for end_point in end_points: x = end_points[end_point] my_summaries.add(tf.summary.histogram('activation/' + end_point, x)) #Now finally create all the summaries you need to monitor and group them into one summary op. my_summaries.add(tf.summary.scalar('losses/Total_Loss', total_loss)) my_summaries.add(tf.summary.scalar('accuracy', accuracy)) my_summaries.add(tf.summary.scalar('learning_rate', lr)) my_summary_op = tf.summary.merge(list(my_summaries)) #session.run(tf.global_variables_initializer()) #Now we need to create a training step function that runs both the train_op, metrics_op and updates the global_step concurrently. def train_step(sess, train_op, global_step): """ Runs a session for the three arguments provided and gives a logging on the time elapsed for each global step """ #Check the time for each sess run start_time = time.time() total_loss, global_step_count, _ = sess.run([train_op, global_step, metrics_op]) time_elapsed = time.time() - start_time #Run the logging to print some results logging.info('global step %s: loss: %.4f (%.2f sec/step)', global_step_count, total_loss, time_elapsed) return total_loss, global_step_count #Now we create a saver function that actually restores the variables from a checkpoint file in a sess saver = tf.train.Saver(variables_to_restore) def restore_fn(sess): return saver.restore(sess, checkpoint_file) #Define your supervisor for running a managed session. Do not run the summary_op automatically or else it will consume too much memory sv = tf.train.Supervisor(logdir = log_dir, summary_op = None, init_fn = restore_fn) #Run the managed session with sv.managed_session() as sess: for step in range(int(num_steps_per_epoch * num_epochs)): # for step in xrange(1): #At the start of every epoch, show the vital information: if step % num_batches_per_epoch == 0: logging.info('Epoch %s/%s', step/num_batches_per_epoch + 1, num_epochs) learning_rate_value, accuracy_value = sess.run([lr, accuracy]) logging.info('Current Learning Rate: %s', learning_rate_value) logging.info('Current Streaming Accuracy: %s', accuracy_value) # optionally, print your logits and predictions for a sanity check that things are going fine. logits_value, probabilities_value, predictions_value, labels_value = sess.run([logits, probabilities, predictions, labels]) print('logits: \n', logits_value) print('Probabilities: \n', probabilities_value) print('predictions: \n', predictions_value) print('Labels:\n:', labels_value) #Log the summaries every 10 step. if step % 10 == 0: loss, _ = train_step(sess, train_op, sv.global_step) summaries = sess.run(my_summary_op) sv.summary_computed(sess, summaries) #If not, simply run the training step else: loss, _ = train_step(sess, train_op, sv.global_step) #We log the final training loss and accuracy logging.info('Final Loss: %s', loss) logging.info('Final Accuracy: %s', sess.run(accuracy)) #Once all the training has been done, save the log files and checkpoint model logging.info('Finished training! Saving model to disk now.') # saver.save(sess, "model.ckpt") sv.saver.save(sess, sv.save_path, global_step = sv.global_step)