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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

【TensorFlow-windows】keras接口——ImageDataGenerator裁剪

發布時間:2023/12/13 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【TensorFlow-windows】keras接口——ImageDataGenerator裁剪 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

Keras中有一個圖像數據處理器ImageDataGenerator,能夠很方便地進行數據增強,并且從文件中批量加載圖片,避免數據集過大時,一下子加載進內存會崩掉。但是從官方文檔發現,并沒有一個比較重要的圖像增強方式:隨機裁剪,本博客就是記錄一下如何在對ImageDataGenerator中生成的batch做圖像裁剪

國際慣例,參考博客:

官方ImageDataGenerator文檔

Keras 在fit_generator訓練方式中加入圖像random_crop

Extending Keras’ ImageDataGenerator to Support Random Cropping

how to use fit_generator with multiple image inputs

第二個博客比較全,第三個博客只介紹了分類數據的增強,如果是圖像分割或者超分辨率,輸出仍是一張圖像,所以涉及到對image和mask進行同步增強

代碼

先介紹一下數據集目錄結構:

在test文件夾下,分別有GT和NGT兩個文件夾,每個文件夾存儲的都是bmp圖像文件

其次需要注意,從ImageDataGenerator中取數據用的是next(generator)函數

  • 載入相關包

    from keras_preprocessing.image import ImageDataGenerator import matplotlib.pyplot as plt import numpy as np
  • 先使用自帶的ImageDataGenerator配合flow_from_director讀取數據
    創建生成器

    train_img_datagen=ImageDataGenerator()#各種預處理 train_mask_datagen=ImageDataGenerator()#各種預處理

    讀取文件

    seed=2 #圖像會隨機打亂即shuffle,但是輸入和輸出的打亂順序必須一樣 batch_size=2 target_size=(1080,1920) train_img_gen=train_img_datagen.flow_from_directory('./test',classes=['NGT'],class_mode=None,batch_size=batch_size,target_size=target_size,shuffle=True,seed=seed,interpolation='bicubic') train_mask_gen=train_img_datagen.flow_from_directory('./test',classes=['GT'],class_mode=None,batch_size=batch_size,target_size=target_size,shuffle=True,seed=seed,interpolation='bicubic')

    封裝打包

    train_generator=zip(train_img_gen,train_mask_gen)
  • 定義裁剪器,裁剪圖像和對應的mask:

    def crop_generator(batch_gen,crop_size=(270,480)):while True:batch_x,batch_y=next(batch_gen)crops_img=np.zeros((batch_x.shape[0],crop_size[0],crop_size[1],3))crops_mask=np.zeros((batch_y.shape[0],crop_size[0],crop_size[1],3))height,width=batch_x.shape[1],batch_x.shape[2]for i in range(batch_x.shape[0]):#裁剪圖像x=np.random.randint(0,height-crop_size[0]+1)y=np.random.randint(0,width-crop_size[1]+1)crops_img[i]=batch_x[i,x:x+crop_size[0],y:y+crop_size[1]]crops_mask[i]=batch_y[i,x:x+crop_size[0],y:y+crop_size[1]]yield (crops_img,crops_mask)
  • 使用裁剪器對Generator進行裁剪

    train_crops=crop_generator(train_generator)

可視化:

img,mask=next(train_crops) print(img.shape) plt.subplot(2,1,1) plt.imshow(img[0]/255) plt.subplot(2,1,2) plt.imshow(mask[0]/255)

后記

記住要用while(True)死循環,并且yield在while循環內部,和for循環外部,代表每個批次

代碼:
鏈接:https://pan.baidu.com/s/1UNZLke5kygBFHJ8iR8wV2A
提取碼:e51e

總結

以上是生活随笔為你收集整理的【TensorFlow-windows】keras接口——ImageDataGenerator裁剪的全部內容,希望文章能夠幫你解決所遇到的問題。

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