2024年1月29日发(作者:)
# -*- encoding: utf-8 -*-import jsonimport torchimport numpy as npfrom PIL import Imagefrom torchvision import transforms, modelsdata_trans = e([([224,224]), or(), ize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])def thresh_sort(x, thresh): idx, = (x > thresh) return idx[t(x[idx])]# 加载模型部分def init_model(): resnet = 50() num_ftrs = _features = (num_ftrs, 20) _state_dict(('',
map_location='cpu')) for param in ters(): es_grad = False () return resnetdef make_prediction(path): img = (path) img_trans = data_trans(img).unsqueeze(0) output = model(img_trans) output = output[0].numpy().ravel() labels = thresh_sort(output, 0.5) if len(labels) == 0 : label_array = "No Categories" status = 0 else: label_array = [cat_to_name[str(i)] for i in labels] status = 1
return label_array, statusif __name__ == '__main__': # 初始化,预加载完成模型 model = init_model()
# 类别信息 with open('class_', 'r') as f: cat_to_name = (f) path = "path/image" label, status = make_prediction(path) print(label, status)
本文发布于:2024-01-29 17:40:42,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170652124217161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |