在Tensorflow对象检测API中使用model_main脚本训练时,将损失和mAP值提取并存储在数据库中

问题描述 投票:1回答:1

我正在使用Tensorflow对象检测API来定制数据集。我需要针对每个时期(基于步骤数)将损失和mAP值存储在数据库中。我正在使用model_main.py进行训练,但找不到任何简单的方法(例如回调)来提取和存储这些值。我需要实时培训和评估损失以及mAP。我该如何实现?

我知道我们可以从训练目录中的事件文件中读取这些值,但是在进行训练时并行运行该方法以读取事件文件似乎并不高效。

python tensorflow object-detection-api
1个回答
0
投票

在下面的示例中,我将在每个时期之后捕获渐变并将其附加到列表中,为简单起见,稍后将其转换为数组。同样,您可以根据自己的情况记录损失。您可以使用callbackson_epoch_beginon_batch_begin等在on_test_batch_begin中设置捕获的频率。您可以找到有关回调here的更多选项。

捕获梯度的代码-

# Importing dependency
%tensorflow_version 2.x
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras import datasets
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
import numpy as np
import tensorflow as tf

tf.keras.backend.clear_session()  # For easy reset of notebook state.
tf.compat.v1.disable_eager_execution()

# Import Data
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Build Model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10))

# Model Summary
model.summary()

# Model Compile 
model.compile(optimizer='adam',
              loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# Define the Gradient Fucntion
epoch_gradient = []

# Define the Gradient Function
def get_gradient_func(model):
    grads = K.gradients(model.total_loss, model.trainable_weights)
    inputs = model._feed_inputs + model._feed_targets + model._feed_sample_weights
    func = K.function(inputs, grads)
    return func

# Define the Required Callback Function
class GradientCalcCallback(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs=None):
      get_gradient = get_gradient_func(model)
      grads = get_gradient([train_images, train_labels, np.ones(len(train_labels))])
      epoch_gradient.append(grads)

epoch = 4

model.fit(train_images, train_labels, epochs=epoch, validation_data=(test_images, test_labels), callbacks=[GradientCalcCallback()])


# (7) Convert to a 2 dimensiaonal array of (epoch, gradients) type
gradient = np.asarray(epoch_gradient)
print("Total number of epochs run:", epoch)
print("Gradient Array has the shape:",gradient.shape)

输出-

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928     
_________________________________________________________________
flatten (Flatten)            (None, 1024)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                65600     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650       
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
Train on 50000 samples, validate on 10000 samples
Epoch 1/4
50000/50000 [==============================] - 85s 2ms/sample - loss: 1.7608 - accuracy: 0.3830 - val_loss: 1.4775 - val_accuracy: 0.4652
Epoch 2/4
50000/50000 [==============================] - 70s 1ms/sample - loss: 1.3134 - accuracy: 0.5332 - val_loss: 1.2572 - val_accuracy: 0.5631
Epoch 3/4
50000/50000 [==============================] - 70s 1ms/sample - loss: 1.1465 - accuracy: 0.6003 - val_loss: 1.1485 - val_accuracy: 0.5987
Epoch 4/4
50000/50000 [==============================] - 70s 1ms/sample - loss: 1.0414 - accuracy: 0.6352 - val_loss: 1.1396 - val_accuracy: 0.6076
Total number of epochs run: 4
Gradient Array has the shape: (4, 10)

希望这能回答您的问题。祝您学习愉快。

© www.soinside.com 2019 - 2024. All rights reserved.