我正在尝试使用预训练的TensorFlow模型对图像进行分类。
我从TensorFlow Hub下载了efficientnet模型。
-
Python代码从.pb文件中加载模型。
-
然后加载一个样本图像,将图像调整到224×224大小,将RGB值压缩到[0,1]范围内,并添加另一个维度使其成为4维(图像集合),因为模型需要这种格式。
-
使用col_x进行推理。最终输入给模型的形状是(1, 224, 224, 3)。
但我得到了这个错误:
Traceback (most recent call last): File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 926, in conv2d "dilations", dilations)tensorflow.python.eager.core._FallbackException: Expecting int64_t value for attr strides, got numpy.int32During handling of the above exception, another exception occurred:Traceback (most recent call last): File "<pyshell>", line 1, in <module> File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 968, in __call__ outputs = self.call(cast_inputs, *args, **kwargs) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 719, in call convert_kwargs_to_constants=base_layer_utils.call_context().saving) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 888, in _run_internal_graph output_tensors = layer(computed_tensors, **kwargs) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 968, in __call__ outputs = self.call(cast_inputs, *args, **kwargs) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 207, in call outputs = self._convolution_op(inputs, self.kernel) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1106, in __call__ return self.conv_op(inp, filter) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 638, in __call__ return self.call(inp, filter) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 237, in __call__ name=self.name) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 2014, in conv2d name=name) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 933, in conv2d data_format=data_format, dilations=dilations, name=name, ctx=_ctx) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 1022, in conv2d_eager_fallback ctx=ctx, name=name) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute inputs, attrs, num_outputs)tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above. [Op:Conv2D]
回答:
我能够重现你的错误,请将最后一行更改为predict或evaluate,或任何你想要进行推理的函数。
model.predict(col_x)
你使用的是model(col_x),这会将图像直接作为类属性发送给模型。
另外,对于另一个错误,我认为你的系统可能没有使用可用的GPU,请安装正确的TensorFlow和CUDA版本以实现此目的。
请访问这个回答哪些TensorFlow和CUDA版本组合是兼容的?以进行更正。
祝好运。