Google Apps Script - ①ログ出力

Google Apps Scriptを使うと、日常的な作業を自動化することができます。

またパソコンを起動していなくても、自動起動することができるのでとても便利です。

今回からはGASのサンプルをいろいろ実行してみてその可能性を探っていきたいと思います。

ログ出力

まずは最も基本的な処理としてログの出力を行ってみます。

[Google Apps Script]

1
2
3
function test() {
Logger.log("Hello World");
}

ログの出力は、処理内容の確認やデバッグでよく使用しますのでとても大切な処理になります。

pyopenjtalk - 日本語の音声合成② 読み上げ音声の作成

今回は日本語音声データセットJSUTの読み上げ音声を、音声合成フレームワークのNVIDIA/tacotron2の読み上げ音声に変換します。

必要ライブラリのインストール

読み上げ音声の変換を行うためには以下のライブラリをインストールします。

[WSLコンソール]

1
2
pip install librosa==0.8.0
pip install PySoundFile==0.9.0.post1

読み上げ音声の変換

JSUTの読み上げ音声を、NVIDIA/tacotron2の読み上げ音声に変換するソースコードは次のようになります。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import librosa
import soundfile as sf

# パス
in_paths = [
'jsut_ver1.1/basic5000/',
'jsut_ver1.1/countersuffix26/',
'jsut_ver1.1/loanword128/',
'jsut_ver1.1/onomatopee300/',
'jsut_ver1.1/precedent130/',
'jsut_ver1.1/repeat500/',
'jsut_ver1.1/travel1000/',
'jsut_ver1.1/utparaphrase512/',
'jsut_ver1.1/voiceactress100/']
out_path = 'wav/'

# 出力フォルダの準備
os.makedirs(out_path, exist_ok=True)

# wavを22KHzに変換
def convert(in_path, filename):
y, sr = librosa.core.load(in_path + 'wav/' + filename, sr=22050, mono=True)
sf.write(out_path + filename, y, sr, subtype="PCM_16")

# wavの変換
count = 0
for in_path in in_paths:
filenames = os.listdir(in_path + 'wav/')
for filename in filenames:
print(str(count) + '/7696')
count += 1
convert(in_path, filename)

上記のソースコードを実行すると次のようなログが表示されます。

[実行結果]

1
2
3
4
5
6
7
8
・・・(途中略)・・・
7689/7696
7690/7696
7691/7696
7692/7696
7693/7696
7694/7696
7695/7696

wavフォルダが作成されその中にNVIDIA/tacotron2用の読み上げ音声が出力されます。

フォルダ容量は1.53GBほどになりました。


次回は、音声合成の学習を行い・・・・たかったのですが、どうしてもうまくいかなかったのでまた別内容の記事を書き始めようと思います。<(_ _)>

pyopenjtalk - 日本語の音声合成① 台本の作成

日本語音声データセットJSUTを転移学習し、音声合成フレームワークのNVIDIA/tacotron2で日本語の音声合成を行います。

今回はNVIDIA/tacotron2用の台本を作成します。

JSUTデータセットの準備

下記サイトのJSUTの「台本」と「読み上げ音声データ」のデータセットを利用します。

JSUT - https://sites.google.com/site/shinnosuketakamichi/publication/jsut

WSLコンソールで下記のコマンドを実行し、ダウンロードと解凍を行います。

[WSLコンソール]

1
2
wget http://ss-takashi.sakura.ne.jp/corpus/jsut_ver1.1.zip
unzip jsut_ver1.1.zip

NVIDIA/tacotron2用の台本作成

ダウンロードしたJSUT用の台本をNVIDIA/tacotron2用の台本に変換するソースコードは以下の通りです。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import pyopenjtalk

# パス
in_paths = [
'jsut_ver1.1/basic5000/',
'jsut_ver1.1/countersuffix26/',
'jsut_ver1.1/loanword128/',
'jsut_ver1.1/onomatopee300/',
'jsut_ver1.1/precedent130/',
'jsut_ver1.1/repeat500/',
'jsut_ver1.1/travel1000/',
'jsut_ver1.1/utparaphrase512/',
'jsut_ver1.1/voiceactress100/']
out_path = 'filelists/'

