Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:张量不是此图的元素_Python_Tensorflow_Keras_Watchdog - Fatal编程技术网

Python ValueError:张量不是此图的元素

Python ValueError:张量不是此图的元素,python,tensorflow,keras,watchdog,Python,Tensorflow,Keras,Watchdog,目前,我正在测试TensorFlow和Keras的一些深入学习。现在我想评估图片。因此,我需要在watchdog类的帮助下知道是否在文件夹中创建了新图像。在这种情况下,我想做一个预测。因此,我需要首先从.json文件加载经过训练的深度学习模型,然后使用.h5文件中的权重对其进行初始化。这一步需要一些时间。因此,我计划加载模型一次,然后我想做很多预测。不幸的是,我收到了以下错误消息,我建议加载的_模型出现问题。如果我为每个预测加载它,就没有问题,但这种方式不是我想要的 ##### Pred

目前,我正在测试TensorFlow和Keras的一些深入学习。现在我想评估图片。因此,我需要在watchdog类的帮助下知道是否在文件夹中创建了新图像。在这种情况下,我想做一个预测。因此,我需要首先从.json文件加载经过训练的深度学习模型,然后使用.h5文件中的权重对其进行初始化。这一步需要一些时间。因此,我计划加载模型一次,然后我想做很多预测。不幸的是,我收到了以下错误消息,我建议加载的_模型出现问题。如果我为每个预测加载它,就没有问题,但这种方式不是我想要的

#####     Prediction-Class     #####

#Import
from keras.models import model_from_json
import numpy as np
from keras.preprocessing import image
from PIL import Image


class PredictionClass():
    #loaded_model = self.LoadModel()
    counter = 0

    def Load_Model(self):
        modelbez = 'modelMyTest30'
        gewichtsbez = 'weightsMyTest30'

        # load json and create model
        print("Loading...")
        json_file = open(modelbez + '.json', 'r')
        loading_model_json = json_file.read()
        json_file.close()
        loading_model = model_from_json(loading_model_json)
        # load weights into new model
        loading_model.load_weights(gewichtsbez + ".h5")
        print('Loaded model from disk:' + 'Modell: ' + modelbez + 'Gewichte: ' + gewichtsbez)

        loading_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
        return loading_model

    def Predict(path, loaded_model):
         test_image = image.load_img(path, target_size = (64, 64))
         test_image = image.img_to_array(test_image)
         test_image = np.expand_dims(test_image, axis = 0)

         # This step causes the error
         result = loaded_model.predict(test_image)
         print('Prediction successful')

         if result[0][0] == 1:
            prediction = 'schlecht'
            img = Image.open(path)
            img.save(path, "JPEG", quality=80, optimize=True, progressive=True)
            #counterschlecht = counterschlecht +1

         else:
            prediction = 'gut'
            img = Image.open(path)
            img.save(path, "JPEG", quality=80, optimize=True, progressive=True)
            #countergut = countergut +1  

         print("Image "+" contains: " + prediction);


#####     FileSystemWatcher     #####

#Import
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer  

#Class-Definition "MyFileSystemHandler"
class MyFileSystemHandler(FileSystemEventHandler):
    def __init__(self, PredictionClass, loaded_model_Para):
        self.predictor = PredictionClass
        self.loaded_model= loaded_model_Para

    def on_created(self, event):
        #Without this wait-Step I got an Error "Permission denied
        time.sleep(10)
        PredictionClass.Predict(event.src_path, self.loaded_model)
        print('Predict')


#####     MAIN     #####

predictor = PredictionClass()
print('Class instantiated')
loaded_model_Erg = predictor.Load_Model()
print('Load Model')

if __name__ == "__main__":
    event_handler = MyFileSystemHandler(predictor, loaded_model_Erg)

    observer = Observer()
    observer.schedule(event_handler, path='C:\\Users\\...\\Desktop', recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(0.1)
    except KeyboardInterrupt:
        #Press Control + C to stop the FileSystemWatcher
        observer.stop()

    observer.join()
错误:


ValueError:Tensor Tensordense_2/Sigmoid:0,shape=?,1,dtype=float32不是此图的元素。

如果要使用keras模型从多个线程进行预测,应首先调用模型。\在线程之前生成\u predict_函数。您可以找到有关和github问题的更多信息


如果您想使用keras模型从多个线程中进行预测,您应该首先调用model。在线程化之前,先调用函数函数。您可以找到有关和github问题的更多信息

predictor = PredictionClass()
print('Class instantiated')
loaded_model_Erg = predictor.Load_Model()
loaded_model_Erg._make_predict_function()
print('Load Model')