【问题标题】:Combine Encoder and Decoder Model结合编码器和解码器模型
【发布时间】:2018-09-03 15:11:06
【问题描述】:

谁能告诉我,这段代码有什么问题?为什么这显示索引错误。编码器和解码器是分开定义的,当我在最后一行将两个模型组合在一起时,它会抛出索引错误。

def Encoder():
    x= Conv2D(128, (3, 3),padding='same',activation='relu')(input_img)
    x= Conv2D(128, (3, 3), activation='relu', padding='same')(x)
     x= MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
     x= Conv2D(64, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(64, (3, 3), activation='relu', padding='same')(x)
     x= MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
     x= Conv2D(32, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(32, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(32, (3, 3), activation='relu', padding='same')(x)
     x= MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
     x= Conv2D(16, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(16, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(8, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(4, (3, 3), activation='relu', padding='same')(x)
     encoded =Flatten()(x)
     return Model(input_img,encoded)

     # Encoder.add(Flatten())
     # autoencoder.add(Reshape((28, 28, 4)))

 def Decoder():
     encoded=Input(shape=(28, 28, 4))
     #encoded = Reshape((28, 28, 4))(encoded)
     x= Conv2D(4, (3, 3), activation='relu',padding='same')(encoded)
     x= Conv2D(8, (3, 3), activation='relu',padding='same')(x)
     x= Conv2D(16, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(16, (3, 3), activation='relu', padding='same')(x)
     x= UpSampling2D(size=(2, 2))(x)
     x= Conv2D(32, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(32, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(32, (3, 3), activation='relu', padding='same')(x)
     x= UpSampling2D(size=(2, 2))(x)
     x= Conv2D(64, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(64, (3, 3), activation='relu', padding='same')(x)
     x= UpSampling2D(size=(2, 2))(x)
     x= Conv2D(128, (3, 3), activation='relu', padding='same')(x)
     x= Conv2D(128, (3, 3), activation='relu', padding='same')(x)
     decoded= Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
     return Model(encoded,decoded)

 x = Input(shape=(224, 224, 3))

 autoencoder = Model(x, Decoder()(Encoder()(x)))

堆栈跟踪是:

IndexError Traceback (most recent call last) in ()
6
7 # make the model:
 ----> 8 autoencoder = Model(x, Decoder()(Encoder()(x)))
9
10

 ~/VirtualEnvs/deep/lib/python3.5/site- 
 packages/keras/engine/base_layer.py in call(self, inputs, **kwargs)
 455 # Actually call the layer,
 456 # collecting output(s), mask(s), and shape(s).
  --> 457 output = self.call(inputs, **kwargs)
  458 output_mask = self.compute_mask(inputs, previous_mask)
  459

  ~/VirtualEnvs/deep/lib/python3.5/site- 
    packages/keras/engine/network.py in call(self, inputs, mask)
   568 return self._output_tensor_cache[cache_key]
  569 else:
  --> 570 output_tensors, _, _ = self.run_internal_graph(inputs, 
  masks)
  571 return output_tensors
  572

  ~/VirtualEnvs/deep/lib/python3.5/site- 
  packages/keras/engine/network.py in run_internal_graph(self, inputs, 
  masks)
  722 if 'mask' not in kwargs:
  723 kwargs['mask'] = computed_mask
  --> 724 output_tensors = to_list(layer.call(computed_tensor, 
  **kwargs))
  725 output_masks = layer.compute_mask(computed_tensor,
  726 computed_mask)

   ~/VirtualEnvs/deep/lib/python3.5/site- 
   packages/keras/layers/convolutional.py in call(self, inputs)
   166 padding=self.padding,
  167 data_format=self.data_format,
   --> 168 dilation_rate=self.dilation_rate)
   169 if self.rank == 3:
   170 outputs = K.conv3d(

 ~/VirtualEnvs/deep/lib/python3.5/site- 
 packages/keras/backend/tensorflow_backend.py in conv2d(x, kernel, 
 strides, padding, data_format, dilation_rate)
 3563 strides=strides,
 3564 padding=padding,
 -> 3565 data_format=tf_data_format)
 3566
 3567 if data_format == 'channels_first' and tf_data_format == 'NHWC':

  ~/VirtualEnvs/deep/lib/python3.5/site- 
 packages/tensorflow/python/ops/nn_ops.py in convolution(input, 
 filter, padding, strides, dilation_rate, name, data_format)
  777 dilation_rate=dilation_rate,
  778 name=name,
  --> 779 data_format=data_format)
 780 return op(input, filter)
 781

  ~/VirtualEnvs/deep/lib/python3.5/site- 
 packages/tensorflow/python/ops/nn_ops.py in init(self, input_shape, 
 filter_shape, padding, strides, dilation_rate, name, data_format)
826
827 if data_format is None or not data_format.startswith("NC"):
--> 828 input_channels_dim = input_shape[num_spatial_dims + 1]
829 spatial_dims = range(1, num_spatial_dims + 1)
830 else:

~/VirtualEnvs/deep/lib/python3.5/site- 
 packages/tensorflow/python/framework/tensor_shape.py in getitem(self, 
 key)
 613 return TensorShape(self._dims[key])
 614 else:
 --> 615 return self._dims[key]
 616 else:
 617 if isinstance(key, slice):

 **IndexError: list index out of range

【问题讨论】:

    标签: python keras autoencoder


    【解决方案1】:

    形状不匹配。 encoder 返回编码图像的扁平表示,即数据的形状为 (BS, -1)。在您的解码器中,您正在定义这样的输入

    encoded=Input(shape=(28, 28, 4))
    

    即您假设输入的形状为(BS, 28, 28, 4),与编码器的输出不兼容。

    【讨论】:

    • @DiptiMishra 应该是encoded=Input(shape=(n_enc))n_enc 是编码器中Flatten 层输出的维数。
    • 谢谢你,先生,但实际上我想将我的扁平层重塑回上述尺寸,即 28*28*4。但是当我给出“重塑(28、28、4)(编码) “命令,它再次显示错误,因为它需要 2 个参数,但给出了 4 个参数。
    • @DiptiMishra 您需要将形状作为元组而不是三个参数传递,即使用Reshape((28, 28, 4))
    • @DiptiMishra 如果您的问题得到解决,请不要忘记对帮助您的回复进行投票并将其标记为答案。
    【解决方案2】:

    您需要更改输入的形状以使其与解码器输出兼容。使用Reshape((28, 28, 4)) 使其匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-11
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2019-06-14
      • 2019-09-13
      • 2011-05-18
      相关资源
      最近更新 更多