日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python cnn识别图像_笨方法学习CNN图像识别(一)—— 图片预处理

發布時間:2023/12/1 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python cnn识别图像_笨方法学习CNN图像识别(一)—— 图片预处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

— 全文閱讀5分鐘 —

在本文中,你將學習到以下內容:

通過數據增強增加樣本量

調整圖片大小便于網絡訓練

前言

圖像識別的準備工作就是要對我們拿到手的樣本圖片進行預處理,具體就是數據增強和調整圖片大小,這些準備工作都是為訓練網絡做準備。圖片預處理一定要合理有效,符合機器學習的要求。

數據增強(data augmentation)

當我們拿到一套圖片數據準備進行機器學習的時候,樣本量往往不夠多,因此需要對現有的圖片進行數據增強。一方面是為了增加樣本量,另一方面能夠提高模型的泛化能力。

假設我們有一組商標圖片,如下:

商標圖片

當我們進行100類的機器學習時,顯然這一類的樣本量不夠多,在這里我們通過keras庫進行數據增強。以商標圖片中的第一張圖片為例:

from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img

pic_path = r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'

augmentation_path = r'./data_augmentation'

1

2

3

4

fromkeras.preprocessing.imageimportImageDataGenerator,img_to_array,load_img

pic_path=r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'

augmentation_path=r'./data_augmentation'

首先導入keras庫,建立圖片路徑和數據增強保存路徑,接下來定義ImageDataGenerator,告訴他通過哪些操作產生新的圖片。

data_gen = ImageDataGenerator(

rotation_range=30,

width_shift_range=0.1,

height_shift_range=0.1,

zoom_range=0.2,

fill_mode='nearest')

1

2

3

4

5

6

data_gen=ImageDataGenerator(

rotation_range=30,

width_shift_range=0.1,

height_shift_range=0.1,

zoom_range=0.2,

fill_mode='nearest')

在這里根據當前的圖片需求,選擇了旋轉、平移、縮放、邊緣填充的操作,其他操作詳見。有些操作的設置要符合實際情況,比如旋轉操作,不能把圖片完全倒立了,這樣的數據增強反而不利機器學習。

img = load_img(pic_path)

x = img_to_array(img)

x = x.reshape((1,) + x.shape)

n = 1

for batch in data_gen.flow(x, batch_size=1, save_to_dir=augmentation_path, save_prefix='train', save_format='jpeg'):

n += 1

if n > 6: # 6表示生成6張新的圖片

break

1

2

3

4

5

6

7

8

img=load_img(pic_path)

x=img_to_array(img)

x=x.reshape((1,)+x.shape)

n=1

forbatchindata_gen.flow(x,batch_size=1,save_to_dir=augmentation_path,save_prefix='train',save_format='jpeg'):

n+=1

ifn>6:# 6表示生成6張新的圖片

break

加載圖片的地址,轉變成array格式給ImageDataGenerator,save_prefix表示新圖片的名字前綴,save_format表示新圖片保存的格式。需要注意的是,在這里根據我們定義的操作,從這些操作中隨機選擇幾種生成6張圖片。

最終在data_augmentation文件夾中生成6張新的商標圖片:

新的商標圖片

在實際操作中,應該多去嘗試數據增強的各種操作。好的樣本擴充能夠增加模型的泛化能力,提高準確率。數據增強完整代碼如下:

from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img

pic_path = r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'

augmentation_path = r'./data_augmentation'

data_gen = ImageDataGenerator(

rotation_range=30,

width_shift_range=0.1,

height_shift_range=0.1,

zoom_range=0.2,

fill_mode='nearest')

img = load_img(pic_path)

x = img_to_array(img)

x = x.reshape((1,) + x.shape)

n = 1

for batch in data_gen.flow(x, batch_size=1, save_to_dir=augmentation_path, save_prefix='train', save_format='jpeg'):

n += 1

if n > 6:

break

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

fromkeras.preprocessing.imageimportImageDataGenerator,img_to_array,load_img

pic_path=r'./3ac79f3df8dcd100755525327e8b4710b8122fdc.jpg'

augmentation_path=r'./data_augmentation'

data_gen=ImageDataGenerator(

rotation_range=30,

width_shift_range=0.1,

height_shift_range=0.1,

zoom_range=0.2,

fill_mode='nearest')

img=load_img(pic_path)

x=img_to_array(img)

x=x.reshape((1,)+x.shape)

n=1

forbatchindata_gen.flow(x,batch_size=1,save_to_dir=augmentation_path,save_prefix='train',save_format='jpeg'):

n+=1

ifn>6:

break

圖片大小調整(resize)

統一調整圖片的大小,便于后面進行機器學習。我們以調整data_augmentation文件夾生成的新圖片為例:

from PIL import Image

import os

img_path = r'./data_augmentation'

resize_path = r'./resize_image'

for i in os.listdir(img_path):

im = Image.open(os.path.join(img_path,i))

out = im.resize((224, 224))

if not os.path.exists(resize_path):

os.makedirs(resize_path)

out.save(os.path.join(resize_path, i))

1

2

3

4

5

6

7

8

9

10

11

12

fromPILimportImage

importos

img_path=r'./data_augmentation'

resize_path=r'./resize_image'

foriinos.listdir(img_path):

im=Image.open(os.path.join(img_path,i))

out=im.resize((224,224))

ifnotos.path.exists(resize_path):

os.makedirs(resize_path)

out.save(os.path.join(resize_path,i))

使用PIL庫改變圖片大小,使用os庫讀取文件路徑,將resize后的圖片放到resize_image文件夾中。resize后的大小為224*224(這個大小是為了后面ResNet使用)。resize后的圖片效果如下:

resize后的圖片

當你完成這一步的時候,圖像識別的準備工作就完成一半了,剩下的就是將這些圖片制成tfrecord格式,方便訓練網絡讀取。

總結

以上是生活随笔為你收集整理的python cnn识别图像_笨方法学习CNN图像识别(一)—— 图片预处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。