【问题标题】:can't manage to use the model.predict() in keras(tensorflow)无法在 keras(tensorflow) 中使用 model.predict()
【发布时间】:2020-03-05 10:48:54
【问题描述】:

背景:

我在 Python 3.6 中使用 Pycharm(不使用更新版本,因为我的库不支持更新版本的 python)。

我为杀毒软件建立了一个 ml 模型并保存(尝试将其保存为 'anti_virus_model.h5' 和文件夹)

我正在尝试为反病毒构建一个 UI,所以我正在使用 tkinter 库。

问题: 我试图加载我的模型(很确定它有效)并预测被选择的文件(在将标题变成向量之后) 我导入了 tensorflow 和 keras,但函数 model.predict(pe) 似乎无法被 pycharm 识别。 [pe 是我的向量]

这是我的代码:

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

(进程不崩溃,我关闭了)

提前致谢!

【问题讨论】:

  • 回溯中的行 print(pe.model.predict()) 与您粘贴的代码 (print(keras.model.predict(pe))) 不匹配。进行更改后,您是否重新启动了应用?
  • 你是对的,我编辑它。
  • @Meon 你用 tensorflow 成功部署我的应用了吗?

标签: python tensorflow machine-learning tkinter keras


【解决方案1】:

将您的问题重构为您可以轻松测试的内容!在这里拥有一个“成熟”的 GUI 程序并不是确保各种零碎工作正常工作的最佳方式。

  1. 同一事物有多个导入,包括 * 导入,这会混淆事物。
  2. load_model() 返回一个模型实例;你没有在任何地方使用它。

简化将 UI 与实际预测代码分开的事情,您会得到易于测试的东西:

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()

【讨论】:

  • 谢谢,但为什么我需要最后一部分?(if 语句)
  • 因为测试一个程序很烦人,你必须点击abs选择文件。最好确保零碎工作,然后将它们组合成更大的程序。
【解决方案2】:

您需要为加载的模型保留一个名称(变量),并使用它来执行 predict()。

替换这两行:

tf.keras.models.load_model('anti_virus_model.h5')
......
    print(keras.model.predict(pe))

以下内容。

model = tf.keras.models.load_model('anti_virus_model.h5')
......
    print(model.predict(pe))

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 2020-09-26
    • 2020-11-09
    • 2020-05-02
    • 2018-09-04
    • 2020-12-24
    • 1970-01-01
    • 2018-02-05
    • 2022-01-07
    相关资源
    最近更新 更多