Python tf.convert_to_张量(pred_标签)-ValueError:参数必须是稠密张量:得到形状[24361022],但需要[2]

Python tf.convert_to_张量(pred_标签)-ValueError:参数必须是稠密张量:得到形状[24361022],但需要[2],python,tensorflow,Python,Tensorflow,我试图将现有光流网络的输出从一个numpy阵列转换回一个张量,这样我就可以通过一个可微插值网络运行它 PWC网络代码获取两个大小相同的输入图像,并计算流对应关系。如果这是一对图像,我认为流动将是x和y方向的像素级位移。尺寸为[1,h,W,2]。 但是,一批中可以有不同数量的图像对。我们可以称之为批量大小的b,所以4D体积变成[b,h,w,2] 通过使用 pred_labels_tensor = tf.convert_to_tensor(pred_labels) 我读过这些 但我仍然不明白

我试图将现有光流网络的输出从一个numpy阵列转换回一个张量,这样我就可以通过一个可微插值网络运行它

PWC网络代码获取两个大小相同的输入图像,并计算流对应关系。如果这是一对图像,我认为流动将是x和y方向的像素级位移。尺寸为[1,h,W,2]。 但是,一批中可以有不同数量的图像对。我们可以称之为批量大小的b,所以4D体积变成[b,h,w,2]

通过使用

pred_labels_tensor = tf.convert_to_tensor(pred_labels)
我读过这些

但我仍然不明白我需要做些什么才能让它发挥作用

我还研究了该文件运行的代码,它确实使用了np.asarray

这两个链接让我觉得这是因为它是一个列表列表,或者可能需要一些零。我怎样才能弄清楚到底是什么问题?我该怎么解决呢

从这个python文件:-

要重新创建问题,可以使用github下载中提供的现有示例,使用此代码而不是for循环

image_path1 = f'./samples/mpisintel_test_clean_ambush_1_frame_0001.png'
image_path2 = f'./samples/mpisintel_test_clean_ambush_1_frame_0002.png'
image1, image2 = imread(image_path1), imread(image_path2)
img_pairs.append((image1, image2))

image_path1 = f'./samples/mpisintel_test_clean_ambush_1_frame_0003.png'
image_path2 = f'./samples/mpisintel_test_clean_ambush_1_frame_0004.png'
image1, image2 = imread(image_path1), imread(image_path2)
img_pairs.append((image1, image2))

pred_labels = nn.predict_from_img_pairs(img_pairs, batch_size=1, verbose=False)

pred_labels_tensor = tf.convert_to_tensor(pred_labels)
我希望得到张量输出,但是,我在MSVSCode的终端中得到了以下错误:-

ValueError: Argument must be a dense tensor: [array([[[ 0.32990038, -0.11566047],
        [ 0.35661912, -0.09227534],
        [ 0.38333783, -0.06889021],
        ...,
        [-0.1237613 ,  0.07946336],
        [-0.1237613 ,  0.07946336],
        [-0.1237613 ,  0.07946336]],

       [[ 0.34405386, -0.09286585],
        [ 0.36766803, -0.07679807],
        [ 0.39128217, -0.06073029],
        ...,
        [-0.10938472,  0.08551764],
        [-0.10938472,  0.08551764],
        [-0.10938472,  0.08551764]],

       [[ 0.35820735, -0.07007124],
        [ 0.37871695, -0.0613208 ],
        [ 0.39922655, -0.05257037],
        ...,
        [-0.09500815,  0.09157193],
        [-0.09500815,  0.09157193],
        [-0.09500815,  0.09157193]],

       ...,

       [[ 0.9003515 ,  1.0893728 ],
        [ 0.93065804,  1.0662789 ],
        [ 0.96096456,  1.0431851 ],
        ...,
        [ 0.43580785,  0.02744378],
        [ 0.43580785,  0.02744378],
        [ 0.43580785,  0.02744378]],

       [[ 0.9003515 ,  1.0893728 ],
        [ 0.93065804,  1.0662789 ],
        [ 0.96096456,  1.0431851 ],
        ...,
        [ 0.43580785,  0.02744378],
        [ 0.43580785,  0.02744378],
        [ 0.43580785,  0.02744378]],

       [[ 0.9003515 ,  1.0893728 ],
        [ 0.93065804,  1.0662789 ],
        [ 0.96096456,  1.0431851 ],
        ...,
        [ 0.43580785,  0.02744378],
        [ 0.43580785,  0.02744378],
        [ 0.43580785,  0.02744378]]], dtype=float32), array([[[ 0.49922907,  0.08599953],
        [ 0.5034714 ,  0.1123561 ],
        [ 0.5077137 ,  0.13871266],
        ...,
        [-0.3719127 ,  0.1080336 ],
        [-0.3719127 ,  0.1080336 ],
        [-0.3719127 ,  0.1080336 ]],

       [[ 0.49763823,  0.11536887],
        [ 0.4972613 ,  0.13717887],
        [ 0.49688435,  0.15898886],
        ...,
        [-0.36932352,  0.11556612],
        [-0.36932352,  0.11556612],
        [-0.36932352,  0.11556612]],

       [[ 0.49604735,  0.14473821],
        [ 0.4910512 ,  0.16200164],
        [ 0.48605505,  0.17926508],
        ...,
        [-0.36673436,  0.12309864],
        [-0.36673436,  0.12309864],
        [-0.36673436,  0.12309864]],

       ...,

       [[ 0.46260613, -0.47470346],
        [ 0.46841043, -0.46383834],
        [ 0.47421476, -0.4529732 ],
        ...,
        [-0.29265293, -0.4021799 ],
        [-0.29265293, -0.4021799 ],
        [-0.29265293, -0.4021799 ]],

       [[ 0.46260613, -0.47470346],
        [ 0.46841043, -0.46383834],
        [ 0.47421476, -0.4529732 ],
        ...,
        [-0.29265293, -0.4021799 ],
        [-0.29265293, -0.4021799 ],
        [-0.29265293, -0.4021799 ]],

       [[ 0.46260613, -0.47470346],
        [ 0.46841043, -0.46383834],
        [ 0.47421476, -0.4529732 ],
        ...,
        [-0.29265293, -0.4021799 ],
        [-0.29265293, -0.4021799 ],
        [-0.29265293, -0.4021799 ]]], dtype=float32)] - [2, 436, 1024, 2], but wanted [2]
