プリンキピア

第14章コンピュータビジョン

14.12.4. Extracting Features14.12.4 特徴を抽出する

We use the VGG-19 model pretrained on the ImageNet dataset to extract image features (Gatys et al., 2016).
英語のヒント
ImageNetのデータセットで事前学習したVGG-19モデルを使って、画像の特徴を抽出する(Gatys et al., 2016)。
pretrained_net = torchvision.models.vgg19(pretrained=True)
Downloading: "https://download.pytorch.org/models/vgg19-dcbb9e9d.pth" to /home/ci/.cache/torch/hub/checkpoints/vgg19-dcbb9e9d.pth
100%|██████████| 548M/548M [00:02<00:00, 213MB/s]
In order to extract the content features and style features of the image, we can select the output of certain layers in the VGG network. Generally speaking, the closer to the input layer, the easier to extract details of the image, and vice versa, the easier to extract the global information of the image. In order to avoid excessively retaining the details of the content image in the synthesized image, we choose a VGG layer that is closer to the output as the content layer to output the content features of the image. We also select the output of different VGG layers for extracting local and global style features. These layers are also called style layers. As mentioned in Section 8.2, the VGG network uses 5 convolutional blocks. In the experiment, we choose the last convolutional layer of the fourth convolutional block as the content layer, and the first convolutional layer of each convolutional block as the style layer. The indices of these layers can be obtained by printing the pretrained_net instance.
英語のヒント
画像のコンテンツの特徴とスタイルの特徴を抽出するため、VGGネットワークの特定の層の出力を選べる。一般に、入力層に近いほど画像の細部を抽出しやすく、逆に、出力層に近いほど画像の大域的な情報を抽出しやすい。合成画像にコンテンツ画像の細部を過度に残さないように、出力に近いVGGの層をコンテンツ層として選び、画像のコンテンツの特徴を出力させる。また、局所的なスタイルの特徴と大域的なスタイルの特徴を抽出するため、異なるVGGの層の出力を選ぶ。これらの層はスタイル層とも呼ばれる。8.2節で述べたように、VGGネットワークは5つの畳み込みブロックを使う。実験では、4番目の畳み込みブロックの最後の畳み込み層をコンテンツ層として、各畳み込みブロックの最初の畳み込み層をスタイル層として選ぶ。これらの層の添字は、pretrained_netインスタンスを出力すれば得られる。
style_layers, content_layers = [0, 5, 10, 19, 28], [25]
When extracting features using VGG layers, we only need to use all those from the input layer to the content layer or style layer that is closest to the output layer. Let’s construct a new network instance net, which only retains all the VGG layers to be used for feature extraction.
英語のヒント
VGGの層を使って特徴を抽出するときは、入力層から、出力層に最も近いコンテンツ層かスタイル層までのすべての層だけを使えばよい。特徴の抽出に使うすべてのVGGの層だけを残した、新しいネットワークのインスタンスnetを作ろう。
net = nn.Sequential(*[pretrained_net.features[i] for i in
                      range(max(content_layers + style_layers) + 1)])
Given the input X, if we simply invoke the forward propagation net(X), we can only get the output of the last layer. Since we also need the outputs of intermediate layers, we need to perform layer-by-layer computation and keep the content and style layer outputs.
英語のヒント
入力Xが与えられたとき、単に順伝播net(X)を呼び出すと、最後の層の出力しか得られない。途中の層の出力も必要なので、層ごとに計算を行い、コンテンツ層とスタイル層の出力を保持する必要がある。
def extract_features(X, content_layers, style_layers):
    contents = []
    styles = []
    for i in range(len(net)):
        X = net[i](X)
        if i in style_layers:
            styles.append(X)
        if i in content_layers:
            contents.append(X)
    return contents, styles
Two functions are defined below: the get_contents function extracts content features from the content image, and the get_styles function extracts style features from the style image. Since there is no need to update the model parameters of the pretrained VGG during training, we can extract the content and the style features even before the training starts. Since the synthesized image is a set of model parameters to be updated for style transfer, we can only extract the content and style features of the synthesized image by calling the extract_features function during training.
英語のヒント
下に二つの関数を定義する。get_contents関数はコンテンツ画像からコンテンツの特徴を抽出し、get_styles関数はスタイル画像からスタイルの特徴を抽出する。訓練のあいだ事前学習したVGGのモデルのパラメータを更新する必要はないので、訓練が始まる前からでも、コンテンツとスタイルの特徴を抽出できる。合成画像はスタイル変換のために更新するモデルのパラメータの組なので、合成画像のコンテンツとスタイルの特徴は、訓練のあいだにextract_features関数を呼び出すことでしか抽出できない。
def get_contents(image_shape, device):
    content_X = preprocess(content_img, image_shape).to(device)
    contents_Y, _ = extract_features(content_X, content_layers, style_layers)
    return content_X, contents_Y

def get_styles(image_shape, device):
    style_X = preprocess(style_img, image_shape).to(device)
    _, styles_Y = extract_features(style_X, content_layers, style_layers)
    return style_X, styles_Y