我创建了这个问题,并在有人告诉我重复该问题后编辑了这个问题,但是我仍然没有找到任何解决方案,即使我做的与本帖子AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy'
完全相同也许“ get_distribution”与“ get_distribution_strategy”不同,
我从this video克隆了此代码。
这是显示摄像机然后预测交通标志的代码
import numpy as np
import cv2
import pickle
frameWidth= 640 # CAMERA RESOLUTION
frameHeight = 480
brightness = 180
threshold = 0.75 # PROBABLITY THRESHOLD
font = cv2.FONT_HERSHEY_SIMPLE
# SETUP THE VIDEO CAMERA
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, brightness)
# IMPORT THE TRANNIED MODEL
pickle_in=open("model_trained.p","rb") ## rb = READ BYTE
model=pickle.load(pickle_in)
def grayscale(img):
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
return img
def equalize(img):
img =cv2.equalizeHist(img)
return img
def preprocessing(img):
img = grayscale(img)
img = equalize(img)
img = img/255
return img
def getCalssName(classNo):
if classNo == 0: return 'Speed Limit 20 km/h'
(43 classes)
while True:
# READ IMAGE
success, imgOrignal = cap.read()
# PROCESS IMAGE
img = np.asarray(imgOrignal)
img = cv2.resize(img, (32, 32))
img = preprocessing(img)
cv2.imshow("Processed Image", img)
img = img.reshape(1, 32, 32, 1)
cv2.putText(imgOrignal, "CLASS: " , (20, 35), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(imgOrignal, "PROBABILITY: ", (20, 75), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
# PREDICT IMAGE
predictions = model.predict(img)
classIndex = model.predict_classes(img)
probabilityValue =np.amax(predictions)
if probabilityValue > threshold:
#print(getCalssName(classIndex))
cv2.putText(imgOrignal,str(classIndex)+" "+str(getCalssName(classIndex)), (120, 35), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(imgOrignal, str(round(probabilityValue*100,2) )+"%", (180, 75), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("Result", imgOrignal)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
错误。 ========================
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
13 cv2.putText(imgOrignal, "PROBABILITY: ", (20, 75), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
14 # PREDICT IMAGE
---> 15 predictions = model.predict(img)
16 classIndex = model.predict_classes(img)
17 probabilityValue =np.amax(predictions)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
83
84 def _method_wrapper(self, *args, **kwargs):
---> 85 if self._in_multi_worker_mode(): # pylint: disable=protected-access
86 raise ValueError('{} is not supported in multi-worker mode.'.format(
87 method.__name__))
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _in_multi_worker_mode(self)
1692
1693 def _in_multi_worker_mode(self):
-> 1694 return self.distribute_strategy.extended._in_multi_worker_mode() # pylint: disable=protected-access
1695
1696 def _get_distribution_strategy(self):
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in distribute_strategy(self)
453 def distribute_strategy(self):
454 """The `tf.distribute.Strategy` this model was created under."""
--> 455 return self._distribution_strategy or ds_context.get_strategy()
456
457 @property
AttributeError: 'Sequential' object has no attribute '_distribution_strategy'
这是我的previos帖子,但我确定不是我的情况,它根本不起作用https://stackoverflow.com/questions/62806582/attributeerror-sequential-object-has-no-attribute-distribution-strategy