当前位置:网站首页>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 !
边栏推荐
- 股票开户免费办理佣金最低的券商,手机上开户安全吗
- AI遮天传 ML-回归分析入门
- Solution to prompt configure: error: curses library not found when configuring and installing crosstool ng tool
- 《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
- Kubernetes Static Pod (静态Pod)
- Four stages of sand table deduction in attack and defense drill
- Malware detection method based on convolutional neural network
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
- 22年秋招心得
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
猜你喜欢
How does the markdown editor of CSDN input mathematical formulas--- Latex syntax summary
Password recovery vulnerability of foreign public testing
浪潮云溪分布式数据库 Tracing(二)—— 源码解析
AI遮天传 ML-回归分析入门
NVIDIA Jetson test installation yolox process record
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
C# 泛型及性能比较
Deep dive kotlin synergy (XXII): flow treatment
深潜Kotlin协程(二十二):Flow的处理
v-for遍历元素样式失效
随机推荐
Binder core API
A brief history of information by James Gleick
德总理称乌不会获得“北约式”安全保障
C# 泛型及性能比较
他们齐聚 2022 ECUG Con,只为「中国技术力量」
Kubernetes Static Pod (静态Pod)
Vscode software
New library launched | cnopendata China Time-honored enterprise directory
letcode43:字符串相乘
C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks
新库上线 | CnOpenData中华老字号企业名录
Invalid V-for traversal element style
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
国外众测之密码找回漏洞
手写一个模拟的ReentrantLock
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
从服务器到云托管,到底经历了什么?
ReentrantLock 公平锁源码 第0篇
v-for遍历元素样式失效
Summary of the third course of weidongshan