小编ben*_*min的帖子

我需要什么K.clear_session()和del model(Keras with Tensorflow-gpu)?

我在做什么
我正在训练和使用卷积神经网络(CNN)进行图像分类,使用Keras和Tensorflow-gpu作为后端.

我正在使用的是什么
- PyCharm Community 2018.1.2
- Python 2.7和3.5(但不是一次)
- Ubuntu 16.04 - Keras
2.2.0
- Tensorflow-GPU 1.8.0作为后端

我想知道的
在许多代码中,我看到人们使用

from keras import backend as K 

# Do some code, e.g. train and save model

K.clear_session()
Run Code Online (Sandbox Code Playgroud)

或使用后删除模型:

del model
Run Code Online (Sandbox Code Playgroud)

keras文档说clear_session:"破坏当前的TF图并创建一个新图.有助于避免旧模型/图层的混乱." - https://keras.io/backend/

这样做有什么意义,我也应该这样做?在加载或创建新模型时,我的模型无论如何都会被覆盖,为什么还要费心呢?

python memory-management keras tensorflow

24
推荐指数
2
解决办法
2万
查看次数

Keras模型(tensorflow后端)在python 3.5中训练得非常好,但在python 2.7中非常糟糕

我尝试做什么
我正在尝试使用Keras和Tensorflow-GPU作为后端在Python 2.7中训练卷积神经网络(CNN)进行图像检测,因为我需要将它与ROS动力学一起使用,它只支持Python 2.7(而不是3.5).我的模型是Sequential(代码见下).

我正在使用
Pycharm-Community 2018.1.4 Keras
2.2.0
Tensorflow-GPU 1.8.0
60000输入图像,100x100像素(3通道),3类("train_set")
20000评估图像,相同尺寸("evaluation_set")

什么工作
当使用Python 3.5在我的train_set上训练模型并使用Python 3.5进行评估时,它的工作完全正常(train_accuracy:0.99874,evaluation_accuracy:0.9993).

什么不起作用
使用Python 2.7在我的train_set上训练模型并使用Python 2.7进行评估时,我的准确率急剧下降(train_accuracy:0.695,evaluation_accuracy:0.543),这不过是猜测3个类(这将是0.3333) .
我还尝试在Python 3.5中训练模型并将其加载到Python 2.7中以进行评估和预测,但结果与以前一样糟糕.

在所有情况下,我使用完全相同的代码:

def build_model(training_input):
    model = Sequential()  
    model.add(Conv2D(32, (3, 3)) # Add some layers

    model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy'])

def train():
    input_training = np.array(input_training_list)    # input_training_list is a list containing the imagedata
    labels_training = np.array(label_training_list)    # label_training_list is a list containing the labels corresponding to the imagedata
    model = create_model(input_training) …
Run Code Online (Sandbox Code Playgroud)

model python-2.7 python-3.5 keras tensorflow

6
推荐指数
1
解决办法
567
查看次数