Используя Threading и Keras я пытался сделать распознавание элементов на картинке и наткнулся на проблему.
ValueError: The name "conv2d_1" is used 2 times in the model. All layer names should be unique.
Слышал, что это типичная проблема Keras в мультипотоках, но как решить - неясно.
Кто-то меняет названия моделей, ещё что-то, но я не могу применить их решения, ибо не пойму куда вставить.
Мой код:
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
percent = 45
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "image3new.jpg"), minimum_percentage_probability=45)
for eachObject in detections:
print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("--------------------------------")
Именно этот код запускается в нескольких потоках.
Добавлю лишь, что имена файлов при обработке каждый раз новые, здесь это не указано.
К слову, ещё одна ошибка была решена командой K.clear_session()
перед строками, начинающимися с detector
.
Код вызывающий поток с функцией:
def execute(b):
if 'photo_url' in b :
#try:
print('debug1')
photo_url__ = b.get('photo_url')
#detection(photo_url__)
global p
p = threading.Thread(target=detection, args=(photo_url__,user__id))
p.start()
Код функции detection()
:
percent = 45
execution_path = os.getcwd()
K.clear_session()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
#detections =
detector.detectObjectsFromImage(input_image=os.path.join(execution_path ,
'image3.jpg'), output_image_path=os.path.join(execution_path ,
"image3new.jpg"), minimum_percentage_probability=45)
img = urllib.request.urlopen(image_url).read()
out = open('img' + str(w) + ".jpg", "wb")
out.write(img)
out.close
name = 'img' + str(w) + '_new.jpg'
print(os.path.join(execution_path , 'img' + str(w) + '.jpg'))
detections =
detector.detectObjectsFromImage(input_image=os.path.join(execution_path ,
'img' + str(w) + '.jpg'), output_image_path=os.path.join(execution_path ,
'img' + str(w) + '_new.jpg'), minimum_percentage_probability=30)