[英]Keras showing images from data generator
我正在為 keras 使用圖像生成器,如下所示:
val_generator = datagen.flow_from_directory(
path+'/valid',
target_size=(224, 224),
batch_size=batch_size,)
x,y = val_generator.next()
for i in range(0,1):
image = x[i]
plt.imshow(image.transpose(2,1,0))
plt.show()
我有兩個問題。
如何解決問題
如何獲取文件的文件名(以便我可以自己從 matplotlib 之類的東西中讀取它)
編輯:這就是我的 datagen 的樣子
datagen = ImageDataGenerator(
rotation_range=3,
# featurewise_std_normalization=True,
fill_mode='nearest',
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True
)
編輯2:
在遵循 Marcin 的回答之后:
image = 255 - image
我得到了正常的顏色,但仍然有一些奇怪的顏色:
您的圖像數組的 dtype 是 'float32',只需將其轉換為 'uint8':
plt.imshow(image.astype('uint8'))
我遇到了與 OP 相同的問題,並通過將像素從 0-255 重新縮放到 0-1 來解決它。
Keras 的 ImageDataGenerator 采用“rescale”參數,我將其設置為 (1/255)。 這產生了具有預期顏色的圖像
image_gen = ImageDataGenerator(rescale=(1/255))
至少有三種方法可以獲得這種扭曲的顏色。 所以:
255 - x
轉換),這在使用某些 GIS 庫時有時會發生。score/255
轉換。您需要檢查在您的情況下發生了哪些選項。
為了自己獲取圖像,我通常使用(當您的文件夾具有適用於flow_from_directory
的格式時)我通常使用os.listdir
和os.path.join
的混合:
list_of_labels = os.listdir(path_to_dir_with_label_dirs) for label in list_of_labels: current_label_dir_path = os.path.join(path_to_dir_with_label_dirs, label list_of_images = os.listdir(current_label_dir_path) for image in list_of_images: current_image_path = os.path.join(current_label_dir_path, image) image = open(current_image_path) # use the function which you want.
顏色問題比較奇怪。 一旦我可以訪問我的 linux 機器,我將嘗試重現它。
對於問題的文件名部分,我想對 Keras 源代碼進行一個小改動:
你可能想看看這個文件: https : //github.com/fchollet/keras/blob/master/keras/preprocessing/image.py它包含圖像預處理例程。
查看第 820 行, DirectoryIterator
的next()
函數:調用該函數以從目錄中獲取新圖像。
在該函數內部,查看第 838 行,如果save_to_dir
已設置為路徑,則生成器會將增強圖像輸出到此路徑,以用於調試目的。 增強圖像的名稱是索引和哈希的混合。 對你沒用。
但是您可以很容易地更改代碼:
filenames=[] #<-------------------------------------------- new code
for i, j in enumerate(index_array):
fname = self.filenames[j]
img = load_img(os.path.join(self.directory, fname),
grayscale=grayscale,
target_size=self.target_size)
x = img_to_array(img, dim_ordering=self.dim_ordering)
x = self.image_data_generator.random_transform(x)
x = self.image_data_generator.standardize(x)
filenames.append(fname) # <-----------------------------store the used image's name
batch_x[i] = x
# optionally save augmented images to disk for debugging purposes
if self.save_to_dir:
for i in range(current_batch_size):
img = array_to_img(batch_x[i], self.dim_ordering, scale=True)
#fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
# index=current_index + i,
# hash=np.random.randint(1e4),
# format=self.save_format)
fname=filenames[i] # <------------------------------ use the stored code instead
img.save(os.path.join(self.save_to_dir, fname))
現在增強圖像以原始文件名保存。
這應該允許您以原始文件名保存圖像。 好的,您實際上如何將其注入 Keras 源?
像這樣做:
git clone https://github.com/fchollet/keras
.
# this is the path to the cloned repository
# if you cloned it next to your script
# then just use keras/
# if it's one folder above
# then use ../keras/
sys.path.insert(0, os.getcwd() + "/path/to/keras/")
import keras
現在DirectoryIterator
是您的修補版本。
我希望這有效,我目前在 Windows 上。 我的 python 堆棧僅在 linux 機器上。 可能有一個小的語法錯誤。
from skimage import io
def imshow(image_RGB):
io.imshow(image_RGB)
io.show()
x,y = train_generator.next()
for i in range(0,11):
image = x[i]
imshow(image)
這個對我有用。
如果您使用test_batches=Imagedatagenerator().flow
from directory,請提供一點建議。 如果您使用它來提供預測生成器,請確保設置shuffle=false
以保持文件和相關預測之間的相關性。 如果您在目錄中有數字標記的文件,例如1.jpg 、 2.jpg等。圖像不會像您想象的那樣獲取。 它們按以下順序獲取: 1.jpg 、 10.jpg 、 2.jpg 、 20.jpg等。這使得很難將預測與特定文件相匹配。 您可以通過使用 0 的填充來解決此問題,例如01.jpg 、 02.jpg等。在問題的第二部分“我如何獲取生成器生成的文件,您可以按如下方式獲取這些文件:
for file in datagen.filenames:
file_names.append(file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.