[英]can't manage to use the model.predict() in keras(tensorflow)
background:背景:
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).我在 Python 3.6 中使用 Pycharm(不使用较新版本,因为我有一个不支持较新版本 python 的库)。
I built a ml model for an anti-virus and saved it(tried saving it as 'anti_virus_model.h5' and as a folder)我为防病毒构建了一个 ml 模型并保存了它(尝试将其保存为“anti_virus_model.h5”和一个文件夹)
I'm trying to build a UI for the anti-virus so I'm using the tkinter library.我正在尝试为防病毒构建一个 UI,所以我正在使用 tkinter 库。
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.问题:我尝试加载我的模型(很确定它有效)并预测选择的文件(将标题转换为向量后)我导入了 tensorflow 和 keras 但函数 model.predict(pe) 似乎无法识别通过 pycharm。 [pe is my vector]
[pe是我的载体]
here is my code:这是我的代码:
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()
the error I get after selecting a file is:选择文件后我得到的错误是:
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
(the procces doesn't crush, I closed it) (过程不会粉碎,我关闭了它)
Thanks in advance!提前致谢!
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.
在这里拥有一个“成熟的”GUI 程序并不是确保各种零碎工作正常工作的最佳方式。
*
import which will confuse things.*
导入,它会混淆事物。load_model()
returns a model instance; load_model()
返回一个模型实例; you aren't using that anywhere. Simplifying things to separate the UI from the actual prediction code, you get something that's easily testable:简化将 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()
You need to keep the loaded model with a name (variable), and use that to do predict().您需要使用名称(变量)保留加载的模型,并使用它来执行 predict()。
Replace these 2 lines:替换这两行:
tf.keras.models.load_model('anti_virus_model.h5')
......
print(keras.model.predict(pe))
with the following.与以下。
model = tf.keras.models.load_model('anti_virus_model.h5')
......
print(model.predict(pe))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.