2017-04-12 161 views
0

我得到试图运行模型时出现以下错误:Keras - ValueError异常:在连续模型中的第一层必须得到一个`input_shape`或`batch_input_shape`参数

Using TensorFlow backend. 
train.py:99: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(pool_size=(2, 2), data_format="channels_last")` 
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")) 
Traceback (most recent call last): 
    File "train.py", line 361, in <module> 
    save_bottleneck_features() 
    File "train.py", line 99, in save_bottleneck_features 
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")) 
    File "C:\Python35\lib\site-packages\keras\models.py", line 420, in add 
    raise ValueError('The first layer in a ' 
ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument. 

这些都是相关的行代码(train.py):

model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf")) 

model.py

def add(self, layer): 
     """Adds a layer instance on top of the layer stack. 

     # Arguments 
      layer: layer instance. 

     # Raises 
      TypeError: If `layer` is not a layer instance. 
      ValueError: In case the `layer` argument does not 
       know its input shape. 
      ValueError: In case the `layer` argument has 
       multiple output tensors, or is already connected 
       somewhere else (forbidden in `Sequential` models). 
     """ 
     if not isinstance(layer, Layer): 
      raise TypeError('The added layer must be ' 
          'an instance of class Layer. ' 
          'Found: ' + str(layer)) 
     if not self.outputs: 
      # first layer in model: check that it is an input layer 
      if not layer.inbound_nodes: 
       # create an input layer 
       if not hasattr(layer, 'batch_input_shape'): 
        raise ValueError('The first layer in a ' 
            'Sequential model must ' 
            'get an `input_shape` or ' 
            '`batch_input_shape` argument.') 
       # Instantiate the input layer. 
       x = Input(batch_shape=layer.batch_input_shape, 
          dtype=layer.dtype, name=layer.name + '_input') 
       # This will build the current layer 
       # and create the node connecting the current layer 
       # to the input layer we just created. 
       layer(x) 

      if len(layer.inbound_nodes) != 1: 
       raise ValueError('A layer added to a Sequential model must ' 
           'not already be connected somewhere else. ' 
           'Model received layer ' + layer.name + 
           ' which has ' + 
           str(len(layer.inbound_nodes)) + 
           ' pre-existing inbound connections.') 

      if len(layer.inbound_nodes[0].output_tensors) != 1: 
       raise ValueError('All layers in a Sequential model ' 
           'should have a single output tensor. ' 
           'For multi-output layers, ' 
           'use the functional API.') 

      self.outputs = [layer.inbound_nodes[0].output_tensors[0]] 
      self.inputs = topology.get_source_inputs(self.outputs[0]) 

      # We create an input node, which we will keep updated 
      # as we add more layers 
      topology.Node(outbound_layer=self, 
          inbound_layers=[], 
          node_indices=[], 
          tensor_indices=[], 
          input_tensors=self.inputs, 
          output_tensors=self.outputs, 
          # no model-level masking for now 
          input_masks=[None for _ in self.inputs], 
          output_masks=[None], 
          input_shapes=[x._keras_shape for x in self.inputs], 
          output_shapes=[self.outputs[0]._keras_shape]) 
     else: 
      output_tensor = layer(self.outputs[0]) 
      if isinstance(output_tensor, list): 
       raise TypeError('All layers in a Sequential model ' 
           'should have a single output tensor. ' 
           'For multi-output layers, ' 
           'use the functional API.') 
      self.outputs = [output_tensor] 
      # update self.inbound_nodes 
      self.inbound_nodes[0].output_tensors = self.outputs 
      self.inbound_nodes[0].output_shapes = [self.outputs[0]._keras_shape] 

     self.layers.append(layer) 
     self.built = False 

我该如何解决这个问题?

回答

1

从错误信息

ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument. 

如果MaxPooling是模型的第一层,你应该通过input_shape(或batch_input_shape)的说法一样

model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf", input_shape=(16, 16))) 
+0

谢谢您的答复。对于input_shape,这是图像的大小吗? – Simplicity

+0

@Simplicity是的。一般来说,这是输入张量的形状(没有批量大小)。 – kvorobiev

+0

是的,我写了下面的“model.add(MaxPooling2D(pool_size =(2,2),dim_ordering =”tf“,input_shape =(1022,767,3)))”,问题似乎已经解决 – Simplicity

相关问题