如何在训练期间重置 CNN (Keras) 模型?

时间:2021-05-12 19:52:26

标签: python tensorflow keras conv-neural-network

我正在为类似于图像语义分割的任务训练预训练 VGG-16 CNN (U-Net) 的改进实现。我们有兴趣研究训练规模对性能的影响。拆分数据集后,从训练子集中抽取随机图像(例如用 500 张随机图像训练、重置模型、用 1000 张随机图像训练、重置模型等)。

用于重置Keras的函数如下:

# Function reset the model
def reset_keras(model):
    sess = tf.compat.v1.keras.backend.get_session() #load current TF session
    clear_session() #clear current session
    sess.close() #close current session
    sess = tf.compat.v1.keras.backend.get_session()
    
    #try to delete the model and pass if an exception is encountered  
    try:
        del model 
    except:
        pass

    print('Number of objects collected and deallocated is', gc.collect()) # returns number of objects collected and deallocated

    # use the same config as you used to create the session
    config = tf.compat.v1.ConfigProto() # configure the session about to be run
    config.gpu_options.per_process_gpu_memory_fraction = 1
    config.gpu_options.visible_device_list = "0" #only see gpu 0
    tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

我怀疑模型在两次训练之间没有正确重置。例如,与会话开始相比,如果在会话结束时运行,使用相对较少的图像进行训练将显示出明显更好的性能。我们希望在所有训练中使用相同的模型架构和权重初始化。只是训练图像的数量不同。

在训练课程之间重置模型的推荐方法是什么?

谢谢!

背景:

  • Google Colab
  • 安装的 Python 版本为:3.7.10
  • 已安装的 TensorFlow 版本为:2.4.1
  • 安装的 Keras 版本为:2.4.3

0 个答案:

没有答案
相关问题