# 出力フォルダの準備
os.makedirs(out_path, exist_ok=True)

# NVIDIA/tacotron2用の書式に変換
def convert(line):
strs = line.split(':')
strs[1] = pyopenjtalk.g2p(strs[1], kana=False)
strs[1] = strs[1].replace('pau',',')
strs[1] = strs[1].replace(' ','')
strs[1] = strs[1] + '.'
return 'wav/' + strs[0] + '.wav|' + strs[1] + '\n'

# transcriptの変換
with open(out_path + 'transcript_utf8.txt', 'w') as wf:
for in_path in in_paths:
with open(in_path + 'transcript_utf8.txt', 'r') as rf:
lines = rf.readlines()
for line in lines:
wf.write(convert(line))

上記の処理を実行すると、filelists/transcript_utf8.txtというファイルが作成されます。

このファイルがNVIDIA/tacotron2用の台本となります。


次回は、読み上げ音声の作成を行います。

pyopenjtalk - 音素表記

音素表記とは、音素が表記の単位になっている文字体系のことです。

今回は、pyopenjtalkというツールで音素表記の単位に変換してみます。

pyopenjtalkのインストール

pyopenjtalkはLinuxとMacOSに対応していますが、Windowsでは動作しません。

ですがWSLでは動作するようですので、WSLコンソールを使ってインストールしてみます。

[WSLコンソール]

1
2
sudo apt-get install cmake
sudo pip install pyopenjtalk

[実行結果]

1
2
3
・・・(途中略)・・・
Installing collected packages: tqdm, numpy, cython, pyopenjtalk
Successfully installed cython-0.29.24 numpy-1.21.3 pyopenjtalk-0.1.5 tqdm-4.62.3

最後に上記のようなログが表示されていれば、インストール完了です。

音素表記に変換

音素表記に変換するソースコードは下記のようになります。

[ソースコード]

1
2
import pyopenjtalk
print(pyopenjtalk.g2p('こんにちは'))

上記のPythonコードを実行します。

[実行結果]

1
2
3
4
Downloading: "https://github.com/r9y9/open_jtalk/releases/download/v1.11.1/open_jtalk_dic_utf_8-1.11.tar.gz"
dic.tar.gz: 100%|███████████████████████████████████████████████████████████████████| 22.6M/22.6M [03:07<00:00, 126kB/s]
Extracting tar file /usr/local/lib/python3.8/dist-packages/pyopenjtalk/dic.tar.gz
k o N n i ch i w a

「k o N n i ch i w a」という音素表記に変換することができました。

Deep Daze - ノートPCで実行(GPUなし)

Big SleepDeep Dazeを使って、テキストから画像を生成する処理を試してみました。

ただGoogle Colaboratoryの制限のために、処理の途中でセッションが切られてしまい最後まで画像を生成することができませんでした。

Google Colaboratoryの有料版を使ってテストしたいところですが、とりあえずローカル実行でどこまでできるかを確認してみます。

ローカルPCのスペック

実行するノートパソコンのスペックは下記の通りです。

  • CPU
    Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz 1.99 GHz
  • GPU
    なし
  • メモリ
    16GB
  • Pythonバージョン
    3.8.3

Big SleepはGPUなしでは実行できなかったので、Deep Dazeの方でローカル実行してみます。

Deep Dazeのインストール

次のコマンドを実行し、Deep Dazeをインストールします。

[コンソール]

1
pip install deep-daze

