在深度学习中,我们通常是对张量进行操作,矩阵可以看成是二阶张量,而一张RGB图片,可以看成是三阶张量(长、宽、颜色)。
Tensorflow中的数据都以张量(tensor)的形式流动,本篇博客中记录一下最近用到的一些在tensorflow中的张量操作,会持续更新。
1. 创建值为0的张量
shape_1 = 1
shape_2 = shape_3 = 600
shape_4 = 3
v1 = tf.Variable(tf.zeros([shape_1, shape_2, shape_3, shape_4]))
2. 连接tensor
tensor_1 = tf.Variable(tf.zeros([shape_1, shape_2, shape_3, 3]))
tensor_2 = tf.Variable(tf.zeros([shape_1, shape_2, shape_3, 3]))
tensor_3 = tf.concat([tensor_1, tensor_2], 3)
3. 图片切割
tf.image.crop_to_bounding_box(image, offset_height, offset_width, height, width)
offset_height, offset_width为切割起始点, height, width为切割长度
4. 张量加减法
tf.add(x, y)
tf.subtract(x, y)
a = tf.convert_to_tensor([[1, 2, 3], [4, 5, 6]])
b = tf.convert_to_tensor([[4, 2, 3], [4, 5, 6]])
c = tf.add(a, b)
with tf.Session() as sess:
print(sess.run(c))
5. 加载预训练模型
一般我们采用:
saver = tf.Saver()
saver.restore(sess, save_path)
或:
checkpoint = tf.train.latest_checkpoint(dir)
saver.restore(sess, checkpoint)
若使用了滑动指数平均(EMA)
tf.train.ExponentialMovingAverage
加载权重时,需要说明,否则不会加载ema参数
variables_to_restore = ema.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
saver.restore(sess, save_path)
6. 图片二值化
首先将图片转化为单通道(灰度图)
image = tf.image.rgb_to_grayscale(image)
然后使用cast函数对值进行截取
image = tf.image.rgb_to_grayscale(image)
image = tf.cast(image > 0, dtype=tf.float32)
获得01的二值图