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).
I built a ml model for an anti-virus and saved it(tried saving it as 'anti_virus_model.h5' and as a folder)
I'm trying to build a UI for the anti-virus so I'm using the tkinter library.
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]
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.
*
import which will confuse things.load_model()
returns a model instance; you aren't using that anywhere.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()
You need to keep the loaded model with a name (variable), and use that to do 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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.