如何在 keras 的类中运行内部类回调?

问题描述

我有一个运行我的模型的类。以下代码是它的缩短版本。该类执行预处理、训练、模型声明等操作。它还声明回调。我正在尝试编写一个自定义回调,将 model.summary() 函数写入文本文件。我让它工作了一分钟,然后下一分钟就坏了。如何将我在外部类中创建的 self 变量传递给内部类以及如何让它运行?

from keras.callbacks import Callback
import keras
import sys


# Model class
class CatDogModel:
    def __init__(self,model_name,version,datafile):
        self.datafile = datafile
        self.version_model_name = f'{version}_{model_name}'
        self.log_dir = f'Model-Graphs&Logs\\Model-Data_{model_name}\\Logs\\{self.version_model_name}'
        self.metric_dir = f'Model-Graphs&Logs\\Model-Data_{model_name}\\Metric-Graphs\\{self.version_model_name}'

    def preprocess(self):
        self.train_gen = procDogCat.train_image_gen(self.datafile)
        self.valid_gen = procDogCat.valid_image_gen(self.datafile)
        self.test_gen = procDogCat.test_image_gen(self.datafile)

    def model(self):
        self.model = modelDogCat.seq_maxpool_cnn()

        class ModelSummaryCallback(keras.callbacks.Callback):
            def model_summary_creation(self):
                with open(f'{self.log_dir}_summary.txt','a') as summary_file:
                    sys.stdout = summary_file
                    self.model.summary()
                    summary_file.close()

        self.model_summary = ModelSummaryCallback()

    def training(self):
        self.history = self.model.fit(self.train_gen,validation_data=self.valid_gen,batch_size=20,steps_per_epoch=40,epochs=1,callbacks=[self.model_summary])


if __name__ == '__main__':
    model_instance = CatDogModel(model_name="dog_cat",version="First_Generation",datafile='F:\\Data-Warehouse\\Dog-Cat-Data\\training_dir')
    model_instance.preprocess()
    model_instance.model()
    model_instance.training()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)