在同一数据集上,fit()与fit_generator()给出的结果不同

时间:2018-12-10 12:19:22

标签: python-3.x tensorflow keras

我一直在与CNN玩耍,以尝试从相对较大的图像中消除相干噪声。由于图像很大,因此无法一次将太多图像加载到内存中。 图片尺寸:1500x250 。由于这个问题,我试图实现一个生成器,将图像馈送到网络。我一直在努力获得不好的结果,但是假设问题出在网络的什么地方。我尝试将fit()与我的部分数据结合使用,并且在不对网络进行任何操作的情况下获得了非常好的结果。在同一子集上测试生成器会导致不良结果。我看不到的收获是什么?为什么我的发电机发生故障?

我的数据集大约有11.4万张图像,大约是475 GB,因此可以解释为什么我无法一次将所有图像都加载到内存中。我得到结果,它们是重新创建图像的实际结果,但是它们非常糟糕。我的生成器类在这里:

    class genOne(k.utils.Sequence):
        def __init__(self, img_rows, img_cols, channels, batch_size, clean_dir,
                noisy_dir, clean_files, noisy_files, shuffle=True):
            """Initialize variables:
            img_rows, img_cols, channels: the shape of the image
            batch_size                  : Self explanatory
            clean_dir, noisy_dir        : directories with files
            clean_files                 : Randomized list with clean images
            noisy_file                  : Randomized list with noise"""
            self.img_rows = img_rows
            self.img_cols = img_cols
            self.channels = channels
            self.batch_size = batch_size
            self.clean_dir = clean_dir
            self.noisy_dir = noisy_dir
            self.clean_files = clean_files.tolist()
            self.noisy_files = noisy_files.tolist()
            self.shuffled_noisy = []
            self.tmp_noisy = []
            self.tmp_clean = []
            self.shuffle = shuffle
            self.on_epoch_end()


        def __len__(self):
            """Sets the number of batches per epoch"""
            return floor((len(self.noisy_files)*len(self.clean_files))/self.batch_size)


        def __getitem__(self, index):
            """Generates data for each batch
               combine every type of noise with each image."""
            X = np.empty((self.batch_size, self.img_rows, self.img_cols,
                                                        self.channels))
            Y = np.zeros((self.batch_size, self.img_rows, self.img_cols,
                                                        self.channels))
            for i in range(self.batch_size):
                if not self.tmp_noisy:
                    self.tmp_noisy = self.shuffled_noisy
                    self.tmp_clean.pop(0)
                x_test = self.tmp_noisy.pop(0)
                X[i,] = np.expand_dims(np.load(self.noisy_dir + x_test).T[
                                  :self.img_rows,:self.img_cols],-1)
                Y[i,] = np.expand_dims(np.load(self.clean_dir + self.tmp_clean[0
                            ]).T[:self.img_rows, :self.img_cols],-1)
                y_test = self.tmp_clean[0]
                # Input equals ground truth + noise
                X[i,] += Y[i,]
                # Normalize data between 0 and 1
                X[i,] = ((X[i,]/np.amax(np.absolute(X[i,])))+1)/2
                Y[i,] = ((Y[i,]/np.amax(np.absolute(Y[i,])))+1)/2
            return X, Y


        def on_epoch_end(self):
            """Refresh all data on epoch end"""
            self.tmp_noisy = self.noisy_files
            self.tmp_clean = self.clean_files
            if self.shuffle == True:
                np.random.shuffle(self.tmp_noisy)
                np.random.shuffle(self.tmp_clean)
            self.shuffled_noisy = self.tmp_noisy

我有475张清晰的图像和300张包含纯噪点的图像。我将它们组合在一起,以便将每种图像与每种类型的噪声一起馈入网络。使用fit()的小情况就是300张图像,其中每个图像都是具有不同噪声的不同干净图像。

我知道我的驱动程序版本很旧,这需要一个旧版本的tensorflow。我无法更新它,所以我陷入了tensorflow 1.4.1。

规格:

  • 2个Nvidia Geforce GTX 1080 7.9 GB
  • Nvidia驱动程序版本367.44
  • cuDNN 6.0.21
  • CUDA 8.0
  • Debian Wheezy 7
  • Tensorflow-gpu 1.4.1
  • Keras 2.0.8
  • Python 3.6.7

0 个答案:

没有答案
相关问题