第14章コンピュータビジョン
14.12.5. Defining the Loss Function14.12.5 損失関数を定義する
Now we will describe the loss function for style transfer. The loss function consists of the content loss, style loss, and total variation loss.
英語のヒント
では、スタイル変換の損失関数を説明する。損失関数は、コンテンツ損失、スタイル損失、全変動損失からなる。
14.12.5.1. Content Loss14.12.5.1 コンテンツ損失
Similar to the loss function in linear regression, the content loss measures the difference in content features between the synthesized image and the content image via the squared loss function. The two inputs of the squared loss function are both outputs of the content layer computed by the
extract_features function.英語のヒント
線形回帰の損失関数と同じように、コンテンツ損失は、2乗損失関数によって、合成画像とコンテンツ画像のコンテンツの特徴の違いを測る。2乗損失関数の二つの入力は、どちらも
extract_features関数で計算したコンテンツ層の出力である。def content_loss(Y_hat, Y):
# We detach the target content from the tree used to dynamically compute
# the gradient: this is a stated value, not a variable. Otherwise the loss
# will throw an error.
return torch.square(Y_hat - Y.detach()).mean()14.12.5.2. Style Loss14.12.5.2 スタイル損失
Style loss, similar to content loss, also uses the squared loss function to measure the difference in style between the synthesized image and the style image. To express the style output of any style layer, we first use the
extract_features function to compute the style layer output. Suppose that the output has 1 example, channels, height , and width , we can transform this output into matrix with rows and columns. This matrix can be thought of as the concatenation of vectors , each of which has a length of . Here, vector represents the style feature of channel .英語のヒント
スタイル損失は、コンテンツ損失と同じように、2乗損失関数を使って合成画像とスタイル画像のスタイルの違いを測る。任意のスタイル層のスタイルの出力を表すため、まず
extract_features関数を使ってスタイル層の出力を計算する。出力が、事例1つ、チャネル個、高さ、幅をもつとすると、この出力を行列の行列に変換できる。この行列は、個のベクトルを連結したものとみなせ、それぞれの長さはである。ここで、ベクトルはチャネルのスタイルの特徴を表す。In the Gram matrix of these vectors , element in row and column is the dot product of vectors and . It represents the correlation of the style features of channels and . We use this Gram matrix to represent the style output of any style layer. Note that when the value of is larger, it likely leads to larger values in the Gram matrix. Note also that the height and width of the Gram matrix are both the number of channels . To allow style loss not to be affected by these values, the
gram function below divides the Gram matrix by the number of its elements, i.e., .英語のヒント
これらのベクトルのグラム行列では、行列の要素は、ベクトルとの内積である。それは、チャネルとのスタイルの特徴の相関を表す。このグラム行列を使って、任意のスタイル層のスタイルの出力を表す。の値が大きいと、グラム行列の値もおそらく大きくなることに注意してほしい。また、グラム行列の高さと幅はどちらもチャネル数であることにも注意してほしい。スタイル損失がこれらの値に影響されないように、下の
gram関数は、グラム行列をその要素の数、つまりで割る。def gram(X):
num_channels, n = X.shape[1], X.numel() // X.shape[1]
X = X.reshape((num_channels, n))
return torch.matmul(X, X.T) / (num_channels * n)Obviously, the two Gram matrix inputs of the squared loss function for style loss are based on the style layer outputs for the synthesized image and the style image. It is assumed here that the Gram matrix
gram_Y based on the style image has been precomputed.英語のヒント
明らかに、スタイル損失の2乗損失関数の二つのグラム行列の入力は、合成画像とスタイル画像についてのスタイル層の出力にもとづく。ここでは、スタイル画像にもとづくグラム行列
gram_Yは事前に計算されていると仮定する。def style_loss(Y_hat, gram_Y):
return torch.square(gram(Y_hat) - gram_Y.detach()).mean()14.12.5.3. Total Variation Loss14.12.5.3 全変動損失
Sometimes, the learned synthesized image has a lot of high-frequency noise, i.e., particularly bright or dark pixels. One common noise reduction method is total variation denoising. Denote by the pixel value at coordinate . Reducing total variation loss
英語のヒント
学習した合成画像には、ときに高周波のノイズ、つまり特に明るい画素や暗い画素が多く含まれる。よく使われるノイズ除去の手法の一つは、全変動ノイズ除去である。座標の画素値をで表す。次の全変動損失を減らす。
(14.12.1)
makes values of neighboring pixels on the synthesized image closer.
英語のヒント
そうすると、合成画像上の隣り合う画素の値が近くなる。
def tv_loss(Y_hat):
return 0.5 * (torch.abs(Y_hat[:, :, 1:, :] - Y_hat[:, :, :-1, :]).mean() +
torch.abs(Y_hat[:, :, :, 1:] - Y_hat[:, :, :, :-1]).mean())14.12.5.4. Loss Function14.12.5.4 損失関数
The loss function of style transfer is the weighted sum of content loss, style loss, and total variation loss. By adjusting these weight hyperparameters, we can balance among content retention, style transfer, and noise reduction on the synthesized image.
英語のヒント
スタイル変換の損失関数は、コンテンツ損失、スタイル損失、全変動損失の重みつき和である。これらの重みのハイパーパラメータを調整することで、合成画像でのコンテンツの保持、スタイルの変換、ノイズの除去のあいだで釣り合いをとれる。
content_weight, style_weight, tv_weight = 1, 1e4, 10
def compute_loss(X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram):
# Calculate the content, style, and total variance losses respectively
contents_l = [content_loss(Y_hat, Y) * content_weight for Y_hat, Y in zip(
contents_Y_hat, contents_Y)]
styles_l = [style_loss(Y_hat, Y) * style_weight for Y_hat, Y in zip(
styles_Y_hat, styles_Y_gram)]
tv_l = tv_loss(X) * tv_weight
# Add up all the losses
l = sum(styles_l + contents_l + [tv_l])
return contents_l, styles_l, tv_l, l