【问题标题】:Keras training model with images带有图像的 Keras 训练模型
【发布时间】:2018-03-14 14:59:40
【问题描述】:

我第一次在数据集上训练模型,这是来自 pandas 数据集的数据

In [16]: exists.head()
Out[16]: 
                 id                                                url  \
1  0a58358a2afd3e4e  http://lh6.ggpht.com/-igpT6wu0mIA/ROV8HnUuABI/...   
2  6b2bb500b6a38aa0  http://lh6.ggpht.com/-vKr5G5MEusk/SR6r6SJi6mI/...   
3  b399f09dee9c3c67  https://lh3.googleusercontent.com/-LOW2cjAqubA...   
4  19ace29d77a5be66  https://lh5.googleusercontent.com/-tnmSXwQcWL8...   
5  2c9c54b62f0a6a37  https://lh5.googleusercontent.com/-mEaSECO7D-4...   
   landmark_id  exists                              filename  
1         6651    True  training_images/0a58358a2afd3e4e.jpg  
2        11284    True  training_images/6b2bb500b6a38aa0.jpg  
3         8429    True  training_images/b399f09dee9c3c67.jpg  
4         6231    True  training_images/19ace29d77a5be66.jpg  
5        10400    True  training_images/2c9c54b62f0a6a37.jpg 

filename中显示训练图像,在landmark_id中显示分类名称

这是我编写模型来训练它的方式

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Dense(activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=0, mode='auto')
checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) # save best model

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              callbacks=[monitor,checkpointer],
              verbose=0,
              epochs=1000,
              metrics=['accuracy'])

batch_size = 16

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)

# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
        'training_images',  # this is the target directory
        target_size=(150, 150),  # all images will be resized to 150x150
        batch_size=batch_size,
        class_mode='binary')  # since we use binary_crossentropy loss, we need binary labels

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        'test_images',
        target_size=(150, 150),
        batch_size=batch_size,
        class_mode='binary')

model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // batch_size,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=800 // batch_size)
model.load_weights('best_weights.hdf5') # load weights from best model
model.save('last_model.h5')

我不知道在训练时应该如何将标签放在图像上。因此,当它训练和循环遍历 training_images 文件夹中的图像时。

【问题讨论】:

    标签: python keras


    【解决方案1】:

    塞缪尔,

    您的 FitGenerator 正在从 flow_from_directory 方法获取训练输入标签。此方法使用文件夹结构来确定训练类别。由于您的类是二进制的,并且您有一个 sigmoid 输出,我假设您正在做一个 Hot Dog - Not Hot Dog 类型的分类,您只需要一个概率值。

    对我来说,您关心某事物是否为类别的单一概率的另一个提示是您的损失函数是 binary_crossentropy。

    检查您的训练数据文件夹。看看数据是如何组织的。应该这样设置,以便正确组织数据。

    您似乎在暗示要创建多个标签(例如,汽车、船、火车)。如果是这种情况,您将需要在 train 下创建这些文件夹并验证并将图像放入相应的文件夹中。但是,如果您这样做,您将需要更改有关模型的一些内容。您的损失、输出层大小和输出层激活值会相应改变。

    【讨论】:

    • 我实际上是基于我在图像训练中找到的最佳示例。我已将其编辑为分类,实际上熊猫读取的 csv 中的文件名字段有一个标签,该标签是一个数字,其中至少有 1000 个。我确实有超过 200,000 张图片,所以我也不认为它会过度拟合。
    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 2020-07-16
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2018-04-15
    相关资源
    最近更新 更多