当前位置:网站首页>Image data preprocessing

Image data preprocessing

2022-07-08 00:55:00 booze-J

1. Download datasets

First of all, we need to download the cat and dog data set on the Internet :

 Cat and dog classification data set download address :https://pan.baidu.com/s/1i4SKqWH
 password :d8mt

2. Data set partitioning

Data just downloaded train and test They are all pictures of cats and dogs , It needs to be modified and re divided train and test The cat and dog in are divided separately . The file structure is as follows :

|_image
    |_train
        |_dog
        |_cat
    |_test
        |_dog
        |_cat

Due to the problem of training time , It's just... Here 2000 Picture for training ,1000 Picture to verify . You can decide the size of the training and test data set .

3. Data preprocessing code

The code running platform is jupyter-notebook, Code blocks in the article , According to jupyter-notebook Written in the order of division in , Run article code , Glue directly into jupyter-notebook that will do .

from keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img
  • rotation_range It's a 0~180 Degrees , Used to specify the angle of randomly selected pictures
  • width_shift and height_shift Used to specify the degree of random movement in horizontal and vertical directions , These are two 0~1 Ratio between
  • rescale The value will be multiplied over the entire image before performing other processing , Our image is RGB All the channels are 0~255 The integer of , Such an operation may make the value of the image too high or too low , So we set this value as 0~1 Number between
  • shear_range Is the degree of shear transformation , Reference shear transform
  • zoom_range For random amplification
  • horizontal_flip Randomly flip the picture horizontally , This parameter is applicable when the horizontal flip does not affect the image semantics
  • fill_mode Used to specify when pixel filling is required , Like rotation 、 Horizontal and vertical displacement , How to fill new pixels
datagen = ImageDataGenerator(
    rotation_range=40,      #  Random rotation angle 
    width_shift_range=0.2,  #  Random horizontal translation 
    height_shift_range=0.2, #  Random vertical translation 
    rescale=1./255,         #  Normalization of values 
    shear_range=0.2,        #  Random cutting 
    zoom_range=0.2,         #  Random amplification 
    horizontal_flip=True,   #  Flip horizontal 
    fill_mode="nearest"     #  fill style 
)

Here we use a picture to demonstrate the effect of data processing :

#  Load Images 
img = load_img("./image/train/cat/cat.1.jpg")
#  Convert picture to array data format 
x = img_to_array(img)
# (280, 300, 3) = (H,W,channels)
print(x.shape)
#  Add a dimension to the picture   This dimension is mainly added because a four-dimensional picture is required during training 
x = x.reshape((1,)+x.shape)
# (1, 280, 300, 3) = (batch_size,H,W,channels)
print(x.shape)
i = 0
#  Generate 21 A picture 
# flow Randomly generated image  save_prefix Prefix the newly generated name 
for batch in datagen.flow(x,batch_size=1,save_to_dir='temp',save_prefix="cat",save_format="jpeg"):
    #  perform 20 Time 
    i += 1
    if i>20:
        break

Pictures of the test :
 Insert picture description here
Code run results :
 Insert picture description here
You can see that the effect of this data enhancement is still good !

原网站

版权声明
本文为[booze-J]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072310361060.html