[実行結果]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Collecting deep-daze
Downloading deep_daze-0.10.3-py3-none-any.whl (1.4 MB)
|████████████████████████████████| 1.4 MB 6.4 MB/s
Requirement already satisfied: fire in c:\util\anaconda3\lib\site-packages (from deep-daze) (0.4.0)
Collecting siren-pytorch>=0.0.8
Downloading siren_pytorch-0.1.5-py3-none-any.whl (3.9 kB)
Requirement already satisfied: regex in c:\util\anaconda3\lib\site-packages (from deep-daze) (2020.6.8)
Collecting torch-optimizer
Downloading torch_optimizer-0.1.0-py3-none-any.whl (72 kB)
|████████████████████████████████| 72 kB ...
Requirement already satisfied: torchvision>=0.8.2 in c:\util\anaconda3\lib\site-packages (from deep-daze) (0.9.1)
Requirement already satisfied: einops>=0.3 in c:\util\anaconda3\lib\site-packages (from deep-daze) (0.3.2)
Requirement already satisfied: imageio>=2.9.0 in c:\util\anaconda3\lib\site-packages (from deep-daze) (2.9.0)
Requirement already satisfied: torch>=1.7.1 in c:\util\anaconda3\lib\site-packages (from deep-daze) (1.8.1)
Requirement already satisfied: tqdm in c:\util\anaconda3\lib\site-packages (from deep-daze) (4.57.0)
Requirement already satisfied: ftfy in c:\util\anaconda3\lib\site-packages (from deep-daze) (6.0.3)
Requirement already satisfied: six in c:\util\anaconda3\lib\site-packages (from fire->deep-daze) (1.15.0)
Requirement already satisfied: termcolor in c:\util\anaconda3\lib\site-packages (from fire->deep-daze) (1.1.0)
Collecting pytorch-ranger>=0.1.1
Downloading pytorch_ranger-0.1.1-py3-none-any.whl (14 kB)
Requirement already satisfied: pillow>=4.1.1 in c:\util\anaconda3\lib\site-packages (from torchvision>=0.8.2->deep-daze) (7.2.0)
Requirement already satisfied: numpy in c:\util\anaconda3\lib\site-packages (from torchvision>=0.8.2->deep-daze) (1.18.5)
Requirement already satisfied: typing-extensions in c:\util\anaconda3\lib\site-packages (from torch>=1.7.1->deep-daze) (3.7.4.2)
Requirement already satisfied: wcwidth in c:\util\anaconda3\lib\site-packages (from ftfy->deep-daze) (0.2.5)
Installing collected packages: siren-pytorch, pytorch-ranger, torch-optimizer, deep-daze
Successfully installed deep-daze-0.10.3 pytorch-ranger-0.1.1 siren-pytorch-0.1.5 torch-optimizer-0.1.0

インストールは問題なく終了しました。

テキストから画像生成

テキストから画像を生成します。

指定するテキストは“shattered plates on the grass”(草の上に粉々になったプレート)としました。

[コンソール]

1
imagine "shattered plates on the grass"

15:00スタート
とりあえず18時間後に生成されている画像は下記の通りです。

(まだ処理が終了していません・・・😥😥😥)


それっぽい画像になっていると思います。

GPUなしだとかなり時間はかかってしまいますが、Google Colaboratoryのように途中でセッションを切られてしまうことはないので安心です。


ちなみに、途中で動画編集ソフトを立ち上げたらメモリ不足エラーになって進捗94%のところで終了してしまいました😥😥😥

