使用Augmentor图像生成的训练Keras模型

时间:2019-04-10 17:11:55

标签: python tensorflow keras augmentor

我正尝试使用来自Augmentor库的keras_generator来训练keras模型,麻烦的是我无法获得比我已有的更多图像。

现在我有一个目录,上面有小节:

images/
 0/0.png
 1/1.png
 ...
 z/z.png

我运行以下python代码:

import Augmentor
pipeline = Augmentor.Pipeline("images")
pipeline.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10)
pipeline.zoom(probability=0.5, min_factor=1.1, max_factor=1.3)
pipeline.random_distortion(probability=0.8, grid_width=4, grid_height=4, magnitude=1)
pipeline.greyscale(1.0)
pipeline.invert(1.0)
pipeline.status()

哪个给出输出:

    Initialised with 22 image(s) found.
Output directory set to images/output.Operations: 5
    0: RotateRange (probability=0.7 max_left_rotation=-10 max_right_rotation=10 )
    1: Zoom (probability=0.5 min_factor=1.1 max_factor=1.3 )
    2: Distort (probability=0.8 grid_width=4 grid_height=4 magnitude=1 randomise_magnitude=True )
    3: Greyscale (probability=1.0 )
    4: Invert (probability=1.0 )
Images: 22
Classes: 22
    Class index: 0 Class label: 0 
    ...
    Class index: 20 Class label: x 
    Class index: 21 Class label: y 
Dimensions: 1
    Width: 28 Height: 28
Formats: 1
     PNG

然后我创建一个模型:

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.optimizers import Adam
from keras.layers import Conv2D, Input, Dropout, Flatten, Dense, MaxPooling2D

img_input = Input(shape=(28, 28, 1))
x = Conv2D(32, 3, activation='relu')(img_input)
x = Conv2D(64, 3, activation='relu')(x)
x = MaxPooling2D(2, 2)(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
output = Dense(22, activation='softmax')(x)

model = Model(img_input, output)
model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['acc'])

并为其提供发电机进行训练:

generator = pipeline.keras_generator(batch_size=128)
model.fit_generator(generator, steps_per_epoch=len(pipeline.augmentor_images)/128, epochs=5, verbose=1)

哪个重播了我的输出

    WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/5
1/0 [==============================================================================================================================================================================] - 1s 1s/step - loss: 3.1237 - acc: 0.0312
Epoch 2/5
1/0 [==============================================================================================================================================================================] - 0s 361ms/step - loss: 2.9429 - acc: 0.1797
Epoch 3/5
1/0 [==============================================================================================================================================================================] - 0s 298ms/step - loss: 2.7841 - acc: 0.2188
Epoch 4/5
1/0 [==============================================================================================================================================================================] - 0s 417ms/step - loss: 2.4919 - acc: 0.3672
Epoch 5/5
1/0 [==============================================================================================================================================================================] - 0s 439ms/step - loss: 2.2927 - acc: 0.3438

因此,它只看到适当目录下图像目录中的一张图像。我该如何喂它成千上万个不保存图片的磁盘?

0 个答案:

没有答案