def __init__(self,imgs,labels,bounds): super(KumarPatchDataSet,self).__init__() self.imgs = torch.tensor(imgs).float() self.labels = torch.tensor(labels).float() self.bounds = torch.tensor(bounds).float() self.idx_array = list(np.arange(64).reshape(8,8)[1:7,1:7].reshape(36,).astype('int')) # 提取patches数据出来 self.img_patches, self.img_ibs, self.img_shs, self.img_sws = extract_patches(self.imgs,256,128) self.label_patches, self.label_ibs, self.label_shs, self.label_sws = extract_patches(self.labels,256,128) self.bound_patches, self.bound_ibs, self.bound_shs, self.bound_sws = extract_patches(self.bounds,256,128)
def operation(chunk, kernel): np.seterr(all='ignore') x = util.extract_patches(chunk, kernel) s1 = np.sum(x, axis=(-3, -2))**2 s2 = np.sum(x**2, axis=(-3, -2)) sembl = s1.sum(axis=-1) / s2.sum(axis=-1) sembl /= kernel[0] * kernel[1] return (sembl)
def operation(chunk, kernel): np.seterr(all='ignore') ki, kj, kk = kernel patches = util.extract_patches(chunk, kernel) out_data = [] for i in range(0, patches.shape[0]): traces = patches[i] traces = traces.reshape(-1, ki * kj * kk) cov = np.apply_along_axis(cov, 1, traces, ki, kj, kk) vals = np.linalg.eigvals(cov) vals = np.abs(vals.max(axis=1) / vals.sum(axis=1)) out_data.append(vals) out_data = np.asarray(out_data).reshape(patches.shape[:3]) return (out_data)
def infer(model, device, image): aug_list = ['ori', 'rot90', 'rot180', 'rot270', 'flip_h', 'flip_w'] image_tensor = torch.FloatTensor(image) image_tensor = image_tensor.unsqueeze(dim=0) image_tensor = image_tensor.permute(0, 3, 1, 2) # (0,3,1000,1000) print('image_tensor: ', image_tensor.shape) img_num, c, h, w = image_tensor.shape patches_d, ibs, shs, sws = extract_patches(image_tensor, 256, 128) sigmoid = nn.Sigmoid() # 定义最后的结果 finalsout = np.zeros([1, h, w], dtype=np.float32) finalcout = np.zeros([1, h, w], dtype=np.float32) # tta操作 for type_aug in aug_list: if type_aug == 'ori': tta_d = patches_d.clone() if type_aug == 'rot90': tta_d = patches_d.rot90(1, dims=(2, 3)) if type_aug == 'rot180': tta_d = patches_d.rot90(2, dims=(2, 3)) if type_aug == 'rot270': tta_d = patches_d.rot90(3, dims=(2, 3)) if type_aug == 'flip_h': tta_d = patches_d.flip(2) if type_aug == 'flip_w': tta_d = patches_d.flip(3) # spred = torch.zeros(tta_d.shape[0], 1, tta_d.shape[2], tta_d.shape[3]) cpred = torch.zeros(tta_d.shape[0], 1, tta_d.shape[2], tta_d.shape[3]) for start_batch in list(range(0, tta_d.shape[0], batch_size)): end_batch = np.min([start_batch + batch_size, tta_d.shape[0]]) input_d = tta_d[start_batch:end_batch] if len(input_d.shape) == 3: input_d = input_d.unsqueeze(dim=0) input_d = input_d.to(device) outs = model(input_d) sout = outs[0] cout = outs[5] spred[start_batch:end_batch] = sout.data.cpu() cpred[start_batch:end_batch] = cout.data.cpu() # Inverse TTA if type_aug == 'rot90': spred = spred.rot90(3, dims=(len(spred.shape) - 2, len(spred.shape) - 1)) cpred = cpred.rot90(3, dims=(len(cpred.shape) - 2, len(cpred.shape) - 1)) elif type_aug == 'rot180': spred = spred.rot90(2, dims=(len(spred.shape) - 2, len(spred.shape) - 1)) cpred = cpred.rot90(2, dims=(len(cpred.shape) - 2, len(cpred.shape) - 1)) elif type_aug == 'rot270': spred = spred.rot90(1, dims=(len(spred.shape) - 2, len(spred.shape) - 1)) cpred = cpred.rot90(1, dims=(len(cpred.shape) - 2, len(cpred.shape) - 1)) elif type_aug == 'flip_h': spred = spred.flip(len(spred.shape) - 2) cpred = cpred.flip(len(cpred.shape) - 2) elif type_aug == 'flip_w': spred = spred.flip(len(spred.shape) - 1) cpred = cpred.flip(len(cpred.shape) - 1) # spred_map = reconstruct_from_patches_weightedall( spred, ibs, shs, sws, 256, 128, img_num, 1, w, h, 1).squeeze().numpy() cpred_map = reconstruct_from_patches_weightedall( cpred, ibs, shs, sws, 256, 128, img_num, 1, w, h, 1).squeeze().numpy() # finalsout += spred_map finalcout += cpred_map finalsout /= len(aug_list) finalcout /= len(aug_list) return finalsout, finalcout
def operation(chunk, kernel): x = util.extract_patches(chunk, (1, 1, kernel[-1])) out = np.trapz(x).reshape(x.shape[:3]) return (out)
def operation(chunk, kernel): x = util.extract_patches(chunk, kernel) out = np.sqrt(np.mean(x**2, axis=(-3, -2, -1))) return (out)