[メモリー不足のログ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
image updated at "./shattered_plates_on_the_grass.000001.jpg"
image updated at "./shattered_plates_on_the_grass.000002.jpg"
image updated at "./shattered_plates_on_the_grass.000003.jpg"
image updated at "./shattered_plates_on_the_grass.000004.jpg"
image updated at "./shattered_plates_on_the_grass.000005.jpg"
image updated at "./shattered_plates_on_the_grass.000006.jpg"
image updated at "./shattered_plates_on_the_grass.000007.jpg"
image updated at "./shattered_plates_on_the_grass.000008.jpg"
image updated at "./shattered_plates_on_the_grass.000009.jpg"
loss: -46.64: 94%|█████████████████████████████████████████████████████████████████████████████▍ | 991/1050 [15:15:47<54:31, 55.45s/it]
epochs: 0%| | 0/20 [15:15:47<?, ?it/s]
Traceback (most recent call last):
File "c:\util\anaconda3\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\util\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Util\anaconda3\Scripts\imagine.exe\__main__.py", line 7, in <module>
File "c:\util\anaconda3\lib\site-packages\deep_daze\cli.py", line 151, in main
fire.Fire(train)
File "c:\util\anaconda3\lib\site-packages\fire\core.py", line 141, in Fire
component_trace = _Fire(component, args, parsed_flag_args, context, name)
File "c:\util\anaconda3\lib\site-packages\fire\core.py", line 466, in _Fire
component, remaining_args = _CallAndUpdateTrace(
File "c:\util\anaconda3\lib\site-packages\fire\core.py", line 681, in _CallAndUpdateTrace
component = fn(*varargs, **kwargs)
File "c:\util\anaconda3\lib\site-packages\deep_daze\cli.py", line 147, in train
imagine()
File "c:\util\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "c:\util\anaconda3\lib\site-packages\deep_daze\deep_daze.py", line 584, in forward
_, loss = self.train_step(epoch, i)
File "c:\util\anaconda3\lib\site-packages\deep_daze\deep_daze.py", line 508, in train_step
self.scaler.scale(loss).backward()
File "c:\util\anaconda3\lib\site-packages\torch\tensor.py", line 245, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
File "c:\util\anaconda3\lib\site-packages\torch\autograd\__init__.py", line 145, in backward
Variable._execution_engine.run_backward(
RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:75] data. DefaultCPUAllocator: not enough memory: you tried to allocate 268435456 bytes. Buy new RAM!

Buy new RAM!(新しいRAM買え)って・・・・そんなログ始めて見ました😭😭😭

Deep Daze - テキストから画像生成②

Deep Daze も、Big Sleepと同じようにテキストから画像を生成するモデルです。

画像生成の深層学習モデルとして CLIP + Deep SIRENを使用しています。


Deep Dazeで生成した画像サンプルは下記のサイトで確認できます。

Deep Daze - https://github.com/lucidrains/deep-daze

PyTorchのインストール

Deep Dazeは、PyTorch 1.7.1であれば高速な画像生成が可能です。

PyTorch 1.7.1をインストールするためには、CUDAの情報が必要です。

以下のコマンドでCUDAのバージョンを表示します。

[Google Colaboratory]

1
2
# CUDAのバージョンの確認
!nvcc --version

[実行結果]

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Oct_12_20:09:46_PDT_2020
Cuda compilation tools, release 11.1, V11.1.105
Build cuda_11.1.TC455_06.29190527_0>

CUDA11であることが分かりました。


以下のサイトから、「v1.7.1」⇒「Wheel」⇒「Linux and Windows」⇒「CUDA 11.0」のコマンドを確認し、実行します。

Previou PyTorch Versions | PyTorch - https://pytorch.org/get-started/previous-versions/

[Google Colaboratory]

1
2
# PyTorch1.7.1のインストール
!pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

[実行結果]

・・・(途中略)・・・
Successfully installed torch-1.7.1+cu110 torchaudio-0.7.2 torchvision-0.8.2+cu110

最後に上記のようなログが表示されていればインストールは完了です。

Deep Dazeのインストール

次のコマンドを実行し、Deep Dazeをインストールします。

[Google Colaboratory]

1
2
# DeepDazeのインストール
!pip install deep-daze

[実行結果]

・・・(途中略)・・・
Successfully installed deep-daze-0.10.3 einops-0.3.2 fire-0.4.0 ftfy-6.0.3 imageio-2.9.0 pytorch-ranger-0.1.1 siren-pytorch-0.1.5 torch-optimizer-0.1.0

最後に上記のようなログが表示されていればインストールは完了です。

テキストから画像生成

テキストから画像を生成してみます。

今回はサンプル画像にある”life during the plague”(疫病の間の生活)を少し変更して“life during the war”(戦争の間の生活)というテキストを指定してみました。

[Google Colaboratory]

1
2
# テキストからの画像生成
!imagine "life during the war"

Google Colaboratoryの制限のため、途中でセッションが切られてしまい最後まで実行できませんでしたが、次のような画像が生成されていました。

life_during_the_war.000010.jpg


砂ぼこりか爆発のようなものがあるような気がしますが、もうちょっと認識しやすいものが生成されてほしいところです。

(有料プランにしないと最後まで実行させるのは難しいのかもしれません。)

Big Sleep - テキストから画像生成①

Big Sleepとは、テキストから画像を生成するモデルです。

画像生成の深層学習モデルとして CLIP + BigGAN を使用しています。


Big Sleepで生成した画像サンプルは下記のサイトで確認できます。

Big Sleep - https://github.com/lucidrains/big-sleep

Big Sleepのインストール

Big Sleepをインストールするためには下記のコマンドを実行します。

[Google Colaboratory]

1
2
# BigSleepのインストール
!pip install big-sleep

テキストから画像生成

テキストから画像を生成してみます。

簡潔なテキストの方が画像生成しやすいと思いまして、“little cat”というテキストを指定しました。

[Google Colaboratory]

1
2
# テキストからの画像生成
!dream "little cat"

次のような画像が生成されました。

little_cat.png


猫の一部のようなものと、中心付近には猫の足跡(?)が描かれた画像が生成されました。

言葉の選択がよくなかったのでしょうか・・・・。

ただテキストから画像を作成するというのはいろいろな可能性を感じるので、ほかのテキストでも画像生成を試してみたいと思います

CLIP - 画像分類(ラベルごとにスコア付け)

CLIPは、OpenAIが開発した画像とテキストの関連性をランク付けするニューラルネットワークです。

CLIPでは、自由にラベルを指定して画像分類を行うことができます。

Huggingface Transformersのインストール

CLIPは、Huggingface Transformersをインストールすることで使用できるようになります。

[Google Colaboratory]

1
!pip install transformers==4.6.0

Huggingface Transformersでは、以下のビジョンタスクのモデルアーキテクチャを使用できます。

  • Vision Transformer(Google AI)
  • DeiT(Facebook)
  • CLIP(OpenAI)

画像の準備

分類する画像として下記のものを使います。

この画像をGoogle Colaboratoryアップロードしておきます。

CLIPの準備

CLIPのモデルとプロセッサを準備し、判定用の画像を読み込みます。

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
import torch
from transformers import CLIPProcessor, CLIPModel

# モデルとプロセッサの準備
model = CLIPModel.from_pretrained('openai/clip-vit-base-patch32')
processor = CLIPProcessor.from_pretrained('openai/clip-vit-base-patch32')

from PIL import Image

# 画像の読み込み
image = Image.open('cat.png')

動物名ラベルでの画像分類

ラベルを定義し、どのラベルのスコアが高いのか判定します。

ラベルは以下の3つを定義してみました。

  • cat
  • dog
  • human

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
# ラベルの準備
labels = ['cat', 'dog', 'human']

# ラベルと画像をテンソルに変換
inputs = processor(text=labels, images=image, return_tensors='pt', padding=True)

# 推論
outputs = model(**inputs)
probs = outputs.logits_per_image.softmax(dim=1) # 3クラスのスコアの配列
predicted_class_idx = probs.argmax(-1).item() # スコアが最大のインデックス
print('class:', labels[predicted_class_idx]) # ラベルの表示
print('score:', probs) # スコアの表示

[実行結果]

class: cat
score: tensor([[0.9811, 0.0098, 0.0091]], grad_fn=)

catのスコアが1番高くなっており、きちんと画像判定できています。

色ラベルでの画像分類

色のラベルを準備し、再度スコアを出してみます。

  • black
  • white
  • red
  • brown

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
# 色のラベルの準備
labels = ['black', 'white', 'red', 'brown']

# ラベルと画像をテンソルに変換
inputs = processor(text=labels, images=image, return_tensors='pt', padding=True)

# 推論
outputs = model(**inputs)
probs = outputs.logits_per_image.softmax(dim=1) # 3クラスのスコアの配列
predicted_class_idx = probs.argmax(-1).item() # スコアが最大のインデックス
print('class:', labels[predicted_class_idx]) # ラベルの表示
print('score:', probs) # スコアの表示

[実行結果]

class: red
score: tensor([[0.0249, 0.1160, 0.5005, 0.3586]], grad_fn=)

redのスコアが一番高いのですが、私の眼には茶色の猫に見えます。

もしかして背景色の赤がredのスコアを高めてしまったのでしょうか。

雰囲気ラベルでの画像分類

最後に雰囲気のラベルでスコア付けをします。

  • pampered
    甘やかされた
  • docile
    従順
  • tsundere
    ツンデレ
  • smart
    頭がいい
  • gentle
    優しい
  • sociable
    社交的

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
# 雰囲気のラベルの準備
labels = ['pampered', 'docile', 'tsundere', 'smart', 'gentle', 'sociable']

# ラベルと画像をテンソルに変換
inputs = processor(text=labels, images=image, return_tensors='pt', padding=True)

# 推論
outputs = model(**inputs)
probs = outputs.logits_per_image.softmax(dim=1) # 3クラスのスコアの配列
predicted_class_idx = probs.argmax(-1).item() # スコアが最大のインデックス
print('class:', labels[predicted_class_idx]) # ラベルの表示
print('score:', probs) # スコアの表示

[実行結果]

class: docile
score: tensor([[0.0451, 0.7161, 0.0311, 0.0363, 0.1571, 0.0144]],
grad_fn=)

docile(従順)のスコアが一番高くなっているのですが、正直この写真の猫の雰囲気を判断するのは困難かと思います(;^_^A

Vision Transformer(Google AI)やDeiT(Facebook)のように、なんの画像かを判定するのとは違い、用意したラベルそれぞれのスコア(合致率)を導き出すのもなかなか興味深いですね。

DeiT - 画像分類

今回は、Facebook社が提供しているDeiTを使って画像分類を行います。

Huggingface Transformersのインストール

DeiTは、Huggingface Transformersをインストールすることで使用できるようになります。

[Google Colaboratory]

1
!pip install transformers==4.6.0

Huggingface Transformersでは、以下のビジョンタスクのモデルアーキテクチャを使用できます。

  • Vision Transformer(Google AI)
    前回の記事で使用したモデル
  • DeiT(Facebook)
    今回の記事で使用するモデル
  • CLIP(OpenAI)

画像分類

分類する画像として下記のものを使います。

この画像をGoogle Colaboratoryアップロードしておきます。


画像分類するためのソースコードは以下のようになります。

2~6行目が前回記事と異なっており、それ以外の箇所はすべて同じです。

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import torch
from transformers import AutoFeatureExtractor, DeiTForImageClassificationWithTeacher

# モデルと特徴抽出器の準備
model = DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224')
feature_extractor = AutoFeatureExtractor.from_pretrained('facebook/deit-base-distilled-patch16-224')

from PIL import Image

# 画像の読み込み
image = Image.open('cat.png')

# 画像をテンソルに変換
inputs = feature_extractor(images=image, return_tensors='pt')

# 推論の実行
outputs = model(**inputs)
logits = outputs.logits # 1000クラスの精度の配列
predicted_class_idx = logits.argmax(-1).item() # 精度が最大のインデックス
print('class:', model.config.id2label[predicted_class_idx]) # インデックスをラベルに変換

実行結果は以下の通りです。

[実行結果]

1
class: tabby, tabby cat

判定結果はtabby cat(ぶち猫)となり、前回のVision Transformer(Google AI)のモデルを使った時と同じ結果となりました。

次回は、CLIPを使った画像分類を行います。

Vision Transformer - 画像分類

今回は、Google社が提供しているVision Transformerを使って画像分類を行います。

Huggingface Transformersのインストール

Vision Transformerは、Huggingface Transformersをインストールすることで使用できるようになります。

[Google Colaboratory]

1
!pip install transformers==4.6.0

Huggingface Transformersでは、以下のビジョンタスクのモデルアーキテクチャを使用できます。

  • Vision Transformer(Google AI)
  • DeiT(Facebook)
  • CLIP(OpenAI)

画像分類

分類する画像として下記のものを使います。

この画像をGoogle Colaboratoryアップロードしておきます。


画像分類するためのソースコードは以下のようになります。

11行目でアップロードした画像を読み込んでいます。

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import torch
from transformers import ViTForImageClassification, ViTFeatureExtractor

# モデルと特徴抽出器の準備
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')
feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')

from PIL import Image

# 画像の読み込み
image = Image.open('cat.png')

# 画像をテンソルに変換
inputs = feature_extractor(images=image, return_tensors='pt')

# 推論の実行
outputs = model(**inputs)
logits = outputs.logits # 1000クラスの精度の配列
predicted_class_idx = logits.argmax(-1).item() # 精度が最大のインデックス
print('class:', model.config.id2label[predicted_class_idx]) # インデックスをラベルに変換

実行結果は以下の通りです。

[実行結果]

1
class: tabby, tabby cat

tabby catは日本語でぶち猫という意味です。

猫の種類はよくわかりませんが、猫には違いないのでちゃんと画像分類できたということになると思います。

次回は、Facebook社が提供しているDeiTを使って画像分類を行います。