【问题标题】:Creating a Dataset to input only Images创建数据集以仅输入图像
【发布时间】:2019-06-03 05:38:43
【问题描述】:

我需要一个仅包含图像的数据集对象,用于在 Chainer 框架中进行无监督学习。为此,我正在尝试使用 DatasetMixin。

Images 是一个包含图像的列表。

class SimpleDataset(dataset.DatasetMixin):
    def __init__(self, Images):
        self.Images = Images
    def __len__(self):
        return len(self.Images)
    def get_example(self, i):
        return self.Images[i]

SimpleDataset 类似乎无法读取图像,因为在运行 trainer.run() 时出现错误:

call() missing 1 required positional argument: 'x'

在将图像列表放入 DatasetMixin 类之前,我是否需要进一步处理它?

使用 DatasetMixin 以这种方式只提供图像有什么问题吗?

我可以做些什么来只为我的模型提供图像(没有任何标签或其他东西)?

class AutoEncoder(chainer.Chain):
    def __init__(self, n_in, n_out):
        super(AutoEncoder, self).__init__(
            l1 = L.Linear(n_in, n_out),
            l2 = L.Linear(n_out, n_in)
    )
        self.add_param('decoder_bias', n_in)
        self.decoder_bias.data[...] = 0

    def __call__(self, x):
        h1 = F.dropout(self.l1(x))
        h2 = F.linear(h1, F.transpose(self.l1.W), self.decoder_bias)
        return F.sigmoid(h2)

    def encode(self, x):
        return F.dropout(self.l1(x))

    def decode(self, x):
        return self.l2(x)

model = L.Classifier(AutoEncoder(40000, 1000), lossfun=F.mean_squared_error)
model.compute_accuracy = False

【问题讨论】:

  • 你能分享你的模型定义吗?您是否使用了Classifier 或一些包装模型?数据集定义本身看起来不错。
  • 嗨@corochann,我已经编辑了问题以添加模型定义。
  • 由于我使用了分类器,所以我尝试使用 model = AutoEncoder(x, y)。在这种情况下,错误是 AttributeError: 'NoneType' object has no attribute 'dtype'

标签: unsupervised-learning chainer imagedata


【解决方案1】:

如果您使用Classifier,则数据集需要以x0, x1, ... xk, y 格式返回,其中x0, x1, ... xk 将被输入predictor(在本例中为AutoEncoder 类)及其输出值y_pred 和实际的y 用于lossfun 指定的损失计算。

在您的情况下,答案y 也与输入相同。我认为您可以编写以下内容来返回 xy,这实际上是相同的:

def get_example(self, i):
    return self.Images[i], self.Images[i]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2020-09-10
    相关资源
    最近更新 更多