加载模型和模型预测操作分开
Created by: legendxty
请问在fluid.dygraph.guard():下面,怎样实现把加载模型和预测给分开呢?我这样操作infer的时候会报错: class model(object): def init(self): with fluid.dygraph.guard(): self.net = ResNet("resnet", class_dim = train_parameters['class_dim']) # load checkpoint model_dict, _ = fluid.dygraph.load_dygraph(train_parameters["save_persistable_dir"]) self.net.load_dict(model_dict) print("checkpoint loaded") # start evaluate mode self.net.eval() self.label_dic = train_parameters["label_dict"] self.label_dic = {v: k for k, v in self.label_dic.items()} def infer(self, image_path_pre, img_list): result_dic = {} for img_path in img_list: img = read_img(os.path.join(image_path_pre, img_path)) results = self.net(fluid.dygraph.to_variable(img)) lab = np.argsort(results.numpy()) s = "{} {}\n".format(img_path, self.label_dic[lab[0][-1]]) result_dic[img_path] = self.label_dic[lab[0][-1]] print(s[:-1]) return result_dic