Tensorflow错误:ValueError:应定义输入的通道维度。找到了“无”

时间:2018-02-15 08:23:45

标签: python tensorflow

我目前正在使用dataset API代替feed_dict来解决性能问题。

im_dataset = tf.data.Dataset.from_tensor_slices(images)
im_dataset = im_dataset.map(lambda image:
tuple(tf.py_func(image_parser, [image], [np.float32, np.float32, np.float64, np.uint8])),num_parallel_calls = 2)
im_dataset = im_dataset.prefetch(4)
iterator = im_dataset.make_initializable_iterator()

result = prediction(iterator.get_next())

results = []
with tf.Session() as sess:
sess.run(iterator.initializer)
for _ in range(num_images):
    results.append(sess.run(result))

我在运行演示代码时报告了此错误:

ValueError: The channel dimension of the inputs should be defined. Found `None`.

当我使用feed_dict时,它可以正常工作。看起来像iterator.get_next()具有未知的形状。所以我试图通过

重塑生成的张量
tf.reshape(image,[item for sublist in [[1], image.shape[1:3], [3]] for item in sublist])

但它报告说:

TypeError: Failed to convert object of type <type 'list'> to Tensor. Contents: [1, Dimension(None), Dimension(None), 3]. Consider casting elements to a supported type.

所以我的问题是,如何在不使用feed_dictplaceholder的情况下制作图形输入的形状[1,无,无,3]?

更新: 这是image_parser函数和后续调用(我上面说的图像张量为blobs['data']):

def image_parser(image_name):
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)
    blobs, im_scales = _get_blobs(im)
    assert len(im_scales) == 1, "Only single-image batch implemented"
    im_blob = blobs['data']
    blobs['im_info'] = np.array([im_blob.shape[1], im_blob.shape[2], im_scales[0]], dtype=np.float32)
    return blobs['data'], blobs['im_info'], im_scales, im


def _get_blobs(im):
  """Convert an image and RoIs within that image into network inputs."""
  blobs = {}
  blobs['data'], im_scale_factors = _get_image_blob(im)

  return blobs, im_scale_factors


def _get_image_blob(im):
  """Converts an image into a network input.
  Arguments:
    im (ndarray): a color image in BGR order
  Returns:
    blob (ndarray): a data blob holding an image pyramid
    im_scale_factors (list): list of image scales (relative to im) used
      in the image pyramid
  """
  im_orig = im.astype(np.float32, copy=True)
  im_orig -= cfg.PIXEL_MEANS

  im_shape = im_orig.shape
  im_size_min = np.min(im_shape[0:2])
  im_size_max = np.max(im_shape[0:2])

  processed_ims = []
  im_scale_factors = []

  for target_size in cfg.TEST.SCALES:
    im_scale = float(target_size) / float(im_size_min)
    # Prevent the biggest axis from being more than MAX_SIZE
    if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE:
      im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max)
    im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
            interpolation=cv2.INTER_LINEAR)
    im_scale_factors.append(im_scale)
    processed_ims.append(im)

  # Create a blob to hold the input images
  blob = im_list_to_blob(processed_ims)

  return blob, np.array(im_scale_factors)


def im_list_to_blob(ims):
  """Convert a list of images into a network input.

  Assumes images are already prepared (means subtracted, BGR order, ...).
  """
  max_shape = np.array([im.shape for im in ims]).max(axis=0)
  num_images = len(ims)
  blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),
                  dtype=np.float32)
  for i in range(num_images):
    im = ims[i]
    blob[i, 0:im.shape[0], 0:im.shape[1], :] = im

  return blob

0 个答案:

没有答案