无法将feed_dict键解释为Tensor:Tensor Tensor("占位符:0",shape =(5,5,1,32),dtype = float32)不是此图的元素

时间:2018-01-28 12:22:52

标签: python django tensorflow

我使用python tensorflow制作了一个预测音频,当第一个上传文件成功但如果我再次重复它会出现这样的错误信息

  

无法将feed_dict键解释为Tensor:Tensor   Tensor("占位符:0",shape =(5,5,1,32),dtype = float32)不是   该图的元素。

是否包含cookies?

def creatematrix(request):

    if request.method == 'POST':

        myfile = request.FILES['sound']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)

        # load json and create model
        json_file = open('TrainedModels/model_CNN.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()

        loaded_model = model_from_json(loaded_model_json)
        # load weights into new model
        loaded_model.load_weights("TrainedModels/model_CNN.h5")
        print("Model restored from disk")

        sound_file_paths = myfile.name # "22347-3-3-0.wav"
        parent_dir = 'learning/static/media/'
        sound_names = ["air conditioner","car horn","children playing","dog bark","drilling","engine idling","gun shot","jackhammer","siren","street music"]

        predict_file = parent_dir + sound_file_paths

        predict_x = extract_feature_array(predict_file)

        test_x_cnn = predict_x.reshape(predict_x.shape[0], 20, 41, 1).astype('float32')
        loaded_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

        # generate prediction, passing in just a single row of features
        predictions = loaded_model.predict(test_x_cnn)

        # get the indices of the top 2 predictions, invert into descending order
        ind = np.argpartition(predictions[0], -2)[-9:]
        ind[np.argsort(predictions[0][ind])]
        ind = ind[::-1]

        a = sound_names[ind[0]], 100 * round(predictions[0,ind[0]],3)
        b = sound_names[ind[1]], 100 * round(predictions[0,ind[1]],3)
        c = sound_names[ind[2]], 100 * round(predictions[0,ind[2]],3)
        d = sound_names[ind[3]], 100 * round(predictions[0,ind[3]],3)
        e = sound_names[ind[4]], 100 * round(predictions[0,ind[4]],3)
        f = sound_names[ind[5]], 100 * round(predictions[0,ind[5]],3)
        g = sound_names[ind[6]], 100 * round(predictions[0,ind[6]],3)
        h = sound_names[ind[7]], 100 * round(predictions[0,ind[7]],3)
        i = sound_names[ind[8]], 100 * round(predictions[0,ind[8]],3)

        return render(request, 'base.html', {
            'a': a,
            'b': b,
            'c': c,
            'd': d,
            'e': e,
            'f': f,
            'g': g,
            'h': h,
            'i': i,

            })
    else:
        return render(request, 'base.html', {

            }) 

0 个答案:

没有答案