# TensorFlow Gram矩阵原理介绍和用法示例

## 本文概述

Gram矩阵来自有限维空间中的一个函数。则Gram矩阵项就是有限维子空间基本服务的内积。我们必须计算样式损失。但是我们还没有得到”为什么使用Gram矩阵计算样式损失”的信息。 Gram矩阵捕获给定层中一组要素映射的”要素分布”。

## 定义优化器

### 定义输入管道

``````def image_gen_function(data_dir, file_match_str, do_shuffle=True):
"""
"	The function returns a produced image, and the color channel is like values.
This is a generator function that is used by the service of tf.data api.

files = [f for f in os.listdir(data_dir) if f.startswith(file_match_str)]
if do_shuffle:
shuffle(files)

mean = np.array([[vgg_mean]])

# For each file preprocess the image
for f in file:
img = Image.open(os.path.join(data_dir, f))

width, height = img.size

#Here, we crop the image to a square by cropping on the longer axis
if width < height:
left, right = 0, width
top, bottom = (height-width)/2, ((height-width)/2) + width
elif width > height:
top, bottom = 0, height
left, right = (width - height)/2, ((width-height)/2) + height
else:
arr = np.array(img.resize((image_size, image_size))).astype(np.float32)
yield (arr, mean)

arr = np.array(img.crop((left, top, right, bottom)).resize((image_size, image_size))).astype(np.float32)
yield (arr, mean)

"""This function returns a dataset iterator of tf.data API.
"""
image_dataset = tf.data.Dataset.from_generator(
gen_func, output_types=(tf.float32, tf.float32), output_shapes=(tf.TensorShape(input_shape[1:]), tf.TensorShape([1, 1, 3]))
)

# If true, the mean will be subtracted``````

## 定义计算图

• 定义提供输入的迭代器
• 定义输入和CNN变量
• 定义内容, 样式和总损失
• 定义优化操作
``````config = tf.ConfigProto(allow_soft_placement=True)

# 1. Define the input pipeline in this step
part_style_gen_func = partial(image_gen_func, 'data', "style_")
part_content_gen_func = partial(image_gen_func, 'data', "content_")

# 2. Defining the inputs and weights
inputs = define_inputs(input_shape)
define_tf_weights()

layer_ids = list(vgg_layers.keys())

## gen_ph is used for initializing the generated image with the pixel value

##trying initializing with white noise
.
## The init_generate gives the initialization operation.
gen_ph = tf.placeholder(shape=input_shape, dtype=tf.float32)
init_generated = tf.assign(inputs["generated"], gen_ph)

# 3. Loss
# 3.1 Content loss in tf
c_loss = define_content_loss(
inputs=inputs, layer_ids=layer_ids, pool_inds=pool_inds, c_weight=alpha
)

# 3.2 Style loss
layer_weights_ph = tf.placeholder(shape=[len(layer_ids)], dtype=tf.float32, name='layer_weights')
s_loss = define_style_loss(
inputs=inputs, layer_ids=layer_ids, pool_inds=pool_inds, s_weight=beta, layer_weights=None
)``````

• 回顶