def _validater(self, dataset_iter, epoch): TP, numPR, numGT = 0, 0, 0 dataset_iter.reset() N = dataset_iter.dataset.__len__() sum_loss = 0 #perm = np.random.permutation(N) for num in range(N): if self.mean_image is None: x_batch, y_batch = dataset_iter.next()[0] else: x_batch, y_batch = dataset_iter.next( )[0][0] - self.mean_image, dataset_iter.next()[0][1] if self.ndim == 2: im_size = x_batch.shape[2:] stride = [self.patchsize[0] / 2, self.patchsize[1] / 2] sh = [stride[0] / 2, stride[1] / 2] elif self.ndim == 3: im_size = x_batch.shape[2:] stride = [ self.patchsize[0] / 2, self.patchsize[1] / 2, self.patchsize[2] / 2 ] sh = [stride[0] / 2, stride[1] / 2, stride[2] / 2] ''' calculation for pad size''' if np.min(self.patchsize) > np.max(im_size): if self.ndim == 2: pad_size = [self.patchsize[0], self.patchsize[1]] elif self.ndim == 3: pad_size = [ self.patchsize[0], self.patchsize[1], self.patchsize[2] ] else: pad_size = [] for axis in range(len(im_size)): if (im_size[axis] + 2 * sh[axis] - self.patchsize[axis]) % stride[axis] == 0: stride_num = (im_size[axis] + 2 * sh[axis] - self.patchsize[axis]) / stride[axis] else: stride_num = (im_size[axis] + 2 * sh[axis] - self.patchsize[axis]) / stride[axis] + 1 pad_size.append(stride[axis] * stride_num + self.patchsize[axis]) gt = copy.deepcopy(y_batch) if self.ndim == 2: x_batch = mirror_extension_image( image=x_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, :, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1]] y_batch = mirror_extension_image( image=y_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1]] pre_img = np.zeros(pad_size) for y in range(0, pad_size[0] - stride[0], stride[0]): for x in range(0, pad_size[1] - stride[1], stride[1]): x_patch = x_batch[:, :, y:y + self.patchsize[0], x:x + self.patchsize[1]] y_patch = y_batch[:, y:y + self.patchsize[0], x:x + self.patchsize[1]] if self.gpu >= 0: x_patch = cuda.to_gpu(x_patch) y_patch = cuda.to_gpu(y_patch) s_loss, s_output = self.model(x=x_patch, t=y_patch, seg=False) sum_loss += float( cuda.to_cpu(s_loss.data) * self.batchsize) if self.gpu >= 0: s_output = cuda.to_cpu(s_output) pred = copy.deepcopy( (0 < (s_output[0][1] - s_output[0][0])) * 255) # Add segmentation image pre_img[y:y + self.patchsize[0] - stride[0], x:x + self.patchsize[1] - stride[1]] += pred[sh[0]:-sh[0], sh[1]:-sh[1]] seg_img = (pre_img > 0) * 1 seg_img = seg_img[0:im_size[0], 0:im_size[1]] elif self.ndim == 3: x_batch = mirror_extension_image( image=x_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, :, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1], self.patchsize[2] - sh[2]:self.patchsize[2] - sh[2] + pad_size[2]] y_batch = mirror_extension_image( image=y_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1], self.patchsize[2] - sh[2]:self.patchsize[2] - sh[2] + pad_size[2]] pre_img = np.zeros(pad_size) for z in range(0, pad_size[0] - stride[0], stride[0]): for y in range(0, pad_size[1] - stride[1], stride[1]): for x in range(0, pad_size[2] - stride[2], stride[2]): x_patch = x_batch[:, :, z:z + self.patchsize[0], y:y + self.patchsize[1], x:x + self.patchsize[2]] y_patch = y_batch[:, z:z + self.patchsize[0], y:y + self.patchsize[1], x:x + self.patchsize[2]] if self.gpu >= 0: x_patch = cuda.to_gpu(x_patch) y_patch = cuda.to_gpu(y_patch) s_loss, s_output = self.model(x=x_patch, t=y_patch, seg=False) sum_loss += float( cuda.to_cpu(s_loss.data) * self.batchsize) if self.gpu >= 0: s_output = cuda.to_cpu(s_output) pred = copy.deepcopy( (0 < (s_output[0][1] - s_output[0][0])) * 255) # Add segmentation image pre_img[z:z + self.patchsize[0] - stride[0], y:y + self.patchsize[1] - stride[1], x:x + self.patchsize[2] - stride[2]] += pred[sh[0]:-sh[0], sh[1]:-sh[1], sh[2]:-sh[2]] seg_img = (pre_img > 0) * 1 seg_img = seg_img[0:im_size[0], 0:im_size[1], 0:im_size[2]] gt = gt[0] io.imsave('{}/segimg{}_validation.tif'.format(self.opbase, num), np.array(seg_img * 255).astype(np.uint8)) io.imsave('{}/gtimg{}_validation.tif'.format(self.opbase, num), np.array(gt * 255).astype(np.uint8)) if epoch > 0: #make Centroid Pred (0 : background, 1 : object) markers_pr = morphology.label(seg_img, neighbors=4) mask_size = np.unique(markers_pr, return_counts=True)[1] < (self.delv + 1) remove_voxel = mask_size[markers_pr] markers_pr[remove_voxel] = 0 labels = np.unique(markers_pr) markers_pr = np.searchsorted(labels, markers_pr) numPR += np.max(markers_pr) center_pr = np.zeros((np.max(markers_pr), self.ndim)) count_pr = np.zeros(np.max(markers_pr)).reshape( np.max(markers_pr), 1) # make Centroid GT markers_gt = morphology.label(gt, neighbors=4) numGT += np.max(markers_gt) center_gt = np.zeros((np.max(markers_gt), self.ndim)) count_gt = np.zeros(np.max(markers_gt)).reshape( np.max(markers_gt), 1) if self.ndim == 2: y, x = np.shape(gt) for i in range(y): for j in range(x): if markers_pr[i][j] > 0: center_pr[markers_pr[i][j] - 1][0] += j center_pr[markers_pr[i][j] - 1][1] += i count_pr[markers_pr[i][j] - 1] += 1 if markers_gt[i][j] > 0: center_gt[markers_gt[i][j] - 1][0] += j center_gt[markers_gt[i][j] - 1][1] += i count_gt[markers_gt[i][j] - 1] += 1 elif self.ndim == 3: z, y, x = np.shape(gt) for i in range(z): for j in range(y): for k in range(x): if markers_pr[i][j][k] > 0: center_pr[markers_pr[i][j][k] - 1][0] += k center_pr[markers_pr[i][j][k] - 1][1] += j center_pr[markers_pr[i][j][k] - 1][2] += i count_pr[markers_pr[i][j][k] - 1] += 1 if markers_gt[i][j][k] > 0: center_gt[markers_gt[i][j][k] - 1][0] += k center_gt[markers_gt[i][j][k] - 1][1] += j center_gt[markers_gt[i][j][k] - 1][2] += i count_gt[markers_gt[i][j][k] - 1] += 1 center_pr /= count_pr center_gt /= count_gt pare = [] for gn in range(len(center_gt)): tmp = 0 chaild = [] for pn in range(len(center_pr)): if np.sum((center_gt[gn] - center_pr[pn])** 2) < self.r_thr**2: chaild.append(pn) tmp += 1 if tmp > 0: pare.append(chaild) used = np.zeros(len(center_pr)) TP += self._search_list(pare, used, 0) evals = self._evaluator(TP, numPR, numGT) return evals, sum_loss
def NuclearDetection(self, image_path): segbase = 'DetectionImages' if not (pt.exists(self.opbase + self.psep + segbase)): os.mkdir(self.opbase + self.psep + segbase) labbase = 'LabelingDetectionImages' if not (pt.exists(self.opbase + self.psep + labbase)): os.mkdir(self.opbase + self.psep + labbase) image = io.imread(image_path) im_size = image.shape if self.ndim == 2: ip_size = (int(image.shape[0] * self.resolution[1]), int(image.shape[1] * self.resolution[0])) sh = [int(self.stride[0] / 2), int(self.stride[1] / 2)] elif self.ndim == 3: ip_size = (int(image.shape[0] * self.resolution[2]), int(image.shape[1] * self.resolution[1]), int(image.shape[2] * self.resolution[0])) sh = [ int(self.stride[0] / 2), int(self.stride[1] / 2), int(self.stride[2] / 2) ] image = tr.resize(image, ip_size, order=1, preserve_range=True) im_size_ip = image.shape # Scaling if self.scaling: image = image.astype(np.float32) #image = image / image.max() image = (image - image.min()) / (image.max() - image.min()) #image = (image - image.mean()) / image.std() ''' calculation for pad size''' #if np.min(self.patchsize) > np.max(im_size): if np.min( self.patchsize) > np.max(np.array(im_size) + np.array(sh) * 2): if self.ndim == 2: pad_size = [self.patchsize[0], self.patchsize[1]] elif self.ndim == 3: pad_size = [ self.patchsize[0], self.patchsize[1], self.patchsize[2] ] else: pad_size = [] for axis in range(len(im_size_ip)): if (im_size_ip[axis] + 2 * sh[axis] - self.patchsize[axis]) % self.stride[axis] == 0: stride_num = int( (im_size_ip[axis] + 2 * sh[axis] - self.patchsize[axis]) / self.stride[axis]) else: stride_num = int( (im_size_ip[axis] + 2 * sh[axis] - self.patchsize[axis]) / self.stride[axis]) + 1 pad_size.append( int(self.stride[axis] * stride_num + self.patchsize[axis])) pre_img = np.zeros(pad_size) if self.ndim == 2: image = mirror_extension_image( image=image, ndim=self.ndim, length=int(np.max(self.patchsize)))[self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1]] for y in range(0, pad_size[0] - self.stride[0], self.stride[0]): for x in range(0, pad_size[1] - self.stride[1], self.stride[1]): x_patch = image[y:y + self.patchsize[0], x:x + self.patchsize[1]] x_patch = np.expand_dims(np.expand_dims(x_patch.astype( np.float32), axis=0), axis=0) if self.gpu >= 0: x_patch = cuda.to_gpu(x_patch) s_output = self.model(x=x_patch, t=None, seg=True) if self.gpu >= 0: s_output = cuda.to_cpu(s_output) pred = copy.deepcopy( (0 < (s_output[0][1] - s_output[0][0])) * 255) # Add segmentation image pre_img[y:y + self.stride[0], x:x + self.stride[1]] += pred[sh[0]:-sh[0], sh[1]:-sh[1]] seg_img = (pre_img > 0) * 255 seg_img = seg_img[0:im_size_ip[0], 0:im_size_ip[1]] elif self.ndim == 3: image = mirror_extension_image( image=image, ndim=self.ndim, length=int(np.max( self.patchsize)))[self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1], self.patchsize[2] - sh[2]:self.patchsize[2] - sh[2] + pad_size[2]] for z in range(0, pad_size[0] - self.stride[0], self.stride[0]): for y in range(0, pad_size[1] - self.stride[1], self.stride[1]): for x in range(0, pad_size[2] - self.stride[2], self.stride[2]): x_patch = image[z:z + self.patchsize[0], y:y + self.patchsize[1], x:x + self.patchsize[2]] x_patch = np.expand_dims(np.expand_dims(x_patch.astype( np.float32), axis=0), axis=0) if self.gpu >= 0: x_patch = cuda.to_gpu(x_patch) s_output = self.model(x=x_patch, t=None, seg=True) if self.gpu >= 0: s_output = cuda.to_cpu(s_output) pred = copy.deepcopy( (0 < (s_output[0][1] - s_output[0][0])) * 255) # Add segmentation image pre_img[z:z + self.stride[0], y:y + self.stride[1], x:x + self.stride[2]] += pred[sh[0]:-sh[0], sh[1]:-sh[1], sh[2]:-sh[2]] seg_img = (pre_img > 0) * 255 seg_img = seg_img[0:im_size_ip[0], 0:im_size_ip[1], 0:im_size_ip[2]] seg_img = (tr.resize(seg_img, im_size, order=1, preserve_range=True) > 0) * 255 filename = os.path.join( self.opbase, segbase, os.path.basename(image_path) [:os.path.basename(image_path).rfind('.')] + '.tif') # filename = self.opbase + self.psep + segbase + self.psep + 'detimg_t{0:03d}.tif'.format(int(image_path[image_path.rfind('/')+1:image_path.rfind('.')])) io.imsave(filename, seg_img.astype(np.uint8)) lab_img = mor.label(seg_img.astype(np.uint16), neighbors=4) mask_size = np.unique(lab_img, return_counts=True)[1] < (self.delv + 1) remove_voxel = mask_size[lab_img] lab_img[remove_voxel] = 0 labels = np.unique(lab_img) lab_img = np.searchsorted(labels, lab_img) filename = os.path.join( self.opbase, labbase, os.path.basename(image_path) [:os.path.basename(image_path).rfind('.')] + '.tif') # filename = self.opbase + self.psep + labbase + self.psep + 'labimg_t{0:03d}.tif'.format(int(image_path[image_path.rfind('/')+1:image_path.rfind('.')])) io.imsave(filename, lab_img.astype(np.uint16)) return lab_img.astype(np.uint16)
def _validater(self, dataset_iter, epoch): TP, TN, FP, FN = 0, 0, 0, 0 dataset_iter.reset() N = dataset_iter.dataset.__len__() sum_loss = 0 #perm = np.random.permutation(N) for num in range(N): if self.mean_image is None: x_batch, y_batch = dataset_iter.next()[0] else: x_batch, y_batch = dataset_iter.next( )[0][0] - self.mean_image, dataset_iter.next()[0][1] if self.ndim == 2: im_size = x_batch.shape[2:] stride = [ int(self.patchsize[0] / 2), int(self.patchsize[1] / 2) ] sh = [int(stride[0] / 2), int(stride[1] / 2)] elif self.ndim == 3: im_size = x_batch.shape[2:] stride = [ int(self.patchsize[0] / 2), int(self.patchsize[1] / 2), int(self.patchsize[2] / 2) ] sh = [ int(stride[0] / 2), int(stride[1] / 2), int(stride[2] / 2) ] ''' calculation for pad size''' if np.min(self.patchsize) > np.max(im_size): if self.ndim == 2: pad_size = [self.patchsize[0], self.patchsize[1]] elif self.ndim == 3: pad_size = [ self.patchsize[0], self.patchsize[1], self.patchsize[2] ] else: pad_size = [] for axis in range(len(im_size)): if (im_size[axis] + 2 * sh[axis] - self.patchsize[axis]) % stride[axis] == 0: stride_num = (im_size[axis] + 2 * sh[axis] - self.patchsize[axis]) / stride[axis] else: stride_num = (im_size[axis] + 2 * sh[axis] - self.patchsize[axis]) / stride[axis] + 1 pad_size.append( int(stride[axis] * stride_num + self.patchsize[axis])) gt = copy.deepcopy(y_batch) pre_img = np.zeros(pad_size) if self.ndim == 2: x_batch = mirror_extension_image( image=x_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, :, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1]] y_batch = mirror_extension_image( image=y_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1]] for y in range(0, pad_size[0] - stride[0], stride[0]): for x in range(0, pad_size[1] - stride[1], stride[1]): x_patch = x_batch[:, :, y:y + self.patchsize[0], x:x + self.patchsize[1]] y_patch = y_batch[:, y:y + self.patchsize[0], x:x + self.patchsize[1]] if self.gpu >= 0: x_patch = cuda.to_gpu(x_patch) y_patch = cuda.to_gpu(y_patch) s_loss, s_output = self.model(x=x_patch, t=y_patch, seg=False) sum_loss += float( cuda.to_cpu(s_loss.data) * self.batchsize) if self.gpu >= 0: s_output = cuda.to_cpu(s_output) pred = copy.deepcopy( (0 < (s_output[0][1] - s_output[0][0])) * 255) # Add segmentation image pre_img[y:y + stride[0], x:x + stride[1]] += pred[sh[0]:-sh[0], sh[1]:-sh[1]] seg_img = (pre_img > 0) * 1 seg_img = seg_img[:im_size[0], :im_size[1]] elif self.ndim == 3: x_batch = mirror_extension_image( image=x_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, :, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1], self.patchsize[2] - sh[2]:self.patchsize[2] - sh[2] + pad_size[2]] y_batch = mirror_extension_image( image=y_batch, ndim=self.ndim, length=int(np.max( self.patchsize)))[:, self.patchsize[0] - sh[0]:self.patchsize[0] - sh[0] + pad_size[0], self.patchsize[1] - sh[1]:self.patchsize[1] - sh[1] + pad_size[1], self.patchsize[2] - sh[2]:self.patchsize[2] - sh[2] + pad_size[2]] for z in range(0, pad_size[0] - self.patchsize[0], stride[0]): for y in range(0, pad_size[1] - self.patchsize[1], stride[1]): for x in range(0, pad_size[2] - self.patchsize[2], stride[2]): x_patch = x_batch[:, :, z:z + self.patchsize[0], y:y + self.patchsize[1], x:x + self.patchsize[2]] y_patch = y_batch[:, z:z + self.patchsize[0], y:y + self.patchsize[1], x:x + self.patchsize[2]] if self.gpu >= 0: x_patch = cuda.to_gpu(x_patch) y_patch = cuda.to_gpu(y_patch) s_loss, s_output = self.model(x=x_patch, t=y_patch, seg=False) sum_loss += float( cuda.to_cpu(s_loss.data) * self.batchsize) if self.gpu >= 0: s_output = cuda.to_cpu(s_output) pred = copy.deepcopy( (0 < (s_output[0][1] - s_output[0][0])) * 255) # Add segmentation image pre_img[z:z + stride[0], y:y + stride[1], x:x + stride[2]] += pred[sh[0]:-sh[0], sh[1]:-sh[1], sh[2]:-sh[2]] seg_img = (pre_img > 0) * 1 seg_img = seg_img[:im_size[0], :im_size[1], :im_size[2]] gt = gt[0] io.imsave('{}/segimg{}_validation.tif'.format(self.opbase, num), np.array(seg_img * 255).astype(np.uint8)) io.imsave('{}/gtimg{}_validation.tif'.format(self.opbase, num), np.array(gt * 255).astype(np.uint8)) countListPos = copy.deepcopy( seg_img.astype(np.int16) + gt.astype(np.int16)) countListNeg = copy.deepcopy( seg_img.astype(np.int16) - gt.astype(np.int16)) TP += len( np.where(countListPos.reshape(countListPos.size) == 2)[0]) TN += len( np.where(countListPos.reshape(countListPos.size) == 0)[0]) FP += len( np.where(countListNeg.reshape(countListNeg.size) == 1)[0]) FN += len( np.where(countListNeg.reshape(countListNeg.size) == -1)[0]) evals = self._evaluator(TP, TN, FP, FN) return evals, sum_loss