第一句是
with tf.Graph().as_default():
设置tensor所属的graph,具体原理目前不清楚,只是当前理解为模板背景
第二句
with tf.device('/gpu:'+str(GPU_INDEX)):
设置tensor运行硬件环境为gpu。不过目前我的代码跑起来不知道什么原因没有用到gpu。非常慢
第三句,第四句
pointclouds_pl, labels_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
调用了设置model内的placeholder_inputs函数,默认是pointnet_cls.py,内部函数实现是
def placeholder_inputs(batch_size, num_point):
pointclouds_pl = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
构建一个三维的32x1024x3的tensor。
labels_pl = tf.placeholder(tf.int32, shape=(batch_size))
构建一个一维的32的tensor
return pointclouds_pl, labels_pl
调用tf.placeholder构建出输入点的tensor以及标签的tensor
is_training_pl = tf.placeholder(tf.bool, shape=())构建一个bool型的shape未知的tensor
不管是调用模型封装的函数还是直接调用,本质都是调用tf.placeholder构建tensor。但是tf.placeholder构建的tensor比较特殊。不能直接放到session(暂时先记住,后面讲解)中运行,会报错,官方给的解释如下:
Key Point: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict
optional argument to Session.run()
, Tensor.eval()
, or Operation.run()
.
官方例子如下:
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.compat.v1.Session() as sess:
print(sess.run(y)) # ERROR: will fail because x was not fed.
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
从上面的code可以看出,必须用feed_dict给x,feed值之后才能进行使用。
这里类似C++的指针(只有类型),如果不给指针分配空间,访问空指针就会报错,这里是一个道理,tf.placeholder构建的tensor可以理解为没有 分配空间的指针,必须给这个tensor赋值初始化才能使用