def load_mask(mask_path, shape, return_mask_img=False): if K.image_dim_ordering() == 'th': _, channels, width, height = shape else: _, width, height, channels = shape mask = imread(mask_path, mode="L") # Grayscale mask load mask = imresize(mask, (width, height)).astype('float32') # Perform binarization of mask mask[mask <= 127] = 0 mask[mask > 127] = 255 max_val = np.amax(mask) mask /= max_val if return_mask_img: return mask mask_shape = shape[1:] mask_tensor = np.empty(mask_shape) for i in range(channels): if K.image_dim_ordering() == "th": mask_tensor[i, :, :] = mask else: mask_tensor[:, :, i] = mask return mask_tensor
def gram_matrix(x): assert K.ndim(x) == 3 if K.image_dim_ordering() == 'th': features = K.batch_flatten(x) else: features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) gram = K.dot(features - 1, K.transpose(features - 1)) return gram
def eval_loss_and_grads(x): if K.image_dim_ordering() == 'th': x = x.reshape((1, 3, img_width, img_height)) else: x = x.reshape((1, img_width, img_height, 3)) outs = f_outputs([x]) loss_value = outs[0] if len(outs[1:]) == 1: grad_values = outs[1].flatten().astype('float64') else: grad_values = np.array(outs[1:]).flatten().astype('float64') return loss_value, grad_values
def total_variation_loss(x): assert K.ndim(x) == 4 if K.image_dim_ordering() == 'th': a = K.square(x[:, :, :img_width - 1, :img_height - 1] - x[:, :, 1:, :img_height - 1]) b = K.square(x[:, :, :img_width - 1, :img_height - 1] - x[:, :, :img_width - 1, 1:]) else: a = K.square(x[:, :img_width - 1, :img_height - 1, :] - x[:, 1:, :img_height - 1, :]) b = K.square(x[:, :img_width - 1, :img_height - 1, :] - x[:, :img_width - 1, 1:, :]) return K.sum(K.pow(a + b, 1.25))
def content_loss(base, combination): channel_dim = 0 if K.image_dim_ordering() == 'th' else -1 channels = K.shape(base)[channel_dim] size = img_width * img_height if args.content_loss_type == 1: multiplier = 1 / (2.0 * (channels**0.5) * (size**0.5)) elif args.content_loss_type == 2: multiplier = 1 / (channels * size) else: multiplier = 1.0 return multiplier * K.sum(K.square(combination - base))
def deprocess_image(x): """ Convert a tensor back to a valid image. """ if K.image_dim_ordering() == 'th': x = x.reshape((3, img_width, img_height)) x = x.transpose((1, 2, 0)) else: x = x.reshape((img_width, img_height, 3)) x[:, :, 0] += 103.939 x[:, :, 1] += 116.779 x[:, :, 2] += 123.68 # BGR => RGB x = x[:, :, ::-1] x = np.clip(x, 0, 255).astype('uint8') return x
def deprocess_image(self, x): ''' Same normalization as in: https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py ''' if np.ndim(x) > 3: x = np.squeeze(x) # normalize tensor: center on 0., ensure std is 0.1 x -= x.mean() x /= (x.std() + 1e-5) x *= 0.1 # clip to [0, 1] x += 0.5 x = np.clip(x, 0, 1) # convert to RGB array x *= 255 if KTF.image_dim_ordering() == 'th': x = x.transpose((1, 2, 0)) # 0~255に正規化 x = np.clip(x, 0, 255).astype('uint8') return x
def preprocess_image(image_path, load_dims=False, read_mode="color"): global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio mode = "RGB" if read_mode == 'color' else "L" img = imread(image_path, mode=mode) if mode == 'L': # expand the 1 channel grayscale to 3 channel grayscale image temp = np.zeros(img.shape + (3, ), dtype=np.uint8) temp[:, :, 0] = img temp[:, :, 1] = img temp[:, :, 2] = img img = temp if load_dims: img_WIDTH = img.shape[0] img_HEIGHT = img.shape[1] aspect_ratio = float(img_HEIGHT) / img_WIDTH img_width = args.img_size if maintain_aspect_ratio: img_height = int(img_width * aspect_ratio) else: img_height = args.img_size img = imresize(img, (img_width, img_height)).astype('float32') # RGB -> BGR img = img[:, :, ::-1] img[:, :, 0] -= 103.939 img[:, :, 1] -= 116.779 img[:, :, 2] -= 123.68 if K.image_dim_ordering() == 'th': img = img.transpose((2, 0, 1)).astype('float32') img = np.expand_dims(img, axis=0) return img
def pooling_func(x): if pooltype == 1: return AveragePooling2D((2, 2), strides=(2, 2))(x) else: return MaxPooling2D((2, 2), strides=(2, 2))(x) # get tensor representation of base image base_image = K.variable( preprocess_image(base_image_path, True, read_mode=read_mode)) style_reference_images = [] for style_path in style_image_paths: style_reference_images.append(K.variable(preprocess_image(style_path))) # this will contain our generated image if K.image_dim_ordering() == 'th': combination_image = K.placeholder((1, 3, img_width, img_height)) else: combination_image = K.placeholder((1, img_width, img_height, 3)) image_tensor = [base_image] for style_image_tensor in style_reference_images: image_tensor.append(style_image_tensor) image_tensor.append(combination_image) nb_tensors = len(image_tensor) nb_style_images = nb_tensors - 2 # Content andoutput image not considered # combine the various image into a single Keras tensor input_tensor = K.concatenate(image_tensor, axis=0)