admin 管理员组

文章数量: 887021


2024年1月17日发(作者:易语言脚本编程助手)

# -*- 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)


本文标签: 模型 加载 脚本 编程