TensorFlow:从文件夹“Can not queeze dim[1]”中读取图像文件时出错

2024-10-01 15:32:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件夹,里面有两张图片,还有一个csv文件名和标签,我把它们放在两个数组中:

文件名:array(['pic1.jpg','pic2.jpg'],dtype=object); 标签_i:数组([0,1])

我创建了一个火车输入函数

def train_input_fn(filenames, labels_i):
    filenames2 = tf.constant(filenames)
    labels2 = tf.constant(labels_i, dtype=tf.int64)

    dataset = tf.data.Dataset.from_tensor_slices((filenames2, labels2))

    def _parse(filename, label):
        image_string = tf.read_file(PATH+"data/"+filename)
        image_decoded = tf.image.decode_jpeg(image_string)

        image_resized = tf.image.resize_images(image_decoded, [28, 28])
        return image_resized, label

    dataset = dataset.map(_parse)

    iterator = dataset.make_one_shot_iterator()

    features, labels = iterator.get_next()
    return features, labels

我的模型与MNIST教程基本相同,我只是修改了输入层的形状[-1,28,28,3]而不是[-1,28,28,1],因为RGB图片。在

^{pr2}$

然后我试着训练模特:

classifier = tf.estimator.Estimator(model_fn=my_model)
classifier.train(input_fn=lambda: train_input_fn(filenames, labels_i),steps=200)

但我有以下错误:

InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 2 for 'sparse_softmax_cross_entropy_loss/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [1,2].

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 2 for 'sparse_softmax_cross_entropy_loss/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [1,2].

完全错误: 信息:tensorflow:调用模型。在

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
    685           graph_def_version, node_def_str, input_shapes, input_tensors,
--> 686           input_tensors_as_shapes, status)
    687   except errors.InvalidArgumentError as err:

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    515             compat.as_text(c_api.TF_Message(self.status.status)),
--> 516             c_api.TF_GetCode(self.status.status))
    517     # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 2 for 'sparse_softmax_cross_entropy_loss/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [?,2].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-13-6a3648164a55> in <module>()
      1 # Train the Model.
----> 2 classifier.train(input_fn=lambda: train_input_fn(filenames, labels_i),steps=200)
      3 

~/pyft/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
    350 
    351     saving_listeners = _check_listeners_type(saving_listeners)
--> 352     loss = self._train_model(input_fn, hooks, saving_listeners)
    353     logging.info('Loss for final step: %s.', loss)
    354     return self

~/pyft/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
    810       worker_hooks.extend(input_hooks)
    811       estimator_spec = self._call_model_fn(
--> 812           features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
    813 
    814       if self._warm_start_settings:

~/pyft/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
    791 
    792     logging.info('Calling model_fn.')
--> 793     model_fn_results = self._model_fn(features=features, **kwargs)
    794     logging.info('Done calling model_fn.')
    795 

<ipython-input-11-776f3754c5df> in my_model(features, labels, mode, params)
     57 
     58     # Compute loss.
---> 59     loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
     60 
     61     # Compute evaluation metrics.

~/pyft/lib/python3.6/site-packages/tensorflow/python/ops/losses/losses_impl.py in sparse_softmax_cross_entropy(labels, logits, weights, scope, loss_collection, reduction)
    829     # therefore, expected_rank_diff=1.
    830     labels, logits, weights = _remove_squeezable_dimensions(
--> 831         labels, logits, weights, expected_rank_diff=1)
    832     losses = nn.sparse_softmax_cross_entropy_with_logits(labels=labels,
    833                                                          logits=logits,

~/pyft/lib/python3.6/site-packages/tensorflow/python/ops/losses/losses_impl.py in _remove_squeezable_dimensions(labels, predictions, weights, expected_rank_diff)
    758   """
    759   labels, predictions = confusion_matrix.remove_squeezable_dimensions(
--> 760       labels, predictions, expected_rank_diff=expected_rank_diff)
    761 
    762   if weights is not None:

~/pyft/lib/python3.6/site-packages/tensorflow/python/ops/confusion_matrix.py in remove_squeezable_dimensions(labels, predictions, expected_rank_diff, name)
     73       rank_diff = predictions_rank - labels_rank
     74       if rank_diff == expected_rank_diff + 1:
---> 75         predictions = array_ops.squeeze(predictions, [-1])
     76       elif rank_diff == expected_rank_diff - 1:
     77         labels = array_ops.squeeze(labels, [-1])

~/pyft/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in squeeze(input, axis, name, squeeze_dims)
   2566   if np.isscalar(axis):
   2567     axis = [axis]
-> 2568   return gen_array_ops._squeeze(input, axis, name)
   2569 
   2570 

~/pyft/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in _squeeze(input, axis, name)
   5167   if _ctx.in_graph_mode():
   5168     _, _, _op = _op_def_lib._apply_op_helper(
-> 5169         "Squeeze", input=input, squeeze_dims=axis, name=name)
   5170     _result = _op.outputs[:]
   5171     _inputs_flat = _op.inputs

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    785         op = g.create_op(op_type_name, inputs, output_types, name=scope,
    786                          input_types=input_types, attrs=attr_protos,
--> 787                          op_def=op_def)
    788       return output_structure, op_def.is_stateful, op
    789 

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   3271         op_def=op_def)
   3272     self._create_op_helper(ret, compute_shapes=compute_shapes,
-> 3273                            compute_device=compute_device)
   3274     return ret
   3275 

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _create_op_helper(self, op, compute_shapes, compute_device)
   3311     # compute_shapes argument.
   3312     if op._c_op or compute_shapes:  # pylint: disable=protected-access
-> 3313       set_shapes_for_outputs(op)
   3314     # TODO(b/XXXX): move to Operation.__init__ once _USE_C_API flag is removed.
   3315     self._add_op(op)

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in set_shapes_for_outputs(op)
   2499     return _set_shapes_for_outputs_c_api(op)
   2500   else:
-> 2501     return _set_shapes_for_outputs(op)
   2502 
   2503 

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _set_shapes_for_outputs(op)
   2472       shape_func = _call_cpp_shape_fn_and_require_op
   2473 
-> 2474   shapes = shape_func(op)
   2475   if shapes is None:
   2476     raise RuntimeError(

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in call_with_requiring(op)
   2402 
   2403   def call_with_requiring(op):
-> 2404     return call_cpp_shape_fn(op, require_shape_fn=True)
   2405 
   2406   _call_cpp_shape_fn_and_require_op = call_with_requiring

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in call_cpp_shape_fn(op, require_shape_fn)
    625     res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
    626                                   input_tensors_as_shapes_needed,
--> 627                                   require_shape_fn)
    628     if not isinstance(res, dict):
    629       # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).

~/pyft/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
    689       missing_shape_fn = True
    690     else:
--> 691       raise ValueError(err.message)
    692 
    693   if missing_shape_fn:

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 2 for 'sparse_softmax_cross_entropy_loss/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [?,2].

Tags: inpyselfinputlabelslibpackagestensorflow
1条回答
网友
1楼 · 发布于 2024-10-01 15:32:07

错误是由于输入的尺寸错误而输入到sparse_softmax_cross_entropy。它期望logits的秩1大于labels,但是在您的例子中,它的秩差是2(参见

            if rank_diff == expected_rank_diff + 1:
 -> 75         predictions = array_ops.squeeze(predictions, [-1])

在堆栈跟踪中。expected_rank_diff是1。)

看看您的代码,我想这是由于您的labels是一个标量-零维。代码不够聪明,无法尝试将其重塑为向量。您应该尝试将您的labels_i指定为array([[0], [1]]),而不是{}。在

相关问题 更多 >

    热门问题