TensorFlow:从文件夹“无法挤压昏暗[1]”

时间:2018-04-11 05:34:54

标签: python tensorflow deep-learning

我有一个包含两张图片的文件夹和一个带有文件名和标签的csv,我已将其放入两个数组中:

filenames:array(['pic1.jpg','pic2.jpg'],dtype = object); labels_i:array([0,1])

我创建了一个火车输入Funktion

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教程基本相同,因为RGB图片,我只修改输入图层以形成[-1,28,28,3]而不是[-1,28,28,1]

def my_model(features, labels, mode, params):

    # Input Layer
    input_layer = tf.reshape(features, [-1, 28, 28, 3])

  # Convolutional Layer #1
    conv1 = tf.layers.conv2d(
        inputs=input_layer,
        filters=32,
        kernel_size=[5, 5],
        padding="same",
        activation=tf.nn.relu)
  # Pooling Layer #1
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

  # Convolutional Layer #2 and Pooling Layer #2
    conv2 = tf.layers.conv2d(
          inputs=pool1,
          filters=64,
          kernel_size=[5, 5],
          padding="same",
          activation=tf.nn.relu)
    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

  # Dense Layer
    pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
    dropout = tf.layers.dropout(
    inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

  # Logits Layer
    logits = tf.layers.dense(inputs=dropout, units=2)

    # Compute predictions.
    predicted_classes = tf.argmax(logits, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'class_ids': predicted_classes[:, tf.newaxis],
            'probabilities': tf.nn.softmax(logits),
            'logits': logits,
        }
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)

    # Compute loss.
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

    # Compute evaluation metrics.
    accuracy = tf.metrics.accuracy(labels=labels,
                                   predictions=predicted_classes,
                                   name='acc_op')
    metrics = {'accuracy': accuracy}
    tf.summary.scalar('accuracy', accuracy[1])

    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(
            mode, loss=loss, eval_metric_ops=metrics)

    # Create training op.
    assert mode == tf.estimator.ModeKeys.TRAIN

    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
    train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

然后我尝试训练模型:

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].

完成错误:     INFO:tensorflow:调用model_fn。

---------------------------------------------------------------------------
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].

1 个答案:

答案 0 :(得分:0)

错误是由于错误尺寸的输入被送到sparse_softmax_cross_entropy。它希望logits的排名比标签大一些,但在你的情况下,排名的差异是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]])而不是array([0, 1])