简体   繁体   English

带图像的Keras训练模型

[英]Keras training model with images

My first go at training a model on a dataset, this is the data from a pandas dataset 我的第一步是在数据集上训练模型,这是来自熊猫数据集的数据

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 

it shows the training image in filename and the classification name in landmark_id 它以filename显示训练图像,以landmark_id filename显示分类名称

This is the way I've written the model to train it 这就是我编写模型进行训练的方式

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')

I don't know how I'm supposed to put the labels to the image while training. 我不知道训练时应该如何在图像上贴标签。 So when it trains and loops through the images in the training_images folder. 因此,当它训练并循环浏览training_images文件夹中的图像时。

Samuel, 塞缪尔

Your FitGenerator is getting the training input labels from the flow_from_directory method. 您的FitGenerator正在从flow_from_directory方法获取训练输入标签。 This method uses the folder structure to determine the training categories. 此方法使用文件夹结构来确定训练类别。 Since your class is binary, and you have a single sigmoid output, I'm assuming that you are doing a Hot Dog - Not Hot Dog type of classification where you just want a single probability value. 由于您的课程是二进制的,并且只有一个S型输出,因此我假设您正在执行“热狗-非热狗”类型的分类,而您只需要一个概率值。

The other hint for me that you care about the single probability that something is a category or not is that your loss function is binary_crossentropy. 对我来说,另一个提示是您关心某物是否为类别的单一可能性是损失函数为binary_crossentropy。

Check your training data folder. 检查您的训练数据文件夹。 Look at how the data is organized. 查看数据的组织方式。 This should be set up such that the data is organized correctly. 应该进行设置,以便正确组织数据。

You seem to be hinting that you want to create multiple labels (eg, car, boat, train). 您似乎暗示要创建多个标签(例如,汽车,轮船,火车)。 If this is the case, you will want to create those folders under train and validate and put the images in the respective folder. 在这种情况下,您将要在训练中创建这些文件夹,然后验证并将图像放入相应的文件夹中。 You will need to change several things about your model if you do this though. 但是,如果要执行此操作,则需要更改有关模型的几件事。 Your loss, output layer size, and output layer activations will change accordingly. 您的损失,输出层大小和输出层激活将相应更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM