即使训练和测试的 Python/TF 版本相同,也会出现未知操作码错误

问题描述 投票:0回答:1

我正在尝试按照本教程创建一个暹罗网络https://pyimagesearch.com/2020/11/30/siamese-networks-with-keras-tensorflow-and-deep-learning/

我唯一修改的是

config.py
中的纪元数。以下是我运行的命令和生成的日志。

(tf-23) D:\Code\burgle\ml>python train_siamese_network.py
2.3.0
[]
[INFO] loading MNIST dataset...
[INFO] preparing positive and negative pairs...
[INFO] building siamese network...
2023-04-23 04:58:24.093663: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
[INFO] compiling model...
[INFO] training model...
Epoch 1/10
1875/1875 [==============================] - 78s 41ms/step - loss: 0.6933 - accuracy: 0.4952 - val_loss: 0.6933 - val_accuracy: 0.4991
Epoch 2/10
1875/1875 [==============================] - 76s 41ms/step - loss: 0.6932 - accuracy: 0.4999 - val_loss: 0.6932 - val_accuracy: 0.4990
Epoch 3/10
1875/1875 [==============================] - 77s 41ms/step - loss: 0.6932 - accuracy: 0.4995 - val_loss: 0.6933 - val_accuracy: 0.4816
Epoch 4/10
1875/1875 [==============================] - 75s 40ms/step - loss: 0.6932 - accuracy: 0.4982 - val_loss: 0.6933 - val_accuracy: 0.4845
Epoch 5/10
1875/1875 [==============================] - 75s 40ms/step - loss: 0.6932 - accuracy: 0.4990 - val_loss: 0.6932 - val_accuracy: 0.4978
Epoch 6/10
1875/1875 [==============================] - 76s 40ms/step - loss: 0.6932 - accuracy: 0.4999 - val_loss: 0.6933 - val_accuracy: 0.5000
Epoch 7/10
1875/1875 [==============================] - 77s 41ms/step - loss: 0.6932 - accuracy: 0.4996 - val_loss: 0.6932 - val_accuracy: 0.5000
Epoch 8/10
1875/1875 [==============================] - 77s 41ms/step - loss: 0.6932 - accuracy: 0.4983 - val_loss: 0.6933 - val_accuracy: 0.4997
Epoch 9/10
1875/1875 [==============================] - 77s 41ms/step - loss: 0.6932 - accuracy: 0.4976 - val_loss: 0.6933 - val_accuracy: 0.5000
Epoch 10/10
1875/1875 [==============================] - 77s 41ms/step - loss: 0.6932 - accuracy: 0.4983 - val_loss: 0.6932 - val_accuracy: 0.5000
[INFO] saving siamese model...
WARNING:tensorflow:From C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
2023-04-23 05:11:11.403233: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:tensorflow:From C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
[INFO] plotting training history...

