def save_image(self, saved_path, name, inputs): """ save unet output as a form of image """ if not os.path.exists(saved_path): os.makedirs(saved_path) output = self.unet(inputs) left = self.restore(inputs) right = self.restore(output) # The above two lines of code are wrong.To be precisely,errors will occur when the value of var left is less than # the value of var right.For example,left=217,right=220,then result is 253 after abs operation. diff = np.where(left > right, left - right, right - left).clip(0, 255).astype(np.uint8) plt.figure(num='unet result', figsize=(8, 8)) plt.subplot(2, 2, 1) plt.title('source image') plt.imshow(left) plt.axis('off') plt.subplot(2, 2, 2) plt.title('unet output') plt.imshow(right) plt.axis('off') plt.subplot(2, 2, 3) plt.imshow(rgb2gray(diff), cmap='jet') plt.colorbar(orientation='horizontal') plt.title('difference in heatmap') plt.axis('off') plt.subplot(2, 2, 4) plt.imshow(rgb2gray(diff.clip(0, 32)), cmap='jet') plt.colorbar(orientation='horizontal') plt.axis('off') plt.tight_layout() plt.savefig(add_prefix(saved_path, name)) plt.close()
def save_single_image(saved_path, model, name, inputs): """ save unet output as a form of image """ if not os.path.exists(saved_path): os.makedirs(saved_path) output = model(inputs) left = restore(inputs) right = restore(output) diff = np.where(left > right, left - right, right - left).clip(0, 255).astype(np.uint8) plt.figure(num='unet result', figsize=(8, 8)) plt.subplot(2, 2, 1) plt.title('source image') plt.imshow(left) plt.axis('off') plt.subplot(2, 2, 2) plt.title('unet output') plt.imshow(right) plt.axis('off') plt.subplot(2, 2, 3) plt.imshow(rgb2gray(diff), cmap='jet') plt.colorbar(orientation='horizontal') plt.title('difference in heatmap') plt.axis('off') plt.subplot(2, 2, 4) plt.imshow(rgb2gray(diff.clip(0, 32)), cmap='jet') plt.colorbar(orientation='horizontal') plt.axis('off') plt.tight_layout() plt.savefig(add_prefix(saved_path, name)) plt.close()
def save_single_image(name, inputs, output, label, phase): """ save single image local :param name: name :param inputs: network input :param output: network output :param label: image label: 'lesion' or 'normal' :param phase: image source: 'training' or 'validate' dataset :return: """ left = restore(inputs) right = restore(output) plt.figure(num='unet result', figsize=(8, 8)) plt.subplot(2, 2, 1) plt.title('source image') plt.imshow(left) plt.axis('off') plt.subplot(2, 2, 2) plt.title('output image') plt.imshow(right) plt.axis('off') diff = np.where(left > right, left - right, right - left).clip(0, 255).astype(np.uint8) plt.subplot(2, 2, 3) plt.imshow(rgb2gray(diff), cmap='jet') plt.colorbar() plt.tight_layout() plt.savefig(add_prefix(args.prefix, '%s/%s/%s' % (phase, label, name))) plt.close()
def save_single_image(label, name, inputs, output, unet, phase): left = restore(inputs) right = restore(unet) # save network input image plt.figure(num='unet result', figsize=(8, 8)) plt.subplot(2, 2, 1) plt.title('source image: %s' % label) plt.imshow(left) plt.axis('off') predict = F.softmax(output, dim=1) predict = to_np(predict).flatten() # save network output image plt.subplot(2, 2, 2) plt.title('lesion: %.2f, normal: %.2f' % (predict[0], predict[1])) plt.imshow(right) plt.axis('off') # save difference directly diff = np.where(left > right, left - right, right - left).clip(0, 255) plt.subplot(2, 2, 3) plt.imshow(rgb2gray(diff), cmap='jet') plt.colorbar() plt.title('difference in abs gray') plt.tight_layout() plt.savefig(add_prefix(args.prefix, '%s/%s/%s' % (phase, label, name))) plt.close()
def contrast(self, saved_path, name, inputs): """ save unet output as a form of image """ if not os.path.exists(saved_path): os.makedirs(saved_path) if os.path.exists(add_prefix(saved_path, name)): return output = self.auto_encoder(inputs) left = self.restore(inputs) right = self.restore(output) diff = np.where(left > right, left - right, right - left).clip(0, 255).astype(np.uint8) plt.figure(num='unet result', figsize=(8, 8)) plt.subplot(2, 2, 1) plt.title('source image') plt.imshow(left) plt.axis('off') plt.subplot(2, 2, 2) plt.title('unet output') plt.imshow(right) plt.axis('off') plt.subplot(2, 2, 3) plt.imshow(rgb2gray(diff), cmap='jet') plt.colorbar(orientation='horizontal') plt.title('difference in heatmap') plt.axis('off') plt.subplot(2, 2, 4) plt.imshow(rgb2gray(diff.clip(0, 32)), cmap='jet') plt.colorbar(orientation='horizontal') plt.axis('off') plt.tight_layout() plt.savefig(add_prefix(saved_path, name)) print('file %s is saved to %s successfully.' %(name, add_prefix(saved_path, name))) plt.close()
def calculate(self, image, name, binary_thresh): left = evaluate.restore(image) right = evaluate.restore(self.auto_encoder(image)) diff = np.where(left > right, left - right, right - left).clip(0, 255) # binaryzation: background: 0 lesion areas: 1 _, binary = cv2.threshold(rgb2gray(diff).astype(np.uint8), binary_thresh, 1, cv2.THRESH_BINARY) bounding_box_lst = self.groundtruth_dict[name] dice_loss_lst = [] for bounding_box in bounding_box_lst: pos_x, pos_y, size = bounding_box[0], bounding_box[1], bounding_box[2] groundtruth = np.ones((size, size)).astype(np.uint8) pred = binary[pos_y: pos_y + size, pos_x: pos_x + size] dice_loss_lst.append(1 - distance.dice(groundtruth.reshape(-1), pred.reshape(-1))) return sum(dice_loss_lst) / len(dice_loss_lst)
def screen(self): return imresize(rgb2gray(self._screen)/255., self.dims)