我正试着用CNN对狗的繁殖鉴定进行分类。我已将图像转换为灰度,并重新缩放以缩小大小。所以现在我试着把它们添加到numpy数组中并进行训练。此外,我将使用Relu激活函数,因为它表现良好的多层和分类交叉熵为不同类别的狗育种。
以下是灰度和重新缩放的代码:
def RescaleGrayscaleImg():
# iterate through the names of contents of the folder
for image_path in os.listdir(path):
# create the full input path and read the file
input_path = os.path.join(path, image_path)
# make image grayscale
img = io.imread(input_path)
img_scaled = rescale(img, 2.0 / 4.0)
GrayImg = color.rgb2gray(img_scaled)
# create full output path, 'example.jpg'
# becomes 'grayscaled_example.jpg', save the file to disk
fullpath = os.path.join(outPath, 'grayscaled_'+image_path)
misc.imsave(fullpath, GrayImg)
如何将图像转换为数组?每一列都是一个图像?任何帮助都会有帮助的。
对于CNN,您的输入必须是一个4-D张量
[batch_size, width, height, channels]
,因此每个图像都是一个3-D子张量。因为图像是灰度的,channels=1
。同样,对于训练,所有图像必须具有相同的大小-WIDTH
和HEIGHT
。^{} 返回的是一个
ndarray
,这对keras非常有效。所以你可以这样读取数据:不知道如何存储标签,但也需要创建一个标签数组。我称之为
y_train
。你可以把它转换成这样一个热的:keras中的模型是非常严格的,这里是最简单的模型(使用relu和x-熵):
可以找到一个完整的工作MNIST示例here。
相关问题 更多 >
编程相关推荐