def Alex_net(image, reuse=tf.AUTO_REUSE, keep_prop=0.5): image = tf.reshape(image, [-1, 224, 224, 3]) with tf.variable_scope(name_or_scope='Alex', reuse=reuse): arg_scope = alexnet.alexnet_v2_arg_scope() with slim.arg_scope(arg_scope): logits, end_point = alexnet.alexnet_v2(image, 1000, is_training=True, dropout_keep_prob=keep_prop) probs = tf.nn.softmax(logits) # probabilities return logits, probs, end_point
def build_train_op(image_tensor, label_tensor, is_training): alexnet_argscope = alexnet_v2_arg_scope(weight_decay=FLAGS.weight_decay) global_step = tf.get_variable(name="global_step", shape=[], dtype=tf.int32, trainable=False) with slim.arg_scope(alexnet_argscope): logits, end_points = alexnet_v2(image_tensor, is_training=is_training, num_classes=100) loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label_tensor)) accuracy = tf.reduce_sum(tf.cast(tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), label_tensor),tf.int32)) end_points['loss'], end_points['accuracy'] = loss, accuracy if is_training: optimizer = tf.train.AdadeltaOptimizer(learning_rate=FLAGS.learning_rate) train_op = optimizer.minimize(loss, global_step=global_step) return train_op, end_points else: return None, end_points
def predict(models_path, image_dir, labels_nums, data_format): [batch_size, resize_height, resize_width, depths] = data_format # labels = np.loadtxt(labels_filename, str, delimiter='\t') input_images = tf.placeholder( dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input') # Define the model: with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): out, end_points = alexnet.alexnet_v2(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=1.0, is_training=False) # 将输出结果进行softmax分布,再求最大概率所属类别 sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, models_path) images_list = glob.glob(os.path.join(image_dir, '*.jpg')) for image_path in images_list: im = read_image(image_path, resize_height, resize_width, normalization=True) im = im[np.newaxis, :] #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0}) pre_score = sess.run([out], feed_dict={input_images: im}) mean_std = statistic.get_score(pre_score, type="mean_std") # print("image_path:{},pre_score:{},mean_std:{}".format(image_path,pre_score,mean_std)) print("image_path:{},mean_std:{}".format(image_path, mean_std)) sess.close()
def classify_image(filepath): with tf.Graph().as_default(): image = open(filepath, 'rb') # Open specified url and load image as a string image_string = image.read() # Decode string into matrix with intensity values image = tf.image.decode_jpeg(image_string, channels=3) # Resize the input image, preserving the aspect ratio # and make a central crop of the resulted image. # The crop will be of the size of the default image size of # the network. processed_image = vgg_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) # Networks accept images in batches. # The first dimension usually represents the batch size. # In our case the batch size is one. processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure # the batch norm parameters. arg_scope is a very convenient # feature of slim library -- you can define default # parameters for layers -- like stride, padding etc. with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): logits, _ = alexnet.alexnet_v2(processed_images, num_classes=6, is_training=False) # In order to get probabilities we apply softmax on the output. probabilities = tf.nn.softmax(logits) # Create a function that reads the network weights # from the checkpoint file that you downloaded. # We will run it in session later. init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'model.ckpt-100000'), slim.get_model_variables('alexnet_v2')) with tf.Session() as sess: # Load weights init_fn(sess) # We want to get predictions, image as numpy matrix # and resized and cropped piece that is actually # being fed to the network. np_image, network_input, probabilities = sess.run( [image, processed_image, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [ i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1]) ] for i in range(6): index = sorted_inds[i] print('Probability %0.2f => [%s]' % (probabilities[index], names[index])) return sorted_inds[0], probabilities
def train(train_filename, train_images_dir, train_log_step, train_param, val_filename, val_images_dir, val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix): ''' :param train_record_file: 训练的tfrecord文件 :param train_log_step: 显示训练过程log信息间隔 :param train_param: train参数 :param val_record_file: 验证的tfrecord文件 :param val_log_step: 显示验证过程log信息间隔 :param val_param: val参数 :param labels_nums: labels数 :param data_shape: 输入数据shape :param snapshot: 保存模型间隔 :param snapshot_prefix: 保存模型文件的前缀名 :return: ''' [base_lr, max_steps] = train_param [batch_size, resize_height, resize_width, depths] = data_shape # # 从record中读取图片和labels数据 tf_image, tf_labels = read_images(train_filename, train_images_dir, data_shape, shuffle=True, type='normalization') train_images_batch, train_labels_batch = get_batch_images( tf_image, tf_labels, batch_size=batch_size, labels_nums=labels_nums, one_hot=False, shuffle=True) # Define the model: with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): out, end_points = alexnet.alexnet_v2(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=keep_prob, is_training=is_training) loss = tf.reduce_sum(tf.squared_difference(x=out, y=input_labels)) # loss1=tf.squared_difference(x=out,y=input_labels) # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y)) train_op = tf.train.AdamOptimizer(learning_rate=base_lr).minimize(loss) # tf.losses.add_loss(loss1) # # slim.losses.add_loss(my_loss) # loss = tf.losses.get_total_loss(add_regularization_losses=True) # 添加正则化损失loss=2.2 # # Specify the optimization scheme: # optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr) # # create_train_op that ensures that when we evaluate it to get the loss, # # the update_ops are done and the gradient updates are computed. # train_op = slim.learning.create_train_op(total_loss=loss, optimizer=optimizer) saver = tf.train.Saver() max_acc = 0.0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(max_steps + 1): batch_input_images, batch_input_labels = sess.run( [train_images_batch, train_labels_batch]) _, train_loss = sess.run( [train_op, loss], feed_dict={ input_images: batch_input_images, input_labels: batch_input_labels, keep_prob: 0.5, is_training: True }) if i % train_log_step == 0: print("%s: Step [%d] train Loss : %f" % (datetime.now(), i, train_loss)) # # train测试(这里仅测试训练集的一个batch) # if i%train_log_step == 0: # train_acc = sess.run(accuracy, feed_dict={input_images:batch_input_images, # input_labels: batch_input_labels, # keep_prob:1.0, is_training: False}) # print "%s: Step [%d] train Loss : %f, training accuracy : %g" % (datetime.now(), i, train_loss, train_acc) # # # val测试(测试全部val数据) # if i%val_log_step == 0: # _, train_loss = sess.run([train_step, loss], feed_dict={input_images: batch_input_images, # input_labels: batch_input_labels, # keep_prob: 1.0, is_training: False}) # print "%s: Step [%d] val Loss : %f, val accuracy : %g" % (datetime.now(), i, mean_loss, mean_acc) # # 模型保存:每迭代snapshot次或者最后一次保存模型 if (i % snapshot == 0 and i > 0) or i == max_steps: print('-----save:{}-{}'.format(snapshot_prefix, i)) saver.save(sess, snapshot_prefix, global_step=i) # # 保存val准确率最高的模型 # if mean_acc>max_acc and mean_acc>0.5: # max_acc=mean_acc # path = os.path.dirname(snapshot_prefix) # best_models=os.path.join(path,'best_models_{}_{:.4f}.ckpt'.format(i,max_acc)) # print('------save:{}'.format(best_models)) # saver.save(sess, best_models) coord.request_stop() coord.join(threads)
return dataset, images, labels graph = tf.Graph() with graph.as_default(): tf.logging.set_verbosity(tf.logging.INFO) dataset, images, labels = getImageBatchAndOneHotLabels( dataset_dir, 'train', num_readers, num_preprocessing_threads, batch_size) _, images_val, labels_val = getImageBatchAndOneHotLabels( dataset_dir, 'validation', 2, 2, batch_size) # Create Model network and endpoints with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): logits, _ = alexnet.alexnet_v2(images, num_classes=dataset.num_classes) with tf.variable_scope(tf.get_variable_scope(), reuse=True): logits_val, _ = alexnet.alexnet_v2(images_val, num_classes=dataset.num_classes) #Metrics accuracy_validation = slim.metrics.accuracy( tf.to_int32(tf.argmax(logits_val, 1)), tf.to_int32(tf.argmax(labels_val, 1))) top5_accuracy = tf.metrics.mean( tf.nn.in_top_k(logits_val, tf.to_int32(tf.argmax(labels_val, 1)), k=5)) # Added Loss Function tf.losses.softmax_cross_entropy(labels, logits)
def train(run_dir, master, task_id, num_readers, from_graspnet_checkpoint, dataset_dir, checkpoint_dir, save_summaries_steps, save_interval_secs, num_preprocessing_threads, num_steps, hparams, scope='graspnet'): for path in [run_dir]: if not tf.gfile.Exists(path): tf.gfile.Makedirs(path) hparams_filename = os.path.join(run_dir, 'hparams.json') with tf.gfile.FastGFile(hparams_filename, 'w') as f: f.write(hparams.to_json()) with tf.Graph().as_default(): with tf.device(tf.train.replica_device_setter(task_id)): global_step = slim.get_or_create_global_step() images, class_labels, theta_labels = get_dataset( dataset_dir, num_readers, num_preprocessing_threads, hparams) ''' with slim.arg_scope(vgg.vgg_arg_scope()): net, end_points = vgg.vgg_16(inputs=images, num_classes=num_classes, is_training=True, dropout_keep_prob=0.7, scope=scope) ''' with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): net, end_points = alexnet.alexnet_v2(inputs=images, num_classes=num_classes, is_training=True, dropout_keep_prob=0.7, scope=scope) loss, accuracy = create_loss(net, class_labels, theta_labels) learning_rate = hparams.learning_rate if hparams.lr_decay_step: learning_rate = tf.train.exponential_decay( hparams.learning_rate, slim.get_or_create_global_step(), decay_steps=hparams.lr_decay_step, decay_rate=hparams.lr_decay_rate, staircase=True) tf.summary.scalar('Learning_rate', learning_rate) optimizer = tf.train.GradientDescentOptimizer(learning_rate) train_op = slim.learning.create_train_op(loss, optimizer) add_summary(images, end_points, loss, accuracy, scope=scope) summary_op = tf.summary.merge_all() variable_map = restore_map( from_graspnet_checkpoint=from_graspnet_checkpoint, scope=scope, model_name=hparams.model_name, checkpoint_exclude_scope='fc8') init_saver = tf.train.Saver(variable_map) def initializer_fn(sess): init_saver.restore(sess, checkpoint_dir) tf.logging.info('Successfully load pretrained checkpoint.') init_fn = initializer_fn session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) session_config.gpu_options.allow_growth = True saver = tf.train.Saver( keep_checkpoint_every_n_hours=save_interval_secs, max_to_keep=100) slim.learning.train( train_op, logdir=run_dir, master=master, global_step=global_step, session_config=session_config, # init_fn=init_fn, summary_op=summary_op, number_of_steps=num_steps, startup_delay_steps=15, save_summaries_secs=save_summaries_steps, saver=saver)