简体   繁体   English

训练使用 tensorflow.keras.Model 和 keras 函数式 API 设计的网络会导致 Python 崩溃

[英]Training a Network designed using tensorflow.keras.Model and the keras functional API causes Python crash

-Keras Version: 2.3.1 -Tensorflow Version: 2.2.0 -OS: Windows 10 -Running on: CPU Processor Intel(R) Core(TM) i7-8850H -Developed in: PyCharm -Python Version: 3.7 -Keras 版本:2.3.1 -Tensorflow 版本:2.2.0 -OS:Windows 10 -运行于:CPU 处理器 Intel(R) Core(TM) i7-8850H -Developed in:PyCharm -Python 版本:3.7

I've made a few attempts at training a network designed using the keras functional API, however training always causes python to crash with the following exit message:我曾尝试训练使用 keras 函数式 API 设计的网络,但是训练总是导致 python 崩溃并显示以下退出消息:

Process finished with exit code -1073740791 (0xC0000409)

Not other stack trace is provided.不提供其他堆栈跟踪。 Below is a simplified version of the code I'm attempting to run:下面是我试图运行的代码的简化版本:

import scipy.io as io
import os
import numpy as np
from tensorflow.keras import layers, losses, Input
from tensorflow.keras.models import Model

directory = 'C:\\Dataset'

def simple_generator(dim1, dim2, dim3, batch_size=5):
    while True:
        samples = np.random.random_sample((batch_size, dim1, dim2, dim3))
        targets = np.random.random_sample((batch_size, 3))
        yield samples, targets

example_shape = (1100, 4096, 2)

train_gen = simple_generator(example_shape[0], example_shape[1], example_shape[2], batch_size=10)
val_gen = simple_generator(example_shape[0], example_shape[1], example_shape[2])
test_gen = simple_generator(example_shape[0], example_shape[1], example_shape[2])

input_tensor = Input(shape=example_shape)
preprocess = layers.Conv2D(32, 3, activation='relu')(input_tensor)
preprocess = layers.Conv2D(32, 3, activation='relu')(preprocess)
preprocess = layers.MaxPool2D(pool_size=(3, 3), strides=3)(preprocess)

"""   Head 1   """
head_1 = layers.Conv2D(32, 3, activation='relu')(preprocess)
head_1 = layers.Conv2D(32, 3, activation='relu')(head_1)
head_1 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_1)
head_1 = layers.Conv2D(32, 3, activation='relu')(head_1)
head_1 = layers.Conv2D(32, 3, activation='relu')(head_1)
head_1 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_1)
head_1 = layers.Conv2D(16, 3, activation='relu')(head_1)
head_1 = layers.Conv2D(16, 3, activation='relu')(head_1)
head_1 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_1)
head_1 = layers.Dense(8, activation='relu')(head_1)

"""   Head 2   """
head_2 = layers.Conv2D(32, 3, activation='relu')(preprocess)
head_2 = layers.Conv2D(32, 3, activation='relu')(head_2)
head_2 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_2)
head_2 = layers.Conv2D(32, 3, activation='relu')(head_2)
head_2 = layers.Conv2D(32, 3, activation='relu')(head_2)
head_2 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_2)
head_2 = layers.Conv2D(16, 3, activation='relu')(head_2)
head_2 = layers.Conv2D(16, 3, activation='relu')(head_2)
head_2 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_2)
head_2 = layers.Dense(8, activation='relu')(head_2)

"""   Head 3   """
head_3 = layers.Conv2D(32, 3, activation='relu')(preprocess)
head_3 = layers.Conv2D(32, 3, activation='relu')(head_3)
head_3 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_3)
head_3 = layers.Conv2D(32, 3, activation='relu')(head_3)
head_3 = layers.Conv2D(32, 3, activation='relu')(head_3)
head_3 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_3)
head_3 = layers.Conv2D(16, 3, activation='relu')(head_3)
head_3 = layers.Conv2D(16, 3, activation='relu')(head_3)
head_3 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_3)
head_3 = layers.Dense(8, activation='relu')(head_3)

concat_out = layers.Concatenate(axis=-1)([head_1, head_2, head_3])
concat_out = layers.Flatten()(concat_out)
output_tensor = layers.Dense(3)(concat_out)
model = Model(input_tensor, output_tensor)

model.compile(loss=losses.mean_absolute_error, optimizer='sgd')
model.summary()

history = model.fit(x=train_gen, steps_per_epoch=25, epochs=12, validation_data=val_gen, validation_steps=10, verbose=True)
model.save(directory + '\\divergent_network.h5')
evaluations = model.evaluate_generator(generator=test_gen, steps=20, verbose=True)
print(evaluations)

Can anyone explain the crash or suggest any potential solutions?任何人都可以解释崩溃或提出任何潜在的解决方案吗? I run this code in an Anaconda environment, however I have tried running in a basic python virtual environment to see if it was an Anaconda issue, but I saw no difference between the two.我在 Anaconda 环境中运行此代码,但是我尝试在基本的 python 虚拟环境中运行以查看它是否是 Anaconda 问题,但我发现两者之间没有区别。

It seems to have been an issue with my conda environment.我的 conda 环境似乎有问题。 I made a new "bare minimum" environment with only the packages I needed and that cleared everything up.我创建了一个新的“最低限度”环境,其中只包含我需要的软件包,并清除了所有内容。 The info I used to do this can be found at:我用来执行此操作的信息可以在以下位置找到:

https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands

https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TensorFlow model 到 Keras 功能 API? - TensorFlow model to Keras functional API? 如何在训练 Keras 功能 API model 时打印不同激活层的准确性? (张量流Python) - How to print accuracy of different activation layers while training a Keras functional API model? (Tensorflow Python) 使用 Tensorflow 2 时传递 `training=true` Keras 功能 API - Passing `training=true` when using Tensorflow 2's Keras Functional API 使用Keras Functional API为Tensorflow LITE建立模型 - Build a model with Keras Functional API for Tensorflow LITE Tensorflow Keras功能API模型可以训练Tensorflow变量吗? 可以在功能性API模型中使用Tensorflow操作吗? - Can a Tensorflow variable be trained using the Tensorflow Keras functional API model? Can a Tensorflow operation be used in the functional API Model? 如何使用 Keras 的功能 api 优化 model - How to optimize a model using the functional api of Keras Tensorflow.keras.Model served by a Flask app + uwsgi gets stuck in model.predict - Tensorflow.keras.Model served by a Flask app + uwsgi gets stuck in model.predict 使用 Keras Functional API 微调模型 - fine tune a model using Keras Functional API Keras 功能 API 和 TensorFlow 集线器 - Keras functional API and TensorFlow Hub 是否可以在子类 ZA559B87068921EEC05086CE584 中使用 Tensorflow Keras 功能 API - Is it possible to use the Tensorflow Keras functional API within a subclassed Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM