【问题标题】:model_config = json_utils.decode(model_config.decode('utf-8')) AttributeError: 'str' object has no attribute 'decode'model_config = json_utils.decode(model_config.decode('utf-8')) AttributeError: 'str' object has no attribute 'decode'
【发布时间】:2021-06-05 02:20:55
【问题描述】:

我正在开发一个 Python 项目,该项目可以检测叶子上的疾病并在叶子上喷洒肥料。

在对其他错误进行了数小时的故障排除后,我得出了以下最后一个错误,该错误总是发生并且我似乎无法修复。

以下是迄今为止我用于依赖项的版本:

  • keras = 2.4.3
  • cv2 = 4.5.1
  • numpy = 1.20.1
  • 张量流 = 2.4.0
  • h5py = 3.2.1
  • 熊猫 = 1.2.3

我面临的错误:

Traceback (most recent call last):
  File "leaf_cnn.py", line 12, in <module>
    model = load_model('Leaf_CNN.h5')
  File "/home/pi/.local/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 207, in load_model
    compile)
  File "/home/pi/.local/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 182, in load_model_from_hdf5
    model_config = json_utils.decode(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

代码文件leaf_cnn.py

# importing files/dependencies

from tensorflow.keras.models import load_model
import cv2
import numpy as np
#import Categories
import time
import RPi.GPIO as GPIO

#loading model/ML algorithm 

model = load_model('Leaf_CNN.h5')
cap = cv2.VideoCapture(0) # capture frame
ret, img = cap.read()

channel = 21 
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel,GPIO.OUT)
#cv2.imshow('aaa',img)  'display image with title'

img = cv2.resize(img,(224,224)) 
img = np.reshape(img,[1,224,224,3])
classes = model.predict(img)
y_pred = np.argmax(classes, axis=1)
y_pred = Categories.categories[int(y_pred)]

if "healthy" not in y_pred:
    GPIO.output(21, GPIO.HIGH) #turn-on relay
    time.sleep(1)
else:
    GPIO.output(21, GPIO.LOW) #turn-off relay
    time.sleep(1)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

【问题讨论】:

  • 我也有同样的问题。
  • 我希望这个问题的标题稍有不同。标题的“model_config = json_utils.decode(model_config.decode('utf-8'))”部分没有描述我的代码或意图,但是出于同样的原因,h5py版本和相同的解决方案,我得到了同样的错误为我工作。如果标题类似于“TensorFlow/Keras 错误:...”,它可能会更好。无论哪种方式,我很高兴我找到了一个有用的答案。 :) 此外,OP 应通过单击@Hagbard 提供的答案的复选框来将此问题标记为已回答。

标签: python numpy tensorflow keras raspberry-pi


【解决方案1】:

似乎有一个issue 带有 h5py 并且必须安装在某个版本中以确保与 tensorflow 兼容。这就是我的诀窍:

pip uninstall h5py
pip install h5py==2.10.0

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 2020-04-26
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多