如果我将批处理大小减少为1,则会出现此错误

ValueError:参数必须是稠密张量:-获得形状[14361022],但需要[1]

要获得最小可重复性示例,需要以下条件:-

Python 3.7.3 Tensorflow 1.13.1(最新稳定版本) 此外,您还需要下载、复制并粘贴下面的脚本到现有脚本上

还有一个模型可以从中下载pwcnet-lg-6-2-multisteps-chairsthingsmix


pred_标签
包含两个形状不同的3D NumPy阵列。TensorFlow解释说,您希望从这些元素中生成4D张量(沿新的第一维度堆叠),但这是不可能的,因为形状不同。如果没有一个完整的最小可复制的示例,就无法判断,但这可能是因为输入图像的大小不同,因此它们的预测也不同。@jdehesa,我用下面的另一个试验更新了这个问题,使用一对图像,而不是一批两个图像。我还添加了一些关于我正在使用的版本的信息。一旦下载了github代码,这是否构成了一个完整的可复制示例?这是我第一次发布到stack overflow。
pred_标签
包含两个形状不同的3D NumPy数组。TensorFlow解释说,您希望从这些元素中生成4D张量(沿新的第一维度堆叠),但这是不可能的,因为形状不同。如果没有一个完整的最小可复制的示例,就无法判断,但这可能是因为输入图像的大小不同,因此它们的预测也不同。@jdehesa,我用下面的另一个试验更新了这个问题,使用一对图像,而不是一批两个图像。我还添加了一些关于我正在使用的版本的信息。一旦下载了github代码,这是否构成了一个完整的可复制示例?这是我第一次向stack overflow发帖。
"""
pwcnet_predict_from_img_pairs.py
Run inference on a list of images pairs.
Written by Phil Ferriere
Licensed under the MIT License (see LICENSE for details)
"""
from __future__ import absolute_import, division, print_function

from voxel_flow_geo_layer_utils import bilinear_interp
from voxel_flow_geo_layer_utils import meshgrid

from copy import deepcopy
from skimage.io import imread
from model_pwcnet import ModelPWCNet, _DEFAULT_PWCNET_TEST_OPTIONS
#from visualize import display_img_pairs_w_flows
import visualize
import numpy as np
import tensorflow as tf

# TODO: Set device to use for inference
# Here, we're using a GPU (use '/device:CPU:0' to run inference on the CPU)
gpu_devices = ['/device:GPU:0']  
controller = '/device:GPU:0'

# TODO: Set the path to the trained model (make sure you've downloaded it first https://drive.google.com/drive/folders/1iRJ8SFF6fyoICShRVWuzr192m_DgzSYp)
ckpt_path = './models/pwcnet-lg-6-2-multisteps-chairsthingsmix/pwcnet.ckpt-595000'

# Build a list of image pairs to process (in this case it's just one image pair)
img_pairs = []
image_path1 = f'./samples/mpisintel_test_clean_ambush_1_frame_0001.png'
image_path2 = f'./samples/mpisintel_test_clean_ambush_1_frame_0002.png'
image1, image2 = imread(image_path1), imread(image_path2)
img_pairs.append((image1, image2))

# Configure the model for inference, starting with the default options
nn_opts = deepcopy(_DEFAULT_PWCNET_TEST_OPTIONS)
nn_opts['verbose'] = True
nn_opts['ckpt_path'] = ckpt_path
nn_opts['batch_size'] = 1
nn_opts['gpu_devices'] = gpu_devices
nn_opts['controller'] = controller

# We're running the PWC-Net-large model in quarter-resolution mode
# That is, with a 6 level pyramid, and upsampling of level 2 by 4 in each dimension as the final flow prediction
nn_opts['use_dense_cx'] = True
nn_opts['use_res_cx'] = True
nn_opts['pyr_lvls'] = 6
nn_opts['flow_pred_lvl'] = 2

# The size of the images in this dataset are not multiples of 64, while the model generates flows padded to multiples
# of 64. Hence, we need to crop the predicted flows to their original size
nn_opts['adapt_info'] = (1, 8, 8, 2)

# Instantiate the model in inference mode and display the model configuration
nn = ModelPWCNet(mode='test', options=nn_opts)
nn.print_config()

# Generate the predictions and display them
pred_labels = nn.predict_from_img_pair # pred_labels shape is [436, 1024,2]
# array has len 1 when there is only one image pair.
pred_labels_tensor = tf.convert_to_tensor(pred_labels)