def extract_voxel_patch( image_path, gt_path=None, save_dir='/home/give/Documents/dataset/ISBI2017/media/nas/01_Datasets/CT/LITS/Training Batch 1_Patch' ): image_volume = read_nii(image_path) if gt_path is not None: gt_volume = read_nii(gt_path) gt_volume = np.asarray(np.asarray(gt_volume) >= 1, np.float32) shape = list(np.shape(image_volume)) batch_image = [] if gt_path is not None: batch_gt = [] for x in range(0, shape[0], Config.vox_size[0]): for y in range(0, shape[1], Config.vox_size[1]): for z in range(0, shape[2], Config.vox_size[2]): end_x = x + Config.vox_size[0] end_y = y + Config.vox_size[1] end_z = z + Config.vox_size[2] if end_x > shape[0]: x = shape[0] - Config.vox_size[0] end_x = shape[0] if end_y > shape[1]: y = shape[1] - Config.vox_size[1] end_y = shape[1] if end_z > shape[2]: z = shape[2] - Config.vox_size[2] end_z = shape[2] cur_image = image_volume[x:end_x, y:end_y, z:end_z] cur_gt = gt_volume[x:end_x, y:end_y, z:end_z] batch_image.append(cur_image) batch_gt.append(cur_gt) if z == shape[2] - Config.vox_size[2]: break if y == shape[1] - Config.vox_size[1]: break if x == shape[0] - Config.vox_size[0]: break print('ImageBatch shape is ', np.shape(batch_image), 'from ', os.path.basename(image_path)) print('GTBatch shape is ', np.shape(batch_gt), ' from ', os.path.basename(gt_path)) for idx, (image, gt) in enumerate(zip(batch_image, batch_gt)): basename = os.path.basename(image_path) file_id = basename.split('.')[0].split('-')[1] gt_mean = np.mean(np.asarray(gt, np.float32)) # print(idx, ' gt_mean: ', gt_mean, np.shape(image)) if gt_mean > 0.01: image_save_path = os.path.join(save_dir, 'positive') gt_save_path = os.path.join(save_dir, 'positive') else: image_save_path = os.path.join(save_dir, 'negative') gt_save_path = os.path.join(save_dir, 'negative') image_save_path = os.path.join( image_save_path, 'volume-' + file_id + '_' + str(idx) + '.nii') gt_save_path = os.path.join( gt_save_path, 'segmentation-' + file_id + '_' + str(idx) + '.nii') save_mhd_image(np.transpose(image, axes=[2, 0, 1]), image_save_path) save_mhd_image(np.transpose(gt, axes=[2, 0, 1]), gt_save_path) return True
def get_next_batch(self, is_training): if is_training: p_image_paths, p_gt_paths = self.train_generator_p.__next__() n_image_paths, n_gt_paths = self.train_generator_n.__next__() else: p_image_paths, p_gt_paths = self.val_generator_p.__next__() n_image_paths, n_gt_paths = self.val_generator_n.__next__() image_paths = np.concatenate([p_image_paths, n_image_paths], axis=0) gt_paths = np.concatenate([p_gt_paths, n_gt_paths], axis=0) image_paths, gt_paths = shuffle(list(zip(image_paths, gt_paths)), is_single=False) batch_images = [read_nii(image_path) for image_path in image_paths] batch_gts = [read_nii(gt_path) for gt_path in gt_paths] batch_images = Reader2.static_scalar(batch_images) batch_gts = np.asarray(np.asarray(batch_gts) >= 1.0, np.float32) return batch_images, batch_gts
def processing1(image_paths, gt_paths, crop_num): # 随机从512*512*XX里面挑选出固定size的patch processed_images = [] processed_gts = [] for (image_path, gt_path) in zip(image_paths, gt_paths): image = read_nii(image_path) gt = read_nii(gt_path) # print(np.shape(image), image_path) # print(np.shape(gt), gt_path) crop_results = random_crop([image, gt], pointed_size=Config.vox_size, crop_num=crop_num) processed_images.extend(crop_results[:, 0, :, :, :]) processed_gts.extend(crop_results[:, 1, :, :, :]) processed_images = np.asarray(processed_images, dtype=np.float32) processed_images = Reader.static_scalar(processed_images, -300, 500) processed_gts = np.asarray(processed_gts, dtype=np.float32) processed_gts[processed_gts >= 1.0] = 1.0 return processed_images, processed_gts
def processing2(image_paths, gt_paths, slice_num): # 从512*512*XX中随机挑选几个slice processed_images = [] processed_gts = [] for (image_path, gt_path) in zip(image_paths, gt_paths): image = read_nii(image_path) gt = read_nii(gt_path) # print(np.shape(image), image_path) # print(np.shape(gt), gt_path) crop_results = random_crop_slice([image, gt], slice_num=slice_num) processed_images.append(crop_results[0, :, :, :]) processed_gts.append(crop_results[1, :, :, :]) # processed_images = np.asarray(processed_images, np.float32) # processed_gts = np.asarray(processed_gts, np.float32) # processed_result = random_crop_slice([processed_images, processed_gts], slice_num=slic) processed_images = np.asarray(processed_images, dtype=np.float32) processed_images = Reader.static_scalar(processed_images, -300, 500) processed_gts = np.asarray(processed_gts, dtype=np.float32) processed_gts[processed_gts >= 1.0] = 1.0 return processed_images, processed_gts
def compute_onefile(sess, input_image_tensor, pred_last_tensor, image_path, save_path): volume = read_nii(image_path) volume = Reader.static_scalar(volume, -300, 500) print(np.max(volume), np.min(volume)) shape = list(np.shape(volume)) # volume = np.transpose(volume, axes=[2, 0, 1]) batch_data = [] for x in range(0, shape[0], Config.vox_size[0]): for y in range(0, shape[1], Config.vox_size[1]): for z in range(0, shape[2], Config.vox_size[2]): end_x = x + Config.vox_size[0] end_y = y + Config.vox_size[1] end_z = z + Config.vox_size[2] if end_x > shape[0]: x = shape[0] - Config.vox_size[0] end_x = shape[0] if end_y > shape[1]: y = shape[1] - Config.vox_size[1] end_y = shape[1] if end_z > shape[2]: z = shape[2] - Config.vox_size[2] end_z = shape[2] cur_data = volume[x:end_x, y:end_y, z:end_z] batch_data.append(cur_data) if z == shape[2] - Config.vox_size[2]: break if y == shape[1] - Config.vox_size[1]: break if x == shape[0] - Config.vox_size[0]: break batch_size = 10 pred_result = [] idx = 0 while idx < len(batch_data): end = idx + batch_size if end > len(batch_data): end = len(batch_data) cur_data = batch_data[idx:end] [pred_last_value] = sess.run( [pred_last_tensor], feed_dict={input_image_tensor: np.expand_dims(cur_data, axis=4)}) print('0, max: ', np.max(pred_last_value[:, :, :, :, 0]), np.min(pred_last_value[:, :, :, :, 0])) print('1, max: ', np.max(pred_last_value[:, :, :, :, 1]), np.min(pred_last_value[:, :, :, :, 1])) print('finished ', np.shape(pred_last_value)) pred_result.extend(pred_last_value) idx = end print('batch_data shape is ', np.shape(batch_data)) print('pred_result shape is ', np.shape(pred_result)) # pred_result = np.random.random(np.shape(batch_data)) pred_mask = np.zeros(np.shape(volume)) index = 0 for x in range(0, shape[0], Config.vox_size[0]): for y in range(0, shape[1], Config.vox_size[1]): for z in range(0, shape[2], Config.vox_size[2]): end_x = x + Config.vox_size[0] end_y = y + Config.vox_size[1] end_z = z + Config.vox_size[2] if end_x > shape[0]: x = shape[0] - Config.vox_size[0] end_x = shape[0] if end_y > shape[1]: y = shape[1] - Config.vox_size[1] end_y = shape[1] if end_z > shape[2]: z = shape[2] - Config.vox_size[2] end_z = shape[2] # pred_mask[x:end_x, y:end_y, z:end_z] = np.logical_and(volume[x:end_x, y:end_y, z:end_z] > 0, # volume[x:end_x, y:end_y, z:end_z] < 100) # print(np.max(np.argmax(pred_result[index], axis=3)), np.min(np.argmax(pred_result[index], axis=3))) # pred_mask[x:end_x, y:end_y, z:end_z] = np.argmax(pred_result[index], axis=3) pred_mask[x:end_x, y:end_y, z:end_z] = np.argmax(pred_result[index], axis=3) index += 1 if z == shape[2] - Config.vox_size[2]: break if y == shape[1] - Config.vox_size[1]: break if x == shape[0] - Config.vox_size[0]: break save_mhd_image(np.transpose(pred_mask, axes=[2, 1, 0]), save_path) return pred_mask
saver = tf.train.Saver(tf.global_variables(), max_to_keep=5) ckpt = tf.train.latest_checkpoint(FLAGS.model_restore_path) print('continue training from previous checkpoint from %s' % ckpt) start_step = int(os.path.basename(ckpt).split('-')[1]) variable_restore_op = slim.assign_from_checkpoint_fn( ckpt, slim.get_trainable_variables(), ignore_missing_vars=True) variable_restore_op(sess) sess.run(tf.assign(global_step, start_step)) pred_mask = compute_onefile( sess, input_image_tensor, tf.nn.softmax(pred_last), '/home/give/PycharmProjects/MICCAI2016_3DDSN/tmp/volume-55.nii', '/home/give/PycharmProjects/MICCAI2016_3DDSN/tmp/pred-55.nii') gt_path = '/home/give/PycharmProjects/MICCAI2016_3DDSN/tmp/segmentation-55.nii' gt_mask = read_nii(gt_path) print('GT mask shape: ', np.shape(gt_mask)) print('Pred mask shape: ', np.shape(pred_mask)) dice_score = calculate_dicescore(gt_mask, pred_mask) print('dice_score: ', dice_score) # test effective on train # input_image_tensor = tf.placeholder(dtype=tf.float32, # shape=[None, Config.vox_size[0], Config.vox_size[1], Config.vox_size[2], 1], # name='image_tensor') # input_gt_tensor = tf.placeholder(dtype=tf.int32, # shape=[None, Config.vox_size[0], Config.vox_size[1], Config.vox_size[2]], # name='gt_tensor') # global_step = tf.train.get_or_create_global_step() # learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, global_step, decay_steps=4000, decay_rate=0.1, # staircase=True)