def gaus_prefilter(): traindataset = Train_dataset(1) XT_total = traindataset.data_true(654) volume_real = XT_total[0][:, :, :, np.newaxis] filename_real = os.path.join(DEFAULT_SAVE_PATH_PREDICTIONS, 'real.nii.gz') img_volume_gen = nib.Nifti1Image(volume_real, np.eye(4)) img_volume_gen.to_filename(filename_real) x_generator = gaussian_filter(volume_real, sigma=1) x_generator = zoom(x_generator, [0.5, 0.5, 0.5, 1], prefilter=True) filename_gen = os.path.join(DEFAULT_SAVE_PATH_PREDICTIONS, 'gaus_prefilter_ds2.nii.gz') img_volume_gen = nib.Nifti1Image(x_generator, np.eye(4)) img_volume_gen.to_filename(filename_gen) x_generator = zoom(x_generator, [0.25, 0.25, 0.25, 1], prefilter=True) filename_gen = os.path.join(DEFAULT_SAVE_PATH_PREDICTIONS, 'gaus_prefilter_ds4.nii.gz') img_volume_gen = nib.Nifti1Image(x_generator, np.eye(4)) img_volume_gen.to_filename(filename_gen)
def evaluate(img_width, img_height, img_depth, upsampling_factor): # dataset & variables traindataset = Train_dataset(1) iterations = math.ceil( (len(traindataset.subject_list) * 0.2)) # 817 subjects total. De 0 a 654 training. De 654 a 817 test. print(len(traindataset.subject_list)) print(iterations) totalpsnr = 0 totalssim = 0 array_psnr = np.empty(iterations) array_ssim = np.empty(iterations) batch_size = 1 div_patches = 4 num_patches = traindataset.num_patches img_width = img_width # 224 img_height = img_height # 224 img_depth = img_depth # 152 # define model t_input_gen = tf.placeholder('float32', [1, None, None, None, 1], name='t_image_input_to_SRGAN_generator') srgan_network = generator(t_input_gen, kernel=3, nb=6, upscaling_factor=upsampling_factor, is_train=False, reuse=False, img_width=img_width, img_height=img_height, img_depth=img_depth) # restore g sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)) saver = tf.train.Saver( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="SRGAN_g")) saver.restore( sess, tf.train.latest_checkpoint( '/work/isanchez/g/ds4-gdl-lrdecay/subpixel')) for i in range(0, iterations): # extract volumes xt_total = traindataset.data_true( 654 + i) # [[self.batch_size, 224, 224, 152]] xt_mask = traindataset.mask(654 + i) xg_generated = np.empty([1, 224, 224, 152, 1]) normfactor = (np.amax(xt_total[0])) / 2 x_generator = ((xt_total[0] - normfactor) / normfactor) res = 1 / upsampling_factor x_generator = x_generator[:, :, :, np.newaxis] x_generator = zoom(x_generator, [res, res, res, 1]) # x_generator = gaussian_filter(x_generator, sigma=1) xg_generated[0] = sess.run(srgan_network.outputs, {t_input_gen: x_generator[np.newaxis, :]}) xg_generated[0] = ((xg_generated[0] + 1) * normfactor) volume_real = xt_total[0] volume_real = volume_real[:, :, :, np.newaxis] volume_generated = xg_generated[0] volume_mask = aggregate(xt_mask) # compute metrics max_gen = np.amax(volume_generated) max_real = np.amax(volume_real) if max_gen > max_real: val_max = max_gen else: val_max = max_real min_gen = np.amin(volume_generated) min_real = np.amin(volume_real) if min_gen < min_real: val_min = min_gen else: val_min = min_real val_psnr = psnr(np.multiply(volume_real, volume_mask), np.multiply(volume_generated, volume_mask), dynamic_range=val_max - val_min) array_psnr[i] = val_psnr totalpsnr += val_psnr val_ssim = ssim(np.multiply(volume_real, volume_mask), np.multiply(volume_generated, volume_mask), dynamic_range=val_max - val_min, multichannel=True) array_ssim[i] = val_ssim totalssim += val_ssim print(val_psnr) print(val_ssim) # save volumes filename_gen = os.path.join(args.path_volumes, str(i) + 'gen.nii.gz') img_volume_gen = nib.Nifti1Image(volume_generated, np.eye(4)) img_volume_gen.to_filename(filename_gen) filename_real = os.path.join(args.path_volumes, str(i) + 'real.nii.gz') img_volume_real = nib.Nifti1Image(volume_real, np.eye(4)) img_volume_real.to_filename(filename_real) print('{}{}'.format('Mean PSNR: ', array_psnr.mean())) print('{}{}'.format('Mean SSIM: ', array_ssim.mean())) print('{}{}'.format('Variance PSNR: ', array_psnr.var())) print('{}{}'.format('Variance SSIM: ', array_ssim.var())) print('{}{}'.format('Max PSNR: ', array_psnr.max())) print('{}{}'.format('Min PSNR: ', array_psnr.min())) print('{}{}'.format('Max SSIM: ', array_ssim.max())) print('{}{}'.format('Min SSIM: ', array_ssim.min()))
def interpolate(): traindataset = Train_dataset(1) iterations = math.ceil((len(traindataset.subject_list) * 0.2)) # 817 subjects total. De 0 a 654 training. De 654 a 817 test. totalpsnr = 0 totalssim = 0 array_psnr = np.empty(iterations) array_ssim = np.empty(iterations) batch_size = 1 div_patches = 4 num_patches = traindataset.num_patches img_width = 32 # 64 img_height = 32 # 64 img_depth = 23 # 46 for i in range(0, iterations): XT_total = traindataset.data_true(654 + i) XT_mask = traindataset.mask(654 + i) volume_real = XT_total[0][:, :, :, np.newaxis] #volume_real_down = zoom(gaussian_filter(volume_real, sigma=1), [0.5, 0.5, 0.5, 1], prefilter=False, order=1) volume_real_down = zoom(volume_real, [0.5, 0.5, 0.5, 1]) volume_generated = zoom(volume_real_down, [2, 2, 2, 1]) #volume_generated = volume_generated[:, :, :, np.newaxis] #volume_real = XT_total[0][:, :, :, np.newaxis] volume_mask = aggregate(XT_mask) # compute metrics max_gen = np.amax(volume_generated) max_real = np.amax(volume_real) if max_gen > max_real: max = max_gen else: max = max_real min_gen = np.amin(volume_generated) min_real = np.amin(volume_real) if min_gen < min_real: min = min_gen else: min = min_real val_psnr = psnr(np.multiply(volume_real, volume_mask), np.multiply(volume_generated, volume_mask), dynamic_range=max - min) # val_psnr = psnr(volume_real, volume_generated, # dynamic_range=max - min) array_psnr[i] = val_psnr totalpsnr += val_psnr val_ssim = ssim(np.multiply(volume_real, volume_mask), np.multiply(volume_generated, volume_mask), dynamic_range=max - min, multichannel=True) array_ssim[i] = val_ssim totalssim += val_ssim print(val_psnr) print(val_ssim) #save volumes filename_gen = os.path.join(DEFAULT_SAVE_PATH_PREDICTIONS, str(i) + 'gen.nii.gz') img_volume_gen = nib.Nifti1Image(volume_generated, np.eye(4)) img_volume_gen.to_filename(filename_gen) filename_real = os.path.join(DEFAULT_SAVE_PATH_PREDICTIONS, str(i) + 'real.nii.gz') img_volume_real = nib.Nifti1Image(volume_real, np.eye(4)) img_volume_real.to_filename(filename_real) filename_down = os.path.join(DEFAULT_SAVE_PATH_PREDICTIONS, str(i) + 'down.nii.gz') img_volume_down = nib.Nifti1Image(volume_real_down, np.eye(4)) img_volume_down.to_filename(filename_down) return array_psnr, array_ssim