21. 【step 01】 Visual Studio
• Visual Studio が必要な理由
(1) CUDA をインストールする際に警告メッセージの表示を
抑止したいなら Visual Studio が必要。
しかし、必須ではなさそう
(2) PyCUDA をインストールするために必要。
22. 【step 01】 Visual Studio (つづき)
• もし、Visual Studio がインストールされていない状態で
CUDA をインストールしようとすると、上図のような警告が表示される。
その場合でも、チェックを入れて続行すれば、後の工程になんら支障を
きたさないようだ。
つまり、Visual Studio のインストールは必須ではないようだ
23. 【step 01】 Visual Studio (つづき)
• もし、Microsoft C++ Build Tools が
インストールされていない状態で
PyCUDA をインストールしようとすると、
上図のようなエラーメッセージが表示され、
エラー終了してしまう。
なので、これを通過させるために、
Microsoft C++ Build Tools は必須
• Visual Studio をインストールすれば解決するが、
代わりに Microsoft C++ Build Tools だけを
インストールするのでも解決する
error: Microsoft Visual C++ 14.0 or greater is required.
Get it with "Microsoft C++ Build Tools":
https://visualstudio.microsoft.com/visual-cpp-build-tools/
24. 【step 01】 Visual Studio (つづき)
• Visual Studio のどのエディションをインストールするかについて、
下記 4 つの選択肢がある。
(1) Build Tools for Visual Studio 2022
(2) Visual Studio
(2-1) Community 2022
(2-2) Professional 2022 (有料)
(2-3) Enterprise 2022 (有料)
• (1) Build Tools for Visual Studio 2022 がいちばん軽い。
しかし、CUDA のインストール中に表示される警告は消えない。
これで行くなら、下記 Microsoft C++ Build Tools の
ウェブサイトへ行く
https://visualstudio.microsoft.com/visual-cpp-build-tools/
• (2-1) Visual Studio Community 2022 なら、CUDA の警告が表示されず、
いちばん安心できる。
これで行くなら、下記 Visual Studio のウェブサイトへ行く
https://visualstudio.microsoft.com/ja/
以下では、こっちについて説明する
25. 【step 01】 Visual Studio (つづき)
• Microsoft Visual Studio のウェブサイトへ行く
https://visualstudio.microsoft.com/ja/
• [Visual Studio のダウンロード] – [Community 2022] を選ぶ
• 下記ファイルがダウンロードされる
“VisualStudioSetup.exe”
39. 【step 02】 CUDA 11.6.2
• Microsoft Visual Studio がインストールしてあれば、
この画面は表示されないはず
• Build Tools for Visual Studio をインストールした場合は、
この画面が表示されるが、チェックを入れて進めば問題なさそう
50. 【step 03】 Python (つづき)
• DOS 窓で path を通す
set path=C:¥Program Files¥Python310¥Scripts¥;%path%
set path=C:¥Program Files¥Python310¥;%path%
• 下記のコマンドで、exe ファイルへのフルパスが表示されればOK。
where pip3
where python
60. 【step 6】 HuggingFace の Transformers
from transformers import pipeline
classifier = pipeline('sentiment-analysis')
results = classifier([ ¥
"We are very happy to show you the ?? Transformers library.", ¥
"We hope you don't hate it."])
for result in results:
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
The cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only
operation.
You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.
Moving 0 files to the new cache system
0it [00:00, ?it/s]
No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b
(https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
Downloading: 100%|████████████████████████████████████████████████████████████████████| 629/629 [00:00<00:00, 40.3kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████| 268M/268M [00:03<00:00, 82.8MB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████| 48.0/48.0 [00:00<?, ?B/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████| 232k/232k [00:00<00:00, 371kB/s]
label: POSITIVE, with score: 0.9996
label: NEGATIVE, with score: 0.5309
• 動作確認: 先ほどの管理者権限の DOS 窓で、
下記の Python プログラムを走らせる
• 結果、下記のように表示されればOK
ジコログ「HuggingfaceのTransformersをインストールする」
https://self-development.info/huggingface%E3%81%AEtransformers%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%82%8B/
67. …
has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]
for idx, has_nsfw_concept in enumerate(has_nsfw_concepts):
if has_nsfw_concept:
images[idx] = np.zeros(images[idx].shape) # black image
if any(has_nsfw_concepts):
logger.warning(
"Potential NSFW content was detected in one or more images. A black image will be returned instead."
" Try again with a different prompt and/or seed."
)
return images, has_nsfw_concepts
…
…
has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]
for idx, has_nsfw_concept in enumerate(has_nsfw_concepts):
if has_nsfw_concept:
# images[idx] = np.zeros(images[idx].shape) # black image
print("image[{:3d} / {:3d}]: potentially NFSW" ¥
.format(idx, len(has_nsfw_concepts)))
if any(has_nsfw_concepts):
logger.warning(
"Potential NSFW content was detected in one or more images. A black image will be returned instead."
" Try again with a different prompt and/or seed."
)
has_nsfw_concepts = [False for has_nsfw_concept in has_nsfw_concepts]
return images, has_nsfw_concepts
…
True または False が生成画像の
枚数分だけ並んだ配列。True: 問題あり
問題のある画像を全面真っ黒な画像で
置き換えている
どの画像に問題があったかの情報を呼び出し側に返していて、
上位の関数でも、ファイル出力の抑制をしている
全面真っ黒な画像で置き換えるところを
コメント・アウトして、実行しなくする
すべて False からなる配列に
書き換える (上位関数をだます)
修正前
修正後
78. …
has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]
for idx, has_nsfw_concept in enumerate(has_nsfw_concepts):
if has_nsfw_concept:
images[idx] = np.zeros(images[idx].shape) # black image
if any(has_nsfw_concepts):
logger.warning(
"Potential NSFW content was detected in one or more images. A black image will be returned instead."
" Try again with a different prompt and/or seed."
)
return images, has_nsfw_concepts
…
…
has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]
for idx, has_nsfw_concept in enumerate(has_nsfw_concepts):
if has_nsfw_concept:
# images[idx] = np.zeros(images[idx].shape) # black image
print("image[{:3d} / {:3d}]: potentially NFSW" ¥
.format(idx, len(has_nsfw_concepts)))
if any(has_nsfw_concepts):
logger.warning(
"Potential NSFW content was detected in one or more images. A black image will be returned instead."
" Try again with a different prompt and/or seed."
)
has_nsfw_concepts = [False for has_nsfw_concept in has_nsfw_concepts]
return images, has_nsfw_concepts
…
True または False が生成画像の
枚数分だけ並んだ配列。True: 問題あり
問題のある画像を全面真っ黒な画像で
置き換えている
どの画像に問題があったかの情報を呼び出し側に返していて、
上位の関数でも、ファイル出力の抑制をしている
全面真っ黒な画像で置き換えるところを
コメント・アウトして、実行しなくする
すべて False からなる配列に
書き換える (上位関数をだます)
修正前
修正後
84. 実行時エラーへの対策 (1): GPU のメモリ不足
• GPU のメモリが不足していると、実行時にエラー終了する
• 実は、さきほどのプログラムで手を打ってある
RuntimeError: CUDA out of memory. Tried to allocate 512.00 MiB (GPU 0; 8.00 GiB total
capacity; 5.82 GiB already allocated; 291.25 MiB free; 5.83 GiB reserved in total by
PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to
avoid fragmentation. See documentation for Memory Management and
PYTORCH_CUDA_ALLOC_CONF