当前位置:网站首页>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 :
Code run results :
You can see that the effect of this data enhancement is still good !
边栏推荐
- New library online | information data of Chinese journalists
- Password recovery vulnerability of foreign public testing
- SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
- Stock account opening is free of charge. Is it safe to open an account on your mobile phone
- fabulous! How does idea open multiple projects in a single window?
- Invalid V-for traversal element style
- NTT template for Tourism
- ReentrantLock 公平锁源码 第0篇
- Kubernetes static pod (static POD)
- How to add automatic sorting titles in typora software?
猜你喜欢

Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现

Service Mesh介绍,Istio概述

赞!idea 如何单窗口打开多个项目?

基于人脸识别实现课堂抬头率检测

Cve-2022-28346: Django SQL injection vulnerability
![Cause analysis and solution of too laggy page of [test interview questions]](/img/8d/3ca92ce5f9cdc85d52dbcd826e477d.jpg)
Cause analysis and solution of too laggy page of [test interview questions]

Play sonar

Jouer sonar

《因果性Causality》教程,哥本哈根大学Jonas Peters讲授

From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
随机推荐
深潜Kotlin协程(二十二):Flow的处理
FOFA-攻防挑战记录
Course of causality, taught by Jonas Peters, University of Copenhagen
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
Basic mode of service mesh
赞!idea 如何单窗口打开多个项目?
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
Password recovery vulnerability of foreign public testing
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
Is it safe to speculate in stocks on mobile phones?
DNS 系列(一):为什么更新了 DNS 记录不生效?
Su embedded training - day4
新库上线 | 中国记者信息数据
Su embedded training - Day3
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
Service Mesh介绍,Istio概述
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
fabulous! How does idea open multiple projects in a single window?
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
C# 泛型及性能比较