Last active
June 24, 2025 10:32
-
-
Save shimataro/3fdbf276d2bd51ad383d7363615f8f24 to your computer and use it in GitHub Desktop.
jupyter-examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "5aa7b4b9", | |
"metadata": {}, | |
"source": [ | |
"# JupyterLab ハンズオン 〜画像生成編〜\n", | |
"\n", | |
"NVIDIA社製GPUと、PyTorchが使えるJupyterLabが起動していることが前提です\n", | |
"([quay.io/jupyter/pytorch-notebook](https://quay.io/repository/jupyter/pytorch-notebook)など)\n", | |
"\n", | |
"## ステップ1: このファイルをJupyterLabで読み込む\n", | |
"\n", | |
"1. 右上↗️にある \"Raw\" をクリック(このファイルのソースが表示される)\n", | |
"1. JupyterLab上で \"File\" → \"Open from URL…\" を選択\n", | |
"1. URL入力欄に、手順1で表示したソースのURLを貼り付け\n", | |
"\n", | |
"このファイルがJupyterLabに読み込まれたら、ステップ1は終了です。続いて下のステップ2に進んでください。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e900a16a-3cd4-4e94-9025-9e899e0dc25e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%pip install diffusers accelerate transformers" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2cea47d0", | |
"metadata": {}, | |
"source": [ | |
"## ステップ2: プロンプトを調整\n", | |
"\n", | |
"下記ソースコードの `prompt` 変数を好きなプロンプトに変更してください。\n", | |
"\n", | |
"プロンプトを変えると生成される画像も変わります。目指せプロンプトマスター!\n", | |
"\n", | |
"`prompt` を変えたら、ステップ2は終了です。続いて下のステップ3に進んでください。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d4bd8d8b-e43a-445f-aa9d-1edeb487ead2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch\n", | |
"from diffusers import StableDiffusionPipeline\n", | |
"\n", | |
"# プロンプト\n", | |
"prompt = 'kitten, sleeping'\n", | |
"\n", | |
"# モデルからパイプラインを作成\n", | |
"pipe = StableDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4', torch_dtype=torch.float16).to('cuda')\n", | |
"\n", | |
"# 画像生成!\n", | |
"image = pipe(prompt).images[0]\n", | |
"image" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d88403bf", | |
"metadata": {}, | |
"source": [ | |
"## ステップ3: 画像生成!\n", | |
"\n", | |
"上部のツールバーから⏩をクリックしてください。\n", | |
"(\"Restart Kernel?\" のアラートが出たら \"Restart\" を押してOK)\n", | |
"\n", | |
"依存パッケージやチェックポイントなどのダウンロードなどでしばらく時間がかかります。\n", | |
"ダウンロードが終わったらいよいよ画像生成が始まり、少し待つと画像が表示されます。\n", | |
"\n", | |
"お気に入りの画像が出てくるまでレッツトライ!\n", | |
"\n", | |
"以上でハンズオンは終了です。お疲れ様でした!" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.12.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment