summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.idea/misc.xml2
-rw-r--r--concurr/basics/tutorials.py48
-rw-r--r--diffusion/tutorials/01_tut.ipynb308
-rw-r--r--diffusion/tutorials/01_tut.py17
-rw-r--r--ocr/image_demo.py12
5 files changed, 386 insertions, 1 deletions
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 7a5c067..676412f 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
- <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" />
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (py3.7)" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
diff --git a/concurr/basics/tutorials.py b/concurr/basics/tutorials.py
new file mode 100644
index 0000000..07fb056
--- /dev/null
+++ b/concurr/basics/tutorials.py
@@ -0,0 +1,48 @@
+
+import asyncio
+import time
+
+
+# define a coroutine
+# async def main():
+# print('hello coroutine!')
+#
+#
+# # pause the execution of a coroutine
+# async def main2():
+# await main()
+# print('continue execution')
+
+
+async def display_time():
+ start_time = time.time()
+ while True:
+ dur = int(time.time() - start_time)
+ if dur % 3 == 0:
+ print('{} seconds have passed.'.format(dur))
+ await asyncio.sleep(1)
+
+
+async def print_num():
+ num = 1
+ while True:
+ print(num)
+ num += 1
+ await asyncio.sleep(0.1)
+
+
+async def main():
+
+ task1 = asyncio.ensure_future(display_time())
+ task2 = asyncio.ensure_future(print_num())
+
+ await asyncio.gather(task1, task2)
+
+if __name__ == '__main__':
+
+ # asyncio.run(main())
+ ev_loop = asyncio.get_event_loop()
+
+ ev_loop.run_until_complete(main())
+
+ ev_loop.close()
diff --git a/diffusion/tutorials/01_tut.ipynb b/diffusion/tutorials/01_tut.ipynb
new file mode 100644
index 0000000..bcb9077
--- /dev/null
+++ b/diffusion/tutorials/01_tut.ipynb
@@ -0,0 +1,308 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "3306ed06",
+ "metadata": {},
+ "source": [
+ "## 1. 注册及token"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "c132f4d2",
+ "metadata": {},
+ "source": [
+ "- 注册:\n",
+ " - https://huggingface.co\n",
+ "- token\n",
+ " - https://huggingface.co/settings/tokens\n",
+ "\n",
+ "- 安装 huggingface 命令行工具:\n",
+ " - `$ python -m pip install huggingface_hub`\n",
+ " - `$ huggingface-cli login`\n",
+ "\n",
+ "- huggingface 获取 CompVis 相关模型的访问权限\n",
+ " - https://huggingface.co/CompVis/stable-diffusion-v1-4"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "99e0b5e9",
+ "metadata": {},
+ "source": [
+ "## 2. github 项目"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "f8ee18a3",
+ "metadata": {},
+ "source": [
+ "- git clone 到本地\n",
+ " - `git clone https://github.com/CompVis/stable-diffusion`\n",
+ "- 配置虚拟环境 `ldm`(latent diffusion model)\n",
+ " \n",
+ "```\n",
+ "conda env create -f environment.yaml\n",
+ "conda activate ldm\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "77e4ea71",
+ "metadata": {},
+ "source": [
+ "## 3. 生成第一个图片"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "0bca3d05",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "/home/chzhang/anaconda3/envs/ldm/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
+ " from .autonotebook import tqdm as notebook_tqdm\n"
+ ]
+ }
+ ],
+ "source": [
+ "from torch import autocast"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "83d22a58",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from diffusers import StableDiffusionPipeline"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "eac64ab0",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Fetching 16 files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 16/16 [00:00<00:00, 29127.11it/s]\n",
+ "The config attributes {'clip_sample': False} were passed to PNDMScheduler, but are not expected and will be ignored. Please verify your scheduler_config.json configuration file.\n"
+ ]
+ }
+ ],
+ "source": [
+ "pipe = StableDiffusionPipeline.from_pretrained(\n",
+ "\t\"CompVis/stable-diffusion-v1-4\",\n",
+ "# revision='fp32',\n",
+ "# revision='fp16'\n",
+ "\tuse_auth_token=True\n",
+ ").to(\"cuda\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "55d7a9ec",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "StableDiffusionPipeline {\n",
+ " \"_class_name\": \"StableDiffusionPipeline\",\n",
+ " \"_diffusers_version\": \"0.6.0\",\n",
+ " \"feature_extractor\": [\n",
+ " \"transformers\",\n",
+ " \"CLIPFeatureExtractor\"\n",
+ " ],\n",
+ " \"safety_checker\": [\n",
+ " \"stable_diffusion\",\n",
+ " \"StableDiffusionSafetyChecker\"\n",
+ " ],\n",
+ " \"scheduler\": [\n",
+ " \"diffusers\",\n",
+ " \"PNDMScheduler\"\n",
+ " ],\n",
+ " \"text_encoder\": [\n",
+ " \"transformers\",\n",
+ " \"CLIPTextModel\"\n",
+ " ],\n",
+ " \"tokenizer\": [\n",
+ " \"transformers\",\n",
+ " \"CLIPTokenizer\"\n",
+ " ],\n",
+ " \"unet\": [\n",
+ " \"diffusers\",\n",
+ " \"UNet2DConditionModel\"\n",
+ " ],\n",
+ " \"vae\": [\n",
+ " \"diffusers\",\n",
+ " \"AutoencoderKL\"\n",
+ " ]\n",
+ "}"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "pipe"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "id": "4f07fd46",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "prompt = 'a photo of a chinese woman riding a horse on mars'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "id": "8a30d00a",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 51/51 [00:12<00:00, 4.19it/s]\n"
+ ]
+ }
+ ],
+ "source": [
+ "with autocast('cuda'):\n",
+ " output = pipe(prompt)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "id": "547d0e63",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "StableDiffusionPipelineOutput(images=[<PIL.Image.Image image mode=RGB size=512x512 at 0x7F5E81449CD0>], nsfw_content_detected=[False])"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "output"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "679a12f1",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[<PIL.Image.Image image mode=RGB size=512x512>]"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "output['images']"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "668beeef",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from PIL import Image"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "id": "ca824027",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "output['images'][0].show()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "01f4eee8",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "NameError",
+ "evalue": "name 'output' is not defined",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
+ "Cell \u001b[0;32mIn [1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43moutput\u001b[49m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimages\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39msave(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m./output/1.png\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
+ "\u001b[0;31mNameError\u001b[0m: name 'output' is not defined"
+ ]
+ }
+ ],
+ "source": [
+ "output['images'][0].save('./output/1.png')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "479e3705",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "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.8.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/diffusion/tutorials/01_tut.py b/diffusion/tutorials/01_tut.py
new file mode 100644
index 0000000..beb6819
--- /dev/null
+++ b/diffusion/tutorials/01_tut.py
@@ -0,0 +1,17 @@
+
+from torch import autocast
+from diffusers import StableDiffusionPipeline
+from PIL import Image
+prompt = 'a photo of an astronaut riding a horse on mars'
+
+pipeline = StableDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4',
+ use_auth_token=True,
+ revision='fp16'
+ ).to('cuda')
+with autocast('cuda'):
+ output = pipeline(prompt)
+
+print(output)
+img = output['images'][0]
+img.show()
+img.save('./output/fp16.png')
diff --git a/ocr/image_demo.py b/ocr/image_demo.py
new file mode 100644
index 0000000..a9d9a6a
--- /dev/null
+++ b/ocr/image_demo.py
@@ -0,0 +1,12 @@
+import pytesseract
+from PIL import Image
+
+img = Image.open('./data/2.jpg')
+
+
+# text = pytesseract.image_to_string(img, lang='eng')
+text = pytesseract.image_to_string(img, lang='chi_sim')
+
+
+print(text)
+