def softmax_preds(images, ckpt_path, return_logits=False): """ Compute softmax activations (probabilities) with the model saved in the path specified as an argument :param images: a np array of images :param ckpt_path: a TF model checkpoint :param logits: if set to True, return logits instead of probabilities :return: probabilities (or logits if logits is set to True) """ # Compute nb samples and deduce nb of batches data_length = len(images) nb_batches = math.ceil(len(images) / FLAGS.batch_size) # Declare data placeholder train_data_node = _input_placeholder() # Build a Graph that computes the logits predictions from the placeholder if FLAGS.deeper: logits = inference_deeper(train_data_node) else: logits = inference(train_data_node) if return_logits: # We are returning the logits directly (no need to apply softmax) output = logits else: # Add softmax predictions to graph: will return probabilities output = tf.nn.softmax(logits) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Will hold the result preds = np.zeros((data_length, FLAGS.nb_labels), dtype=np.float32) # Create TF session with tf.Session() as sess: # Restore TF session from checkpoint file saver.restore(sess, ckpt_path) # Parse data by batch for batch_nb in xrange(0, int(nb_batches + 1)): # Compute batch start and end indices start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size) # Prepare feed dictionary feed_dict = {train_data_node: images[start:end]} # Run session ([0] because run returns a batch with len 1st dim == 1) preds[start:end, :] = sess.run([output], feed_dict=feed_dict)[0] # Reset graph to allow multiple calls tf.reset_default_graph() return preds
def softmax_preds(images, ckpt_path, return_logits=False): """ Compute softmax activations (probabilities) with the model saved in the path specified as an argument :param images: a np array of images :param ckpt_path: a TF model checkpoint :param logits: if set to True, return logits instead of probabilities :return: probabilities (or logits if logits is set to True) """ # Compute nb samples and deduce nb of batches data_length = len(images) nb_batches = math.ceil(len(images) / FLAGS.batch_size) # Declare data placeholder train_data_node = _input_placeholder() # Build a Graph that computes the logits predictions from the placeholder if FLAGS.deeper: logits = inference_deeper(train_data_node) else: logits = inference(train_data_node) if return_logits: # We are returning the logits directly (no need to apply softmax) output = logits else: # Add softmax predictions to graph: will return probabilities output = tf.nn.softmax(logits) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Will hold the result preds = np.zeros((data_length, FLAGS.nb_labels), dtype=np.float32) # Create TF session with tf.Session() as sess: # Restore TF session from checkpoint file saver.restore(sess, ckpt_path) # Parse data by batch for batch_nb in xrange(0, int(nb_batches+1)): # Compute batch start and end indices start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size) # Prepare feed dictionary feed_dict = {train_data_node: images[start:end]} # Run session ([0] because run returns a batch with len 1st dim == 1) preds[start:end, :] = sess.run([output], feed_dict=feed_dict)[0] # Reset graph to allow multiple calls tf.reset_default_graph() return preds
def train(images, labels, ckpt_path, dropout=False): """ This function contains the loop that actually trains the model. :param images: a numpy array with the input data :param labels: a numpy array with the output labels :param ckpt_path: a path (including name) where model checkpoints are saved :param dropout: Boolean, whether to use dropout or not :return: True if everything went well """ # Check training data assert len(images) == len(labels) assert images.dtype == np.float32 assert labels.dtype == np.int32 # Set default TF graph with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Declare data placeholder train_data_node = _input_placeholder() # Create a placeholder to hold labels train_labels_shape = (FLAGS.batch_size,) train_labels_node = tf.placeholder(tf.int32, shape=train_labels_shape) print("Done Initializing Training Placeholders") # Build a Graph that computes the logits predictions from the placeholder if FLAGS.deeper: logits = inference_deeper(train_data_node, dropout=dropout) else: logits = inference(train_data_node, dropout=dropout) # Calculate loss loss = loss_fun(logits, train_labels_node) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = train_op_fun(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.global_variables()) print("Graph constructed and saver created") # Build an initialization operation to run below. init = tf.global_variables_initializer() # Create and init sessions sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement)) #NOLINT(long-line) sess.run(init) print("Session ready, beginning training loop") # Initialize the number of batches data_length = len(images) nb_batches = math.ceil(data_length / FLAGS.batch_size) for step in xrange(FLAGS.max_steps): # for debug, save start time start_time = time.time() # Current batch number batch_nb = step % nb_batches # Current batch start and end indices start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size) # Prepare dictionnary to feed the session with feed_dict = {train_data_node: images[start:end], train_labels_node: labels[start:end]} # Run training step _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) # Compute duration of training step duration = time.time() - start_time # Sanity check assert not np.isnan(loss_value), 'Model diverged with loss = NaN' # Echo loss once in a while if step % 100 == 0: num_examples_per_step = FLAGS.batch_size examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print (format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch)) # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, ckpt_path, global_step=step) return True
def train(images, labels, ckpt_path, dropout=False): """ This function contains the loop that actually trains the model. :param images: a numpy array with the input data :param labels: a numpy array with the output labels :param ckpt_path: a path (including name) where model checkpoints are saved :param dropout: Boolean, whether to use dropout or not :return: True if everything went well """ # Check training data assert len(images) == len(labels) assert images.dtype == np.float32 assert labels.dtype == np.int32 # Set default TF graph with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Declare data placeholder train_data_node = _input_placeholder() # Create a placeholder to hold labels train_labels_shape = (FLAGS.batch_size, ) train_labels_node = tf.placeholder(tf.int32, shape=train_labels_shape) print("Done Initializing Training Placeholders") # Build a Graph that computes the logits predictions from the placeholder if FLAGS.deeper: logits = inference_deeper(train_data_node, dropout=dropout) else: logits = inference(train_data_node, dropout=dropout) # Calculate loss loss = loss_fun(logits, train_labels_node) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = train_op_fun(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.all_variables()) print("Graph constructed and saver created") # Build an initialization operation to run below. init = tf.initialize_all_variables() # Create and init sessions sess = tf.Session( config=tf.ConfigProto(log_device_placement=FLAGS. log_device_placement)) #NOLINT(long-line) sess.run(init) print("Session ready, beginning training loop") # Initialize the number of batches data_length = len(images) nb_batches = math.ceil(data_length / FLAGS.batch_size) for step in xrange(FLAGS.max_steps): # for debug, save start time start_time = time.time() # Current batch number batch_nb = step % nb_batches # Current batch start and end indices start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size) # Prepare dictionnary to feed the session with feed_dict = { train_data_node: images[start:end], train_labels_node: labels[start:end] } # Run training step _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) # Compute duration of training step duration = time.time() - start_time # Sanity check assert not np.isnan(loss_value), 'Model diverged with loss = NaN' # Echo loss once in a while if step % 100 == 0: num_examples_per_step = FLAGS.batch_size examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) format_str = ( '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch)) # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, ckpt_path, global_step=step) return True
def softmax_preds(images, ckpt_path, return_logits=False): """ Compute softmax activations (probabilities) with the model saved in the path specified as an argument :param images: a np array of images :param ckpt_path: a TF model checkpoint :param logits: if set to True, return logits instead of probabilities :return: probabilities (or logits if logits is set to True) """ # Compute nb samples and deduce nb of batches 计算nb样本,并推导出批量nb data_length = len(images) nb_batches = math.ceil(len(images) / FLAGS.batch_size) # Declare data placeholder 申报数据占位符 train_data_node = _input_placeholder() # Build a Graph that computes the logits predictions from the placeholder 建立一个图表,计算出占位符的预测 if FLAGS.deeper: logits = inference_deeper(train_data_node) else: logits = inference(train_data_node) #构建CNN模型 if return_logits: # We are returning the logits directly (no need to apply softmax) output = logits else: # Add softmax predictions to graph: will return probabilities #添加softmax预测图:将返回概率 output = tf.nn.softmax( logits ) # tf.nn.softmax函数默认(dim=-1)是对张量最后一维的shape=(p,)向量进行softmax计算,得到一个概率向量。 #滑动平均模型 # Restore the moving average version of the learned variables for eval. 为eval恢复学习变量的移动平均版本。 variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY ) # 定义一个滑动平均的类(class)。初始化时给定了衰减率(0.99)和控制衰减率的变量step。 variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # 在所有可训练的变量上使用滑动平均 # Will hold the result preds = np.zeros((data_length, FLAGS.nb_labels), dtype=np.float32) #返回给定形状和类型的新数组,填充零。 # Create TF session with tf.Session() as sess: #建立会话 # Restore TF session from checkpoint file saver.restore(sess, ckpt_path) #测试阶段使用saver.restore()方法恢复变量 # Parse data by batch解析数据批处理 for batch_nb in xrange(0, int(nb_batches + 1)): # Compute batch start and end indices 计算批量开始和结束索引 start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size) # Prepare feed dictionary feed_dict = {train_data_node: images[start:end]} # Run session ([0] because run returns a batch with len 1st dim == 1) preds[start:end, :] = sess.run([output], feed_dict=feed_dict)[0] # Reset graph to allow multiple calls tf.reset_default_graph() #清除默认图的堆栈,并设置全局图为默认图 return preds
def train(images, labels, ckpt_path, dropout=False): """ This function contains the loop that actually trains the model.#这个函数包含实际训练模型的循环 :param images: a numpy array with the input data #一个带有输入数据的numpy数组 :param labels: a numpy array with the output labels #一个带有输出标签的numpy数组 :param ckpt_path: a path (including name) where model checkpoints are saved #保存模型检查点的路径(包括名称) :param dropout: Boolean, whether to use dropout or not :return: True if everything went well """ # Check training data assert len(images) == len(labels) assert images.dtype == np.float32 assert labels.dtype == np.int32 # Set default TF graph 设置默认TF图 with tf.Graph().as_default(): #返回一个上下文管理器,使此图形成为默认图形。 global_step = tf.Variable(0, trainable=False) # 迭代的计数器 # Declare data placeholder 申报数据占位符 train_data_node = _input_placeholder() # Create a placeholder to hold labels 创建一个占位符来保存标签 train_labels_shape = (FLAGS.batch_size, ) train_labels_node = tf.placeholder( tf.int32, shape=train_labels_shape) #TensorFlow提供了一个占位符操作,必须用数据执行。 #在训练循环(training loop)的后续步骤中,传入的整个图像和标签数据集会被切片,以符合 # 每一个操作所设置的batch_size值,占位符操作将会填补以符合这个batch_size值。然后使用feed_dict参数,将数据传入sess.run()函数。 print("Done Initializing Training Placeholders") #完成初始化培训占位符 #1.inference() —— 尽可能地构建好图表,满足促使神经网络向前反馈并做出预测的要求。 #inference()函数会尽可能地构建图表,做到返回包含了预测结果(output prediction)的Tensor。 #2.loss() —— 往inference图表中添加生成损失(loss)所需要的操作(ops)。 #3.training() —— 往损失图表中添加计算并应用梯度(gradients)所需的操作。 # Build a Graph that computes the logits predictions from the placeholder构建一个图形,它计算来自占位符的逻辑预测 if FLAGS.deeper: logits = inference_deeper(train_data_node, dropout=dropout) else: logits = inference(train_data_node, dropout=dropout) #构建CNN模型 # Calculate loss计算损失 loss = loss_fun(logits, train_labels_node) # Build a Graph that trains the model with one batch of examples and # updates the model parameters.构建一个图表,用一批示例来训练模型,并更新模型参数。 train_op = train_op_fun(loss, global_step) #梯度下降算法优化+滑动平均模型+直方图 # Create a saver.保存和恢复变量,最简单的保存和恢复模型的方法是使用tf.train.Saver 对象 saver = tf.train.Saver( tf.global_variables()) #tf.global_variables都是获取程序中的变量,返回的值是变量的一个列表 print("Graph constructed and saver created") #创建的图和保护程序 # Build an initialization operation to run below.构建一个初始化操作,以在下面运行 init = tf.global_variables_initializer() # Create and init sessions sess = tf.Session( config=tf.ConfigProto(log_device_placement=FLAGS. log_device_placement)) #NOLINT(long-line) sess.run(init) print("Session ready, beginning training loop") #准备好,开始训练循环 # Initialize the number of batches data_length = len(images) nb_batches = math.ceil(data_length / FLAGS.batch_size) for step in xrange(FLAGS.max_steps): # for debug, save start time start_time = time.time() # Current batch number batch_nb = step % nb_batches # Current batch start and end indices start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size) #计算一个批开始和结束索引 # Prepare dictionnary to feed the session with feed_dict = { train_data_node: images[start:end], train_labels_node: labels[start:end] } # Run training step _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) # Compute duration of training step duration = time.time() - start_time # Sanity check assert not np.isnan(loss_value), 'Model diverged with loss = NaN' # Echo loss once in a while if step % 100 == 0: num_examples_per_step = FLAGS.batch_size examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) format_str = ( '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch)) # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == FLAGS.max_steps: saver.save(sess, ckpt_path, global_step=step) return True