问题描述
我一直在尝试使用 tensorflow lite 进行机器学习推理,并且遇到了 Grad-Cam 的类激活图。我想知道是否可以将 Grad-Cam 与 tensorflow lite 编译模型一起使用,原因是,我正在使用来自 PyCoral 的鸟类分类模型,我想知道哪些特征用于分类.如果可能,那么我如何将 GradCam 与我的分类脚本一起使用?
这是基于模型对图像进行分类的当前脚本:
import argparse
import time
import cv2 as cv
from PIL import Image
from pycoral.adapters import classify
from pycoral.adapters import common
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpformatter)
parser.add_argument('-m','--model',required=True,help='File path of .tflite file.')
parser.add_argument('-i','--input',help='Image to be classified.')
parser.add_argument('-l','--labels',help='File path of labels file.')
parser.add_argument('-k','--top_k',type=int,default=1,help='Max number of classification results')
parser.add_argument('-t','--threshold',type=float,default=0.51,help='Classification score threshold')
parser.add_argument('-c','--count',default=5,help='Number of times to run inference')
args = parser.parse_args()
labels = read_label_file(args.labels) if args.labels else {}
interpreter = make_interpreter(*args.model.split('@'))
interpreter.allocate_tensors()
size = common.input_size(interpreter)
image = cv.imread(args.input)
cv.waitKey(0)
cv.destroyAllWindows()
common.set_input(interpreter,image)
print('----INFERENCE TIME----')
print('Note: The first inference on Edge TPU is slow because it includes','loading the model into Edge TPU memory.')
for _ in range(args.count):
start = time.perf_counter()
interpreter.invoke()
inference_time = time.perf_counter() - start
classes = classify.get_classes(interpreter,args.top_k,args.threshold)
print('%.1fms' % (inference_time * 1000))
print('-------RESULTS--------')
for c in classes:
print('%s: %.5f' % (labels.get(c.id,c.id),c.score))
if __name__ == '__main__':
main()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)