Keras训练模型与图像

时间:2018-03-14 14:59:40

标签: python keras

我第一次在数据集上训练模型,这是来自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文件夹中的图像时。

1 个答案:

答案 0 :(得分:0)

塞缪尔,

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

另一个暗示我,你关心某事物是否属于类别的单一概率是你的损失函数是binary_crossentropy。

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

您似乎暗示您想要创建多个标签(例如,汽车,船,火车)。如果是这种情况,您将需要在列车下创建这些文件夹并验证并将图像放在相应的文件夹中。如果你这样做,你需要改变你的模型的几个方面。您的损耗,输出图层大小和输出图层激活将相应更改。