简体   繁体   English

Keras Conv2D 输入形状

[英]Keras Conv2D input Shape

I am using Keras for my CNN Model.我正在将 Keras 用于我的 CNN 模型。 In that model, I am training it using images.在那个模型中,我使用图像来训练它。 My images are in shape 256*256.我的图像形状为 256*256。 However I trained it as 64*64.但是我将其训练为 64*64。 When I resize my images as 64*64 and trained again my accuracy rate was decreased drastically.当我将图像大小调整为 64*64 并再次训练时,我的准确率急剧下降。 What am I missing ?我错过了什么?

When I arrange Convolution2D input shape as当我将 Convolution2D 输入形状排列为

classifier.add(Convolution2D(32,3,3,input_shape = (256,256), activation ='relu'))

It takes many time.这需要很多时间。 Because of that I arranged my Convolution2D as classifier.add(Convolution2D(32,3,3,input_shape = (64,64), activation ='relu')) and I trained my first model.因此,我将我的 Convolution2D 安排为classifier.add(Convolution2D(32,3,3,input_shape = (64,64), activation ='relu'))并训练了我的第一个模型。 It predicting very well.它预测得很好。

When I resized my input images shape as 64*64 and training with Convolution2D as当我将输入图像的大小调整为 64*64 并使用 Convolution2D 进行训练时

classifier.add(Convolution2D(32,3,3,input_shape = (64,64) 

my accuracy rate was decreased.我的准确率下降了。 What is the problem ?问题是什么 ?

Here is the code这是代码

classifier = Sequential()
classifier.add(Convolution2D(32,3,3,input_shape = (64,64,3), activation ='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.fit_generator(
        training_set,
        steps_per_epoch=8000,
        epochs=10,
        validation_data=test_set,
        validation_steps=800)

Here is the my reshape code这是我的重塑代码

from PIL import Image
import os
path = 'TestForTrain2'
for file in os.listdir('TestForTrain2'):
    img = Image.open(os.path.join('TestForTrain2', file))
    width, height = img.size
    stringName = str(file)
    print(width," === ",height)
    print(stringName)
    f, e = os.path.splitext(path + file)
    imResize = img.resize((64, 64), Image.ANTIALIAS)
    imResize.save( stringName + '.jpg', 'JPEG', quality=90)

Your model definition does not seem complete, it seems to miss at least a final Dense() layer to perform the actual classification.您的模型定义似乎不完整,似乎至少缺少一个最终的Dense()层来执行实际分类。 However, decreasing the input resolution by 4 in 2 dimensions will decrease your 'raw information' input by 16 (4ˆ2) which, will ultimately negatively impact your prediction accuracy.但是,在 2 维中将输入分辨率降低 4 将使您的“原始信息”输入减少 16 (4^2),这最终会对您的预测准确性产生负面影响。

With a lot less information given it is only logical your model can't predict classes as accurately as before.鉴于信息少得多,您的模型无法像以前那样准确地预测类别是合乎逻辑的。

When to preprocess: This may be image of preprocessing.何时预处理:这可能是预处理的图像。 We only preprocess data when needed because when we pre-process our data we are losing some information.我们只在需要时预处理数据,因为当我们预处理数据时,我们会丢失一些信息。 If we dont pre-process our data in certain cases than algorithms may take time to process big values(not preprocessed data).如果我们在某些情况下不预处理我们的数据,那么算法可能需要时间来处理大值(不是预处理数据)。

  1. We will pre-process data when we have less re-sources to train our model当我们训练模型的资源较少时,我们将预处理数据
  2. We pre-process data because their are very large and very-small values in our data.我们预处理数据,因为它们在我们的数据中非常大和非常小。 than we normalize it standardize it and get our data in some range.比我们标准化它标准化它并在某个范围内获得我们的数据。 like (0,1)像 (0,1)
  3. Their are many other reasons also to preprocess.他们还有许多其他原因也需要预处理。

But we don't preprocess every data.但我们不会预处理每个数据。 You have first know the nature of data and than pre-process it.您首先了解数据的性质,然后对其进行预处理。

Your Solution: Now you are pre-processing your data by resizing your images.您的解决方案:现在您正在通过调整图像大小来预处理数据。 By converting your image from ((256,256)) to ((64,64)) now when you have large size image their are more pixel values in your image and every pixel give some information to us.现在,通过将图像从 ((256,256)) 转换为 ((64,64)),当您拥有大尺寸图像时,图像中的像素值会更多,并且每个像素都会向我们提供一些信息。 Now when you resize your image you have less pixels so that why less information that your model can use to classify.现在,当您调整图像大小时,像素较少,因此模型可用于分类的信息较少。 But when you don't resize your data your machine takes time to process your images.但是当您不调整数据大小时,您的机器需要时间来处理您的图像。 Now find some middle way by experimentation that which size you select which will give enough information to your model and machine will do less effort to process that.现在通过实验找到一些中间方法,即您选择的尺寸将为您的模型提供足够的信息,而机器将减少处理这些信息的工作量。 try (180*180) (164 *164) keep going down until your achieve your required accuracy according to the data.尝试 (180*180) (164 *164) 继续下降,直到根据数据达到所需的精度。

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

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