如果我在我的 Mac 上运行,在最坏的情况下运行 model.predict() 需要 1 秒
但我想在服务器中部署机器人,所以我在 ubuntu 服务器(使用 vultr.com)中克隆了存储库并安装了 pip3 要求。
运行 python 脚本将阻止 model.predict() 中的执行,因此机器人无法正常工作
问题是脚本没有错误,但我认为是预测速度的问题。需要无限的时间
在 ubuntu 中使用 tensorflow 存在 cuda 和 tensorflow 兼容性问题,模型是用 cpu 而不是 gpu 预测的
但是是一个非常小的输入大小所以我想知道为什么 tensorflow predict 需要无限的时间
这是我的代码
from json import loads
from pickle import load
from nltk.stem import WordNetLemmatizer
from tensorflow.python.keras.models import load_model
from nltk import word_tokenize
from numpy import array
path = "/root/Jarvis_V3/process1_classification/my_nltk"
model = load_model(f'{path}/db/best_model.h5')
intents = loads(open(f'{path}/db/intents1.json').read())
words = load(open(f'{path}/db/words.pkl', 'rb'))
classes = load(open(f'{path}/db/classes.pkl', 'rb'))
class MyNltk:
def __init__(self):
self.lemmatizer = WordNetLemmatizer()
def clean_up_sentence(self, sentence):
# preprocessamento input utente
print("tokenize")
sentence_words = word_tokenize(sentence)
print("lemmatizer")
sentence_words = [self.lemmatizer.lemmatize(word.lower()) for word in sentence_words]
print("end")
return sentence_words
def bow(self, sentence, words_, show_details=True):
# creazione bag of words
sentence_words = self.clean_up_sentence(sentence)
print("bow")
bag = [0] * len(words_)
for s in sentence_words:
for i, w in enumerate(words_):
if w == s:
bag[i] = 1
if show_details:
print("found in bag: %s" % w)
return array(bag)
def calcola_pred(self, sentence, model_):
p = self.bow(sentence, words, show_details=False)
print("predction")
**res = model_.predict(array([p]), verbose=1)[0]** # <-- HERE THE PROBLEM: INFINITE TIME EXECUTION
print("end")
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
print("result")
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
print(return_list)
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list
def getRisposta(self, ints, intents_json):
# getting the possible targets
tag = ints[0]['intent']
list_of_intents = intents_json['intents']
target = "none"
for i in list_of_intents:
if i['tag'] == tag:
target = tag
break
return target
def run(self, msg: str) -> str:
print("nltk run")
ints = self.calcola_pred(msg, model)
target = self.getRisposta(ints, intents)
return target
谢谢
我尝试解决了安装与tensorflow兼容的cuda依赖的问题
但是遇到很多问题比如
找不到cuda
安装失败无法安装cuda 11
如果安装了 cuda,tensorflow 无法看到正确的版本
我尝试删除、更新stackoverflow中与cuda相关的所有东西
比如
https://gist.github.com/ksopyla/bf74e8ce2683460d8de6e0dc389fc7f5 https://docs.vmware.com/en/VMware-vSphere-Bitfusion/3.0/Example-Guide/GUID-ABB4A0B1-F26E-422E-85C5-BA9F2454363A.html https://gist.github.com/ksopyla/bf74e8ce2683460d8de6e0dc389fc7f5 https://developer.nvidia.com/cuda-11.0-download-archive