简体   繁体   English

将每个批次或时期的验证准确性打印到控制台(Keras)

[英]Printing out the validation accuracy to the console for every batch or epoch (Keras)

I'm using ImageDataGenerator and flow_from_directory to generate my data, and using model.fit_generator to fit the data.我正在使用ImageDataGeneratorflow_from_directory来生成我的数据,并使用model.fit_generator来拟合数据。

This defaults to outputting the accuracy for training data set only.这默认为仅输出训练数据集的准确度。 There doesn't seem to be an option to output validation accuracy to the terminal.似乎没有将验证准确性输出到终端的选项。

Here is the relevant portion of my code:这是我的代码的相关部分:

#train data generator


print('Starting Preprocessing')

train_datagen = ImageDataGenerator(preprocessing_function = preprocess)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size, 
class_mode = 'categorical')  #class_mode = 'categorical'


#same for validation
val_datagen = ImageDataGenerator(preprocessing_function = preprocess)

validation_generator = val_datagen.flow_from_directory(
        validation_data_dir,
        target_size = (img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')





########################Model Creation###################################

#create the base pre-trained model
print('Finished Preprocessing, starting model creating \n')
base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(12, activation='softmax')(x)
model = Model(input=base_model.input, output=predictions)




for layer in model.layers[:-34]:
   layer.trainable = False
for layer in model.layers[-34:]:
   layer.trainable = True


from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.001, momentum=0.92),
              loss='categorical_crossentropy',
              metrics = ['accuracy'])



#############SAVE Model #######################################


file_name = str(datetime.datetime.now()).split(' ')[0] + '_{epoch:02d}.hdf5'
filepath = os.path.join(save_dir, file_name)



checkpoints =ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                save_best_only=False, save_weights_only=False,
                                mode='auto', period=2)

###############Fit Model #############################

model.fit_generator(
train_generator,
steps_per_epoch =total_samples//batch_size,
epochs = epochs,
validation_data=validation_generator,
validation_steps=total_validation//batch_size,
callbacks = [checkpoints],
shuffle= True)

UPDATE OUTPUT:更新输出:

Throughout training, I'm only getting the output of training accuracy, but at the end of training, I"m getting both training, validation accuracy.在整个训练过程中,我只得到训练准确度的输出,但在训练结束时,我得到了训练准确度和验证准确度。

Epoch 1/10

  1/363 [..............................] - ETA: 1:05:58 - loss: 2.4976 - acc: 0.0640
  2/363 [..............................] - ETA: 51:33 - loss: 2.4927 - acc: 0.0760  
  3/363 [..............................] - ETA: 48:55 - loss: 2.5067 - acc: 0.0787
  4/363 [..............................] - ETA: 47:26 - loss: 2.5110 - acc: 0.0770
  5/363 [..............................] - ETA: 46:30 - loss: 2.5021 - acc: 0.0824
  6/363 [..............................] - ETA: 45:56 - loss: 2.5063 - acc: 0.0820

Validation loss and validation accuracy gets printed for every epoch once you specify the validation_split.一旦您指定了validation_split,就会为每个时期打印验证损失和验证准确度。

model.fit(X, Y, epochs=1000, batch_size=10, validation_split=0.2)

I have used the above in my code, and val_loss and val_acc are getting printed for every epoch, but not after every batch.我在我的代码中使用了上面的内容,并且 val_loss 和 val_acc 在每个时期都被打印出来,但不是在每个批次之后。

Hope that answers your question.希望这能回答你的问题。

Epoch 1/500
1267/1267 [==============================] - 0s 376us/step - loss: 0.6428        - acc: 0.6409 - val_loss: 0.5963 - val_acc: 0.6656

The idea is that you go through you validation set after each epoch, not after each batch.这个想法是你在每个 epoch 之后检查你的验证集,而不是在每个批次之后。 If after every batch, you had to evaluate the performances of the model on the whole validation set, you would loose a lot of time.如果在每批之后,您都必须在整个验证集上评估模型的性能,那么您将浪费大量时间。

After each epoch, you will have the corresponding losses and accuracies both for training and validation.在每个 epoch 之后,您将获得相应的损失和准确度以进行训练和验证。 But during one epoch, you will only have access to the training loss and accuracy.但是在一个 epoch 中,您将只能访问训练损失和准确性。

In fit_generator ,fit_generator

fit_generator(generator, steps_per_epoch=None, epochs=1, verbose=1, callbacks=None, **validation_data=None, validation_steps=None**, validation_freq=1, class_weight=None, max_queue_size=10, workers=1, use_multiprocessing=False, shuffle=True, initial_epoch=0)

since there is no validation_split parameter, you can create two different ImageDataGenerator flow, one for training and one for validating and then place that 'validation_generator' in validation_data .由于没有validation_split参数,您可以创建两种不同的ImageDataGenerator流,一种用于训练,一种用于验证,然后将该 'validation_generator' 放在validation_data Then it will print the validation loss and accuracy.然后它将打印验证损失和准确性。

is it possible to print out parameters from the model.fit(.....) say for example I would like to print out the validation_split=0.2 parameter!是否可以从 model.fit(.....) 打印参数,例如我想打印validation_split=0.2 参数!

Why?为什么? I am trying to compute the percentage of videos taken from the training set automatically.我正在尝试自动计算从训练集中拍摄的视频的百分比。 every time I change my parameters I have to manually compute the size of the training set every time I change this validation parameter.每次更改参数时,每次更改此验证参数时,我都必须手动计算训练集的大小。

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

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