def load(self, dataset_dir): import glob abs_dirname = os.path.join(submit.get_path_from_template(dataset_dir), '*') fnames = sorted(glob.glob(abs_dirname)) if len(fnames) == 0: print('\nERROR: No files found using the following glob pattern:', abs_dirname, '\n') sys.exit(1) images = [] for fname in fnames: try: if config.is_image_npy(): im = np.load(fname) else: if config.get_nb_channels() == 1: im = PIL.Image.open(fname).convert('L') else: im = PIL.Image.open(fname).convert('RGB') arr = np.array(im, dtype=np.float32) # If only one channel, we can have arr.shape = (256, 256) instead of (1, 256, 256) or (256, 256, 1) if len(arr.shape) == 2: reshaped = np.array([arr / 255.0 - 0.5]) elif config.is_image_npy(): reshaped = arr / 255.0 - 0.5 else: reshaped = arr.transpose([2, 0, 1]) / 255.0 - 0.5 images.append(reshaped) except OSError as e: print('Skipping file', fname, 'due to error: ', e) self.images = images
def add_validation_noise_np(self, im_log: np.ndarray): """ Add noise to training dataset. Author: Emanuele Dalsasso <*****@*****.**> PAPER REFERENCE: https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1000333 "Statistical properties of logaritmically transformed speckle" :param im_log: log of RGB image of size [3,256,256] NP :return: """ # We compute the shape of the noise array to compute if config.get_nb_channels() == 3: shape = im_log[0].shape else: shape = im_log.shape # If quick noise computation, we pick the noise from pre-computed list if self.quick_noise: log_speckle_norm = np.random.choice(self.noise_sample, size=shape) else: # Otherwise, we compute the Speckle noise s = np.zeros(shape=shape) for k in range(0, self.L): gamma = (np.abs( np.random.normal(size=shape, scale=1) + 1j * np.random.normal(size=shape, scale=1))**2) / 2 s = s + gamma s_amplitude = np.sqrt(s / self.L) # Get the log of the SPeckle noise, remove the bias and normalize it log_speckle = np.log(s_amplitude) log_speckle = self.add_bias_np(log_speckle) log_speckle_norm = log_speckle / (self.norm_max - self.norm_min) # If we have 3 channels, we add the same noise to all the channels. if config.get_nb_channels() == 3: log_speckle_norm_3ch = np.array( [log_speckle_norm, log_speckle_norm, log_speckle_norm]) im_noisy = im_log + log_speckle_norm_3ch else: im_noisy = im_log + log_speckle_norm return im_noisy
def add_train_noise_tf(self, im_log): """ Add noise to training dataset. Author: Emanuele Dalsasso <*****@*****.**> PAPER REFERENCE: https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1000333 "Statistical properties of logaritmically transformed speckle" :param im_log: log of RGB image of size [3,256,256] TF :return: """ # We compute the Speckle noise s = tf.zeros(shape=tf.shape(im_log)) if config.get_nb_channels() == 3: s = s[0] for k in range(0, self.L): gamma = (tf.abs( tf.complex(tf.random_normal(shape=tf.shape(s), stddev=1), tf.random_normal(shape=tf.shape(s), stddev=1)))** 2) / 2 s = s + gamma s_amplitude = tf.sqrt(s / self.L) # We get the log, remove the bias, and normalize the image log_speckle = tf.log(s_amplitude) log_speckle = self.add_bias_tf(log_speckle) log_speckle_norm = log_speckle / (self.norm_max - self.norm_min) # Depending on the number of channels, we add the noise one to three times if config.get_nb_channels() == 3: log_speckle_norm_3ch = tf.concat( [[log_speckle_norm], [log_speckle_norm], [log_speckle_norm]], axis=0) im_noisy = im_log + log_speckle_norm_3ch else: im_noisy = im_log + log_speckle_norm return im_noisy
def save_image(submit_config, img_t, filename): t = img_t.transpose([1, 2, 0]) # [RGB, H, W] -> [H, W, RGB] if t.dtype in [np.float32, np.float64]: t = clip_to_uint8(t) else: assert t.dtype == np.uint8 if config.get_nb_channels() == 1: PIL.Image.fromarray(t[:, :, 0], 'L').save( os.path.join(submit_config.run_dir, filename)) else: PIL.Image.fromarray(t, 'RGB').save( os.path.join(submit_config.run_dir, filename))
def load_image(fname): global format_stats, size_stats im = PIL.Image.open(fname) format_stats[im.mode] += 1 if (im.width < 256 or im.height < 256): size_stats['< 256x256'] += 1 else: size_stats['>= 256x256'] += 1 if get_nb_channels() == 1: arr = np.array(im.convert('L'), dtype=np.uint8) # 'L' for Black and White else: arr = np.array(im.convert('RGB'), dtype=np.uint8) # 'L' for Black and White assert len(arr.shape) == 2 return arr
def autoencoder(x, width=256, height=256, **_kwargs): if config.get_nb_channels() == 1: x.set_shape([None, 1, height, width]) else: x.set_shape([None, 3, height, width]) skips = [x] n = x n = conv_lr('enc_conv0', n, 48) n = conv_lr('enc_conv1', n, 48) n = maxpool2d(n) skips.append(n) n = conv_lr('enc_conv2', n, 48) n = maxpool2d(n) skips.append(n) n = conv_lr('enc_conv3', n, 48) n = maxpool2d(n) skips.append(n) n = conv_lr('enc_conv4', n, 48) n = maxpool2d(n) skips.append(n) n = conv_lr('enc_conv5', n, 48) n = maxpool2d(n) n = conv_lr('enc_conv6', n, 48) # ----------------------------------------------- n = upscale2d(n) n = tf.concat([n, skips.pop()], axis=1) n = conv_lr('dec_conv5', n, 96) n = conv_lr('dec_conv5b', n, 96) n = upscale2d(n) n = tf.concat([n, skips.pop()], axis=1) n = conv_lr('dec_conv4', n, 96) n = conv_lr('dec_conv4b', n, 96) n = upscale2d(n) n = tf.concat([n, skips.pop()], axis=1) n = conv_lr('dec_conv3', n, 96) n = conv_lr('dec_conv3b', n, 96) n = upscale2d(n) n = tf.concat([n, skips.pop()], axis=1) n = conv_lr('dec_conv2', n, 96) n = conv_lr('dec_conv2b', n, 96) n = upscale2d(n) n = tf.concat([n, skips.pop()], axis=1) n = conv_lr('dec_conv1a', n, 64) n = conv_lr('dec_conv1b', n, 32) if config.get_nb_channels() == 1: n = conv('dec_conv1', n, 1, gain=1.0) else: n = conv('dec_conv1', n, 3, gain=1.0) return n
def conv(name, x, fmaps, gain): with tf.variable_scope(name): if config.get_nb_channels() == 1: return conv2d_bias(x, fmaps, 1, gain) else: return conv2d_bias(x, fmaps, 3, gain)
def conv_lr(name, x, fmaps): with tf.variable_scope(name): if config.get_nb_channels() == 1: return tf.nn.leaky_relu(conv2d_bias(x, fmaps, 1), alpha=0.1) else: return tf.nn.leaky_relu(conv2d_bias(x, fmaps, 3), alpha=0.1)