def eval_ss(self, data_loader, epoch=0, model_file_name=None, save_path=None): if model_file_name is not None: Tools.print("Load model form {}".format(model_file_name), txt_path=self.config.ss_save_result_txt) self.load_model(model_file_name) pass un_norm = MyTransform.transform_un_normalize() self.net.eval() metrics = StreamSegMetrics(self.config.ss_num_classes) with torch.no_grad(): for i, (inputs, labels, _) in tqdm(enumerate(data_loader), total=len(data_loader)): inputs = inputs.float().cuda() labels = labels.long().cuda() outputs = self.net(inputs) preds = outputs.detach().max(dim=1)[1].cpu().numpy() targets = labels.cpu().numpy() metrics.update(targets, preds) if save_path: for j, (input_one, label_one, pred_one) in enumerate(zip(inputs, targets, preds)): un_norm(input_one.cpu()).save(os.path.join(save_path, "{}_{}.JPEG".format(i, j))) DataUtil.gray_to_color(np.asarray(label_one, dtype=np.uint8)).save( os.path.join(save_path, "{}_{}_l.png".format(i, j))) DataUtil.gray_to_color(np.asarray(pred_one, dtype=np.uint8)).save( os.path.join(save_path, "{}_{}_p.png".format(i, j))) pass pass pass pass score = metrics.get_results() Tools.print("{} {}".format(epoch, metrics.to_str(score)), txt_path=self.config.ss_save_result_txt) return score
def inference_ss_train(self, model_file_name=None, data_loader=None, save_path=None): if model_file_name is not None: Tools.print("Load model form {}".format(model_file_name), txt_path=self.config.ss_save_result_txt) self.load_model(model_file_name) pass final_save_path = Tools.new_dir("{}_final".format(save_path)) self.net.eval() with torch.no_grad(): for i, (inputs, labels, image_info_list) in tqdm(enumerate(data_loader), total=len(data_loader)): assert len(image_info_list) == 1 # 标签 basename = image_info_list[0].split("Data/DET/")[1] final_name = os.path.join(final_save_path, basename.replace(".JPEG", ".png")) final_name = Tools.new_dir(final_name) if os.path.exists(final_name): continue # 预测 outputs = self.net(inputs[0].float().cuda()).cpu() preds = outputs.max(dim=1)[1].numpy() if save_path: Image.open(image_info_list[0]).convert("RGB").save( Tools.new_dir(os.path.join(save_path, basename))) DataUtil.gray_to_color(np.asarray(preds[0], dtype=np.uint8)).save( os.path.join(save_path, basename.replace(".JPEG", ".png"))) Image.fromarray(np.asarray(preds[0], dtype=np.uint8)).save(final_name) pass pass pass pass
def __init__(self, image_size=256, num_classes=200): self.image_size = image_size self.num_classes = num_classes self.net = ClassNet(num_classes=self.num_classes).cuda() self.name_to_label_id, self.label_info_dict = DataUtil.get_class_name(data_root=self.get_data_root_path()) self.transform_train, self.transform_test = MyTransform.transform_train_cam(image_size=self.image_size) pass
def __init__(self, image_size=224, num_classes=200): self.image_size = image_size self.net = CAMNet(num_classes=num_classes).cuda() self.name_to_label_id, self.label_info_dict = DataUtil.get_class_name() self.transform_train, self.transform_test = MyTransform.transform_train_cam( image_size=self.image_size) self.transform_un_normalize = MyTransform.transform_un_normalize() pass
def train_eval_ss(self, data_loader, epoch=0, model_file_name=None): if model_file_name is not None: Tools.print("Load model form {}".format(model_file_name), txt_path=self.config.ss_save_result_txt) self.load_model(model_file_name) pass self.net.eval() with torch.no_grad(): for i, (inputs, labels, image_paths) in tqdm(enumerate(data_loader), total=len(data_loader)): inputs = inputs.float().cuda() outputs = self.net(inputs) preds = outputs.detach().max(dim=1)[1].cpu().numpy() for j, (pred_one, label_one, image_path) in enumerate(zip(preds, labels, image_paths)): im = Image.open(image_path) now_name = image_path.split("Data/DET/")[1] result_filename = Tools.new_dir(os.path.join(self.config.ss_train_eval_save_dir, now_name)) im.save(result_filename) DataUtil.gray_to_color(np.asarray(pred_one, dtype=np.uint8)).resize(im.size).save( result_filename.replace(".JPEG", "_new.png")) DataUtil.gray_to_color(np.asarray(label_one, dtype=np.uint8)).resize(im.size).save( result_filename.replace(".JPEG", "_old.png")) pred_one[pred_one != label_one & label_one != 255] = 255 DataUtil.gray_to_color(np.asarray(pred_one, dtype=np.uint8)).resize(im.size).save( result_filename.replace(".JPEG", ".png")) pass pass pass pass
def inference_ss(self, model_file_name=None, data_loader=None, save_path=None): if model_file_name is not None: Tools.print("Load model form {}".format(model_file_name), txt_path=self.config.ss_save_result_txt) self.load_model(model_file_name) pass final_save_path = Tools.new_dir("{}_final".format(save_path)) self.net.eval() metrics = StreamSegMetrics(self.config.ss_num_classes) with torch.no_grad(): for i, (inputs, labels, image_info_list) in tqdm(enumerate(data_loader), total=len(data_loader)): assert len(image_info_list) == 1 # 标签 max_size = 1000 size = Image.open(image_info_list[0]).size basename = os.path.basename(image_info_list[0]) final_name = os.path.join(final_save_path, basename.replace(".JPEG", ".png")) if os.path.exists(final_name): continue if size[0] < max_size and size[1] < max_size: targets = F.interpolate(torch.unsqueeze( labels[0].float().cuda(), dim=0), size=(size[1], size[0]), mode="nearest").detach().cpu() else: targets = F.interpolate(torch.unsqueeze(labels[0].float(), dim=0), size=(size[1], size[0]), mode="nearest") targets = targets[0].long().numpy() # 预测 outputs = 0 for input_index, input_one in enumerate(inputs): output_one = self.net(input_one.float().cuda()) if size[0] < max_size and size[1] < max_size: outputs += F.interpolate( output_one, size=(size[1], size[0]), mode="bilinear", align_corners=False).detach().cpu() else: outputs += F.interpolate(output_one.detach().cpu(), size=(size[1], size[0]), mode="bilinear", align_corners=False) pass pass outputs = outputs / len(inputs) preds = outputs.max(dim=1)[1].numpy() # 计算 metrics.update(targets, preds) if save_path: Image.open(image_info_list[0]).save( os.path.join(save_path, basename)) DataUtil.gray_to_color( np.asarray(targets[0], dtype=np.uint8)).save( os.path.join(save_path, basename.replace(".JPEG", "_l.png"))) DataUtil.gray_to_color(np.asarray( preds[0], dtype=np.uint8)).save( os.path.join(save_path, basename.replace(".JPEG", ".png"))) Image.fromarray(np.asarray( preds[0], dtype=np.uint8)).save(final_name) pass pass pass score = metrics.get_results() Tools.print("{}".format(metrics.to_str(score)), txt_path=self.config.ss_save_result_txt) return score
def eval_mlc_cam(self): Tools.print("Load model form {}".format(self.config.model_file_name)) self.load_model(self.config.model_file_name) self.net.eval() with torch.no_grad(): for _, (inputs, labels, image_paths) in tqdm(enumerate(self.data_loader_vis_cam), total=len(self.data_loader_vis_cam)): inputs_cuda = inputs.float().cuda() logits, out_features = self.net.forward(inputs_cuda, is_vis=True) logits = logits.detach().cpu().numpy() # 标签选择策略 label_for_cam = self.label_select_strategy( logits=logits, image_labels=labels.numpy(), thr=self.config.top_k_thr) # 生成 CAM np_cam = self.generate_cam( weights=self.net.head_linear.weight, features=out_features, indexes=label_for_cam, image_size=inputs_cuda.shape[-2:], num_classes=self.config.mlc_num_classes, bg_thr=self.config.bg_thr) # CRF 或 GrubCut 细化 CAM np_cam_crf = np.zeros_like(np_cam) np_cam_cut = np.zeros_like(np_cam) for bz_i, (cam, label_for_cam_one, input_one) in enumerate( zip(np_cam, label_for_cam, inputs)): image_input = np.asarray( self.transform_un_normalize(input_one)) for label_index, label_one in enumerate(label_for_cam_one): # CRF result_one = CRFTool.crf(image_input, np.expand_dims(cam[label_one + 1], axis=0), t=5) np_cam_crf[bz_i, label_one + 1] = result_one # GrubCut result_one = self.grub_cut_mask( image_input, cam[label_one + 1]) np_cam_cut[bz_i, label_one + 1] = result_one pass pass ####################################################################################################### # 可视化 for bz_i, (cam, image_path) in enumerate(zip(np_cam, image_paths)): # 预测结果 cam_label = np.asarray(np.argmax(cam, axis=0), dtype=np.uint8) now_name = image_path.split("Data/DET/")[1] result_filename = Tools.new_dir( os.path.join(self.config.mlc_cam_dir, now_name)) # 保存原图 Image.open(image_path).save(result_filename) # 训练数据逆增强 image_input = self.transform_un_normalize(inputs[bz_i]) image_input.save( result_filename.replace(".JPEG", "_i.JPEG")) # 对结果进行彩色可视化 im_color = DataUtil.gray_to_color(cam_label) im_color = im_color.resize( size=Image.open(image_path).size, resample=Image.NEAREST) im_color.save(result_filename.replace("JPEG", "png")) # 对CAM进行可视化 for label_index, label_one in enumerate( label_for_cam[bz_i]): now_cam_im = Image.fromarray( np.asarray(cam[label_one + 1] * 255, dtype=np.uint8)) now_cam_im.save( result_filename.replace( ".JPEG", "_{}.bmp".format(label_one + 1))) now_cam_crf_im = Image.fromarray( np.asarray(np_cam_crf[bz_i][label_one + 1] * 255, dtype=np.uint8)) now_cam_crf_im.save( result_filename.replace( ".JPEG", "_{}_crf.bmp".format(label_one + 1))) now_cam_cut_im = Image.fromarray( np.asarray(np_cam_cut[bz_i][label_one + 1] * 255, dtype=np.uint8)) now_cam_cut_im.save( result_filename.replace( ".JPEG", "_{}_cut.bmp".format(label_one + 1))) pass pass ####################################################################################################### pass pass pass
def _eval_mlc_cam_2_inner(self, index, pkl_path): if index % 1000 == 0: Tools.print("now is {}".format(index)) pass try: if not os.path.exists(pkl_path): return pkl_data = Tools.read_from_pkl(pkl_path) label_one = pkl_data["label"] image_path_one = pkl_data["image_path"] label_for_cam_one = pkl_data["label_for_cam"] cam_one = pkl_data["cam"] now_name = image_path_one.split("Data/DET/")[1] result_filename = Tools.new_dir( os.path.join(self.config.mlc_cam_dir, now_name)) if os.path.exists(result_filename.replace("JPEG", "png")): return im = Image.open(image_path_one) im.save(result_filename) image_size = im.size # 预测结果, 对结果进行彩色可视化 np_single_cam = 0 np_cam = np.zeros(shape=(self.config.mlc_num_classes + 1, image_size[1], image_size[0])) np_cam[0] = self.config.fg_thr * 255 # 0.25 for label in label_for_cam_one: cam_resize = [ self.torch_resize( cam, (self.config.mlc_size, self.config.mlc_size)) for cam in cam_one[label] ] norm_cam = np.sum(cam_resize, axis=0) / len(cam_resize) norm_cam = norm_cam / np.max(norm_cam) now_cam_im = Image.fromarray( np.asarray(norm_cam * 255, dtype=np.uint8)).resize(size=image_size) now_cam_im.save( result_filename.replace(".JPEG", "_{}.bmp".format(label + 1))) np_cam[label + 1] = np.asarray(now_cam_im) np_single_cam += np.asarray(now_cam_im, dtype=np.float) pass # cam_crf_one = CRFTool.crf_inference(np.asarray(im), np_cam / 255, t=5, n_label=len(np_cam)) # cam_crf_one = np.asarray(np.argmax(cam_crf_one, axis=0), dtype=np.uint8) # now_cam_crf_im = DataUtil.gray_to_color(cam_crf_one) # now_cam_crf_im.save(result_filename.replace(".JPEG", "_crf.png")) cam_label = np.asarray(np.argmax(np_cam, axis=0), dtype=np.uint8) cam_label[cam_label == 0] = 255 if len(label_for_cam_one) > 0: cam_label[(np_single_cam / len(label_for_cam_one)) < self.config.bg_thr * 255] = 0 # 0.05 pass im_color = DataUtil.gray_to_color(cam_label).resize( size=image_size, resample=Image.NEAREST) im_color.save(result_filename.replace("JPEG", "png")) except Exception(): Tools.print("{} {}".format(index, pkl_path)) pass pass
def _eval_mlc_cam_2_inner_old(self, index, pkl_path): if index % 1000 == 0: Tools.print("now is {}".format(index)) pass pkl_data = Tools.read_from_pkl(pkl_path) label_one = pkl_data["label"] image_path_one = pkl_data["image_path"] label_for_cam_one = pkl_data["label_for_cam"] cam_one = pkl_data["cam"] im = Image.open(image_path_one) image_size = im.size now_name = image_path_one.split("Data/DET/")[1] result_filename = Tools.new_dir( os.path.join(self.config.mlc_cam_dir, now_name)) # 保存原图 im.save(result_filename) # 预测结果, 对结果进行彩色可视化 np_cam = np.zeros(shape=(self.config.mlc_num_classes + 1, image_size[1], image_size[0])) np_cam[0] = self.config.fg_thr * 255 for label in label_for_cam_one: image_input = np.asarray( im.resize((self.config.mlc_size, self.config.mlc_size))) tensor_cam = torch.tensor(cam_one[label]) # cam = torch.sigmoid(tensor_cam) norm_cam = torch.squeeze( self._feature_norm(torch.unsqueeze(tensor_cam, dim=0))) now_cam_im = Image.fromarray( np.asarray(norm_cam * 255, dtype=np.uint8)).resize(size=image_size) now_cam_im.save( result_filename.replace(".JPEG", "_{}.bmp".format(label + 1))) np_cam[label + 1] = np.asarray(now_cam_im) cam_crf_one = self.torch_resize(norm_cam, size=(self.config.mlc_size, self.config.mlc_size)) cam_crf_one = CRFTool.crf(image_input, np.expand_dims(cam_crf_one, axis=0), t=5) now_cam_crf_im = Image.fromarray( np.asarray(cam_crf_one * 255, dtype=np.uint8)).resize(size=image_size) now_cam_crf_im.save( result_filename.replace(".JPEG", "_crf_{}.bmp".format(label + 1))) # cam_cut_one = self.torch_resize(tensor_cam, size=(self.config.mlc_size, self.config.mlc_size)) # cam_cut_one = self.grub_cut_mask(image_input, cam_cut_one) # now_cam_cut_im = Image.fromarray(np.asarray(cam_cut_one * 255, dtype=np.uint8)).resize(size=image_size) # now_cam_cut_im.save(result_filename.replace(".JPEG", "_{}_cut.bmp".format(label + 1))) pass cam_label = np.asarray(np.argmax(np_cam, axis=0), dtype=np.uint8) im_color = DataUtil.gray_to_color(cam_label).resize( size=image_size, resample=Image.NEAREST) im_color.save(result_filename.replace("JPEG", "png")) pass
def _eval_mlc_cam_2_inner(self, index, pkl_path): if index % 1000 == 0: Tools.print("now is {}".format(index)) pass try: pkl_data = Tools.read_from_pkl(pkl_path) label_one = pkl_data["label"] image_path_one = pkl_data["image_path"] label_for_cam_one = pkl_data["label_for_cam"] cam_one = pkl_data["cam"] im = Image.open(image_path_one) image_size = im.size now_name = image_path_one.split("Data/DET/")[1] result_filename = Tools.new_dir( os.path.join(self.config.mlc_cam_dir, now_name)) # 保存原图 # im.save(result_filename) # 预测结果, 对结果进行彩色可视化 np_single_cam = 0 np_cam = np.zeros(shape=(self.config.mlc_num_classes + 1, image_size[1], image_size[0])) for label in label_for_cam_one: image_input = np.asarray( im.resize((self.config.mlc_size, self.config.mlc_size))) tensor_cam = torch.tensor(cam_one[label]) norm_cam = torch.squeeze( self._feature_norm(torch.unsqueeze(tensor_cam, dim=0))) now_cam_im = Image.fromarray( np.asarray(norm_cam * 255, dtype=np.uint8)).resize(size=image_size) # now_cam_im.save(result_filename.replace(".JPEG", "_{}.bmp".format(label + 1))) np_single_cam += np.asarray(now_cam_im, dtype=np.float) cam_crf_one = self.torch_resize(norm_cam, size=(self.config.mlc_size, self.config.mlc_size)) cam_crf_one = CRFTool.crf(image_input, np.expand_dims(cam_crf_one, axis=0), t=5) now_cam_crf_im = Image.fromarray( np.asarray(cam_crf_one * 255, dtype=np.uint8)).resize(size=image_size) # now_cam_crf_im.save(result_filename.replace(".JPEG", "_crf_{}.bmp".format(label + 1))) np_cam[label + 1] = np.asarray(now_cam_im) / 2 + np.asarray( now_cam_crf_im) / 2 pass np_cam[0] = self.config.fg_thr * 255 # 0.7 cam_label = np.asarray(np.argmax(np_cam, axis=0), dtype=np.uint8) cam_label[cam_label == 0] = 255 if len(label_for_cam_one) > 0: cam_label[(np_single_cam / len(label_for_cam_one)) < self.config.bg_thr * 255] = 0 # 0.1 pass im_color = DataUtil.gray_to_color(cam_label).resize( size=image_size, resample=Image.NEAREST) im_color.save(result_filename.replace("JPEG", "png")) pass except Exception(): Tools.print("{} {}".format(index, pkl_path)) pass pass