简体   繁体   English

将图像转换为 CNN 的数组

[英]Convert image to array for CNN

I am trying to categorize the dog breeding identification using CNN.我正在尝试使用 CNN 对狗的繁殖识别进行分类。 I have converted the images to gray scale and re-scaled them in order to be smaller in size.我已将图像转换为灰度并重新缩放它们以缩小尺寸。 So now I am trying to add them in numpy array and do the training.所以现在我试图将它们添加到 numpy 数组中并进行培训。 Also I will use Relu activation function because it performs well with multi layer and a categorical cross entropy for the different categories of dog breeding.此外,我将使用 Relu 激活函数,因为它在多层和分类交叉熵下对不同类别的狗繁殖表现良好。

Below is the code for grayscale and re-scale:下面是灰度和重新缩放的代码:

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)

How I will convert the images to array?我将如何将图像转换为数组? Will each column be an image?每列都是一个图像吗?

For CNN, your input must be a 4-D tensor [batch_size, width, height, channels] , so each image is a 3-D sub-tensor.对于 CNN,您的输入必须是 4-D 张量[batch_size, width, height, channels] ,因此每个图像都是 3-D 子张量。 Since your images are gray-scale, channels=1 .由于您的图像是灰度的,所以channels=1 Also for training all images must be of the same size - WIDTH and HEIGHT .同样对于训练,所有图像必须具有相同的大小 - WIDTHHEIGHT

The skimage.io.imread is returning an ndarray , and this works perfectly for keras. skimage.io.imread返回一个ndarray ,这对 keras 非常有效。 So you can read the data like this:所以你可以像这样读取数据:

all_images = []
for image_path in os.listdir(path):
  img = io.imread(image_path , as_grey=True)
  img = img.reshape([WIDTH, HEIGHT, 1])
  all_images.append(img)
x_train = np.array(all_images)

Not sure how you store the labels, but you'll need to make an array of labels as well.不确定如何存储标签,但您还需要制作一系列标签。 I call it y_train .我称之为y_train You can convert it to one-hot like this:您可以像这样将其转换为 one-hot:

y_train = keras.utils.to_categorical(y_train, num_classes)

The model in keras is pretty straighforward, here's the simplest one (uses relu and x-entropy): keras 中的模型非常简单,这是最简单的模型(使用 relu 和 x-entropy):

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', 
                 input_shape=[WIDTH, HEIGHT, 1]))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=100, epochs=10, verbose=1)

A complete working MNIST example can be found here .可以在此处找到完整的 MNIST 示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM