tensorflow - 尝试使用 'tensorflow.keras.applications' 导入和预测时 VScode 内核崩溃

标签 tensorflow visual-studio-code keras jupyter-notebook

我尝试在我的机器上运行 Jupyter notebook。这是我想要做的:

  1. 载入一张图片
  2. 从“tensorflow.keras.applications”加载预训练模型
  3. 做出预测

这是示例代码

from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions

model = ResNet50(weights='imagenet', include_top=True)
img = tf.keras.preprocessing.image.load_img(imgfile,
                                            grayscale=False, 
                                            color_mode='rgb',
                                            target_size=(224,224),)

img = tf.keras.preprocessing.image.img_to_array(img)
img = preprocess_input(img)
img = np.reshape(img,(-1,224,224,3))

preds = model.predict(img) # crash at this line

然后内核崩溃并显示以下消息:

warn 15:23:44.556: StdErr from Kernel Process 2022-05-22 15:23:44.55 6754: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8400 error 15:23:45.519: Disposing session as kernel process died ExitCode: 3221226505, Reason: C:\XXXXX\Python39\site-packages\traitlets\traitlets.py:2202: FutureWarning: Supporting extra quotes around strings is deprecated in traitlets 5.0. You can use 'hmac-sha256' instead of '"hmac-sha256"' if you require traitlets >=5.
  warn( 
C:\XXXXX\Python39\site-packages\traitlets\traitlets.py:2157: FutureWarning: Supporting extra quotes around Bytes is deprecated in traitlets 5.0. Use '7176e2c9-27f6-42c2-8ae4-00b7ea3c655d' instead of 'b"7176e2c9-27f6-42c2-8ae4-00b7ea3c655d"'.
  warn(
2022-05-22 15:23:36.484356: I tensorflow/core/platform/cpu_feature_guard.cc:151] 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.
2022-05-22 15:23:37.856705: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 3969 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 2060, pci bus id: 0000:01:00.0, compute capability: 7.5
2022-05-22 15:23:44.556754: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8400

info 15:23:45.520: Dispose Kernel process
info 15:23:45.520: kill daemon
error 15:23:45.520: Raw kernel process exited code: 3221226505
error 15:23:45.522: Error in waiting for cell to complete [Error: Canceled future for execute_request message before replies were done
    at t.KernelShellFutureHandler.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1327723)
    at c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1346775
    at Map.forEach (<anonymous>)
    at v._clearKernelState (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1346760)
    at v.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1340242)
    at c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:539831
    at t.swallowExceptions (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:924542)
    at u.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:539809)
    at t.RawSession.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:543488)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)]
warn 15:23:45.523: Cell completed with errors {
  message: 'Canceled future for execute_request message before replies were done'
}
info 15:23:45.525: Cancel all remaining cells true || Error || undefined
info 15:23:45.525: Cancel pending cells
info 15:23:45.525: Cell 5 executed with state Error

环境

  • Win10
  • python 3.9.6
  • VScode 1.67.0
    • Jupyter 扩展 v2022.5.1001311010
    • Python 扩展 v2022.6.2
  • CUDA v11.2 和 v11.6
    • 不确定tensorflow使用的是哪个版本

搜了一些文章,有的说可能是GPU开发设置安装不全,有的说是VScode的bug jupyter extension .

不幸的是,我没有运气来解决我的问题。

如有任何建议,我们将不胜感激。

谢谢!

最佳答案

在此处(回答部分)提供解决方案,即使它出现在评论部分也是为了社区的利益。

此问题是由于版本不兼容造成的。根据 tested build configurations , Tensorflow 2.9.0 兼容 CUDA 11.2cuDNN 8.1。将其降级到 cuDNN 8.1 并使用 CUDA 11.2 会有所帮助。

关于tensorflow - 尝试使用 'tensorflow.keras.applications' 导入和预测时 VScode 内核崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72335755/

相关文章:

python - 在 macOS 上的 VS Code 中启动 Python 脚本时出现 `SyntaxError: invalid syntax`

google-chrome-devtools - 如何配置 Visual Studio Code 调试器以使用 Chrome Canary?

python - Keras 错误 : Expected size[1] in [0, 0],但得到 1

Tensorflow-Deeplearning - 输入和输出之间的相关性

visual-studio-code - VS Code中的多行列复制粘贴

math - Tensorflow L2 损失定义

python - 如何添加 keras dropout 层?

python - 警告 :tensorflow:Model was constructed with shape (None, ....)

python - 为什么 tf.random.log_uniform_candidate_sampler 给出真正的类?

python - 在 Dynamic_RNN 中使用可变序列长度时我应该做损失掩蔽吗