(tf-23) D:\Code\burgle\ml>python test_siamese_network.py --input examples
2.3.0
[INFO] loading test dataset...
[INFO] loading siamese model...
2023-04-23 15:27:34.627626: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
XXX lineno: 43, opcode: 47
Traceback (most recent call last):
  File "test_siamese_network.py", line 27, in <module>
    model = load_model(config.MODEL_PATH)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\save.py", line 187, in load_model
    return saved_model_load.load(filepath, compile, options)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 120, in load
    model = tf_load.load_internal(
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\saved_model\load.py", line 632, in load_internal
    loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 194, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\saved_model\load.py", line 130, in __init__
    self._load_all()
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 221, in _load_all
    self._finalize_objects()
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 530, in _finalize_objects
    self._reconstruct_all_models()
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 548, in _reconstruct_all_models
    self._reconstruct_model(model_id, model, layers)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 588, in _reconstruct_model
    created_layers) = functional_lib.reconstruct_from_config(
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1214, in reconstruct_from_config
    process_node(layer, node_data)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1162, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 925, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1117, in _functional_construction_call
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\heihachi\anaconda3\envs\tf-23\lib\site-packages\tensorflow\python\keras\layers\core.py", line 903, in call
    result = self.function(inputs, **kwargs)
  File "D:/Code/burgle/ml/pyimagesearch/utils.py", line 43, in euclidean_distance
    (featsA, featsB) = vectors
SystemError: unknown opcode

我按照教程作者的建议使用 TF 2.3 和 Python3.8。这两个脚本都在同一环境中运行。以下是我的输出

conda list
:

(tf-23) D:\Code\burgle\ml>conda list
# packages in environment at C:\Users\heihachi\anaconda3\envs\tf-23:
#
# Name                    Version                   Build  Channel
_tflow_select             2.3.0                     eigen
abseil-cpp                20211102.0           hd77b12b_0
absl-py                   1.3.0            py38haa95532_0
aiohttp                   3.8.3            py38h2bbff1b_0
aiosignal                 1.2.0              pyhd3eb1b0_0
appdirs                   1.4.4              pyhd3eb1b0_0
astor                     0.8.1            py38haa95532_0
astunparse                1.6.3                      py_0
async-timeout             4.0.2            py38haa95532_0
attrs                     22.1.0           py38haa95532_0
blas                      1.0                         mkl
blinker                   1.4              py38haa95532_0
brotli                    1.0.9                h2bbff1b_7
brotli-bin                1.0.9                h2bbff1b_7
brotlipy                  0.7.0           py38h2bbff1b_1003
c-ares                    1.19.0               h2bbff1b_0
ca-certificates           2022.12.7            h5b45459_0    conda-forge
cachetools                4.2.2              pyhd3eb1b0_0
certifi                   2022.12.7          pyhd8ed1ab_0    conda-forge
cffi                      1.15.1           py38h2bbff1b_3
charset-normalizer        2.0.4              pyhd3eb1b0_0
click                     8.0.4            py38haa95532_0
colorama                  0.4.6            py38haa95532_0
contourpy                 1.0.5            py38h59b6b97_0
cryptography              39.0.1           py38h21b164f_0
cycler                    0.11.0             pyhd3eb1b0_0
eigen                     3.4.0                h2d74725_0    conda-forge
fonttools                 4.25.0             pyhd3eb1b0_0
freetype                  2.12.1               ha860e81_0
frozenlist                1.3.3            py38h2bbff1b_0
gast                      0.4.0              pyhd3eb1b0_0
giflib                    5.2.1                h8cc25b3_3
glib                      2.69.1               h5dc1a3c_2
google-auth               2.6.0              pyhd3eb1b0_0
google-auth-oauthlib      0.4.4              pyhd3eb1b0_0
google-pasta              0.2.0              pyhd3eb1b0_0
grpc-cpp                  1.48.2               hf108199_0
grpcio                    1.48.2           py38hf108199_0
gst-plugins-base          1.18.5               h9e645db_0
gstreamer                 1.18.5               hd78058f_0
h5py                      2.10.0           py38h5e291fa_0
hdf5                      1.10.4               h7ebc959_0
icc_rt                    2022.1.0             h6049295_2
icu                       58.2                 ha925a31_3
idna                      3.4              py38haa95532_0
importlib-metadata        6.0.0            py38haa95532_0
importlib_resources       5.2.0              pyhd3eb1b0_1
imutils                   0.5.4            py38haa244fe_3    conda-forge
intel-openmp              2021.4.0          haa95532_3556
jpeg                      9e                   h2bbff1b_1
keras-applications        1.0.8                      py_1
keras-preprocessing       1.1.2              pyhd3eb1b0_0
kiwisolver                1.4.4            py38hd77b12b_0
krb5                      1.19.4               h5b6d351_0
lerc                      3.0                  hd77b12b_0
libbrotlicommon           1.0.9                h2bbff1b_7
libbrotlidec              1.0.9                h2bbff1b_7
libbrotlienc              1.0.9                h2bbff1b_7
libclang                  14.0.6          default_hb5a9fac_1
libclang13                14.0.6          default_h8e68704_1
libdeflate                1.17                 h2bbff1b_0
libffi                    3.4.2                hd77b12b_6
libiconv                  1.16                 h2bbff1b_2
libogg                    1.3.5                h2bbff1b_1
libpng                    1.6.39               h8cc25b3_0
libprotobuf               3.20.3               h23ce68f_0
libtiff                   4.5.0                h6c2663c_2
libvorbis                 1.3.7                he774522_0
libwebp                   1.2.4                hbc33d0d_1
libwebp-base              1.2.4                h2bbff1b_1
libxml2                   2.10.3               h0ad7f3c_0
libxslt                   1.1.37               h2bbff1b_0
lz4-c                     1.9.4                h2bbff1b_0
markdown                  3.4.1            py38haa95532_0
markupsafe                2.1.1            py38h2bbff1b_0
matplotlib                3.7.1            py38haa244fe_0    conda-forge
matplotlib-base           3.7.1            py38hf11a4ad_1
mkl                       2021.4.0           haa95532_640
mkl-service               2.4.0            py38h2bbff1b_0
mkl_fft                   1.3.1            py38h277e83a_0
mkl_random                1.2.2            py38hf11a4ad_0
multidict                 6.0.2            py38h2bbff1b_0
munkres                   1.1.4                      py_0
numpy                     1.23.5           py38h3b20f71_0
numpy-base                1.23.5           py38h4da318b_0
oauthlib                  3.2.2            py38haa95532_0
opencv                    4.6.0            py38h104de81_2
openssl                   1.1.1t               h2bbff1b_0
opt_einsum                3.3.0              pyhd3eb1b0_1
packaging                 23.0             py38haa95532_0
pcre                      8.45                 hd77b12b_0
pillow                    9.5.0                    pypi_0    pypi
pip                       23.0.1           py38haa95532_0
ply                       3.11                     py38_0
pooch                     1.4.0              pyhd3eb1b0_0
protobuf                  3.20.3           py38hd77b12b_0
pyasn1                    0.4.8              pyhd3eb1b0_0
pyasn1-modules            0.2.8                      py_0
pycparser                 2.21               pyhd3eb1b0_0
pyjwt                     2.4.0            py38haa95532_0
pyopenssl                 23.0.0           py38haa95532_0
pyparsing                 3.0.9            py38haa95532_0
pyqt                      5.15.7           py38hd77b12b_0
pyqt5-sip                 12.11.0          py38hd77b12b_0
pyreadline                2.1                      py38_1
pysocks                   1.7.1            py38haa95532_0
python                    3.8.16               h6244533_3
python-dateutil           2.8.2              pyhd3eb1b0_0
python_abi                3.8                      2_cp38    conda-forge
qt-main                   5.15.2               he8e5bd7_8
qt-webengine              5.15.9               hb9a9bb5_5
qtwebkit                  5.212                h2bbfb41_5
re2                       2022.04.01           hd77b12b_0
requests                  2.28.1           py38haa95532_1
requests-oauthlib         1.3.0                      py_0
rsa                       4.7.2              pyhd3eb1b0_1
scipy                     1.10.1           py38h321e85e_0
setuptools                66.0.0           py38haa95532_0
sip                       6.6.2            py38hd77b12b_0
six                       1.16.0             pyhd3eb1b0_1
sqlite                    3.41.2               h2bbff1b_0
tensorboard               2.10.0           py38haa95532_0
tensorboard-data-server   0.6.1            py38haa95532_0
tensorboard-plugin-wit    1.8.1            py38haa95532_0
tensorflow                2.3.0           mkl_py38h8c0d9a2_0
tensorflow-base           2.3.0           eigen_py38h75a453f_0
tensorflow-estimator      2.6.0              pyh7b7c402_0
termcolor                 2.1.0            py38haa95532_0
tk                        8.6.12               h2bbff1b_0
toml                      0.10.2             pyhd3eb1b0_0
tornado                   6.2              py38h2bbff1b_0
urllib3                   1.26.15          py38haa95532_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
werkzeug                  2.2.3            py38haa95532_0
wheel                     0.38.4           py38haa95532_0
win_inet_pton             1.1.0            py38haa95532_0
wrapt                     1.14.1           py38h2bbff1b_0
xz                        5.2.10               h8cc25b3_1
yarl                      1.8.1            py38h2bbff1b_0
zipp                      3.11.0           py38haa95532_0
zlib                      1.2.13               h8cc25b3_0
zstd                      1.5.5                hd43e919_0

问题出在哪里?我发现的有关此错误的所有内容都与 python 版本中的问题有关。我也尝试过使用最新的 TF 版本,但是,附加信息也会出现相同的错误,如下所示:

XXX lineno: 43, opcode: 47
Traceback (most recent call last):
  File "D:\Code\burgle\ml\test_siamese_network.py", line 27, in <module>
    model = load_model(config.MODEL_PATH)
  File "C:\Users\heihachi\anaconda3\envs\tf\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "D:/Code/burgle/ml/pyimagesearch/utils.py", line 43, in euclidean_distance
    (featsA, featsB) = vectors
SystemError: Exception encountered when calling layer "lambda" (type Lambda).

unknown opcode

Call arguments received by layer "lambda" (type Lambda):
  • inputs=['tf.Tensor(shape=(None, 48), dtype=float32)', 'tf.Tensor(shape=(None, 48), dtype=float32)']
  • mask=None
  • training=False

这个 github 问题(https://github.com/tensorflow/tensorflow/issues/18660)可能相关吗?

python tensorflow machine-learning keras deep-learning
1个回答
0
投票

我在使用

tensorflow 2.4.0
时也遇到同样的问题。看来这是一个“已知问题”。 Lambda层出了问题,
文档
甚至在使用它们时警告(反)序列化“限制”,似乎子类化和创建自己的层可以解决问题。 对我有用的是在训练后保存网络的权重。然后,加载回(实例化)网络架构并加载原始权重,然后进行预测,如下所示:

# Creating the DNN: def createModel(): model = tf.keras.Sequential([ keras.layers.Dense(512, activation='relu', input_shape=(784,)), keras.layers.Dropout(0.2), keras.layers.Dense(10) ]) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]) return model # Create the model: model = createModel() # Get summary: model.summary() # After training... # Save the network's weights: model.save_weights("modelWeights.h5") # Later, possibly in another script: model2 = createModel() # Load the original weights: model2.load_weights("modelWeights.h5") # Get summary: model2.summary() # Predict: predictions = model2.predict(testX)

总之,只保存权重,创建一个新的模型对象实例化网络的原始架构,不进行训练,只需将保存的权重加载到其中,然后进行预测。

更新:2023年9月24日

我设法通过对图层类进行子分类来解决最初的问题,本质上是编写一个

EuclideanDistance

层来计算两个张量之间的距离。实现很简单,这是一个名为

EuclideanDistance.py
:
 的新文件
# Imports: import tensorflow as tf from tensorflow.keras.layers import Layer class EuclideanDistance(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, inputs): # Unpack the vectors into separate lists featsA, featsB = inputs # Compute the sum of squared distances between the vectors sumSquared = tf.keras.backend.sum(tf.keras.backend.square(featsA - featsB), axis=1, keepdims=True) # Return the euclidean distance between the vectors return tf.keras.backend.sqrt(tf.keras.backend.maximum(sumSquared, tf.keras.backend.epsilon())) def get_config(self): config = super().get_config() return config

您可以像这样实例化图层。只需传递两个张量的列表即可:

from EuclideanDistance import EuclideanDistance distance = EuclideanDistance()([image1Embeddings, image2Embeddings])

然后,要保存模型,您需要包含 
dict

自定义对象,以便可以正确引用和加载它们:

# Load the model. Possibly in another file:
from tensorflow.keras.models import load_model
from EuclideanDistance import EuclideanDistance

# Include the custom objects dict:
model = load_model(modelPath, custom_objects={"EuclideanDistance": EuclideanDistance})

© www.soinside.com 2019 - 2024. All rights reserved.