为编码-解码图像构建分层智能模型

时间:2019-04-26 05:22:29

标签: image-processing autoencoder chainer

我正在编写一个用于图像编码解码问题的自动编码器模型。 我想了解适用于图像的模型每一层中的节点分布。

对于以下代码,我正在使用10个形状为(21 * 28 * 3)的图像。

class Autoencoder(Chain):
  def __init__(self, activation=F.relu):
    super().__init__()
    with self.init_scope():
  # encoder part
      self.l1 = L.Linear(1764,882)
      self.l2 = L.Linear(882,441)
  # decoder part
      self.l3 = L.Linear(441,882)
      self.l4 = L.Linear(882,1764)
      self.activation = activation

  def forward(self,x):
      h = self.encode(x)
      x_recon = self.decode(h)
      return x_recon

  def __call__(self,x):
      x_recon = self.forward(x)
      loss = F.mean_squared_error(h, x)
      return loss

  def encode(self, x):
      h = F.dropout(self.activation(self.l1(x)))
      return self.activation(self.l2(x))

  def decode(self, h, train=True):
      h = self.activation(self.l3(h))
      return self.l4(x)

gpu_id = 0
n_epoch = 5
batch_size = 2

model = Autoencoder()

optimizer = optimizers.SGD(lr=0.05).setup(model)
train_iter = iterators.SerialIterator(xs,batch_size)
valid_iter = iterators.SerialIterator(xs,batch_size)

updater = training.StandardUpdater(train_iter,optimizer)
trainer = training.Trainer(updater,(n_epoch,"epoch"),out="result")

from chainer.training import extensions
trainer.extend(extensions.Evaluator(valid_iter, model, device=gpu_id))

正在运行trainer.run()时

InvalidType: 
Invalid operation is performed in: LinearFunction (Forward)
Expect: x.shape[1] == W.shape[1]
Actual: 1764 != 882

我想了解节点分布在模型中如何逐层工作。请提出任何资源。还有在训练图像数量较少的情况下如何分层分配节点。

0 个答案:

没有答案