def gen_batch(self):
        file_list = gen_file_list(self.file_path)
        mask_list = gen_file_list(self.mask_path)

        while True:
            # file_list, mask_list = random_shuffle(file_list, mask_list)
            for (file, mask) in zip(file_list, mask_list):

                X = load_data(file, self.file_path)
                Y = load_data(mask, self.mask_path)
                # X, Y = random_shuffle(X, Y)
                X = np.array(X)
                Y = np.array(Y)
                for i in range(X.shape[0] // self.batch_size):
                    x_batch = X[i * self.batch_size:(i + 1) * self.batch_size, ...]
                    y_batch = Y[i * self.batch_size:(i + 1) * self.batch_size, ...]

                    yield x_batch, y_batch
Exemplo n.º 2
0
def gen_stats(gt_path, recon_path, noisy_path, name):
    gt_list = gen_file_list(gt_path)
    recon_list = gen_file_list(recon_path)
    noisy_list = gen_file_list(noisy_path)

    _mse_array = []
    _psnr_array = []
    _ssim_array = []
    _ms_ssim_array = []
    current_best = 0
    current_worst = 1

    for (gt, recon, noisy) in zip(gt_list, recon_list, noisy_list):
        X = load_data(gt, gt_path)
        Y = load_data(recon, recon_path)
        Z = load_data(noisy, noisy_path)

        X = np.array(X)
        Y = np.array(Y)
        Z = np.array(Z)
        X = np.squeeze(X)
        Y = np.squeeze(Y)
        Z = np.squeeze(Z)

        for i in range(X.shape[0]):
            _mse_array.append(_MSE(X[i], Y[i]))
            _psnr_array.append(PSNR(X[i], Y[i]))
            _ssim_array.append(SSIM(X[i], Y[i]))
            # fig, ax = plt.subplots(1, 2, figsize=(12, 6))
            # ax[0].imshow(X[i], cmap='gray')
            # ax[1].imshow(Y[i], cmap='gray')
            # plt.show()

            x = X[i, :, :, np.newaxis]
            y = Y[i, :, :, np.newaxis]
            _ms_ssim = get_value(tf.image.ssim_multiscale(x, y, 2))
            _ms_ssim_array.append(_ms_ssim)

            if (_ms_ssim > current_best):
                current_best = _ms_ssim
                best_x = X[i]
                best_y = Y[i]
                best_z = Z[i]
            if (_ms_ssim < current_worst):
                current_worst = _ms_ssim
                worst_x = X[i]
                worst_y = Y[i]
                worst_z = Z[i]
    nrrd.write(recon_path + '/best_gt_{}.nrrd'.format(name),
               best_x,
               compression_level=1,
               index_order='C')
    nrrd.write(recon_path + '/best_recon_{}.nrrd'.format(name),
               best_y,
               compression_level=1,
               index_order='C')
    nrrd.write(recon_path + '/best_noisy_{}.nrrd'.format(name),
               best_z,
               compression_level=1,
               index_order='C')

    nrrd.write(recon_path + '/worst_gt_{}.nrrd'.format(name),
               worst_x,
               compression_level=1,
               index_order='C')
    nrrd.write(recon_path + '/worst_recon_{}.nrrd'.format(name),
               worst_y,
               compression_level=1,
               index_order='C')
    nrrd.write(recon_path + '/worst_noisy_{}.nrrd'.format(name),
               worst_z,
               compression_level=1,
               index_order='C')

    stats = np.empty((6, 4))
    stats[:, 0] = calc_stats(_mse_array)
    stats[:, 1] = calc_stats(_psnr_array)
    stats[:, 2] = calc_stats(_ssim_array)
    stats[:, 3] = calc_stats(_ms_ssim_array)

    np.savetxt(recon_path + '/nvidia.csv', stats, delimiter=',')
    def gen_batch(self):
        file_list = gen_file_list(self.file_path)
        mask_list = gen_file_list(self.mask_path)

        while True:
            #file_list, mask_list = random_shuffle(file_list, mask_list)

            file_list, mask_list = random_shuffle(file_list, mask_list)
            for (file, mask) in zip(file_list, mask_list):
                X = load_dataGAN(file, self.file_path)
                Y = load_data(mask, self.mask_path)

                # X, Y = random_shuffle(X, Y)
                X = np.array(X)
                Y = np.array(Y)
                for i in range(X.shape[0] // self.batch_size):
                    y_batch = Y[i * self.batch_size:(i + 1) * self.batch_size, ...]
                    x_batch = X[i * self.batch_size:(i + 1) * self.batch_size, ...]
                    # dict = {}
                    temp_batch = []
                    # it = 1
                    for j in range(self.batch_size):
                        it = 0

                        # it2 = 1
                        # output_name = 'output_{:03d}'.format(it)
                        num_proj = x_batch[j].shape[1]
                        temp_projections = np.zeros((Y.shape[1], Y.shape[2], num_proj))
                        # print(temp_projections.shape)
                        if num_proj == 2:
                            theta = np.linspace(0, 180, num_proj, False)
                        else:
                            theta = np.linspace(0, 180, num_proj, True)
                        rec = iradon(x_batch[j], theta=theta, output_size=Y.shape[1], gen_individual_backprojections=True,
                                     filter=None)
                        for image in rec:
                            # name = 'img_{:03d}_{:03d}'.format(it, it2)
                            # dict = {name : image_new, output_name : Y[(it-1)*self.batch_size]}
                            # dict[name] = image_new
                            # it2 +=
                            temp_projections[:,:,it] = image
                            it += 1
                        # dict[output_name] = Y[(it-1)*self.batch_size]
                        # it+=1
                        temp_batch.append(temp_projections)
                    # temp_batch = 2. * (temp_batch - np.min(temp_batch)) / np.ptp(temp_batch) - 1
                    # np.clip(temp_batch, -1., 1., out=temp_batch)
                    # print(temp_batch.shape)
                # for y in y_batch:
                #     z = np.squeeze(y)
                #     plt.imshow(z, cmap='gray')
                #     plt.show()
                    # for t in temp_batch:
                    #     g = np.transpose(t, (2,1,0))
                    #     for data in g:
                    #         plt.imshow(data, cmap='gray')
                    #         plt.show()
                    # for projections in temp_batch:
                    # print(y_batch.shape)

                    yield np.asarray(temp_batch), y_batch
Exemplo n.º 4
0
import nrrd
from tqdm import tqdm
import matplotlib.pyplot as plt

num_files_per_nrrd = 128
batchsize = 4

train_data_path = "./2dprojections/test_sino"
train_mask_path = "./2dprojections/test_gt"
valid_data_path = "./results_statistics/sparse_view_sino/15_angles"
valid_mask_path = "./results_statistics/sparse_view_ground_truth"
proba = BatchDataGenGAN(train_data_path, train_mask_path,
                        batchsize).gen_batch()
# x_batch, y_batch = next(proba)

valid_list = gen_file_list(valid_data_path)

model = unet_build(2, (512, 512, 15),
                   filters=[32, 64, 128, 256, 512],
                   actfn='relu',
                   is_batchnorm=False,
                   lastActivation='tanh',
                   residual=True)
# model = simple_autoencoder()
model.load_weights("eeeh_100.h5")

# model = tf.keras.models.load_model("unet_only_56.h5", custom_objects={'loss':SSIM}, compile=False)
results = []
gt = []
noisy = []
# for i in tqdm(range(len(valid_list)*num_files_per_nrrd // batchsize)):
Exemplo n.º 5
0
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Model
from keras import backend as K
from keras.optimizers import Adam
from neural_net_training.batch_generator import BatchDataGenGAN
from rand_functions.helper_functions import gen_file_list
from neural_net_training.save_load_model import save_model
from dists_loss import DISTS

tf.compat.v1.disable_eager_execution()

img_shape = (512, 512, 1)
batch_size = 4
train_data_path = "./2dprojections/selected_60"
train_mask_path = "./2dprojections/selected_gt"
file_list_length = len(gen_file_list(train_data_path))
train_datagen = BatchDataGenGAN(train_data_path, train_mask_path,
                                batch_size).gen_batch()
predict_gen = BatchDataGenGAN(train_data_path, train_mask_path,
                              batch_size).gen_batch()
x_batch, y_batch = next(train_datagen)
num_files_per_nrrd = 128
num_epochs = 100

steps_per_epoch_calc = file_list_length * num_files_per_nrrd // batch_size

penaltyLambda = 10  # d_loss = f_loss - r_loss + λ・penalty
gen_lambda = 1e-3

# critic(discriminator) iterations per generator iteration
trainRatio = 5