def testModelVariables(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) alexnet.alexnet_v2(inputs, num_classes) expected_names = ['alexnet_v2/conv1/weights', 'alexnet_v2/conv1/biases', 'alexnet_v2/conv2/weights', 'alexnet_v2/conv2/biases', 'alexnet_v2/conv3/weights', 'alexnet_v2/conv3/biases', 'alexnet_v2/conv4/weights', 'alexnet_v2/conv4/biases', 'alexnet_v2/conv5/weights', 'alexnet_v2/conv5/biases', 'alexnet_v2/fc6/weights', 'alexnet_v2/fc6/biases', 'alexnet_v2/fc7/weights', 'alexnet_v2/fc7/biases', 'alexnet_v2/fc8/weights', 'alexnet_v2/fc8/biases', ] model_variables = [v.op.name for v in slim.get_model_variables()] self.assertSetEqual(set(model_variables), set(expected_names))
def testModelVariables(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random.uniform((batch_size, height, width, 3)) alexnet.alexnet_v2(inputs, num_classes) expected_names = ['alexnet_v2/conv1/weights', 'alexnet_v2/conv1/biases', 'alexnet_v2/conv2/weights', 'alexnet_v2/conv2/biases', 'alexnet_v2/conv3/weights', 'alexnet_v2/conv3/biases', 'alexnet_v2/conv4/weights', 'alexnet_v2/conv4/biases', 'alexnet_v2/conv5/weights', 'alexnet_v2/conv5/biases', 'alexnet_v2/fc6/weights', 'alexnet_v2/fc6/biases', 'alexnet_v2/fc7/weights', 'alexnet_v2/fc7/biases', 'alexnet_v2/fc8/weights', 'alexnet_v2/fc8/biases', ] model_variables = [v.op.name for v in slim.get_model_variables()] self.assertSetEqual(set(model_variables), set(expected_names))
def testForward(self): batch_size = 1 height, width = 224, 224 with self.test_session() as sess: inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs) sess.run(tf.global_variables_initializer()) output = sess.run(logits) self.assertTrue(output.any())
def testForward(self): batch_size = 1 height, width = 224, 224 with self.test_session() as sess: inputs = tf.random.uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs) sess.run(tf.compat.v1.global_variables_initializer()) output = sess.run(logits) self.assertTrue(output.any())
def choose_model(x, model): """ 选择模型 :param x: :param model: :return: """ # 模型保存路径,模型名,预训练文件路径,前向传播 if model == 'Alex': log_dir = "E:/alum/log/Alex" y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_dir = "E:/alum/log/VGG" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'VGG2': log_dir = "E:/alum/log/VGG2" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'Incep4': log_dir = "E:/alum/log/Incep4" y, _ = inception_v4.inception_v4(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, reuse=None, scope='InceptionV4', create_aux_logits=True) elif model == 'Res': log_dir = "E:/alum/log/Res" y, _ = resnet_v2.resnet_v2_50(x, num_classes=CLASSES, is_training=True, global_pool=GLOBAL_POOL, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') else: print('Error: model name not exist') return return y, log_dir
def testFullyConvolutional(self): batch_size = 1 height, width = 300, 400 num_classes = 1000 with self.test_session(): inputs = tf.random.uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs, num_classes, spatial_squeeze=False) self.assertEquals(logits.op.name, 'alexnet_v2/fc8/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, 4, 7, num_classes])
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs, num_classes) self.assertEquals(logits.op.name, 'alexnet_v2/fc8/squeezed') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def testFullyConvolutional(self): batch_size = 1 height, width = 300, 400 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs, num_classes, spatial_squeeze=False) self.assertEquals(logits.op.name, 'alexnet_v2/fc8/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, 4, 7, num_classes])
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random.uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs, num_classes) self.assertEquals(logits.op.name, 'alexnet_v2/fc8/squeezed') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def predict(root_path, model='Alex'): # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 1]) # 模型保存路径,前向传播 if model == 'Alex': log_path = "../log/Alex" y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool else: log_path = '../log/My' y, _ = lenet5(x) saver = tf.train.Saver() with tf.Session() as sess: # 恢复模型权重 print('Reading checkpoints: ', model) ckpt = tf.train.get_checkpoint_state(log_path) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('Error: no checkpoint file found') return file_list = os.listdir(root_path) for file_name in file_list: if file_name.split('.')[-1] == 'jpg': img = Image.open(os.path.join(root_path, file_name)) img_show = img.resize((SHOW_SIZE, SHOW_SIZE)) img_show = np.array(img_show, np.uint8) img = img.resize((IMG_SIZE, IMG_SIZE)) img = np.array(img, np.uint8) img = np.expand_dims(img, axis=0) img = np.expand_dims(img, axis=3) pre = np.squeeze(sess.run(y, feed_dict={x: img})) print('predict:', pre) points = [[pre[0] * SHOW_SIZE, pre[1] * SHOW_SIZE], [pre[2] * SHOW_SIZE, pre[3] * SHOW_SIZE], [pre[4] * SHOW_SIZE, pre[5] * SHOW_SIZE], [pre[6] * SHOW_SIZE, pre[7] * SHOW_SIZE]] points = np.int0(points) cv2.polylines(img_show, [points], True, (0, 0, 255), 1) cv2.imshow('img_show', img_show) cv2.waitKey() cv2.destroyAllWindows()
def testGlobalPool(self): batch_size = 1 height, width = 256, 256 num_classes = 1000 with self.test_session(): inputs = tf.random.uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(inputs, num_classes, spatial_squeeze=False, global_pool=True) self.assertEquals(logits.op.name, 'alexnet_v2/fc8/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, 1, 1, num_classes])
def testEvaluation(self): batch_size = 2 height, width = 224, 224 num_classes = 1000 with self.test_session(): eval_inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(eval_inputs, is_training=False) self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes]) predictions = tf.argmax(logits, 1) self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
def testEvaluation(self): batch_size = 2 height, width = 224, 224 num_classes = 1000 with self.test_session(): eval_inputs = tf.random.uniform((batch_size, height, width, 3)) logits, _ = alexnet.alexnet_v2(eval_inputs, is_training=False) self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes]) predictions = tf.argmax(input=logits, axis=1) self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
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 testTrainEvalWithReuse(self): train_batch_size = 2 eval_batch_size = 1 train_height, train_width = 224, 224 eval_height, eval_width = 300, 400 num_classes = 1000 with self.test_session(): train_inputs = tf.random_uniform( (train_batch_size, train_height, train_width, 3)) logits, _ = alexnet.alexnet_v2(train_inputs) self.assertListEqual(logits.get_shape().as_list(), [train_batch_size, num_classes]) tf.get_variable_scope().reuse_variables() eval_inputs = tf.random_uniform( (eval_batch_size, eval_height, eval_width, 3)) logits, _ = alexnet.alexnet_v2(eval_inputs, is_training=False, spatial_squeeze=False) self.assertListEqual(logits.get_shape().as_list(), [eval_batch_size, 4, 7, num_classes]) logits = tf.reduce_mean(logits, [1, 2]) predictions = tf.argmax(logits, 1) self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def testTrainEvalWithReuse(self): train_batch_size = 2 eval_batch_size = 1 train_height, train_width = 224, 224 eval_height, eval_width = 300, 400 num_classes = 1000 with self.test_session(): train_inputs = tf.random.uniform( (train_batch_size, train_height, train_width, 3)) logits, _ = alexnet.alexnet_v2(train_inputs) self.assertListEqual(logits.get_shape().as_list(), [train_batch_size, num_classes]) tf.compat.v1.get_variable_scope().reuse_variables() eval_inputs = tf.random.uniform( (eval_batch_size, eval_height, eval_width, 3)) logits, _ = alexnet.alexnet_v2(eval_inputs, is_training=False, spatial_squeeze=False) self.assertListEqual(logits.get_shape().as_list(), [eval_batch_size, 4, 7, num_classes]) logits = tf.reduce_mean(input_tensor=logits, axis=[1, 2]) predictions = tf.argmax(input=logits, axis=1) self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = alexnet.alexnet_v2(inputs, num_classes) expected_names = [ 'alexnet_v2/conv1', 'alexnet_v2/pool1', 'alexnet_v2/conv2', 'alexnet_v2/pool2', 'alexnet_v2/conv3', 'alexnet_v2/conv4', 'alexnet_v2/conv5', 'alexnet_v2/pool5', 'alexnet_v2/fc6', 'alexnet_v2/fc7', 'alexnet_v2/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
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 main(): """ You can also run these commands manually to generate the pb file 1. git clone https://github.com/tensorflow/models.git 2. export PYTHONPATH=Path_to_your_model_folder 3. python alexnet.py """ height, width = 224, 224 inputs = tf.Variable(tf.random_uniform((1, height, width, 3)), name = 'input') net, end_points = alexnet.alexnet_v2(inputs, is_training=False) print("nodes in the graph") for n in end_points: print(n + " => " + str(end_points[n])) net_outputs = map(lambda x: tf.get_default_graph().get_tensor_by_name(x), argv[2].split(',')) run_model(net_outputs, argv[1], 'alexnet', argv[3] == 'True')
def load_model(model='Mobile'): global x, y, sess # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 1]) # 模型保存路径,前向传播 if model == 'Alex': log_path = 'weight/Alex' y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=False, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'Mobile': log_path = 'weight/Mobile' y, _ = mobilenet_v1.mobilenet_v1(x, num_classes=CLASSES, dropout_keep_prob=1.0, is_training=False, min_depth=8, depth_multiplier=1.0, conv_defs=None, prediction_fn=None, spatial_squeeze=True, reuse=None, scope='MobilenetV1', global_pool=GLOBAL_POOL) else: print('Error: model name not exist') return y = tf.nn.softmax(y) saver = tf.train.Saver() sess = tf.Session() # 恢复模型权重 # print('Reading checkpoints from: ', model) ckpt = tf.train.get_checkpoint_state(log_path) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) # print('Loading success, global_step is %s' % global_step) else: print('Error: no checkpoint file found') return -1
def testNoClasses(self): batch_size = 5 height, width = 224, 224 num_classes = None with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) net, end_points = alexnet.alexnet_v2(inputs, num_classes) expected_names = [ 'alexnet_v2/conv1', 'alexnet_v2/pool1', 'alexnet_v2/conv2', 'alexnet_v2/pool2', 'alexnet_v2/conv3', 'alexnet_v2/conv4', 'alexnet_v2/conv5', 'alexnet_v2/pool5', 'alexnet_v2/fc6', 'alexnet_v2/fc7' ] self.assertSetEqual(set(end_points.keys()), set(expected_names)) self.assertTrue(net.op.name.startswith('alexnet_v2/fc7')) self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 4096])
def main(): """ You can also run these commands manually to generate the pb file 1. git clone https://github.com/tensorflow/models.git 2. export PYTHONPATH=Path_to_your_model_folder 3. python alexnet.py """ tf.set_random_seed(1) height, width = 224, 224 inputs = tf.Variable(tf.random_uniform((1, height, width, 3)), name = 'input') inputs = tf.identity(inputs, "input_node") net, end_points = alexnet.alexnet_v2(inputs, is_training=False) print("nodes in the graph") for n in end_points: print(n + " => " + str(end_points[n])) net_outputs = map(lambda x: tf.get_default_graph().get_tensor_by_name(x), argv[2].split(',')) run_model(net_outputs, argv[1], 'alexnet', argv[3] == 'True')
def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = alexnet.alexnet_v2(inputs, num_classes) expected_names = ['alexnet_v2/conv1', 'alexnet_v2/pool1', 'alexnet_v2/conv2', 'alexnet_v2/pool2', 'alexnet_v2/conv3', 'alexnet_v2/conv4', 'alexnet_v2/conv5', 'alexnet_v2/pool5', 'alexnet_v2/fc6', 'alexnet_v2/fc7', 'alexnet_v2/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
def load_model(model='Alex'): global x, y, sess # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 3]) # 模型保存路径,前向传播 if model == 'Alex': log_path = 'weight/Alex' y, _ = alexnet.alexnet_v2(x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_path = 'weight/VGG' y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) else: print('Error: model name not exist') return y = tf.nn.softmax(y) saver = tf.train.Saver() sess = tf.Session() # 恢复模型权重 print('Reading checkpoints from: ', model) ckpt = tf.train.get_checkpoint_state(log_path) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('Error: no checkpoint file found') return -1
def testNoClasses(self): batch_size = 5 height, width = 224, 224 num_classes = None with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) net, end_points = alexnet.alexnet_v2(inputs, num_classes) expected_names = ['alexnet_v2/conv1', 'alexnet_v2/pool1', 'alexnet_v2/conv2', 'alexnet_v2/pool2', 'alexnet_v2/conv3', 'alexnet_v2/conv4', 'alexnet_v2/conv5', 'alexnet_v2/pool5', 'alexnet_v2/fc6', 'alexnet_v2/fc7' ] self.assertSetEqual(set(end_points.keys()), set(expected_names)) self.assertTrue(net.op.name.startswith('alexnet_v2/fc7')) self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 4096])
def model_fn(features,labels,mode,params): global_step = tf.train.get_global_step() inputs = features['MRI'] logits, end_points= alexnet.alexnet_v2(inputs=inputs, num_classes=30) # predict mode predicted_classes = tf.argmax(logits, 1) if mode == tf.estimator.ModeKeys.PREDICT: predictions = { 'class':predicted_classes, 'prob':tf.nn.softmax(logits) } return tf.estimator.EstimatorSpec(mode,predictions=predictions) with tf.name_scope('accuracy'): accuracy = tf.metrics.accuracy(labels=labels,predictions=predicted_classes) my_acc = tf.reduce_mean(tf.cast(tf.equal(labels, predicted_classes), tf.float32)) tf.summary.scalar('accuracy',my_acc) # compute loss loss = tf.losses.sparse_softmax_cross_entropy(labels=labels,logits=logits) # hook train_hook_list = [] train_tensors_log = {'accuracy':accuracy[1], 'my_acc': my_acc, 'loss':loss, 'global_step':global_step} train_hook_list.append(tf.train.LoggingTensorHook(tensors=train_tensors_log,every_n_iter=200)) # training op if mode == tf.estimator.ModeKeys.TRAIN: optimizer = tf.train.MomentumOptimizer(learning_rate=0.001,momentum=0.9) train_op = optimizer.minimize(loss,global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode=mode,loss=loss,train_op=train_op,training_hooks=train_hook_list) # compute evaluation metrics eval_metric_ops = { 'accuracy':tf.metrics.accuracy(labels=labels,predictions=predicted_classes) } return tf.estimator.EstimatorSpec(mode=mode,loss=loss,eval_metric_ops=eval_metric_ops)
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 predict(img_path, model='Alex'): """ 预测图片 :param img_path: 图片路径 :param model: 模型名 :return: none """ # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 3]) # 模型保存路径,前向传播 if model == 'Alex': log_path = "../log/Alex" y, _ = alexnet.alexnet_v2(x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_path = "../log/VGG" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'Incep4': log_path = "../log/Incep4" y, _ = inception_v4.inception_v4(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, reuse=None, scope='InceptionV4', create_aux_logits=True) elif model == 'Res': log_path = "../log/Res" y, _ = resnet_v2.resnet_v2_50(x, num_classes=CLASSES, is_training=True, global_pool=GLOBAL_POOL, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') else: print('Error: model name not exist') return y = tf.nn.softmax(y) saver = tf.train.Saver() with tf.Session() as sess: # 恢复模型权重 print('Reading checkpoints: ', model) ckpt = tf.train.get_checkpoint_state(log_path) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('Error: no checkpoint file found') return img = read_img(img_path) # 第一次运行时间会较长 sess.run(y, feed_dict={x: img}) start_time = time.clock() predictions = sess.run(y, feed_dict={x: img}) pre = np.argmax(predictions) end_time = time.clock() runtime = end_time - start_time print('prediction is:', predictions) print('predict class is:', pre) print('run time:', runtime)
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
# tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_0_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 25, current size 0) #定义网络结构 #调用修改过的alexnet_v2 # train_network_fn = nets_factory.get_network_fn( # 'alexnet_v2', # num_classes=CHAR_SET_LEN, # weight_decay=0.0005, # is_training=True) # train_network_fn = alexnet.alexnet_v2(num_classes=CHAR_SET_LEN,is_training=True) with tf.Session() as sess: # inputs: a tensor of size [batch_size, height, width, channels] X = tf.reshape(x, [BATCH_SIZE, 224, 224, 1]) # 数据输入网络得到输出值 logits0,logits1,logits2,logits3,end_points = alexnet.alexnet_v2(inputs=X,num_classes=CHAR_SET_LEN,is_training=True) #train_network_fn(X) # 把标签转成one_hot的形式 one_hot_labels0 = tf.one_hot(indices=tf.cast(y0, tf.int32), depth=CHAR_SET_LEN) one_hot_labels1 = tf.one_hot(indices=tf.cast(y1, tf.int32), depth=CHAR_SET_LEN) one_hot_labels2 = tf.one_hot(indices=tf.cast(y2, tf.int32), depth=CHAR_SET_LEN) one_hot_labels3 = tf.one_hot(indices=tf.cast(y3, tf.int32), depth=CHAR_SET_LEN) # 计算loss #用sigmoid_cross_entropy_with_logits代替softmax_cross_entropy_with_logits_v2,更合理一些,这与alexnet实现有关 loss0 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits0,labels=one_hot_labels0)) loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits1,labels=one_hot_labels1)) loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits2,labels=one_hot_labels2)) loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits3,labels=one_hot_labels3)) # loss0 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits0,labels=one_hot_labels0)) # loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits1,labels=one_hot_labels1)) # loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits2,labels=one_hot_labels2)) # loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits3,labels=one_hot_labels3))
def _construct_model(model_type='resnet_v1_50'): """Constructs model for the desired type of CNN. Args: model_type: Type of model to be used. Returns: end_points: A dictionary from components of the network to the corresponding activations. Raises: ValueError: If the model_type is not supported. """ # Placeholder input. images = array_ops.placeholder( dtypes.float32, shape=(1, None, None, 3), name=_INPUT_NODE) # Construct model. if model_type == 'inception_resnet_v2': _, end_points = inception.inception_resnet_v2_base(images) elif model_type == 'inception_resnet_v2-same': _, end_points = inception.inception_resnet_v2_base( images, align_feature_maps=True) elif model_type == 'inception_v2': _, end_points = inception.inception_v2_base(images) elif model_type == 'inception_v2-no-separable-conv': _, end_points = inception.inception_v2_base( images, use_separable_conv=False) elif model_type == 'inception_v3': _, end_points = inception.inception_v3_base(images) elif model_type == 'inception_v4': _, end_points = inception.inception_v4_base(images) elif model_type == 'alexnet_v2': _, end_points = alexnet.alexnet_v2(images) elif model_type == 'vgg_a': _, end_points = vgg.vgg_a(images) elif model_type == 'vgg_16': _, end_points = vgg.vgg_16(images) elif model_type == 'mobilenet_v1': _, end_points = mobilenet_v1.mobilenet_v1_base(images) elif model_type == 'mobilenet_v1_075': _, end_points = mobilenet_v1.mobilenet_v1_base( images, depth_multiplier=0.75) elif model_type == 'resnet_v1_50': _, end_points = resnet_v1.resnet_v1_50( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_101': _, end_points = resnet_v1.resnet_v1_101( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_152': _, end_points = resnet_v1.resnet_v1_152( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_200': _, end_points = resnet_v1.resnet_v1_200( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_50': _, end_points = resnet_v2.resnet_v2_50( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_101': _, end_points = resnet_v2.resnet_v2_101( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_152': _, end_points = resnet_v2.resnet_v2_152( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_200': _, end_points = resnet_v2.resnet_v2_200( images, num_classes=None, is_training=False, global_pool=False) else: raise ValueError('Unsupported model_type %s.' % model_type) return end_points
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) total_loss = slim.losses.get_total_loss()
checkpoints_dir = './network1/model.ckpt-4832' path_unnormal = './CROP_IMG/unnormal/' path_normal = './CROP_IMG/normal/' image_names = os.listdir(path_unnormal) image_names.sort() tf.reset_default_graph() images = tf.placeholder(tf.float32, [1, 224, 224, 3], name='input_images') with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): logits, end_points1 = alexnet.alexnet_v2(images, num_classes=2, is_training=False) probabilities1 = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn(checkpoints_dir, slim.get_variables_to_restore(), ignore_missing_vars=True) sess = tf.Session() init_fn(sess) for j, image_name in enumerate(image_names): print('unnormal:', j) image_path = os.path.join(path_unnormal, image_name)
def train(inherit=False, model='Alex'): # 加载数据集 images = np.load('../data/phone_data.npy') labels = np.load('../data/phone_label.npy') train_data, val_data, train_label, val_label = train_test_split( images, labels, test_size=0.2, random_state=222) # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 1], name='x-input') y_ = tf.placeholder(tf.float32, [None, CLASSES], name='y-input') my_global_step = tf.Variable(0, name='global_step', trainable=False, dtype=tf.int64) # 前向传播 if model == 'Alex': log_path = "../log/Alex" model_name = 'alex.ckpt' y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool else: log_path = '../log/My' model_name = 'my.ckpt' y, _ = lenet5(x) # 交叉熵、损失值、优化器、准确率 loss = tf.reduce_mean(tf.sqrt(tf.square(y_ - y) + 0.0000001)) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, my_global_step, 100, LEARNING_RATE_DECAY) train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=my_global_step) # 模型保存器、初始化 saver = tf.train.Saver() init = tf.global_variables_initializer() tf.summary.scalar("loss", loss) # tf.summary.histogram("W1", W) merged_summary_op = tf.summary.merge_all() # 训练迭代 with tf.Session() as sess: summary_writer1 = tf.summary.FileWriter('../log/curve/train', sess.graph) summary_writer2 = tf.summary.FileWriter('../log/curve/test') step = 0 if inherit: ckpt = tf.train.get_checkpoint_state(log_path) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print(model, 'continue train from %s:' % global_step) step = int(global_step) else: print('Error: no checkpoint file found') return else: print(model, 'restart train:') sess.run(init) # 迭代 while step < MAX_STEP: start_time = time.clock() image_batch, label_batch = utils.get_batch(train_data, train_label, BATCH_SIZE) # 训练,损失值和准确率 _, train_loss = sess.run([train_op, loss], feed_dict={ x: image_batch, y_: label_batch }) end_time = time.clock() runtime = end_time - start_time step += 1 # 训练信息、曲线和保存模型 if step % INFO_STEP == 0 or step == MAX_STEP: summary_str = sess.run(merged_summary_op, feed_dict={ x: image_batch, y_: label_batch }) summary_writer1.add_summary(summary_str, step) test_loss, summary_str = sess.run([loss, merged_summary_op], feed_dict={ x: val_data, y_: val_label }) summary_writer2.add_summary(summary_str, step) print( 'step: %d, runtime: %.2f, train loss: %.4f, test loss: %.4f' % (step, runtime, train_loss, test_loss)) if step % SAVE_STEP == 0: checkpoint_path = os.path.join(log_path, model_name) saver.save(sess, checkpoint_path, global_step=step)
def evaluate(model='Alex'): """ 评估模型 :param model: model name :return: none """ # 预测为某类的样本个数,某类预测正确的样本个数 pre_pos = np.zeros(CLASSES, dtype=np.uint16) true_pos = np.zeros(CLASSES, dtype=np.uint16) # 加载测试集 images = np.load('../data/data_299.npy') labels = np.load('../data/label_299.npy') _, val_data, _, val_label = train_test_split(images, labels, test_size=0.2, random_state=222) test_num = val_data.shape[0] # 存放预测结果 res = np.zeros(test_num, dtype=np.uint16) # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 3], name="x_input") y_ = tf.placeholder(tf.uint8, [None], name="y_input") # 模型保存路径,模型名,预训练文件路径,前向传播 if model == 'Alex': log_dir = "../log/Alex" y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_dir = "../log/VGG" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'Incep4': log_dir = "E:/alum/log/Incep4" y, _ = inception_v4.inception_v4(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, reuse=None, scope='InceptionV4', create_aux_logits=True) elif model == 'Res': log_dir = "E:/alum/log/Res" y, _ = resnet_v2.resnet_v2_50(x, num_classes=CLASSES, is_training=True, global_pool=GLOBAL_POOL, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') else: print('Error: model name not exist') return # 预测结果 y_pre = tf.argmax(y, 1) saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: # 恢复模型权重 print('Reading checkpoints of', model) ckpt = tf.train.get_checkpoint_state(log_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, step is %s' % global_step) else: print('Error: no checkpoint file found') return # 遍历一次测试集需要次数 num_iter = int(math.ceil(test_num / test_num)) step = 0 start = 0 while step < num_iter: # 获取一个 batch if step == num_iter - 1: end = test_num else: end = start + BATCH_SIZE image_batch = val_data[start:end] label_batch = val_label[start:end] # 准确率和预测结果统计信息 pre = sess.run(y_pre, feed_dict={x: image_batch, y_: label_batch}) pre = np.squeeze(pre) res[start:end] = pre start += BATCH_SIZE step += 1 # 计算准确率 normal_num = 0 for i in range(test_num): pre_pos[res[i]] += 1 if res[i] == val_label[i]: true_pos[res[i]] += 1 if val_label[i] == 0: normal_num += 1 precision = true_pos / pre_pos print('测试样本数:', test_num) print('预测数:', pre_pos) print('正确数:', true_pos) print('各类精度:', precision) print('准确率:', np.sum(true_pos) / test_num) print('平均准确率:', np.mean(precision)) print('漏检率:', (pre_pos[0] - true_pos[0]) / (test_num - normal_num)) print('过杀率:', (normal_num - true_pos[0]) / normal_num)
def submit(test_dir, model='VGG'): """ 测试集预测结果保存为 csv 文件 :param test_dir: 测试集根路径 :param model: 模型名称 :return: none """ # 类别名称 print('running submit') tf.reset_default_graph() class_label = [ 'norm', 'defect1', 'defect2', 'defect3', 'defect4', 'defect5', 'defect6', 'defect7', 'defect8', 'defect9', 'defect10', 'defect11' ] img_index, res = [], [] # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 3]) # 模型保存路径,前向传播 if model == 'Alex': log_dir = "../log/Alex" y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_dir = "../log/VGG" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'Incep4': log_dir = "E:/alum/log/Incep4" y, _ = inception_v4.inception_v4(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, reuse=None, scope='InceptionV4', create_aux_logits=True) elif model == 'Res': log_dir = "E:/alum/log/Res" y, _ = resnet_v2.resnet_v2_50(x, num_classes=CLASSES, is_training=True, global_pool=GLOBAL_POOL, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') else: print('Error: model name not exist') return saver = tf.train.Saver() with tf.Session() as sess: # 恢复模型权重 print("Reading checkpoints...") # ckpt 有 model_checkpoint_path 和 all_model_checkpoint_paths 两个属性 ckpt = tf.train.get_checkpoint_state(log_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('Error: no checkpoint file found') return img_list = os.listdir(test_dir) for img_name in img_list: # print(img_name) # 读取图片并缩放 img = Image.open(os.path.join(test_dir, img_name)) img = img.resize((IMG_SIZE, IMG_SIZE)) # 图片转为numpy的数组 img = np.array(img, np.float32) img = np.expand_dims(img, axis=0) predictions = sess.run(y, feed_dict={x: img}) pre = np.argmax(predictions) img_index.append(img_name) res.append(class_label[int(pre)]) print('image: ', img_index, '\n', 'class:', res) # 存为csv文件。两列:图片名,预测结果 data = pd.DataFrame({'0': img_index, '1': res}) # data.sort_index(axis=0) data.to_csv("../submit/vgg_2.csv", index=False, header=False)
def _construct_model(model_type='resnet_v1_50'): """Constructs model for the desired type of CNN. Args: model_type: Type of model to be used. Returns: end_points: A dictionary from components of the network to the corresponding activations. Raises: ValueError: If the model_type is not supported. """ # Placeholder input. images = array_ops.placeholder(dtypes.float32, shape=(1, None, None, 3), name=_INPUT_NODE) # Construct model. if model_type == 'inception_resnet_v2': _, end_points = inception.inception_resnet_v2_base(images) elif model_type == 'inception_resnet_v2-same': _, end_points = inception.inception_resnet_v2_base( images, align_feature_maps=True) elif model_type == 'inception_v2': _, end_points = inception.inception_v2_base(images) elif model_type == 'inception_v2-no-separable-conv': _, end_points = inception.inception_v2_base(images, use_separable_conv=False) elif model_type == 'inception_v3': _, end_points = inception.inception_v3_base(images) elif model_type == 'inception_v4': _, end_points = inception.inception_v4_base(images) elif model_type == 'alexnet_v2': _, end_points = alexnet.alexnet_v2(images) elif model_type == 'vgg_a': _, end_points = vgg.vgg_a(images) elif model_type == 'vgg_16': _, end_points = vgg.vgg_16(images) elif model_type == 'mobilenet_v1': _, end_points = mobilenet_v1.mobilenet_v1_base(images) elif model_type == 'mobilenet_v1_075': _, end_points = mobilenet_v1.mobilenet_v1_base(images, depth_multiplier=0.75) elif model_type == 'resnet_v1_50': _, end_points = resnet_v1.resnet_v1_50(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_101': _, end_points = resnet_v1.resnet_v1_101(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_152': _, end_points = resnet_v1.resnet_v1_152(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_200': _, end_points = resnet_v1.resnet_v1_200(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_50': _, end_points = resnet_v2.resnet_v2_50(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_101': _, end_points = resnet_v2.resnet_v2_101(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_152': _, end_points = resnet_v2.resnet_v2_152(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_200': _, end_points = resnet_v2.resnet_v2_200(images, num_classes=None, is_training=False, global_pool=False) else: raise ValueError('Unsupported model_type %s.' % model_type) return end_points
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)
def train(model='Alex', inherit=False, fine_tune=False): """ train a specified model :param model: the train model name :param inherit: whether to continue training :param fine_tune: whether to fine tuning :return: none """ # 占位符 x = tf.placeholder(tf.float32, [None, IMG_SIZE, IMG_SIZE, 3], name="x_input") y_ = tf.placeholder(tf.uint8, [None], name="y_input") my_global_step = tf.Variable(0, name='global_step', trainable=False, dtype=tf.int64) # 加载数据集 images = np.load('../data/data_' + str(IMG_SIZE) + '.npy') labels = np.load('../data/label_' + str(IMG_SIZE) + '.npy') train_data, val_data, train_label, val_label = train_test_split( images, labels, test_size=0.2, random_state=222) # 模型保存路径,模型名,预训练文件路径,前向传播 if model == 'Alex': log_path = "../log/Alex" model_name = 'alex.ckpt' if fine_tune: print('Error: alex has no pre-train model') return y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_path = "../log/VGG" model_name = 'vgg_299.ckpt' fine_tune_path = '../data/vgg_16_2016_08_28/vgg_16.ckpt' y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=0.8, spatial_squeeze=True, global_pool=GLOBAL_POOL) variables_to_restore = slim.get_variables_to_restore( exclude=['vgg_16/fc8']) elif model == 'Incep': log_path = "E:/alum/log/Incep" model_name = 'incep.ckpt' fine_tune_path = 'E:/alum/weight/inception_v4_2016_09_09/inception_v4.ckpt' y, _ = inception_v4.inception_v4(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=0.8, reuse=None, scope='InceptionV4', create_aux_logits=True) variables_to_restore = slim.get_variables_to_restore( exclude=['InceptionV4/Logits', 'InceptionV4/AuxLogits']) elif model == 'Res': log_path = "E:/alum/log/Res" model_name = 'res.ckpt' fine_tune_path = 'E:/alum/weight/resnet_v2_50_2017_04_14/resnet_v2_50.ckpt' y, _ = resnet_v2.resnet_v2_50(x, num_classes=CLASSES, is_training=True, global_pool=GLOBAL_POOL, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') variables_to_restore = slim.get_variables_to_restore( exclude=['resnet_v2_50/logits']) elif model == 'IncepRes': log_path = "E:/alum/log/IncepRes" model_name = 'incepres.ckpt' fine_tune_path = 'E:/alum/weight/inception_resnet_v2_2016_08_30/inception_resnet_v2_2016_08_30.ckpt' y, _ = inception_resnet_v2.inception_resnet_v2( x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, reuse=None, scope='InceptionResnetV2', create_aux_logits=True, activation_fn=tf.nn.relu) variables_to_restore = slim.get_variables_to_restore(exclude=[ 'InceptionResnetV2/Logits', 'InceptionResnetV2/AuxLogits' ]) else: print('Error: model name not exist') return y_hot = tf.one_hot(y_, CLASSES) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_hot, name='entropy') loss = tf.reduce_mean(cross_entropy, name='loss') learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, my_global_step, 100, LEARNING_RATE_DECAY) optimizer = tf.train.AdamOptimizer(learning_rate) train_op = optimizer.minimize(loss, global_step=my_global_step) correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_hot, 1)) accuracy = tf.reduce_mean(tf.cast(correct, "float")) # 模型保存的Saver saver = tf.train.Saver(tf.global_variables()) init = tf.global_variables_initializer() tf.summary.scalar("loss", loss) # tf.summary.histogram("W1", W) merged_summary_op = tf.summary.merge_all() # 训练迭代 with tf.Session() as sess: step = 0 train_summary_writer = tf.summary.FileWriter('../log/curve/train', sess.graph) test_summary_writer = tf.summary.FileWriter('../log/curve/test') # 是否使用预训练模型 if fine_tune: print('finetune:', model) # 预训练模型的Saver init_fine_tune = slim.assign_from_checkpoint_fn( fine_tune_path, variables_to_restore, ignore_missing_vars=True) sess.run(init) init_fine_tune(sess) # 是否继续训练,ckpt 有 model_checkpoint_path 和 all_model_checkpoint_paths 两个属性 elif inherit: ckpt = tf.train.get_checkpoint_state(log_path) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print(model, 'continue train from %s:' % global_step) step = int(global_step) else: print('Error: no checkpoint file found') return else: print(model, 'restart train:') sess.run(init) # 迭代 while step < MAX_STEP: start_time = time.clock() image_batch, label_batch = utils.get_batch(train_data, train_label, BATCH_SIZE) # 如果输入是灰色图,要增加一维 # image_batch = np.expand_dims(image_batch, axis=3) # 训练,损失值和准确率 _ = sess.run(train_op, feed_dict={x: image_batch, y_: label_batch}) end_time = time.clock() runtime = end_time - start_time step += 1 # 训练信息和保存模型 if step % INFO_STEP == 0 or step == MAX_STEP: train_loss, train_ac, train_summary = sess.run( [loss, accuracy, merged_summary_op], feed_dict={ x: image_batch, y_: label_batch }) train_summary_writer.add_summary(train_summary, step) test_loss, test_ac, test_summary = sess.run( [loss, accuracy, merged_summary_op], feed_dict={ x: val_data, y_: val_label }) test_summary_writer.add_summary(test_summary, step) print( 'step: %d, runtime: %.2f, train_loss: %.4f, test_loss: %.4f,train accuracy: %.4f, ' 'test accuracy: %.4f' % (step, runtime, train_loss, test_loss, train_ac, test_ac)) if step % SAVE_STEP == 0: print('learning_rate: ', sess.run(learning_rate)) checkpoint_path = os.path.join(log_path, model_name) saver.save(sess, checkpoint_path, global_step=step)
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)