File size: 7,775 Bytes
17adc5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "Run both of the following two codes"
      ],
      "metadata": {
        "id": "OnuCk_wNLM_D"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import drive \n",
        "drive.mount(\"/content/drive\")"
      ],
      "metadata": {
        "id": "liEiK8Iioscq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install torch safetensors\n",
        "!pip install wget"
      ],
      "metadata": {
        "id": "pXr7oNJzwwgU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Replace the following links, etc. with the desired ones and then run the following code"
      ],
      "metadata": {
        "id": "7Ils-K70k15Y"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title <font size=\"-0\">Download Models</font>\n",
        "#@markdown Please specify the model name or download link for Google Drive, separated by commas\n",
        "#@markdown - If it is the model name on Google Drive, specify it as a relative path to My Drive\n",
        "#@markdown - If it is a download link, copy the link address by right-clicking and paste it in place of the link below\n",
        "\n",
        "import shutil\n",
        "import urllib.parse\n",
        "import urllib.request\n",
        "import wget\n",
        "\n",
        "models = \"Please use your own model in place of this example, example.safetensors, https://huggingface.co./stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.ckpt\" #@param {type:\"string\"}\n",
        "models = [m.strip() for m in models.split(\",\") if not models == \"\"]\n",
        "for model in models:\n",
        "  if 0 < len(urllib.parse.urlparse(model).scheme): # if model is url\n",
        "    wget.download(model)\n",
        "    # once the bug on python 3.8 is fixed, replace the above code with the following code\n",
        "    ## model_data = urllib.request.urlopen(model).read()\n",
        "    ## with open(os.path.basename(model), mode=\"wb\") as f:\n",
        "    ##   f.write(model_data)\n",
        "  elif model.endswith((\".ckpt\", \".safetensors\", \".pt\", \".pth\")):\n",
        "    from_ = \"/content/drive/MyDrive/\" + model\n",
        "    to_ = \"/content/\" + model\n",
        "    shutil.copy(from_, to_)\n",
        "  else:\n",
        "    print(f\"\\\"{model}\\\"はURLではなく、正しい形式のファイルでもありません\")"
      ],
      "metadata": {
        "cellView": "form",
        "id": "4vd3A09AxJE0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "if you use a relatively newer model such as SD2.1, run the following code"
      ],
      "metadata": {
        "id": "m1mHzOMjcDhz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install pytorch-lightning"
      ],
      "metadata": {
        "id": "TkrmByc0aYVN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Run either of the following two codes. If you run out of memory and crash, use a smaller model or a paid high-memory runtime"
      ],
      "metadata": {
        "id": "0SUK6Alv2ItS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title <font size=\"-0\">If you specify the name of the model you want to convert and convert it manually</font>\n",
        "import os\n",
        "import torch\n",
        "import safetensors.torch\n",
        "\n",
        "model = \"v2-1_768-ema-pruned.ckpt\" #@param {type:\"string\"}\n",
        "model_name, model_ext = os.path.splitext(model)\n",
        "as_fp16 = True #@param {type:\"boolean\"}\n",
        "save_directly_to_Google_Drive = True #@param {type:\"boolean\"}\n",
        "\n",
        "with torch.no_grad():\n",
        "  if model_ext == \".safetensors\":\n",
        "    weights = safetensors.torch.load_file(model_name + model_ext, device=\"cpu\")\n",
        "  elif model_ext == \".ckpt\":\n",
        "    weights = torch.load(model_name + model_ext, map_location=torch.device('cpu'))[\"state_dict\"]\n",
        "  else:\n",
        "    raise Exception(\"対応形式は.ckptと.safetensorsです\\n\" + f\"\\\"{model}\\\"は対応形式ではありません\")\n",
        "  if as_fp16:\n",
        "    model_name = model_name + \"-fp16\"\n",
        "    for key in weights.keys():\n",
        "      weights[key] = weights[key].half()\n",
        "  if save_directly_to_Google_Drive:\n",
        "    os.chdir(\"/content/drive/MyDrive\")\n",
        "    safetensors.torch.save_file(weights, model_name + \".safetensors\")\n",
        "    os.chdir(\"/content\")\n",
        "  else:\n",
        "    safetensors.torch.save_file(weights, model_name + \".safetensors\")\n",
        "  del weights\n",
        "\n",
        "!reset"
      ],
      "metadata": {
        "id": "9OmSG98HxJg2",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title <font size=\"-0\">If you automatically convert all pre-loaded models</font>\n",
        "import os\n",
        "import glob\n",
        "import torch\n",
        "import safetensors.torch\n",
        "\n",
        "as_fp16 = True #@param {type:\"boolean\"}\n",
        "save_directly_to_Google_Drive = True #@param {type:\"boolean\"}\n",
        "\n",
        "with torch.no_grad():\n",
        "  model_paths = glob.glob(r\"/content/*.ckpt\") + glob.glob(r\"/content/*.safetensors\") + glob.glob(r\"/content/*.pt\") + glob.glob(r\"/content/*.pth\")\n",
        "  for model_path in model_paths:\n",
        "    model_name, model_ext = os.path.splitext(os.path.basename(model_path))\n",
        "    if model_ext == \".safetensors\":\n",
        "      weights = safetensors.torch.load_file(model_name + model_ext, device=\"cpu\")\n",
        "    elif model_ext == \".ckpt\":\n",
        "      weights = torch.load(model_name + model_ext, map_location=torch.device('cpu'))[\"state_dict\"]\n",
        "    else:\n",
        "      print(\"対応形式は.ckpt\tと.safetensorsです\\n\" + f\"\\\"{model}\\\"は対応形式ではありません\")\n",
        "      break\n",
        "    if as_fp16:\n",
        "      model_name = model_name + \"-fp16\"\n",
        "      for key in weights.keys():\n",
        "        weights[key] = weights[key].half()\n",
        "    if save_directly_to_Google_Drive:\n",
        "      os.chdir(\"/content/drive/MyDrive\")\n",
        "      safetensors.torch.save_file(weights, model_name + \".safetensors\")\n",
        "      os.chdir(\"/content\")\n",
        "    else:\n",
        "      safetensors.torch.save_file(weights, model_name + \".safetensors\")\n",
        "    del weights\n",
        "\n",
        "!reset"
      ],
      "metadata": {
        "id": "5TUvrW5VzLst",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Choose your favorite model from https://huggingface.co./models?other=stable-diffusion or other model link collections"
      ],
      "metadata": {
        "id": "yaLq5Nqe6an6"
      }
    }
  ]
}