无法在keras(tensorflow)中使用model.predict() [英] can't manage to use the model.predict() in keras(tensorflow)

查看:758
本文介绍了无法在keras(tensorflow)中使用model.predict()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我正在将Pycharm与Python 3.6配合使用(不使用较新的版本,因为我有一个不支持较新版本的python的库).

I'm using Pycharm with Python 3.6(not using a newer version because I have a library that doesn't support a newer version of python).

我为防病毒构建了一个ml模型并将其保存(试图将其保存为"anti_virus_model.h5"和文件夹)

I built a ml model for an anti-virus and saved it(tried saving it as 'anti_virus_model.h5' and as a folder)

我正在尝试为反病毒软件构建UI,所以我正在使用tkinter库.

I'm trying to build a UI for the anti-virus so I'm using the tkinter library.

问题: 我试图加载我的模型(非常确定它可以正常工作)并预测所选择的文件(将标头转换为矢量之后) 我导入了tensorflow和keras,但是pycharm似乎无法识别功能model.predict(pe). [pe是我的载体]

The problem: I tried to load my model(pretty sure it worked) and predict the file that was selected(after turning the header into a vector) I imported tensorflow and keras but the function model.predict(pe) doesnt seem to be recognized by pycharm. [pe is my vector]

这是我的代码:

from tkinter import *
from tkinter import filedialog
from tensorflow import keras

import vector_build
import tkinter as Tk
import tensorflow as tf



tf.keras.models.load_model('anti_virus_model.h5')

def browse_file():
    fname = filedialog.askopenfilename(filetypes=(("exe files", "*.exe"), ("exe files", "*.exe")))
    print(fname)
    pe = vector_build.encode_pe(fname)
    print(pe)
    print(keras.model.predict(pe))



root = Tk.Tk()
root.wm_title("Browser")
broButton = Tk.Button(master=root, text='Browse', width=80, height=25, command=browse_file)
broButton.pack(side=Tk.LEFT, padx=2, pady=2)

Tk.mainloop()

选择文件后出现的错误是:

2020-03-05 12:37:14.611731: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-03-05 12:37:14.611883: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-03-05 12:37:16.837699: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-03-05 12:37:16.837815: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-03-05 12:37:16.841558: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-GT2BTVK
2020-03-05 12:37:16.841817: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-GT2BTVK
2020-03-05 12:37:16.842185: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
WARNING:tensorflow:Sequential models without an `input_shape` passed to the first layer cannot reload their optimizer state. As a result, your model isstarting with a freshly initialized optimizer.
C:/Program Files (x86)/Steam/Steam.exe

*(big vector, no need to include)*

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\0123m\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/0123m/PycharmProjects/anti_virus_project/predictorUI.py", line 18, in browse_file
        print(keras.model.predict(pe))

AttributeError: 'numpy.ndarray' object has no attribute 'model'

Process finished with exit code 0

(过程不会崩溃,我将其关闭了)

提前谢谢!

推荐答案

将问题重构为可以轻松测试的东西!在这里拥有一个成熟"的GUI程序并不是确保各种细节都能正常工作的最佳方法.

Refactor your problem to something you can easily test! Having a "full-fledged" GUI program here isn't the best way to make sure the various bits and pieces work as they should.

  1. 您对同一事物进行了多次导入,包括*导入会混淆事物.
  2. load_model()返回一个模型实例;您不会在任何地方使用它.
  1. You have multiple imports of the same thing, including a * import which will confuse things.
  2. load_model() returns a model instance; you aren't using that anywhere.

简化操作以将UI与实际的预测代码分开,您将获得易于测试的内容:

Simplifying things to separate the UI from the actual prediction code, you get something that's easily testable:

import tkinter as Tk
from tkinter import filedialog
from tensorflow import keras
import vector_build

model = keras.models.load_model("anti_virus_model.h5")


def predict_file(fname):
    print(fname)  # Debugging
    pe = vector_build.encode_pe(fname)
    print(pe)  # Debugging
    result = model.predict(pe)
    print(result)  # Debugging
    return result


def browse_file():
    fname = filedialog.askopenfilename(filetypes=(("exe files", "*.exe"),))
    result = predict_file(fname)
    # TODO: Do something with `result`


def ui_main():
    root = Tk.Tk()
    root.wm_title("Browser")
    broButton = Tk.Button(master=root, text="Browse", width=80, height=25, command=browse_file)
    broButton.pack(side=Tk.LEFT, padx=2, pady=2)

    Tk.mainloop()


if True:  # First make this branch work correctly,
    predict_file("C:/Windows/Calc.exe")
else:  # ... then switch to this.
    ui_main()

这篇关于无法在keras(tensorflow)中使用model.predict()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