博主研究MaskR-CNN已有一年左右,前段时间工作中需要绘制epoch-loss曲线图,网上对这块的讲解比较少,因此博主在这讲一下,如何绘制训练时的epoch与loss关系图,博主所用的mask r-snn代码为Mask R-CNN源码。由于我自己对代码有些修改,可能行数对不上,但是就在附近,大家找一下就好。
第一步:
在mrcnn文件夹下mode.py中, 修改一下代码(大概在2360行左右):
history = self.keras_model.fit_generator(
train_generator,
initial_epoch=self.epoch,
epochs=epochs,
steps_per_epoch=self.config.STEPS_PER_EPOCH,
callbacks=callbacks,
validation_data=val_generator,
validation_steps=self.config.VALIDATION_STEPS,
max_queue_size=100,
workers=workers,
use_multiprocessing=True,
)
self.epoch = max(self.epoch, epochs)
try:
a = history.epoch
b = history.history['loss']
c = history.history['val_loss']
epoch_list.extend(a)
tra_loss_list.extend(b)
val_loss_list.extend(c)
except Exception:
pass
在model.py中1820行左右添加以下代码:
epoch_list = []
tra_loss_list = []
val_loss_list = []
在model.py最后添加如下代码:
def return_value(epoch_loss, tra_loss, val_loss):
return epoch_loss, tra_loss, val_loss
def call_back(): # 回调loss值
a, b, c = return_value(epoch_list, tra_loss_list, val_loss_list)
return a, b, c
第二步:在train代码中添加以下代码(就是在你训练的主代码中):
def loss_visualize(epoch, tra_loss, val_loss):
plt.style.use("ggplot")
plt.figure()
plt.subplot(1, 1, 1)
plt.title("Epoch_Loss")
plt.plot(epoch, tra_loss, label='train_loss', color='r', linestyle='-', marker='o')
plt.plot(epoch, val_loss, label='val_loss', linestyle='-', color='b', marker='^')
plt.legend()
plt.xlabel('epoch')
plt.ylabel('loss')
plt.savefig(os.path.join(RESULT_DIR, 'loss.jpg'))
plt.show()
并用以下代码调用:
x_epoch, y_tra_loss, y_val_loss = modellib.call_back()
loss_visualize(x_epoch, y_tra_loss, y_val_loss)