我想通過 Keras 上的以下代碼對 8*8 像素灰度影像進行資料增強(像素值只有 0 和 1):
from ctypes import sizeof
from re import X
from turtle import shape
from keras.preprocessing.image import ImageDataGenerator
from skimage import io
import numpy as np
from PIL import Image
datagen = ImageDataGenerator(
rotation_range=45, #Random rotation between 0 and 45
width_shift_range=0.2, #% shift
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest') #Also try nearest, constant, reflect, wrap
# forming a binary 8*8 array
array = np.array([[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,1,0],[0,0,0,0,0,0,0,0]])
# scale values to uint8 maximum 255, and convert it to greyscale image
array = ((array) * 255).astype(np.uint8)
x = Image.fromarray(array)
i = 0
for batch in datagen.flow(x, batch_size=16,
save_to_dir='augmented',
save_prefix='aug',
save_format='png'):
i = 1
if i > 20:
break # otherwise the generator would loop indefinitely
但是我在輸出中得到這個錯誤(當我有.flow函式時):
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (8, 8))
誰能給我一些手嗎?
uj5u.com熱心網友回復:
ImageDataGenerator接受輸入為 4 維張量,其中第一個維度是樣本數,最后一個維度是顏色通道。在您的代碼中,您應該將此(8,8)張量轉換為(1,8,8,1)張量。這可以通過
array = np.expand_dims(array, (0, -1))
此外,您不應該在將陣列傳遞給生成器之前將其轉換為影像,就像您在此處所做的那樣
x = Image.fromarray(array)
你應該簡單地傳遞array給生成器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514727.html
下一篇:過濾器/查詢復選框
