TensorFlow系列: tf.shape() 与 tensor.get_shape()

tf.shape()

shape(
    input,
    name=None,
    out_type=tf.int32
)

返回一个代表 input 的 shape 的 1-D tensor.

也就是说获得shape的信息,需要用sess.run().

>>> import tensorflow as tf
>>> a = tf.constant([[1, 2, 3], [4, 5, 6]])
>>> a                          # 2-D tensor
<tf.Tensor 'Const:0' shape=(2, 3) dtype=int32>
>>> a_shape = tf.shape(a)
>>> a_shape                    # 1-D tensor, 长度和a的维度一样
<tf.Tensor 'Shape:0' shape=(2,) dtype=int32>
>>> sess = tf.Session()
>>> a_shape_eval = sess.run(a_shape)
>>> a_shape_eval
array([2, 3], dtype=int32)

tensor.get_shape()

返回一个tf.TensorShape的,代表当前tensor的shape

不需要运行计算图就可以获得shape的信息.
使用tensor.shape也可以得到当前tensor的shape

>>> import tensorflow as tf
>>> a = tf.constant([[1, 2, 3], [4, 5, 6]])
>>> a                        # 2-D tensor
<tf.Tensor 'Const:0' shape=(2, 3) dtype=int32>
>>> a.shape                  # 类型是 tf.TensorShape
TensorShape([Dimension(2), Dimension(3)])
>>> a.get_shape()            # 功能和tensor.shape一样
TensorShape([Dimension(2), Dimension(3)])
>>> a.get_shape().as_list()  # 转化为list
[2, 3]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将这两个代码结合import cv2 import numpy as np import urllib.request import tensorflow as tf # 下载DeepLabv3+模型权重文件 model_url = "http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz" tar_filename = "deeplabv3_mnv2_pascal_train_aug.tar.gz" urllib.request.urlretrieve(model_url, tar_filename) # 解压缩 with tarfile.open(tar_filename, "r:gz") as tar: tar.extractall() model_filename = "deeplabv3_mnv2_pascal_train_aug/frozen_inference_graph.pb" # 加载模型 graph = tf.Graph() with graph.as_default(): od_graph_def = tf.GraphDef() with tf.io.gfile.GFile(model_filename, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # 读取图像 image_path = "your_image.jpg" image = cv2.imread(image_path) # 进行图像分割 with tf.compat.v1.Session(graph=graph) as sess: input_tensor = graph.get_tensor_by_name('ImageTensor:0') output_tensor = graph.get_tensor_by_name('SemanticPredictions:0') output = sess.run(output_tensor, feed_dict={input_tensor: image}) # 解码并可视化分割结果 segmentation_mask = np.squeeze(output) segmentation_mask = np.uint8(segmentation_mask) segmentation_mask = cv2.resize(segmentation_mask, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST) # 显示原始图像和分割结果 cv2.imshow("Image", image) cv2.imshow("Segmentation Mask", segmentation_mask) cv2.waitKey(0) cv2.destroyAllWindows() model1 = models.CellposeModel(gpu=True, model_type='livecell') model2 = models.Cellpose(gpu=True,model_type='nuclei') model3= models.Cellpose(gpu=True,model_type='cyto2') 集成DeepLabv3+模型和cellpose模型
最新发布
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值