如何解决将 Grad-cam 与 tensorflow lite 编译模型一起使用
我一直在尝试使用 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 举报,一经查实,本站将立刻删除。