subaqua commited on
Commit
11eecbe
1 Parent(s): 833754f

Update as_safetensors+fp16.ipynb

Browse files
Files changed (1) hide show
  1. as_safetensors+fp16.ipynb +69 -10
as_safetensors+fp16.ipynb CHANGED
@@ -28,7 +28,7 @@
28
  {
29
  "cell_type": "markdown",
30
  "source": [
31
- "以下のコードを上から順番に両方とも実行"
32
  ],
33
  "metadata": {
34
  "id": "OnuCk_wNLM_D"
@@ -37,7 +37,29 @@
37
  {
38
  "cell_type": "code",
39
  "source": [
40
- "from google.colab import drive \n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  "drive.mount(\"/content/drive\")"
42
  ],
43
  "metadata": {
@@ -46,15 +68,29 @@
46
  "execution_count": null,
47
  "outputs": []
48
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  {
50
  "cell_type": "code",
51
  "source": [
52
- "!pip install torch safetensors\n",
53
- "!pip install pytorch-lightning\n",
54
- "!pip install wget"
55
  ],
56
  "metadata": {
57
- "id": "pXr7oNJzwwgU"
58
  },
59
  "execution_count": null,
60
  "outputs": []
@@ -91,8 +127,8 @@
91
  " print(f\"\\\"{model}\\\"はURLではなく、正しい形式のファイルでもありません\")"
92
  ],
93
  "metadata": {
94
- "cellView": "form",
95
- "id": "4vd3A09AxJE0"
96
  },
97
  "execution_count": null,
98
  "outputs": []
@@ -109,10 +145,25 @@
109
  "import safetensors.torch\n",
110
  "from functools import partial\n",
111
  "\n",
 
 
 
 
112
  "models = \"wd-1-4-anime_e1.ckpt, wd-1-4-anime_e1.yaml\" #@param {type:\"string\"}\n",
113
  "as_fp16 = True #@param {type:\"boolean\"}\n",
114
  "save_directly_to_Google_Drive = True #@param {type:\"boolean\"}\n",
115
  "save_type = \".safetensors\" #@param [\".safetensors\", \".ckpt\"]\n",
 
 
 
 
 
 
 
 
 
 
 
116
  "\n",
117
  "def convert_yaml(file_name):\n",
118
  " with open(file_name) as f:\n",
@@ -126,6 +177,8 @@
126
  " file_name = os.path.splitext(file_name)[0] + \"-fp16.yaml\"\n",
127
  " with open(file_name, mode=\"w\") as f:\n",
128
  " f.write(yaml)\n",
 
 
129
  " os.chdir(\"/content\")\n",
130
  "\n",
131
  "if models == \"\":\n",
@@ -153,7 +206,13 @@
153
  " weights[key] = weights[key].half()\n",
154
  " if save_directly_to_Google_Drive:\n",
155
  " os.chdir(\"/content/drive/MyDrive\")\n",
156
- " save_model(weights, model_name + save_type)\n",
 
 
 
 
 
 
157
  " os.chdir(\"/content\")\n",
158
  " del weights\n",
159
  "\n",
@@ -191,7 +250,7 @@
191
  {
192
  "cell_type": "markdown",
193
  "source": [
194
- "モデルのリンク集: https://huggingface.co/models?other=stable-diffusion 等から好きなモデルを選ぼう"
195
  ],
196
  "metadata": {
197
  "id": "yaLq5Nqe6an6"
 
28
  {
29
  "cell_type": "markdown",
30
  "source": [
31
+ "最初に以下のコードを実行"
32
  ],
33
  "metadata": {
34
  "id": "OnuCk_wNLM_D"
 
37
  {
38
  "cell_type": "code",
39
  "source": [
40
+ "!pip install torch safetensors\n",
41
+ "!pip install pytorch-lightning\n",
42
+ "!pip install wget"
43
+ ],
44
+ "metadata": {
45
+ "id": "pXr7oNJzwwgU"
46
+ },
47
+ "execution_count": null,
48
+ "outputs": []
49
+ },
50
+ {
51
+ "cell_type": "markdown",
52
+ "source": [
53
+ "Google Drive上のファイルを読み書きしたい場合は、以下のコードを実行"
54
+ ],
55
+ "metadata": {
56
+ "id": "NsncqZOha2e0"
57
+ }
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "source": [
62
+ "from google.colab import drive\n",
63
  "drive.mount(\"/content/drive\")"
64
  ],
65
  "metadata": {
 
68
  "execution_count": null,
69
  "outputs": []
70
  },
71
+ {
72
+ "cell_type": "markdown",
73
+ "source": [
74
+ "<details><summary><font size=\"-0\">変換したモデルをHugging Faceに投稿したい場合は、以下のコードを実行</font></summary>\n",
75
+ "\n",
76
+ "1. [このページ](https://huggingface.co/settings/tokens)にアクセスしてNew tokenからName=適当, Role=writeでAccess Tokenを取得\n",
77
+ "\n",
78
+ "2. 取得したTokenをコピー & 実行後に出現する以下の欄に貼り付け & Login\n",
79
+ "</details>"
80
+ ],
81
+ "metadata": {
82
+ "id": "MHmy8IVla-IL"
83
+ }
84
+ },
85
  {
86
  "cell_type": "code",
87
  "source": [
88
+ "!pip install huggingface_hub\n",
89
+ "from huggingface_hub import login\n",
90
+ "login()"
91
  ],
92
  "metadata": {
93
+ "id": "gt40d8zUZnQV"
94
  },
95
  "execution_count": null,
96
  "outputs": []
 
127
  " print(f\"\\\"{model}\\\"はURLではなく、正しい形式のファイルでもありません\")"
128
  ],
129
  "metadata": {
130
+ "id": "4vd3A09AxJE0",
131
+ "cellView": "form"
132
  },
133
  "execution_count": null,
134
  "outputs": []
 
145
  "import safetensors.torch\n",
146
  "from functools import partial\n",
147
  "\n",
148
+ "from sys import modules\n",
149
+ "if \"huggingface_hub\" in modules:\n",
150
+ " from huggingface_hub import HfApi, Repository\n",
151
+ "\n",
152
  "models = \"wd-1-4-anime_e1.ckpt, wd-1-4-anime_e1.yaml\" #@param {type:\"string\"}\n",
153
  "as_fp16 = True #@param {type:\"boolean\"}\n",
154
  "save_directly_to_Google_Drive = True #@param {type:\"boolean\"}\n",
155
  "save_type = \".safetensors\" #@param [\".safetensors\", \".ckpt\"]\n",
156
+ "#@markdown 変換したモデルをHugging Faceに投稿する場合は「yourname/yourrepo」の形式で投稿先リポジトリを指定<br>\n",
157
+ "#@markdown 投稿しない場合は何も入力しない<br>\n",
158
+ "#@markdown 5GB以上のファイルを投稿する場合は、投稿先リポジトリを丸ごとダウンロードする工程が挟まるので、時間がかかる場合があります\n",
159
+ "repo_id = \"\" #@param {type:\"string\"}\n",
160
+ "\n",
161
+ "def upload_to_hugging_face(file_name):\n",
162
+ " api = HfApi()\n",
163
+ " api.upload_file(path_or_fileobj=file_name,\n",
164
+ " path_in_repo=file_name,\n",
165
+ " repo_id=repo_id,\n",
166
+ " )\n",
167
  "\n",
168
  "def convert_yaml(file_name):\n",
169
  " with open(file_name) as f:\n",
 
177
  " file_name = os.path.splitext(file_name)[0] + \"-fp16.yaml\"\n",
178
  " with open(file_name, mode=\"w\") as f:\n",
179
  " f.write(yaml)\n",
180
+ " if repo_id != \"\":\n",
181
+ " upload_to_hugging_face(file_name)\n",
182
  " os.chdir(\"/content\")\n",
183
  "\n",
184
  "if models == \"\":\n",
 
206
  " weights[key] = weights[key].half()\n",
207
  " if save_directly_to_Google_Drive:\n",
208
  " os.chdir(\"/content/drive/MyDrive\")\n",
209
+ " save_model(weights, saved_model := model_name + save_type)\n",
210
+ " if repo_id != \"\":\n",
211
+ " if os.path.getsize(saved_model) >= 5*1000*1000*1000:\n",
212
+ " with Repository(os.path.basename(repo_id), clone_from=repo_id, token=True).commit(commit_message=f\"Upload {saved_model} with huggingface_hub\"):\n",
213
+ " save_model(weights, saved_model)\n",
214
+ " else:\n",
215
+ " upload_to_hugging_face(saved_model)\n",
216
  " os.chdir(\"/content\")\n",
217
  " del weights\n",
218
  "\n",
 
250
  {
251
  "cell_type": "markdown",
252
  "source": [
253
+ "[モデルのリンク集](https://huggingface.co/models?other=stable-diffusion)等から好きなモデルを選ぼう"
254
  ],
255
  "metadata": {
256
  "id": "yaLq5Nqe6an6"