首页 > 解决方案 > 如何将多个图像作为输入提供给卷积神经网络

问题描述

我对CNN很陌生。我计划构建一个分类器,您将在其中将两个图像作为输入提供给分类器。它应该输出它是否“匹配”。

我不确定从哪里开始以及如何输入两个图像并训练神经网络。如果您可以发布示例代码,那将有很大帮助。请帮忙

谢谢你

标签: image-processingclassificationconv-neural-network

解决方案


您首先需要获取这两个图像并将它们放入一个数组中。因此,如果每个图像都是 26x26,那么阵列形状应该是 2x26x26。现在您必须将这些数组中的每一个都放入您的训练数据数组中,但请确保在训练之前将您的训练数据数组重塑为 26x26x2。您可以通过键入numpy.array(your_array_.reshape(-1, 26, 26, 2)您的 fit 函数输入来执行此操作。

这是一个例子:

import numpy as np

image1 = # put your image array here
image2 = # put other image array here
both_images = [image1, image2]

training_data.append(both_images) # Feel free to add as much training data as you would like

same = 0
labels = [same]

model = create_model() # Make a function to create your model and set your model to a variable

model.fit(np.array(training_data).reshape(-1, 26, 26, 2), np.array(labels), batch_size=32)

推荐阅读