UnimplementedError:图形执行错误:在TensorFlow上运行NN

发布于 2025-01-23 12:36:39 字数 3515 浏览 0 评论 0原文

我一直在遇到这个错误,我不知道为什么,尤其是因为我完全关注某人的代码,并且运行时没有错误,这

img_shape = (128,128,3)

# load pretrained model
base_model = tf.keras.applications.VGG19(input_shape=img_shape, include_top=False, weights='imagenet')

# freezing the model
base_model.trainable = False

#define the custom head for network
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)

# output / prediction layer
prediction_layer = tf.keras.layers.Dense(units=1, activation='sigmoid')(global_average_layer)

model = tf.keras.models.Model(inputs=base_model.input, outputs=prediction_layer)

# compile the model
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

# create data generators
# import library
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# define objects
data_gen_train = ImageDataGenerator(rescale=1/255.0)
data_gen_test = ImageDataGenerator(rescale=1/255.0)

# define variables
train_generator = data_gen_train.flow_from_directory(directory=training_dir, target_size=(128,128), batch_size=128, class_mode='binary')
test_generator = data_gen_test.flow_from_directory(directory=test_dir, target_size=(128,128), batch_size=128, class_mode='binary')

model.fit_generator(generator=train_generator, epochs=5, validation_data=test_generator)

是我遇到的错误

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  """Entry point for launching an IPython kernel.
Epoch 1/5
---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call last)
<ipython-input-46-18b18ca5977c> in <module>()
----> 1 model.fit_generator(generator=train_generator, epochs=5, validation_data=test_generator)

2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   2221         use_multiprocessing=use_multiprocessing,
   2222         shuffle=shuffle,
-> 2223         initial_epoch=initial_epoch)
   2224 
   2225   @doc_controls.do_not_generate_docs

/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     53     ctx.ensure_initialized()
     54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:
     57     if name is not None:

UnimplementedError: Graph execution error:

Detected at node 'model/block1_conv1/Conv2D' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)

,我无法在结尾处遇到完全错误, 但 拜托,有人可以告诉我怎么了吗

I have been having this error, and I don't know why, especially since I am following someone's code exactly and the person had no error when running this

img_shape = (128,128,3)

# load pretrained model
base_model = tf.keras.applications.VGG19(input_shape=img_shape, include_top=False, weights='imagenet')

# freezing the model
base_model.trainable = False

#define the custom head for network
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)

# output / prediction layer
prediction_layer = tf.keras.layers.Dense(units=1, activation='sigmoid')(global_average_layer)

model = tf.keras.models.Model(inputs=base_model.input, outputs=prediction_layer)

# compile the model
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

# create data generators
# import library
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# define objects
data_gen_train = ImageDataGenerator(rescale=1/255.0)
data_gen_test = ImageDataGenerator(rescale=1/255.0)

# define variables
train_generator = data_gen_train.flow_from_directory(directory=training_dir, target_size=(128,128), batch_size=128, class_mode='binary')
test_generator = data_gen_test.flow_from_directory(directory=test_dir, target_size=(128,128), batch_size=128, class_mode='binary')

model.fit_generator(generator=train_generator, epochs=5, validation_data=test_generator)

This is the error I am getting

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  """Entry point for launching an IPython kernel.
Epoch 1/5
---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call last)
<ipython-input-46-18b18ca5977c> in <module>()
----> 1 model.fit_generator(generator=train_generator, epochs=5, validation_data=test_generator)

2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   2221         use_multiprocessing=use_multiprocessing,
   2222         shuffle=shuffle,
-> 2223         initial_epoch=initial_epoch)
   2224 
   2225   @doc_controls.do_not_generate_docs

/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     53     ctx.ensure_initialized()
     54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:
     57     if name is not None:

UnimplementedError: Graph execution error:

Detected at node 'model/block1_conv1/Conv2D' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)

I could not put in the complete error at the end, but
Please, can someone tell me what's wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她比我温柔 2025-01-30 12:36:39

很容易使用train_generator旧,您可以使用它。

它是关于您创建矩阵初始操作的内存,该操作将减少工作负载并在数据尺寸上表现良好。

[示例]:

import os
from os.path import exists

import tensorflow as tf

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
img_shape = (128,128,3)
BATCH_SIZE = 1
IMG_SIZE = (128, 128)

database_buffer = "F:\\models\\buffer\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
database_buffer_dir = os.path.dirname(database_buffer)

if not exists(database_buffer_dir) : 
    os.mkdir(database_buffer_dir)
    print("Create directory: " + database_buffer_dir)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSets
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PATH = 'F:\\datasets\\downloads\\cats_name'
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir,
                                                                        shuffle=True,
                                                                        batch_size=BATCH_SIZE,
                                                                        image_size=IMG_SIZE)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# load pretrained model
base_model = tf.keras.applications.VGG19(input_shape=img_shape, include_top=False, weights='imagenet')

# freezing the model
base_model.trainable = False

#define the custom head for network
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)

# output / prediction layer
prediction_layer = tf.keras.layers.Dense(units=1, activation='sigmoid')(global_average_layer)

model = tf.keras.models.Model(inputs=base_model.input, outputs=prediction_layer)
model.summary()

# compile the model
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( train_dataset, batch_size=100, epochs=50 )

[output]:

sample << /a>

it is easy the train_generator is old you can use this.

It is about the memory you create the matrixes initial operations that will reduce the working loads and perform well on data sizes.

[ Sample ]:

import os
from os.path import exists

import tensorflow as tf

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
img_shape = (128,128,3)
BATCH_SIZE = 1
IMG_SIZE = (128, 128)

database_buffer = "F:\\models\\buffer\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
database_buffer_dir = os.path.dirname(database_buffer)

if not exists(database_buffer_dir) : 
    os.mkdir(database_buffer_dir)
    print("Create directory: " + database_buffer_dir)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSets
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PATH = 'F:\\datasets\\downloads\\cats_name'
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir,
                                                                        shuffle=True,
                                                                        batch_size=BATCH_SIZE,
                                                                        image_size=IMG_SIZE)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# load pretrained model
base_model = tf.keras.applications.VGG19(input_shape=img_shape, include_top=False, weights='imagenet')

# freezing the model
base_model.trainable = False

#define the custom head for network
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)

# output / prediction layer
prediction_layer = tf.keras.layers.Dense(units=1, activation='sigmoid')(global_average_layer)

model = tf.keras.models.Model(inputs=base_model.input, outputs=prediction_layer)
model.summary()

# compile the model
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( train_dataset, batch_size=100, epochs=50 )

[ Output ]:

Sample

我也只是我 2025-01-30 12:36:39

如果您使用的是Colab,请尝试将GPU切换到TPU。这可能会有所帮助

If you are using colab, try to switch the gpu to tpu. It might helpful

命硬 2025-01-30 12:36:39

使用jit_compile = false,这对我有用:

my_model.compile(optimizer="Adam",...., jit_compile=False)

use jit_compile=False, this worked for me:

my_model.compile(optimizer="Adam",...., jit_compile=False)
记忆消瘦 2025-01-30 12:36:39

解决此错误的解决方案是检查图像形状,并且您还为神经网络添加了形状。两者都必须相同。

这解决了我的问题。

The solution to this error is to check the image shape and also you have added shape to the neural network. Both must be the same.

It is solve my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文