diff --git a/diffusers_in_Colab.ipynb b/diffusers_in_Colab.ipynb index ec634f8..09941d5 100644 --- a/diffusers_in_Colab.ipynb +++ b/diffusers_in_Colab.ipynb @@ -10,25 +10,66 @@ "\"Open" ] }, + { + "cell_type": "markdown", + "source": [ + "#README" + ], + "metadata": { + "id": "_QODtoEhqApV" + } + }, + { + "cell_type": "markdown", + "source": [ + ">Disclaimer:\n", + "\n", + "* This project was created as a hobby and is not intended for commercial use.\n", + "\n", + "* I am not responsible for any problems that may arise from the use of this project.\n", + "\n", + "* Please check the [License](https://github.com/suzukimain/diffusers_in_Colab/blob/main/LICENSE) for more information.\n", + "\n", + "\\\n", + "\n", + "---\n", + "\n", + ">Project Objectives\n", + "\n", + "1. Personal learning of programming.\n", + "\n", + "2. An image generation notebook based on diffusers.\n", + "\n", + "\\\n", + "\n", + "---\n", + "\n", + ">Acknowledgment\n", + "\n", + "I used open source resources and free tools in the creation of this project. These resources and tools made this possible.\\\n", + "I would like to take this opportunity to thank the open source community and those who provided free tools.\n", + "\n" + ], + "metadata": { + "id": "irVVq_Sup6N6" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Setup" + ], + "metadata": { + "id": "vvNKs0U4qFCv" + } + }, { "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "eicnuls7qg7l", - "outputId": "3f7a4657-2bcb-4555-b600-86f411582195" + "id": "eicnuls7qg7l" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Mounted at /content/drive\n" - ] - } - ], + "outputs": [], "source": [ "#@title (option) Mount GoogleDrive { run: \"auto\", display-mode: \"form\"}\n", "Conect_GoogleDrive = True # @param {type:\"boolean\"}\n", @@ -55,72 +96,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "background_save": true, - "base_uri": "https://localhost:8080/", - "height": 81, - "referenced_widgets": [ - "301a6219daf04342a4119b07df94ee85", - "e468509df2a5401b8f49299c30fb0ce3" - ] - }, - "id": "C4UJqC_WpaGw", - "outputId": "ba663030-9596-43eb-f077-61689f53b379" - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "301a6219daf04342a4119b07df94ee85", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "cuda runtime setting: 0%| | 0/8 [00:00 bool:\n", - " response = requests.head(url)\n", - " try:\n", - " response.raise_for_status()\n", - " except requests.RequestException:\n", - " return False\n", - " else:\n", - " return True\n", - " finally:\n", - " logger.debug(f\"response.status_code: {response.status_code}\")\n", + "class runtime_func:\n", + " def __init__(self):\n", + " \"\"\"\n", + " NOTE:\n", + " Functions that should be included in the `basic_config` class are needed in the `install_packages` class, so these functions are separated into the `runtime_func` class.\n", + " This is because the `install_packages` class is executed before the `basic_config` class is executed, so these functions are separated and inherited later.\n", + " \"\"\"\n", + " self.device_type = self.device_type_check()\n", + "\n", + "\n", + " @staticmethod\n", + " def module_version(module_name):\n", + " try:\n", + " version = importlib.metadata.version(module_name)\n", + " return re.match(r\"^\\d+\\.\\d+\\.\\d+\", version).group(0)\n", + " except importlib.metadata.PackageNotFoundError:\n", + " return None\n", + "\n", + "\n", + " @staticmethod\n", + " def device_type_check():\n", + " _device_type = jax.devices()[0].device_kind\n", + " if \"TPU\" in _device_type:\n", + " return \"TPU\"\n", + " elif \"cpu\" in _device_type:\n", + " return \"cpu\"\n", + " else:\n", + " return \"cuda\"\n", + "\n", + "\n", + " @staticmethod\n", + " def is_url_valid(url) -> bool:\n", + " response = requests.head(url)\n", + " try:\n", + " response.raise_for_status()\n", + " except requests.RequestException:\n", + " return False\n", + " else:\n", + " return True\n", + " finally:\n", + " logger.debug(f\"response.status_code: {response.status_code}\")\n", + "\n", + "\n", + " @staticmethod\n", + " def custom_logger(DEBUG=False):\n", + " format = '%(levelname)s: : %(message)s'\n", + " logger = logging.getLogger(__name__)\n", + " logger.propagate = False\n", + " if not logger.handlers:\n", + " formatter = logging.Formatter(format)\n", + " handler = logging.StreamHandler()\n", + " handler.setFormatter(formatter)\n", + " logger.addHandler(handler)\n", + " if DEBUG:\n", + " logger.setLevel(logging.DEBUG)\n", + " else:\n", + " logger.setLevel(logging.WARNING)\n", + " return logger\n", + "\n", + "\n", + "run_func = runtime_func()\n", + "device_type = run_func.device_type\n", + "logger = run_func.custom_logger(DEBUG)\n", + "\n", + "\n", "\n", "class ProcessBarRun:\n", " \"\"\"\n", @@ -198,7 +245,6 @@ " pofix_dot:bool = False\n", "\n", " If the download_bar is true, the URL and save_path are required\n", - "\n", " \"\"\"\n", "\n", " def __init__(self,\n", @@ -223,7 +269,6 @@ " self.fin_pofix = fin_pofix\n", " self.desc_dot = desc_dot\n", " self.pofix_dot = pofix_dot\n", - "\n", " self.ex_word = ex_word\n", " self.queue_obj = queue.Queue()\n", " self._count = 0\n", @@ -237,16 +282,12 @@ " self.stop_event = threading.Event()\n", " self.stop_dot = threading.Event()\n", " self.tqdm_lock = threading.Lock()\n", - "\n", " self.tqdm_obj = tqdm(total=total, desc=desc, postfix=pofix)\n", - "\n", - "\n", " self.arg_update(desc=desc, postfix=pofix, **ex_word)\n", " self.dot_thread = threading.Thread(target=self.prosess_dot)\n", " self.dot_thread.start()\n", "\n", "\n", - "\n", " def __del__(self):\n", " self.stop()\n", "\n", @@ -287,7 +328,6 @@ " self.tqdm_obj.write(chunk)\n", "\n", "\n", - "\n", " def prosess_dot(self,):\n", " \"\"\"\n", " NOTE:\n", @@ -342,27 +382,18 @@ " self.tqdm_obj.set_description_str(self.desc)\n", " self.tqdm_obj.set_postfix_str(self.pofix)\n", " self.tqdm_obj.refresh()\n", - "\n", " if exit:\n", " self.stop()\n", "\n", "\n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", "class config_check:\n", " base_config_json = \"/tmp/diffusers_in_colab_config.json\"\n", "\n", " def __init__(self):\n", " pass\n", "\n", + "\n", " def get_json_dict(self):\n", " \"\"\"Retrieve the JSON dictionary from the config file.\"\"\"\n", " config_dict = {}\n", @@ -374,6 +405,7 @@ " pass\n", " return config_dict\n", "\n", + "\n", " def update_json_dict(self, key, value):\n", " \"\"\"Update the JSON dictionary with a new key-value pair.\"\"\"\n", " basic_json_dict = self.get_json_dict()\n", @@ -381,6 +413,7 @@ " with open(self.base_config_json, \"w\") as json_file:\n", " json.dump(basic_json_dict, json_file, indent=4)\n", "\n", + "\n", " def check_func_hist(self,*return_key, **kwargs):\n", " \"\"\"\n", " Check and optionally update the history of a given element.\n", @@ -417,10 +450,8 @@ " else:\n", " raise TypeError(\"Missing 'key' argument.\")\n", "\n", - "\n", " basic_json_dict = self.get_json_dict()\n", " hist_value = basic_json_dict.get(key)\n", - "\n", " if hist_value == value:\n", " value_match = True\n", " else:\n", @@ -436,50 +467,26 @@ "\n", "\n", "\n", - "class runtime_func:\n", - " def __init__(self):\n", - " self.device_type = self.device_type_check()\n", - "\n", - " @staticmethod\n", - " def module_version(module_name):\n", - " try:\n", - " version = importlib.metadata.version(module_name)\n", - " return re.match(r\"^\\d+\\.\\d+\\.\\d+\", version).group(0)\n", - " except importlib.metadata.PackageNotFoundError:\n", - " return None\n", - "\n", - " @staticmethod\n", - " def device_type_check():\n", - " _device_type = jax.devices()[0].device_kind\n", - " if \"TPU\" in _device_type:\n", - " return \"TPU\"\n", - " elif \"cpu\" in _device_type:\n", - " return \"cpu\"\n", - " else:\n", - " return \"cuda\"\n", - "\n", - "\n", - "\n", - "device_type = runtime_func().device_type\n", - "\n", - "\n", - "\n", "class install_packages(runtime_func):\n", - " repo_url = 'https://github.com/huggingface/diffusers.git'\n", - " repo_dir = 'diffusers'\n", + " repo_url = \"https://github.com/huggingface/diffusers.git\"\n", + " repo_dir = \"diffusers\"\n", "\n", " def __init__(self):\n", " super().__init__()\n", "\n", + "\n", " def torch_install_packages(self):\n", " __sub_process_list =[\n", - " (f'git clone {self.repo_url}', 'Cloning diffusers'),\n", + " (f'if [ ! -d {self.repo_dir} ]; then git clone {self.repo_url} {self.repo_dir}; fi', 'Cloning diffusers'),\n", " (f'cd {self.repo_dir} && pip install .[torch]', 'Installing diffusers'),\n", " ]\n", - " #Without +1, the bar stops in the middle.\n", + " if self.device_type == \"cuda\":\n", + " cuda_ex_prosess = (f'pip install accelerate', 'Installing accelerate')\n", + " __sub_process_list.append(cuda_ex_prosess)\n", + "\n", " torch_install = ProcessBarRun(\n", " total=len(__sub_process_list),\n", - " desc = \"Installing torch packages\",\n", + " desc = \"Installing packages\",\n", " pofix = \"Install process Start\",\n", " pofix_dot = True,\n", " )\n", @@ -493,18 +500,20 @@ "\n", "\n", " def flax_install_packages(self):\n", + " \"\"\"\n", + " 'natsort' is not installed by default in the TPUv2 runtime\n", + " \"\"\"\n", " __sub_process_list = [\n", - " (f'git clone {self.repo_url}', 'Cloning diffusers'),\n", + " (f'if [ ! -d {self.repo_dir} ]; then git clone {self.repo_url} {self.repo_dir}; fi', 'Cloning diffusers'),\n", " (f'cd {self.repo_dir} && pip install .[flax]', 'Installing diffusers'),\n", " ('pip uninstall -y tensorflow', 'Uninstalling tensorflow'),\n", " ('pip install tensorflow-cpu', 'Installing tensorflow-cpu'),\n", " ('pip install natsort', 'Installing other librarys')\n", " ]\n", "\n", - " #Without +1, the bar stops in the middle.\n", " flax_install = ProcessBarRun(\n", " total=len(__sub_process_list),\n", - " desc = \"Installing flax packages\",\n", + " desc = \"Installing packages\",\n", " pofix = \"Install process Start\",\n", " pofix_dot = True,\n", " )\n", @@ -516,52 +525,29 @@ "\n", " flax_install.bar_update(exit=True)\n", "\n", + "\n", " def package_install(self):\n", - " if os.path.exists(self.repo_dir):\n", - " shutil.rmtree(self.repo_dir)\n", "\n", " if self.device_type == \"TPU\":\n", " self.flax_install_packages()\n", " else:\n", " self.torch_install_packages()\n", "\n", - "#device_type = runtime_func().device_type_check()\n", "\n", "Step1 = ProcessBarRun(\n", - " total=8,\n", + " total=4,\n", " desc = f\"{device_type} runtime setting\",\n", " pofix_dot = True)\n", "\n", - "\n", - "\n", "Step1.bar_update(pofix=\"Installing libraries\")\n", "\n", "install_packages().package_install()\n", "\n", - "if device_type == \"cuda\":\n", - " try:\n", - " import accelerate\n", - " except:\n", - " !pip install -q accelerate\n", - " import accelerate\n", - "\n", - "\n", - "Step1.bar_update(pofix=\"Importing modules\")\n", - "\n", - "import torch\n", + "Step1.bar_update(pofix=\"Importing libraries\")\n", "\n", "\n", "\n", - "from PIL import Image, PngImagePlugin\n", - "from google.colab import drive\n", - "from huggingface_hub import hf_hub_download\n", "from natsort import natsorted\n", - "from requests import HTTPError\n", - "from torch import Generator\n", - "from typing import Any, Dict, List, Literal, Optional, Union\n", - "import yaml\n", - "\n", - "from IPython.display import display, Markdown, HTML\n", "\n", "import diffusers\n", "from diffusers import (\n", @@ -575,75 +561,24 @@ ")\n", "from diffusers import logging as df_logging\n", "\n", - "import transformers\n", - "from transformers import (\n", - " pipeline,\n", - " AutoModelForCausalLM,\n", - " CLIPTokenizer,\n", - " AutoTokenizer,\n", - " FlaxAutoModelForCausalLM\n", - ")\n", - "from transformers import logging as tf_logging\n", "\n", + "# Device-specific imports\n", "if device_type == \"TPU\":\n", - " !pip install -q -U tensorflow-cpu\n", " from flax.jax_utils import replicate\n", " from flax.training.common_utils import shard\n", - "\n", "else:\n", - " Step1.bar_update(update_rate=1, pofix=\"Installing accelerate\")\n", - "\n", - " #!pip install -q -U accelerate\n", - "\n", - " #import accelerate\n", " from diffusers import AutoPipelineForText2Image\n", "\n", - "import inspect\n", - "import ipywidgets as widgets\n", - "from google.colab import output\n", - "from typing import get_origin, get_args, Union\n", - "import typing\n", - "\n", - "\n", - "def custom_logger():\n", - " format = '%(levelname)s: : %(message)s'\n", - " logger = logging.getLogger(__name__)\n", - " logger.propagate = False\n", - " if not logger.handlers:\n", - " formatter = logging.Formatter(format)\n", - " handler = logging.StreamHandler()\n", - " handler.setFormatter(formatter)\n", - " logger.addHandler(handler)\n", - " return logger\n", - "\n", - "logger = custom_logger()\n", - "\n", - "\n", "\n", + "# Configure warnings\n", "if DEBUG:\n", " df_logging.set_verbosity_warning()\n", " tf_logging.set_verbosity_warning()\n", - " logger.setLevel(logging.DEBUG)\n", + " warnings.filterwarnings(\"always\", category=DeprecationWarning)\n", "else:\n", " df_logging.set_verbosity_error()\n", " tf_logging.set_verbosity_error()\n", - " logger.setLevel(logging.WARNING)\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "'''\n", - "about: unexplained crash\n", - "\n", - "If you get an unexplained crash in transfomers or diffusers while using TPUv2, try the following\n", - "\n", - "----do----\n", - "pip uninstall tensorflow\n", - "pip install tensorflow-cpu\n", - "----------\n", - "'''\n", - "\n", + " warnings.filterwarnings(\"ignore\")\n", "\n", "\n", "\n", @@ -754,16 +689,21 @@ "\n", "\n", "\n", - "\n", "class basic_config(data_config,config_check,runtime_func):\n", - "\n", " def __init__(self):\n", - " #super().__init__()\n", " self.device_count = self.count_device()\n", " self.device_type = self.device_type_check()\n", " self.device = self.device_set()\n", - " self.use_TPU = True if self.device_type==\"TPU\" else False\n", - " self.conect_gdrive = True if drive._os.path.ismount(\"/content/drive\") else False\n", + " if self.device_type == \"TPU\":\n", + " self.use_TPU = True\n", + " else:\n", + " self.use_TPU = False\n", + "\n", + " if drive._os.path.ismount(\"/content/drive\"):\n", + " self.conect_gdrive = True\n", + " else:\n", + " self.conect_gdrive = False\n", + "\n", "\n", " @classmethod\n", " def get_inherited_class(cls,class_name) -> list:\n", @@ -773,12 +713,12 @@ "\n", " @staticmethod\n", " def device_set():\n", + " logger.debug(f\"device_type: {device_type}\")\n", " if device_type == \"TPU\":\n", " #import torch_xla.core.xla_model as xm\n", " #device = xm.xla_device()\n", " device = device_type\n", " else:\n", - " logger.debug(f\"device_type: {device_type}\")\n", " device = device_type\n", " return device\n", "\n", @@ -791,8 +731,7 @@ "\n", "\n", " def is_safetensors(self,path):\n", - " path = os.path.basename(path)\n", - " if \"safetensors\" == path.split(\".\")[-1]:\n", + " if \".safetensors\" == os.path.splitext(path)[1]:\n", " return True\n", " else:\n", " return False\n", @@ -824,7 +763,8 @@ "\n", " flax_list = [\"FlaxDiffusionPipeline\",]\n", "\n", - " #assert not isinstance(Target_class, str),f\"Target_class can not use str\"\n", + " if isinstance(Target_class,str):\n", + " Target_class = getattr(diffusers, Target_class)\n", "\n", " cls_method= self.get_inherited_class(Target_class)\n", "\n", @@ -857,9 +797,12 @@ " key_dict[key] = keyword\n", " return key_in\n", "\n", - " def get_call_method(self,\n", - " class_name,\n", - " method_name : str = '__call__') ->list:\n", + "\n", + " def get_call_method(\n", + " self,\n", + " class_name,\n", + " method_name : str = '__call__'\n", + " ) ->list:\n", " \"\"\"\n", " Acquire the arguments of the function specified by 'method_name'\n", " for the class specified by 'class_name'\n", @@ -872,17 +815,29 @@ " arg_names.append(param.name)\n", " return arg_names\n", "\n", - " def get_class_elements(self,search):\n", + "\n", + " def get_class_elements(\n", + " self,\n", + " search\n", + " ):\n", " return list(search.__class__.__annotations__.keys())\n", "\n", - " def check_for_safetensors(self,path):\n", + "\n", + " def check_for_safetensors(\n", + " self,\n", + " path\n", + " ):\n", " _ext = os.path.basename(path).split(\".\")[-1]\n", " if _ext == \"safetensors\":\n", " return True\n", " else:\n", " return False\n", "\n", - " def pipe_class_type(self,class_name):\n", + "\n", + " def pipe_class_type(\n", + " self,\n", + " class_name\n", + " ):\n", " \"\"\"\n", " Args:\n", " class_name : class\n", @@ -890,7 +845,7 @@ " Returns:\n", " Literal['txt2img','img2img','txt2video']\n", " \"\"\"\n", - " _txt2img_method_list = []#else\n", + " _txt2img_method_list = [] #else\n", " _img2img_method_list = [\"image\"]\n", " _img2video_method_list = [\"video_length\",\"fps\"]\n", "\n", @@ -905,9 +860,11 @@ " return pipeline_type\n", "\n", "\n", - " def import_on_str(self,\n", - " desired_function_or_class,\n", - " module_name = \"\"):\n", + " def import_on_str(\n", + " self,\n", + " desired_function_or_class,\n", + " module_name = \"\"\n", + " ):\n", " if not module_name:\n", " import_object = __import__(desired_function_or_class)\n", " else:\n", @@ -915,11 +872,19 @@ " return import_object\n", "\n", "\n", - " def max_temper(self,search_word, search_list):\n", + " def max_temper(\n", + " self,\n", + " search_word,\n", + " search_list\n", + " ):\n", " return difflib.get_close_matches(search_word, search_list,cutoff=0, n=1)\n", "\n", "\n", - " def sort_list_obj(self,list_obj,need_txt):\n", + " def sort_list_obj(\n", + " self,\n", + " list_obj,\n", + " need_txt\n", + " ):\n", " sorted_list=[]\n", " for module_obj in list_obj:\n", " if need_txt.lower() in module_obj.lower():\n", @@ -936,6 +901,9 @@ " is_upscale = False\n", " ):\n", " \"\"\"\n", + " NOTE:\n", + " Return SD if you do not know, because an error may occur if None is used.\n", + "\n", " About model_type:\n", " The model_type itself will be left for possible use at some point.\n", " \"\"\"\n", @@ -961,16 +929,16 @@ " else:\n", " return \"SD\"\n", "\n", - "\n", - "\n", " elif isinstance(checkpoint_path_or_dict, dict):\n", " checkpoint = checkpoint_path_or_dict\n", " else:\n", " raise TypeError(f\"checkpoint_path_or_dict: {checkpoint_path_or_dict}\")\n", + "\n", " if \"global_step\" in checkpoint:\n", " global_step = checkpoint[\"global_step\"]\n", " else:\n", " global_step = None\n", + "\n", " while \"state_dict\" in checkpoint:\n", " checkpoint = checkpoint[\"state_dict\"]\n", " if original_config_file is None:\n", @@ -978,9 +946,7 @@ " key_name_sd_xl_base = \"conditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias\"\n", " key_name_sd_xl_refiner = \"conditioner.embedders.0.model.transformer.resblocks.9.mlp.c_proj.bias\"\n", " #is_upscale = pipeline_class == StableDiffusionUpscalePipeline\n", - "\n", " config_url = None\n", - "\n", " # model_type = \"v1\"\n", " if config_files is not None and \"v1\" in config_files:\n", " original_config_file = config_files[\"v1\"]\n", @@ -996,12 +962,14 @@ " if global_step == 110000:\n", " # v2.1 needs to upcast attention\n", " upcast_attention = True\n", + "\n", " elif key_name_sd_xl_base in checkpoint:\n", " # only base xl has two text embedders\n", " if config_files is not None and \"xl\" in config_files:\n", " original_config_file = config_files[\"xl\"]\n", " else:\n", " config_url = \"https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_base.yaml\"\n", + "\n", " elif key_name_sd_xl_refiner in checkpoint:\n", " # only refiner xl has embedder and one text embedders\n", " if config_files is not None and \"xl_refiner\" in config_files:\n", @@ -1016,8 +984,10 @@ " try:\n", " original_config_file = BytesIO(requests.get(config_url).content)\n", " except:\n", - " logger.warning(f\"Could not download the Config_file to find out the model type from the following URL.: {config_url}\")\n", - " return None\n", + " logger.error(f\"Could not download the Config_file to find out the model type from the following URL: {config_url}\")\n", + " if model_type is None:\n", + " logger.warning(\"model_type is set to None\")\n", + " return model_type\n", " else:\n", " with open(original_config_file, \"r\") as f:\n", " original_config_file = f.read()\n", @@ -1044,23 +1014,20 @@ " return \"SDXL\"\n", "\n", " else:\n", + " if model_type is None:\n", + " logger.warning(\"model_type is set to None\")\n", " return model_type\n", "\n", "\n", - "\n", "'''\n", - "\"\"\"\n", - "init\n", - "\"\"\"\n", - "gl_txt_pipe = None\n", - "strength = 1\n", - "入力する画像 = \"\"\n", - "video_length= 8\n", - "video_fps = 8\n", - "safety_level = \"MAX\"\n", - "momentum = 1\n", - "momentum_hist = -0.1\n", - "history_d = \"rand_init\"\n", + "about: unexplained crash\n", + "\n", + "If you get an unexplained crash in transfomers or diffusers while using TPUv2, try the following\n", + "\n", + "----do----\n", + "pip uninstall tensorflow\n", + "pip install tensorflow-cpu\n", + "----------\n", "'''\n", "\n", "\n", @@ -1076,15 +1043,20 @@ "sys.path.append(\"/content/script\")\n", "\n", "\n", - "Step1.bar_update(exit=True,pofix=\"Finish!\")\n", + "Step1.bar_update(exit=True, pofix=\"Finish!\")\n", "\n", "print(\"\\n\\033[34m___________________________________________\\n\")\n", "print(f\"Devie: {runtime_func().device_type_check()}\\n\")\n", "print(f\"Googledrive: {Connect_Gdrive}\")\n", "print(\"\\n\\033[32mSetup completed successfully\\033[0m\")\n", "\n", - "step1_finish =True" - ] + "step1_finish =Trueself.self.is_url_valid" + ], + "metadata": { + "id": "lRJ148nGucXo" + }, + "execution_count": null, + "outputs": [] }, { "cell_type": "code", @@ -1146,7 +1118,7 @@ "\n", "#@markdown >Config\n", "\n", - "auto = True # @param {type:\"boolean\"}\n", + "auto = False # @param {type:\"boolean\"}\n", "# @markdown Automatically select repository & model files (recommended: ON)\n", "\n", "\n", @@ -1173,7 +1145,6 @@ " #self.model_select = \"\"\n", "\n", "\n", - "\n", " def repo_name_or_path(self,model_name_or_path):\n", " pattern = r\"([^/]+)/([^/]+)/(?:blob/main/)?(.+)\"\n", " weights_name = None\n", @@ -1187,6 +1158,7 @@ " weights_name = match.group(3)\n", " return repo_id, weights_name\n", "\n", + "\n", " def run_hf_download(self,url_or_path):\n", " \"\"\"\n", " retrun:\n", @@ -1197,7 +1169,7 @@ " return model_path\n", "\n", " if any(url_or_path.startswith(checked) for checked in self.VALID_URL_PREFIXES):\n", - " if not is_url_valid(url_or_path):\n", + " if not self.self.is_url_valid(url_or_path):\n", " raise HTTPError(\"Invalid URL\")\n", " hf_path, file_name =self.repo_name_or_path(url_or_path)\n", " logger.debug(f\"url_or_path:{url_or_path}\")\n", @@ -1212,7 +1184,6 @@ " else:\n", " raise TypeError(\"Invalid path_or_url\")\n", "\n", - "\n", " #from hf_repo\n", " elif self.diffusers_model_check(url_or_path):\n", " logger.debug(f\"url_or_path: {url_or_path}\")\n", @@ -1223,7 +1194,6 @@ " return model_file_path\n", "\n", "\n", - "\n", " def model_safe_check(self,model_list) ->str:\n", " if len(model_list)>1:\n", " for check_model in model_list:\n", @@ -1232,6 +1202,7 @@ " return check_model\n", " return model_list[0]\n", "\n", + "\n", " def list_safe_check(self,model_list) -> list:\n", " for check_model in model_list:\n", " if bool(re.search(r\"(?i)[-ー__]sfw\", check_model)):\n", @@ -1241,14 +1212,14 @@ " return model_list\n", "\n", "\n", - "\n", - "\n", " def diffusers_model_check(self,checked_model: str) -> bool:\n", " index_url=f\"https://huggingface.co/{checked_model}/blob/main/model_index.json\"\n", - " return is_url_valid(index_url)\n", + " return self.is_url_valid(index_url)\n", + "\n", "\n", " def hf_model_check(self,path) -> bool:\n", - " return is_url_valid(f\"https://huggingface.co/{path}\")\n", + " return self.is_url_valid(f\"https://huggingface.co/{path}\")\n", + "\n", "\n", " def data_get(self,path) -> list:\n", " url = f\"https://huggingface.co/api/models/{path}\"\n", @@ -1639,7 +1610,7 @@ "\n", "\n", " def download_model(self, url, save_path):\n", - " if not is_url_valid(url):\n", + " if not self.is_url_valid(url):\n", " raise requests.HTTPError(\"URL is invalid.\")\n", "\n", " response = requests.get(url, stream=True)\n", @@ -2059,7 +2030,7 @@ " file_path = \"\"\n", " if model_select in self.model_dict:\n", " model_path_to_check = self.model_dict[model_select]\n", - " if is_url_valid(f\"https://huggingface.co/{model_path_to_check}\"):\n", + " if self.is_url_valid(f\"https://huggingface.co/{model_path_to_check}\"):\n", " model_select = model_path_to_check\n", "\n", " if model_select == \"search\":\n", @@ -2069,7 +2040,7 @@ " return_dict[\"url_or_path\"] = model_path\n", "\n", " elif model_select.startswith(\"https://huggingface.co/\"):\n", - " if not is_url_valid(model_select):\n", + " if not self.is_url_valid(model_select):\n", " raise ValueError(self.Error_M1)\n", " else:\n", " if download:\n", @@ -2280,7 +2251,7 @@ "\n", "#@markdown >Scheduler set\n", "\n", - "Scheduler_select = \"DDPM\" # @param [\"DPM\", \"DDPM\", \"DDIM\", \"DEISM\", \"DPM_S\", \"EulerA\", \"Euler\", \"HeunD\", \"K_DPM2D\", \"K_DPM2AD\", \"LMSD\", \"PNDM\", \"UniPCM\", \"EulerA_with_sonar\", \"Euler_with_sonar\"] {allow-input: true}\n", + "Scheduler_select = \"DDIM\" # @param [\"DPM\", \"DDPM\", \"DDIM\", \"DEISM\", \"DPM_S\", \"EulerA\", \"Euler\", \"HeunD\", \"K_DPM2D\", \"K_DPM2AD\", \"LMSD\", \"PNDM\", \"UniPCM\", \"EulerA_with_sonar\", \"Euler_with_sonar\"] {allow-input: true}\n", "if not Scheduler_select:\n", " raise TypeError(\"Scheduler_select has not been entered\")\n", "\n", @@ -2840,11 +2811,6 @@ "\n", "unload_lora = False # @param {type:\"boolean\"}\n", "\n", - "try:\n", - " import peft\n", - "except ImportError:\n", - " !pip install -q peft\n", - " import peft\n", "\n", "if not unload_lora:\n", " LORA_dict = pipe_set.model_set(\n", @@ -2877,7 +2843,7 @@ "source": [ "#@title (option) load textual inversion {display-mode: \"form\"}\n", "\n", - "Repo_id_or_path = \"\" # @param {type:\"string\"}\n", + "model_path = \"\" # @param {type:\"string\"}\n", "\n", "#weight_name= \"\" # @param {type:\"string\"}\n", "\n", @@ -2935,6 +2901,7 @@ " print(f\"CustomToken: {','.join(token_list)}\\n\")\n", " return main_pipe\n", "\n", + "\n", " def unload_textual(\n", " self,\n", " token,\n", @@ -2955,7 +2922,7 @@ "textual_cls = load_textual_inversion_cls()\n", "if not unload_textual_inversion:\n", " main_pipe = textual_cls.load_textual(\n", - " pretrained_path=Repo_id_or_path,\n", + " pretrained_path=model_path,\n", " token=token,\n", " auto=auto,\n", " main_pipe=main_pipe)\n", @@ -2974,16 +2941,13 @@ "id": "5qIH-0uIGiw7" }, "source": [ - "#main step" + "#Execution step" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "background_save": true - }, "id": "xXSW3nl8UT7x" }, "outputs": [], @@ -3001,7 +2965,7 @@ "\n", "seed = -1 # @param {type:\"number\"}\n", "\n", - "num_imgs = 4 # @param {type:\"integer\"}\n", + "num_imgs = 1 # @param {type:\"integer\"}\n", "\n", "input_image_path_or_dir = \"\" # @param {type:\"string\"}\n", "\n", @@ -3878,7 +3842,7 @@ "id": "iIEW0jvHK7I1" }, "source": [ - "#サブ機能" + "#Sub-function" ] }, { @@ -3889,7 +3853,7 @@ }, "outputs": [], "source": [ - "#@title メモリの初期化(Beta) {display-mode: \"form\"}\n", + "#@title Memory initialization {display-mode: \"form\"}\n", "\n", "import gc\n", "import torch\n", @@ -3898,9 +3862,9 @@ "\n", "for name in variable_names:\n", " if name in globals():\n", - " del globals()[name]\n", + " del globals()[name]\n", " if name in locals():\n", - " del locals()[name]\n", + " del locals()[name]\n", "\n", "torch.cuda.empty_cache()\n", "gc.collect()" @@ -3914,74 +3878,63 @@ }, "outputs": [], "source": [ - "#@title 画像のメタデータを見る {display-mode: \"form\"}\n", - "from PIL import Image, PngImagePlugin\n", + "#@title View Image Metadata {display-mode: \"form\"}\n", "import os\n", - "image_path = \"\" #@param {type:\"string\"}\n", + "from PIL import Image, PngImagePlugin\n", "\n", - "try:\n", - " output_info = Image.open(image_path).info\n", - "except:\n", - " raise FileNotFoundError(\"画像を読み込めませんでした\")\n", + "image_path = \"\" #@param {type:\"string\"}\n", "\n", - "if image_path and os.path.isfile(image_path):\n", - " output_info = Image.open(image_path).info\n", + "def show_png_info(image_path):\n", + " try:\n", + " output_info = Image.open(image_path).info\n", + " except Exception as err:\n", + " if not image_path:\n", + " print(\"\\33[31mPlease enter the image path\\33[0m\")\n", + " return\n", + " elif isinstance(err, FileNotFoundError):\n", + " print(\"\\33[31mThe specified image file cannot be found\\33[0m\")\n", + " return\n", + " else:\n", + " raise (\"An error occurred while opening the image file.\") from err\n", "\n", + " if not output_info:\n", + " print(\"\\33[31mMetadata not found\\33[0m\")\n", " for key,info in output_info.items():\n", - " print(f\"{key} : {info}\\n\")\n", + " print(f\"\\33[32m{key} : {info}\\33[0m\\n\")\n", "\n", - "\n", - "\n", - "\n" + "show_png_info(image_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "OOtCwKbZpm-q", - "outputId": "09ea1ae4-4694-41f4-cea9-f9948ecce095" + "id": "OOtCwKbZpm-q" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[34mSuccessfully extracted to /content/Generated-No.3 \u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ + "#@title Unzip 7zip{display-mode: \"form\"}\n", + "\n", "from google.colab import files\n", "import os\n", "import subprocess\n", "\n", - "# 7-Zipがインストールされているか確認し、必要に応じてインストール\n", + "\n", "try:\n", " subprocess.run(['7z'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n", "except FileNotFoundError:\n", - " print(\"7-Zip is not installed. Installing 7-Zip...\")\n", " !apt-get install -y p7zip-full\n", "\n", - "#@title 解凍{display-mode: \"form\"}\n", - "#@markdown >Specify 7z file to extract.\n", "\n", - "path = \"/content/Generated-No.3.7z\" # @param {type:\"string\"}\n", + "path = \"\" # @param {type:\"string\"}\n", "if not path:\n", - " path = \"/content/Generated-No.1.7z\" # デフォルトの7zファイルパス\n", + " path = \"/content/Generated-No.1.7z\"\n", "\n", "if os.path.isfile(path) and path.endswith('.7z'):\n", " try:\n", - " # 解凍先ディレクトリを決定\n", " extract_path = path.replace('.7z', '')\n", " os.makedirs(extract_path, exist_ok=True)\n", - "\n", - " # 7-Zipコマンドを使って解凍\n", " subprocess.run(['7z', 'x', path, f'-o{extract_path}'], check=True)\n", - "\n", " print(f'\\033[34mSuccessfully extracted to {extract_path} \\033[0m')\n", " except Exception as e:\n", " print(f\"\\033[31mExtraction failed. {e}\\033[0m\")\n", @@ -3993,88 +3946,11 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 52 - }, - "id": "QH5cPNi297Lr", - "outputId": "60e8a12f-01f9-4813-cc65-879aaf46ab87" + "id": "QH5cPNi297Lr" }, - "outputs": [ - { - "data": { - "application/javascript": [ - "\n", - " async function download(id, filename, size) {\n", - " if (!google.colab.kernel.accessAllowed) {\n", - " return;\n", - " }\n", - " const div = document.createElement('div');\n", - " const label = document.createElement('label');\n", - " label.textContent = `Downloading \"${filename}\": `;\n", - " div.appendChild(label);\n", - " const progress = document.createElement('progress');\n", - " progress.max = size;\n", - " div.appendChild(progress);\n", - " document.body.appendChild(div);\n", - "\n", - " const buffers = [];\n", - " let downloaded = 0;\n", - "\n", - " const channel = await google.colab.kernel.comms.open(id);\n", - " // Send a message to notify the kernel that we're ready.\n", - " channel.send({})\n", - "\n", - " for await (const message of channel.messages) {\n", - " // Send a message to notify the kernel that we're ready.\n", - " channel.send({})\n", - " if (message.buffers) {\n", - " for (const buffer of message.buffers) {\n", - " buffers.push(buffer);\n", - " downloaded += buffer.byteLength;\n", - " progress.value = downloaded;\n", - " }\n", - " }\n", - " }\n", - " const blob = new Blob(buffers, {type: 'application/binary'});\n", - " const a = document.createElement('a');\n", - " a.href = window.URL.createObjectURL(blob);\n", - " a.download = filename;\n", - " div.appendChild(a);\n", - " a.click();\n", - " div.remove();\n", - " }\n", - " " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/javascript": [ - "download(\"download_adddbb2a-d4b0-4100-8238-09479ec0a90f\", \"Generated-No.1.zip\", 22239538)" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[34mZip file name is Generated-No.1.zip \u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ - "#@title ダウンロード{display-mode: \"form\"}\n", + "#@title download{display-mode: \"form\"}\n", "#@markdown >Specify file or directory to download.\n", "\n", "from google.colab import files\n", @@ -4186,12 +4062,13 @@ "from requests import HTTPError\n", "Youtube_path = \"\" # @param {type:\"string\"}\n", "\n", - "ローカルにダウンロードする = False # @param {type:\"boolean\"}\n", + "Download_to_local = False # @param {type:\"boolean\"}\n", + "\n", "def Youtube_download(Youtube_path,local_download):\n", " Not_download_list=[]\n", " mkdir_list=[\"/content/Youtube/File\",\"/content/Youtube/List\"]\n", " for mk_path in mkdir_list:\n", - " os.makedirs(mk_path,exist_ok=True)\n", + " os.makedirs(mk_path, exist_ok=True)\n", " os.chdir(\"/content/Youtube\")\n", " if Youtube_path:\n", " if Youtube_path.startswith(\"https://www.youtube.com/playlist?list=\"):\n", @@ -4209,7 +4086,7 @@ " except:\n", " Not_download_list.append(path)\n", " if Not_download_list:\n", - " print(\"ダウンロードに失敗したパス\")\n", + " print(\"Failed download path\")\n", " for ndl in Not_download_list:\n", " print(ndl)\n", " if local_download:\n", @@ -4224,99 +4101,87 @@ " yt.streams.first().download()\n", " yt_save_path=os.path.join(\"/content/Youtube/File\" , (yt.title+\".mp4\"))\n", " except:\n", - " raise HTTPError(\"URLが無効です\")\n", + " raise HTTPError(\"Invalid URL\")\n", " if local_download:\n", " try:\n", " files.download(yt_save_path)\n", " except FileNotFoundError:\n", - " print(f\"ファイルのダウンロードに失敗しました。\")\n", + " print(f\"File download failed\")\n", "\n", " else:\n", - " raise HTTPError(\"URLが無効です\")\n", + " raise HTTPError(\"Invalid URL\")\n", " else:\n", - " raise HTTPError(\"URLの入力をお願いします\")\n", + " raise HTTPError(\"Please enter the URL\")\n", "\n", " os.chdir(\"/content\")\n", - " print(\"処理が終了しました\")\n", - " print(f\"セーブパス: {os.path.basename(yt_save_path)}\")\n", + " print(\"The process has been completed.\")\n", + " print(f\"save_path: {os.path.basename(yt_save_path)}\")\n", "\n", - "Youtube_download(Youtube_path,ローカルにダウンロードする)\n", + "Youtube_download(Youtube_path,Download_to_local)\n", "\n", "\n", "\n" ] }, { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "FC3E3W81SL8n" - }, - "outputs": [], "source": [ - "#@title グリッド画像の分割 (iamge generation){display-mode: \"form\"}\n", + "#@title Split Grid Image {display-mode: \"form\"}\n", "from PIL import Image\n", "import os\n", "import sys\n", "import math\n", "\n", - "### 各定数の設定\n", - "# 縦方向分割数\n", - "\n", - "# 分割元ファイルパス\n", - "input_path = \"\" # @param {type:\"string\"}\n", + "input_img_path = \"\" # @param {type:\"string\"}\n", "output_dir = \"\" # @param {type:\"string\"}\n", - "縦の分割回数 = 1 # @param {type:\"number\"}\n", - "横の分割回数 = 3 # @param {type:\"number\"}\n", - "#@markdown >heightとwidthは2以上の自然数の入力をお願いします\n", - "height=縦の分割回数\n", - "width=横の分割回数\n", - "base_img_path=input_path\n", + "Vertical_Image_Count = 2 # @param {type:\"number\"}\n", + "Horizontal_Image_Count = 2 # @param {type:\"number\"}\n", "\n", + "if (not input_img_path) or (not output_dir):\n", + " raise ValueError(\"Required path is missing\")\n", "\n", - "if base_img_path==\"\" or output_dir == \"\":\n", - " raise TypeError(\"pathが未入力です\")\n", - "if output_dir is not dir:\n", - " output_dir = os.path.dirname(output_dir)\n", + "if Vertical_Image_Count < 1 or Horizontal_Image_Count < 1:\n", + " raise ValueError(\"The number of installments must be greater than or equal to 1\")\n", "\n", - "def is_positive_integer(value):\n", - " try:\n", - " value = int(value) # 整数に変換\n", - " if value > 0: # 1以上の場合は True を返す\n", - " return True\n", - " else:\n", - " return False\n", - " except ValueError: # ValueError が発生した場合は False を返す\n", - " return False\n", - "\n", - "if not is_positive_integer(height) or is_positive_integer(width):\n", - " raise TypeError(\"heightとwidthは、2以上の自然数である必要があります。\")\n", + "if os.path.isfile(output_dir):\n", + " output_dir = os.path.dirname(output_dir)\n", "\n", - "def ImgSplit(im,w,h):\n", - " # 読み込んだ画像の高さと幅を指定分割数で割る\n", - " HEIGHT = h / height\n", - " WIDTH = w / width\n", "\n", - " # 縦の分割枚数\n", - " for h1 in range(height):\n", - " # 横の分割枚数\n", - " for w1 in range(width):\n", + "def ImgSplit(\n", + " im,\n", + " width,\n", + " height,\n", + " Vertical_Image_Count,\n", + " Horizontal_Image_Count\n", + " ):\n", + " HEIGHT = height / Vertical_Image_Count\n", + " WIDTH = width / Horizontal_Image_Count\n", + " for h1 in range(Vertical_Image_Count):\n", + " for w1 in range(Horizontal_Image_Count):\n", " w2 = w1 * WIDTH\n", " h2 = h1 * HEIGHT\n", " yield im.crop((w2, h2, WIDTH + w2, HEIGHT + h2))\n", "\n", - "#if __name__ == '__main__': このセルが直接実行された場合\n", - " # 画像の読み込み\n", - "im = Image.open(base_img_path)\n", + "\n", + "\n", + "im = Image.open(input_img_path)\n", "w = im.size[0]\n", "h = im.size[1]\n", - "length = math.log10(height * width) + 1\n", + "length = math.log10(Vertical_Image_Count * Horizontal_Image_Count) + 1\n", + "\n", "os.makedirs(output_dir, exist_ok=True)\n", - "for number, ig in enumerate(ImgSplit(im,w,h), 1):\n", - " # 出力\n", + "\n", + "\n", + "for number, ig in enumerate(ImgSplit(im, w, h, Vertical_Image_Count, Horizontal_Image_Count), 1):\n", " ig.save(output_dir + \"/\" + str(number).zfill(int(length)) + \".PNG\", \"PNG\")\n", - "print(f\"\\033[34m分割が完了しました: {output_dir}\\033[0m\")" - ] + "\n", + "print(f\"\\033[34mSplit is complete: {os.path.abspath(output_dir)}\\033[0m\")" + ], + "cell_type": "code", + "metadata": { + "id": "ldFCpUIAg5Je" + }, + "execution_count": null, + "outputs": [] }, { "cell_type": "code", @@ -4326,7 +4191,7 @@ }, "outputs": [], "source": [ - "#@title 単語が含まれているファイルを移動{display-mode: \"form\"}\n", + "#@title Move files containing the specified words {display-mode: \"form\"}\n", "import os\n", "import shutil\n", "\n", @@ -4335,10 +4200,10 @@ " os.makedirs(destination_dir)\n", "\n", " for root, dirs, files in os.walk(source_dir):\n", - " for file in files:\n", - " if keyword in file:\n", - " source_file = os.path.join(root, file)\n", - " destination_file = os.path.join(destination_dir, file)\n", + " for _file in files:\n", + " if keyword in _file:\n", + " source_file = os.path.join(root, _file)\n", + " destination_file = os.path.join(destination_dir, _file)\n", " shutil.move(source_file, destination_file)\n", " print(f\"Moved: {source_file} -> {destination_file}\")\n", "\n", @@ -4367,14 +4232,14 @@ }, "outputs": [], "source": [ - "#@title dir_size_counter {display-mode: \"form\"}\n", - "#@markdown\n", + "#@title Directory or File size Counter {display-mode: \"form\"}\n", + "\n", "import os\n", "\n", "def get_folder_size(folder_path):\n", " total_size = 0\n", " if os.path.isfile(folder_path):\n", - " total_size+=os.path.getsize(folder_path)\n", + " total_size = os.path.getsize(folder_path)\n", " else:\n", " for path, dirs, files in os.walk(folder_path):\n", " for file in files:\n", @@ -4383,18 +4248,18 @@ "\n", " return total_size\n", "\n", - "folder_path = \"\" # @param {type:\"string\"}\n", - "if not os.path.exists(folder_path):\n", - " raise FileNotFoundError(\"pathが無効です\")\n", + "path = \"\" # @param {type:\"string\"}\n", "\n", - "size_in_bytes = get_folder_size(folder_path)\n", + "if not os.path.exists(path):\n", + " raise FileNotFoundError(\"Invalid path\")\n", "\n", + "size_in_bytes = get_folder_size(path)\n", "\n", "size_in_kb = size_in_bytes / 1024\n", "size_in_mb = size_in_kb / 1024\n", "size_in_gb = size_in_mb / 1024\n", "\n", - "print(f\"Folder Size: {size_in_bytes} bytes\")\n", + "print(f\"Folder Size: {size_in_bytes:.2f} B\")\n", "print(f\"Folder Size: {size_in_kb:.2f} KB\")\n", "print(f\"Folder Size: {size_in_mb:.2f} MB\")\n", "print(f\"Folder Size: {size_in_gb:.2f} GB\")" @@ -4402,101 +4267,127 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "BRRD_VF59fA_" - }, - "outputs": [], "source": [ - "#@title 画像変換 {display-mode: \"form\"}\n", + "#@title Change image format {display-mode: \"form\"}\n", + "\n", + "\"\"\"\n", + "後で修正必要。\n", + "83行ら辺を修正\n", + "\"\"\"\n", + "\n", + "\n", + "input_path = \".\" #@param {type:\"string\"}\n", + "output_dir=\"\" #@param {type:\"string\"}\n", + "output_format = \"jpg\" # @param [\"png\", \"jpg\"]\n", + "\n", "import os\n", "from PIL import Image\n", "import sys\n", - "input_path = \"\" #@param {type:\"string\"}\n", - "output_path=\"\" #@param {type:\"string\"}\n", - "Change_mode = \"png2jpg\" # @param [\"png2jpg\", \"jpg2png\"]\n", - "\n", - "#@markdown * png2jpg :png画像をjpg画像に変換します。\n", - "\n", - "\n", - "#@markdown * jpg2png :jpg画像をpng画像に変換します。\n", - "\n", - "if not os.path.exists(input_path):\n", - " #raise FileNotFoundError('\\033[33m指定された \"dir\"または\"file\" が見つかりませんでした。\\033[0m')\n", - " sys.exit('\"input_path\"にdirまたはfileのパスを入力お願いします。')\n", - "\n", - "if not input_path.endswith(\".png\" or \".jpg\" or \".jpeg\"):\n", - " if output_path==\"\":\n", - " # 保存先のディレクトリパスを作成\n", - " save_dir_path = input_path +\"-\" +Change_mode\n", - " else:\n", - " save_dir_path=output_path\n", - " # 保存先のディレクトリが存在しない場合は作成する\n", - " if not os.path.exists(save_dir_path):\n", - " os.makedirs(save_dir_path)\n", - "\n", - " # ディレクトリ内のファイルを取得\n", - " file_list = os.listdir(input_path)\n", - "\n", - " # ファイルごとに処理を行う\n", - " for file_name in file_list:\n", - " # ファイルのパスを作成\n", - " file_path = os.path.join(save_dir_path, file_name)\n", - "\n", - " # ファイルの拡張子を取得\n", - " file_extension = os.path.splitext(file_name)[1]\n", - " if Change_mode == \"png2jpg\":\n", - " # 最後が特定の単語であるかをチェックする\n", - " if file_extension.endswith(\".png\"):\n", - " try:\n", - " # 画像を開く\n", - " im = Image.open(file_path)\n", - " file_name_new = file_name.replace(\".png\", \".jpg\")#pngをjpg\n", - " # 保存先のファイルパスを作成\n", - " save_file_path = os.path.join(save_dir_path, file_name_new)\n", - " # 変換後の画像を保存する\n", - " im.save(save_file_path,format=\"JPEG\")\n", - " print(f\"\\033[34m{save_file_path}を保存しました。\\033[0m\")\n", - " except Exception as e:\n", - " print(f\"\\033[31m{file_name}の変換中にエラーが発生しました: {str(e)}\\033[0m\")\n", - " else:\n", - " print(f\"{file_name}はpngファイルではないため、変換をスキップしました。\")\n", - " elif Change_mode == \"jpg2png\":\n", - " try:\n", - " # 画像を開く\n", - " im = Image.open(file_path)\n", - " file_name_new = file_name.replace((\".jpg\"or\".jpeg\"), \".png\")#jpgをpng\n", - " # 保存先のファイルパスを作成\n", - " save_file_path = os.path.join(save_dir_path, file_name_new)\n", - " # 変換後の画像を保存する\n", - " im.save(save_file_path,format=\"PNG\")\n", - " print(f\"\\033[34m{save_file_path}を保存しました。\\033[0m\")\n", - " except Exception as e:\n", - " print(f\"\\033[31m{file_path}の変換中にエラーが発生しました: {str(e)}\\033[0m\")\n", - "else:\n", - " if Change_mode ==\"png2jpg\":\n", - " if not output_path==\"\":\n", - " basename_new = input_path.replace(\".png\", \".jpg\")#jpgをpng\n", - " save_file_path=os.path.join(output_path, basename_new)\n", - " im = Image.open(input_path)\n", - " im.save(save_file_path,format=\"JPEG\")\n", - " else:\n", - " im = Image.open(input_path)\n", - " save_file_path = input_path.replace(\".png\", \".jpg\")\n", - " im.save(save_file_path,format=\"JPEG\")\n", - " print(f'\\033[34m\"{save_file_path}\"に保存しました。\\033[0m')\n", - " elif Change_mode == \"jpg2png\":\n", - " if not output_path==\"\":\n", - " basename_new = input_path.replace((\".jpg\"or\".jpeg\"), \".png\")#jpgをpng\n", - " save_file_path=os.path.join(output_path, basename_new)\n", - " im = Image.open(input_path)\n", - " im.save(save_file_path,format=\"PNG\")\n", - " else:\n", - " im = Image.open(input_path)\n", - " save_file_path = input_path.replace((\".jpg\"or\".jpeg\"), \".png\")\n", - " im.save(save_file_path,format=\"PNG\")\n", - " print(f'\\033[34m\"{save_file_path}\"に保存しました。\\033[0m')" - ] + "\n", + "class Image_Format_Converter:\n", + " def __init__(self):\n", + " \"\"\"\n", + " NOTE:\n", + " When there are a very large number of files,\n", + " the maximum value at that time is stored to lighten the processing as much as possible.\n", + "\n", + " About exc_dict:\n", + " key : value = Target extension to format : (output file exc , Save format)\n", + " \"\"\"\n", + "\n", + " self.max_exc_number = 0\n", + " self.exc_dict = {\".png\":(\".jpg\",\"JPEG\"),\n", + " \".jpg\":(\".png\",\"PNG\")}\n", + "\n", + "\n", + " def file_name_set(self,path):\n", + " if os.path.isfile(path):\n", + " base_file_path, exc = os.path.splitext(path)\n", + " dir_path = os.path.dirname(base_file_path)\n", + " os.makedirs(dir_path,exist_ok=True)\n", + " sp_file_number = 1\n", + " while True:\n", + " _try_file_path = f\"{base_file_path}({sp_file_number}){exc}\"\n", + " if not os.path.isfile(_try_file_path):\n", + " return _try_file_path\n", + " else:\n", + " sp_file_number += 1\n", + " else:\n", + " return path\n", + "\n", + "\n", + " def dir_name_set(self,path):\n", + " if os.path.isdir(path):\n", + " dir_sp_number = 1\n", + " while True:\n", + " _try_dir_path = f\"{path}({dir_sp_number})\"\n", + " if not os.path.isdir(_try_dir_path):\n", + " return _try_dir_path\n", + " else:\n", + " dir_sp_number += 1\n", + " else:\n", + " return path\n", + "\n", + "\n", + " def image_format_converter(\n", + " self,\n", + " input_path,\n", + " output_path,\n", + " return_format\n", + " ):\n", + " \"\"\"\n", + " NOTE:\n", + " If output_path is not entered,\n", + " create a folder to store the images to be input in the directory where they are located.\n", + " \"\"\"\n", + " if not os.path.exists(input_path):\n", + " print('\\033[31mPlease enter the directory or file path\\033[0m')\n", + " return\n", + "\n", + " elif os.path.isdir(input_path):\n", + " if not output_path:\n", + " base_dir_name = os.path.dirname(input_path)\n", + " output_path = self.dir_name_set(base_dir_name)\n", + "\n", + " for File_path in os.listdir(input_path):\n", + " if os.path.splis\n", + " #後でやる\n", + "\n", + "\n", + " def img_conver_func(\n", + " self,\n", + " input_path,\n", + " output_path,\n", + " return_format\n", + " ):\n", + " base_save_path, input_file_exc = os.path.splitext(File_path)\n", + " if input_file_exc in self.exc_dict:\n", + " target_save_exc, save_format = self.exc_dict[input_file_exc]\n", + " try:\n", + " target_image = Image.open(File_path)\n", + " except Exception as err:\n", + " if not isinstance(err, KeyboardInterrupt):\n", + " print(f\"\\033[31mFailed to load image: {os.path.abspath(File_path)}\\033[0m\")\n", + " else:\n", + " raise err\n", + " else:\n", + " save_file_path = self.file_name_set(f\"{base_save_path}{target_save_exc}\")\n", + " target_image.save(save_file_path, format=save_format)\n", + " print(f\"\\033[34mFormatted image path: {save_file_path}\\033[0m\")\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " Img_Format_cls = Image_Format_Converter()\n", + " Img_Format_cls.image_format_converter(\n", + " input_path=input_path,\n", + " output_path=output_dir,\n", + " return_format=output_format)\n" + ], + "metadata": { + "id": "svvD5rfHfPHq" + }, + "execution_count": null, + "outputs": [] }, { "cell_type": "code", @@ -4506,21 +4397,46 @@ }, "outputs": [], "source": [ - "#@title mp4を再生 {display-mode: \"form\"}\n", - "MP4_path= \"\" #@param {type:\"string\"}\n", - "# mp4動画の再生\n", + "#@title Play mp4 {display-mode: \"form\"}\n", + "\n", + "Path_of_mp4_file = \"\" #@param {type:\"string\"}\n", + "\n", "import os\n", "from IPython.display import HTML\n", "from base64 import b64encode\n", - "try:\n", - " mp4 = open(MP4_path, 'rb').read()\n", - "except:\n", - " raise FileNotFoundError(\"指定されたmp4が見つかりませんでした\")\n", - "data_url = 'data:video/mp4;base64,' + b64encode(mp4).decode()\n", - "HTML(f\"\"\"\n", - "\"\"\")" + "\n", + "class Show_video:\n", + " def play_mp4(self,path):\n", + " if os.path.isfile(path):\n", + " self.show_func(path)\n", + " elif os.path.isdir(path):\n", + " mp4_list = []\n", + " for mp4_path in os.listdir(path):\n", + " if os.path.splitext(mp4_path)[1] == \".mp4\":\n", + " mp4_list.append(os.path.abspath(mp4_path))\n", + "\n", + " if not mo4_list:\n", + " raise FileNotFoundError(\"No mp4 file exists in the specified directory\")\n", + " else:\n", + " for mp4_file_path in mp4_list:\n", + " self.show_func(mp4_file_path)\n", + " else:\n", + " if not path:\n", + " raise ValueError(\"Please enter the path to the file or directory\")\n", + " else:\n", + " raise FileNotFoundError(\"Invalid path\")\n", + "\n", + "\n", + " def show_func(self,mp4_file):\n", + " mp4 = open(mp4_file, 'rb').read()\n", + " data_url = 'data:video/mp4;base64,' + b64encode(mp4).decode()\n", + " HTML(f\"\"\"\n", + " \n", + " \"\"\")\n", + "\n", + "Show_video().play_mp4(Path_of_mp4_file)" ] }, { @@ -4531,7 +4447,7 @@ }, "outputs": [], "source": [ - "#@title 文字列トークンカウンター{display-mode: \"form\"}\n", + "#@title String token counter{display-mode: \"form\"}\n", "import pprint\n", "from transformers import AutoTokenizer\n", "\n", @@ -4539,9 +4455,9 @@ "try:\n", " tokens = tokenizer.tokenize(prompt)\n", "except:\n", - " text_model_id = \"laion/CLIP-ViT-H-14-laion2B-s32B-b79K\"\n", - " tokenizer = AutoTokenizer.from_pretrained(text_model_id)\n", - " tokens = tokenizer.tokenize(prompt)\n", + " text_model_id = \"laion/CLIP-ViT-H-14-laion2B-s32B-b79K\"\n", + " tokenizer = AutoTokenizer.from_pretrained(text_model_id)\n", + " tokens = tokenizer.tokenize(prompt)\n", "print(len(tokens))\n", "pprint.pprint(tokens)" ] @@ -4554,7 +4470,7 @@ }, "outputs": [], "source": [ - "#@title 高画質化 {display-mode: \"form\"}\n", + "#@title Higher image quality{display-mode: \"form\"}\n", "from PIL import Image\n", "from io import BytesIO\n", "from diffusers import StableDiffusionUpscalePipeline\n", @@ -4574,11 +4490,11 @@ " )\n", " pipeline2 = pipeline2.to(\"cuda\")\n", "if not os.path.exists(low_res_img_path):\n", - " raise FileNotFoundError(\"ファイルが見つかりませんでした\")\n", + " raise FileNotFoundError(\"File not found\")\n", "try:\n", " low_res_img = Image.open(low_res_img_path)\n", "except:\n", - " raise FileNotFoundError(\"画像を読み込めませんでした\")\n", + " raise FileNotFoundError(\"Image could not be loaded.\")\n", "low_res_img = low_res_img.resize((128, 128))\n", "\n", "negative_prompt=\"low quality:2.0\"\n", @@ -4592,7 +4508,7 @@ "os.makedirs(\"/content/low_res_imgs\",exist_ok=True)\n", "path1=(f\"/content/low_res_imgs/No{L}.png\")\n", "upscaled_image.save(path1)\n", - "print(f\"画像保存パス: ({path1})\")" + "print(f\"img save path: ({path1})\")" ] }, { @@ -4603,27 +4519,27 @@ }, "outputs": [], "source": [ - "#@title 画像フォルダを削除 {display-mode: \"form\"}\n", + "#@title Delete folder{display-mode: \"form\"}\n", "\n", "del_path = \"\" #@param {type:\"string\"}\n", "if del_path ==\"\":\n", " del_path=\"/content/Generated\"\n", "import shutil\n", "if not os.path.isdir(del_path):\n", - " raise TypeError(\"ディレクトリのみ削除が可能です\")\n", - "#@markdown 未入力の場合 /content/Generatedを削除します\n", + " raise TypeError(\"Only directories can be deleted\")\n", + "\n", "\n", - "YorS=input(\"本当に削除しますか?[yes/no]: \")\n", - "if YorS.lower() in (\"yes\", \"y\"): # 入力された文字列を小文字にして、yesやyと一致するか判定する\n", + "YorS=input(\"Are you sure you want to delete it? [yes/no]: \")\n", + "if YorS.lower() in (\"yes\", \"y\"):\n", " try:\n", " shutil.rmtree(del_path)\n", - " print(\"削除しました\")\n", + " print(\"Deleted.\")\n", " except FileNotFoundError:\n", - " print(f\"\\033[31m{del_path}が見つかりませんでした\\033[0m\")\n", - "elif YorS.lower() in (\"no\", \"n\"): # 入力された文字列を小文字にして、noやnと一致するか判定する\n", - " print(\"フォルダの削除を中止しました\")\n", + " print(f\"\\033[31mNot Found dir: {del_path}\\033[0m\")\n", + "elif YorS.lower() in (\"no\", \"n\"):\n", + " print(\"Deletion of the folder has been aborted.\")\n", "else:\n", - " print(\"yes/no のみの入力をお願いします。\")" + " print(\"Please enter only yes/no.\")" ] }, { @@ -4664,22 +4580,13 @@ "download_file(url, save_path)" ] }, - { - "cell_type": "markdown", - "metadata": { - "id": "zi4EonYLXtN0" - }, - "source": [ - "#追加オプション" - ] - }, { "cell_type": "markdown", "metadata": { "id": "a1QPxPSzF_aP" }, "source": [ - "#学習" + "#Training" ] }, { @@ -4690,85 +4597,52 @@ }, "outputs": [], "source": [ - "#@title モデルを学習 {display-mode: \"form\"}\n", - "\n", - "学習で使用する単語 = \"Cat ears girl\" # @param {type:\"string\"}\n", - "#概念を説明する単語 = \"\" # @param {type:\"string\"}\n", - "学習ステップ = 20 # @param {type:\"number\"}\n", + "#@title Training Lora {display-mode: \"form\"}\n", "\n", - "入力する画像フォルダ = \"/content/drive/MyDrive/GS\" # @param {type:\"string\"}\n", - "出力するフォルダ = \"/content/drive/MyDrive/make_model_2\" # @param {type:\"string\"}\n", - "#概念のタイプ = \"style\" # @param [\"style\",\"object\"]\n", - "モデル名前_or_パス = \"runwayml/stable-diffusion-v1-5\" # @param [\"runwayml/stable-diffusion-v1-5\", \"stabilityai/stable-diffusion-2-1\"] {allow-input: true}\n", - "出力ファイル名 = \"\" # @param {type:\"string\"}\n", + "instance_prompt = \"Cat ears girl\" # @param {type:\"string\"}\n", "\n", + "max_train_steps = 20 # @param {type:\"number\"}\n", "\n", - "#@markdown ---\n", - "\n", - "終了したらランタイムを切断 = False # @param {type:\"boolean\"}\n", - "#@markdown sd1.5系列とsd2.1系列では埋め込みに互換性がありません。\n", + "input_img_dir = \"\" # @param {type:\"string\"}\n", + "output_dir = \"/content/drive/MyDrive\" # @param {type:\"string\"}\n", "\n", - "driveに接続 = True # @param {type:\"boolean\"}\n", + "model_name_or_path = \"runwayml/stable-diffusion-v1-5\" # @param [\"runwayml/stable-diffusion-v1-5\", \"stabilityai/stable-diffusion-2-1\"] {allow-input: true}\n", "\n", - "# @markdown >用語の説明\n", - "# @markdown * placeholder_token : 学習内で、使用するトークンです。新しい概念を入力します。\n", - "# @markdown * initializer_token : 新しい概念が何であるかを要約する単語です。\n", - "import os\n", - "from google.colab import drive\n", - "drive_cn=driveに接続\n", - "if driveに接続:\n", - " if not drive._os.path.ismount('/content/drive'):\n", - " try:\n", - " drive.mount('/content/drive')\n", - " except:\n", - " print(\"ドライブの接続に失敗しました。\")\n", - " drive_cn=False\n", + "output_filename = \"\" # @param {type:\"string\"}\n", "\n", "\n", "\n", - "if drive_cn==False and \"/content/drive/MyDrive\" in 出力するフォルダ:\n", - " raise TypeError(\"Googleドライブに接続されていないため、ドライブに保存できません。\")\n", "\n", - "if not os.path.isdir(入力する画像フォルダ):\n", - " raise FileNotFoundError(\"画像フォルダが存在しません\")\n", + "Disconnect_runtime_when_finished = False # @param {type:\"boolean\"}\n", "\n", - "if 出力するフォルダ is None:\n", - " raise FileNotFoundError(\"結果を出力するフォルダを入力してください\")\n", "\n", + "#driveに接続 = True # @param {type:\"boolean\"}\n", "\n", + "import os\n", "\n", - "os.makedirs(出力するフォルダ,exist_ok=True)\n", - "try:\n", - " import diffusers,transformers\n", - "except:\n", - " !pip install transformers diffusers -q\n", - " import transformers,diffusers\n", "\n", - "if not os.path.exists(\"/content/script/diffusers\"):\n", - " %cd /content/script\n", - " !git clone https://github.com/huggingface/diffusers\n", - " %cd diffusers\n", + "os.makedirs(output_dir,exist_ok=True)\n", "\n", "try:\n", - " import accelerate,transformers,ftfy\n", + " import ftfy\n", "except:\n", - " !pip install -q accelerate>=0.16.0 transformers>=4.25.1 ftfy Jinja2 bitsandbytes\n", - " import bitsandbytes,accelerate,transformers,ftfy\n", - "import accelerate,torchvision,transformers,ftfy ,diffusers,bitsandbytes\n", + " !pip install ftfy -q\n", + " import ftfy\n", + " #!pip install -q accelerate>=0.16.0 transformers>=4.25.1 ftfy Jinja2 bitsandbytes\n", + " #import bitsandbytes,accelerate,transformers,ftfy\n", + "#import accelerate,torchvision,transformers,ftfy ,diffusers,bitsandbytes\n", "\n", "size=512\n", - "if モデル名前_or_パス==\"runwayml/stable-diffusion-v1-5\":\n", - " size=512\n", - "#else:\n", - "# size=768\n", + "#if model_name_or_path==\"runwayml/stable-diffusion-v1-5\":\n", + "# size=512\n", + "\n", + "#Can not use export\n", + "%env pretrained_model_name_or_path=$model_name_or_path\n", + "%env train_data_dir=$input_img_dir\n", "\n", - "#exportは使用不可\n", - "%env pretrained_model_name_or_path=$モデル名前_or_パス\n", - "%env train_data_dir=$入力する画像フォルダ\n", - "%env placeholder_token=$学習で使用する単語\n", "%env resolution=$size\n", - "%env max_train_steps=$学習ステップ\n", - "%env output_dir=$出力するフォルダ\n", + "%env output_dir=$output_dir\n", + "%env max_train_steps = $max_train_steps\n", "\n", "\n", "%cd /content/script/diffusers/examples/textual_inversion\n", @@ -4785,25 +4659,19 @@ " --lr_warmup_steps=0 \\\n", " --max_train_steps=$max_train_steps \\\n", "\n", - "base=os.path.join(出力するフォルダ,\"learned_embeds.safetensors\")\n", - "if not 出力ファイル名==\"\":\n", - " after_name=出力ファイル名+\".safetensors\"\n", - " after=os.path.join(出力するフォルダ,after_name)\n", - " if os.path.exists(base):\n", - " os.rename(base,after)\n", - " else:\n", - " print(\"埋め込みファイルが見つかりませんでした。\")\n", + "base=os.path.join(output_dir,\"learned_embeds.safetensors\")\n", + "if output_filename:\n", + " after_name = output_filename + \".safetensors\"\n", + " after = os.path.join(output_dir,after_name)\n", + "\n", "else:\n", " after=base\n", "\n", - "print(f\"\"\"\\033[34m\n", - "学習が終了しました\n", - "path: {出力するフォルダ}\\033[0m\n", - "\"\"\")\n", + "print(f\"\\033[34mpath: {output_dir}\\033[0m\")\n", "\n", - "if 終了したらランタイムを切断:\n", + "if Disconnect_runtime_when_finished:\n", " from google.colab import runtime\n", - " print(\"ランタイムを切断中...\")\n", + " print(\"Disconnect runtime...\")\n", " runtime.unassign()" ] }, @@ -4815,70 +4683,45 @@ }, "outputs": [], "source": [ - "#@title 埋め込みを学習 {display-mode: \"form\"}\n", + "#@title Training textual inversion {display-mode: \"form\"}\n", "\n", - "学習で使用するトークン = \"flans\" # @param {type:\"string\"}\n", - "概念を説明する単語 = \"flandre\" # @param {type:\"string\"}\n", - "学習ステップ = 50 # @param {type:\"number\"}\n", + "model_name_or_path = \"runwayml/stable-diffusion-v1-5\" # @param [\"runwayml/stable-diffusion-v1-5\", \"stabilityai/stable-diffusion-2-1\"] {allow-input: true}\n", + "placeholder_token = \"\" # @param {type:\"string\"}\n", + "initializer_token = \"\" # @param {type:\"string\"}\n", + "max_train_steps = 50 # @param {type:\"number\"}\n", "\n", - "入力する画像フォルダ = \"/content/drive/MyDrive/GS\" # @param {type:\"string\"}\n", - "出力するフォルダ = \"/content/drive/MyDrive/textjual\" # @param {type:\"string\"}\n", - "概念のタイプ = \"style\" # @param [\"style\",\"object\"]\n", - "モデル名前_or_パス = \"runwayml/stable-diffusion-v1-5\" # @param [\"runwayml/stable-diffusion-v1-5\", \"stabilityai/stable-diffusion-2-1\"] {allow-input: true}\n", + "input_imgs_dir = \"\" # @param {type:\"string\"}\n", + "output_dir = \"/content/drive/MyDrive/textual\" # @param {type:\"string\"}\n", + "input_imgs_dir = \"style\" # @param [\"style\",\"object\"]\n", "\n", - "出力ファイル名 = \"Flans\" # @param {type:\"string\"}\n", + "output_file_name = \"model\" # @param {type:\"string\"}\n", "\n", "\n", "#@markdown ---\n", "\n", - "終了したらランタイムを切断 = False # @param {type:\"boolean\"}\n", - "#@markdown sd1.5系列とsd2.1系列では埋め込みに互換性がありません。\n", + "Disconnect_runtime_when_finished = False # @param {type:\"boolean\"}\n", "\n", - "driveに接続 = True # @param {type:\"boolean\"}\n", + "conect_Gdrive = True # @param {type:\"boolean\"}\n", "\n", - "# @markdown >用語の説明\n", - "# @markdown * placeholder_token : 学習内で、使用するトークンです。新しい概念を入力します。\n", - "# @markdown * initializer_token : 新しい概念が何であるかを要約する単語です。\n", - "import os\n", - "from google.colab import drive\n", - "drive_cn=driveに接続\n", - "if driveに接続:\n", + "\n", + "\n", + "if conect_Gdrive:\n", " if not drive._os.path.ismount('/content/drive'):\n", " try:\n", " drive.mount('/content/drive')\n", " except:\n", - " print(\"ドライブの接続に失敗しました。\")\n", " drive_cn=False\n", "\n", "\n", "\n", - "if drive_cn==False and \"/content/drive/MyDrive\" in 出力するフォルダ:\n", - " raise TypeError(\"Googleドライブに接続されていないため、ドライブに保存できません。\")\n", + "os.makedirs(output_dir,exist_ok=True)\n", "\n", - "if not os.path.isdir(入力する画像フォルダ):\n", - " raise FileNotFoundError(\"画像フォルダが存在しません\")\n", - "\n", - "if 出力するフォルダ is None:\n", - " raise FileNotFoundError(\"結果を出力するフォルダを入力してください\")\n", - "\n", - "\n", - "\n", - "os.makedirs(出力するフォルダ,exist_ok=True)\n", - "\n", - "\n", - "try:\n", - " import diffusers,transformers\n", - "except:\n", - " !pip install transformers diffusers -q\n", - " import transformers,diffusers\n", "\n", "if not os.path.exists(\"/content/script/diffusers\"):\n", " %cd /content/script\n", " !git clone https://github.com/huggingface/diffusers\n", "\n", "\n", - "\n", - "\n", "if \"setup_txt\" not in locals():\n", " %cd /content/script/diffusers/examples/textual_inversion\n", " !pip install -r ./requirements.txt -q\n", @@ -4890,24 +4733,24 @@ "%cd /content/diffusers/examples/textual_inversion\n", "\n", "size=512\n", - "if モデル名前_or_パス==\"runwayml/stable-diffusion-v1-5\":\n", + "if model_name_or_path==\"runwayml/stable-diffusion-v1-5\":\n", " size=512\n", "else:\n", " size=768\n", "\n", - "#exportは使用不可\n", - "%env pretrained_model_name_or_path=$モデル名前_or_パス\n", - "%env train_data_dir=$入力する画像フォルダ\n", - "%env learnable_property=$概念のタイプ\n", - "%env placeholder_token=$学習で使用するトークン\n", - "%env initializer_token=$概念を説明する単語\n", + "#Can not use export\n", + "%env pretrained_model_name_or_path=$model_name_or_path\n", + "%env train_data_dir=$input_imgs_dir\n", + "%env learnable_property=$input_imgs_dir\n", + "%env placeholder_token=$placeholder_tokenトークン\n", + "%env initializer_token=$initializer_token\n", "%env resolution=$size\n", - "%env max_train_steps=$学習ステップ\n", - "%env output_dir=$出力するフォルダ\n", + "%env max_train_steps=$max_train_steps\n", + "%env output_dir=$output_dir\n", "\n", "#--learning_rate=5 \\\n", "#--mixed_precision=\"fp16\" \\\n", - "#learning_rateは整数\n", + "#learning_rate is int type.\n", "!accelerate launch textual_inversion.py \\\n", " --pretrained_model_name_or_path=$pretrained_model_name_or_path \\\n", " --train_data_dir=$train_data_dir \\\n", @@ -4923,26 +4766,23 @@ " --lr_warmup_steps=0 \\\n", " --output_dir=$output_dir\n", "\n", - "base=os.path.join(出力するフォルダ,\"learned_embeds.safetensors\")\n", - "if not 出力ファイル名==\"\":\n", - " after_name=出力ファイル名+\".safetensors\"\n", - " after=os.path.join(出力するフォルダ,after_name)\n", - " if os.path.exists(base):\n", - " os.rename(base,after)\n", - " else:\n", - " print(\"埋め込みファイルが見つかりませんでした。\")\n", + "base=os.path.join(output_dir,\"learned_embeds.safetensors\")\n", + "if output_file_name:\n", + " after_name=output_file_name+\".safetensors\"\n", + " after=os.path.join(output_dir,after_name)\n", + " if os.path.exists(base):\n", + " os.rename(base,after)\n", + " else:\n", + " print(\"Not Found model File\")\n", "else:\n", - " after=base\n", + " after=base\n", "\n", - "print(f\"\"\"\\033[34m\n", - "学習が終了しました\n", - "path: {after}\\033[0m\n", - "\"\"\")\n", + "print(f\"\\033[34mpath: {after}\\033[0m\")\n", "\n", - "if 終了したらランタイムを切断:\n", - " from google.colab import runtime\n", - " print(\"ランタイムを切断中...\")\n", - " runtime.unassign()" + "if Disconnect_runtime_when_finished:\n", + " from google.colab import runtime\n", + " print(\"Disconnect runtime...\")\n", + " runtime.unassign()" ] }, { @@ -4951,229 +4791,222 @@ "id": "OJteaxBCoPKM" }, "source": [ - "#説明書" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "P3nODsN-mW21" - }, - "outputs": [], - "source": [ - "#@title 埋め込みを適用 {display-mode: \"form\"}\n", - "\n", - "\n", - "# @markdown * Repo_id_or_path : hugface_idまたはfile_pathの入力をお願いします。\n", - "\n", - "# @markdown * token : 埋め込みを呼び出す場合に\"Prompt\"または\"N_prompt\"のいずれかに入力お願いします。\n", - "\n", - "# @markdown * weight_name : Repo_idの場合にファイル名前を指定してください\n", - "\n", - "# @markdown ーstep.3の終了後に使用可能です\n", - "\n", - "\n", - "\n" + "#Description" ] }, { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ADhtGAW9qhmv" - }, - "outputs": [], + "cell_type": "markdown", "source": [ - "#@title プロンプトの特殊トークン {display-mode: \"form\"}\n", + "**Function Description**\n", + ">**Important Functions**\n", + "\n", + "\n", + "* auto\n", + " * desc\n", + " * Minimize user input by automatically selecting the most highly rated models when searching for models.\n", + "\n", + " * type\n", + " * bool\n", + "\n", + "* model_select\n", + " * arg\n", + " 1. url\n", + " 2. hugface repository\n", + " 3. Path where the model is\n", + " 4. Keywords to search\n", + "\n", + " * desc\n", + " 1. url\n", + " * Download the model from the specified URL\n", + " * Given the URL of a hugface repository, it will search that repository.\n", + " * The same process as in 2. hugface repository is performed.\n", + "\n", + " 2. hugface repository\n", + " * Use hugface api to receive and select a model file from the repository.\n", + " * If there is a model (in the format of diffusers) that can be loaded \"from_pretrained\", add it to the candidates\n", + " * If auto is on, this is the preferred choice\n", + " \n", + " 3. Path where the model is stored\n", + " * Processes to find files with the extensions safetensors, ckpt, and bin\n", + "\n", + " 4. Keywords to search\n", + " * After using the hugface api and selecting a repository, do the same process as in 2.\n", + "\n", + " * If the user chooses to search outside of hugface, or if the file is not found in the repository, pass the keyword to the civitai api and process to search the model again.\n", + " * The following is how civitai selects models.\n", + " 1. Select Repository\n", + " 2. Select model version\n", + " 3. If more than one file exists in a given version, it processes the selection. If not, skip.\n", + " * In all processes, the candidates are sorted in order of popularity.\n", + " * In the case of auto, the indentation is 0, i.e., the one judged most popular is selected in all processes.\n", + "\n", + " * type\n", + " * string\n", + "\n", + "\\\n", "\n", - "# @markdown >ランダムワード\n", - "# @markdown\n", - "# @markdown * {cat,dog},cute = \"cat , cute\" / \"dog , cute\"\n", - "# @markdown * color,{blue,red,green} = \"color , blue\" / \"color , red\" / \"color , green\"\n", + "---\n", "\n", - "#@markdown >''=文字のスイッチ\n", + "> (option)Load lora\n", "\n", - "#@markdown * man,{boy,girl, ' '} = \"man\" / \"man,boy\" / \"man,girl\"\n", + "NOTE : Available after completion of step.3\n", "\n", - "#@markdown >file_nameの特殊トークンについて\n", + "* model_name\n", + " * See model_select in Important Functions\n", + " \n", + " \n", + "* auto\n", + " * See auto in Important Functions\n", "\n", - "#@markdown ファイル名にパラメータなどの値を入力できます。\n", + "\\\n", "\n", - "#@markdown * {prompt} : Prompt\n", + "---\n", "\n", - "#@markdown * {seed} : seed値\n", + "> (option) load textual inversion\n", "\n", - "#@markdown * {model_name} : model_name\n", + "NOTE : Available after completion of step.3\n", "\n", - "#@markdown * {g_scale} : guidance_scale\n", + "* model_path\n", + " * See model_select in Important Functions\n", "\n", - "#@markdown * {time} : 現在時刻\n", + " (However, models in diffusers format are ignored (because they cannot be loaded).)\n", "\n", - "#@markdown * {number} : 生成した回数" - ] + "* token\n", + " * desc\n", + " * Specifies a token to invoke textual inversion.\n", + " * Duplicate token strings are not allowed. If you want to use it, please unload it.\n", + " * type\n", + " * string\n", + "\n" + ], + "metadata": { + "id": "RkzUWWTw1VWx" + } }, { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "bDhgcTiB1sbA" - }, - "outputs": [], + "cell_type": "markdown", "source": [ - "#@title \"自動で条件を決めて生成\" の詳細 {display-mode: \"form\"}\n", + "**Prompt special tokens**\n", "\n", "\n", - "#@markdown * 詳細設定を推奨の値に設定\n", + ">Random Words\n", "\n", - "#@markdown * 次の機能をオンにします\n", - "#@markdown * 画面に表示\n", - "#@markdown * 画像の質を上げるプロントを追加する\n", - "#@markdown * プロンプトアシストを使う ( MagicPrompt )\n", - "#@markdown * 推奨するネガティブプロントを使用\n", - "#@markdown * 条件をメタデーターとして追加する\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "EK6-GlDG5Dvx" - }, - "outputs": [], - "source": [ - "#@title プロンプトアシスタントの詳細 {display-mode: \"form\"}\n", + "* Description\n", + " * Randomly select letters separated by { }\n", + " * if you include spaces, you can choose to ignore words.\n", "\n", - "# @markdown アニメ調の画像に適したアシスタントは \" anime-anything-promptgen-v2 \"\n", + "* Example\n", + " * { cat, dog }, cute\n", + " 1. cat, cute\n", + " 2. dog, cute\n", "\n", - "# @markdown 多目的のアシスタントは \" MagicPrompt-Stable-Diffusion \"\n", + " * color, { blue, red, green }\n", + " 1. color, blue\n", + " 2. color, red\n", + " 3. color, green\n", "\n", - "# @markdown 使用しない場合は \" None \" の選択をお願いします" - ] + " * man, { boy, girl, \" \" }\n", + " 1. **man** \n", + " 2. man, boy\n", + " 3. man, girl" + ], + "metadata": { + "id": "V_Cs8HWTclid" + } }, { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "OIqEx5TDV2IA" - }, - "outputs": [], + "cell_type": "markdown", "source": [ - "#@title 用語の説明 {display-mode: \"form\"}\n", + "**File name Special tokens**\n", + "* Description\n", + " * This function allows you to include parameters and other elements in the name of the file.\n", "\n", - "# @markdown * txt2img : テキストから画像\n", + "* Arguments\n", + " * {prompt}\n", + " * Prompt\n", "\n", - "# @markdown * img2img : 画像から画像\n", + " * {seed}\n", + " * Seed value\n", "\n", - "# @markdown * Inpaint : 書いた画像に色をつける\n", + " * {model_name}\n", + " * Path or URL or name of the model\n", "\n", - "# @markdown * txt2video : テキストから動画\n", + " * {g_scale}\n", + " * Guidance scale\n", "\n", - "# @markdown * safe : より安定した安全な画像の生成(txt2img)\n", + " * {time}\n", + " * Current time\n", "\n", - "# @markdown * seed (\"-1\"以上) / seed値を指定します。0の場合ランダムな数字を割り当てます。\n", + " * {number}\n", + " * Number of times generation has been performed at this runtime\n", "\n", - "# @markdown * guidance_scale (5≦15 推奨\"7.5\") / promptの強さの値です。強すぎるとノイズが発生する一方、弱すぎると絵が崩壊します。\n", - "\n", - "# @markdown * history_d (\"rand_new\", \"rand_init\" ) 推奨:\"rand_new\n", - "\n", - "# @markdown * moment(0.1≦1.0 ) 推奨\"0.8\"\n", - "\n", - "# @markdown * momentum_hist(-1.0≦1.0) 推奨\"0.2\"\n", - "\n", - "#@markdown * 拡散ステップ(1≦1000 推奨\"50\") / 計算をする回数を指定します。回数を減らすほど生成速度が速くなります\n", - "\n", - "# @markdown * 縦・横の大きさ ( 推奨\"512\" ) / 大きくすればするほど生成速度が遅くなります(img2img、Inpaint**以外のみ**)\n", - "\n", - "#@markdown * safety_level(強さ) : WEAK<MEDIUM<STRONG<MAX\n", - "\n", - "#@markdown * 条件を統一 : プロンプトアシスタントを使用するとき、最初の画像のプロンプトを繰り返し使用します\n", - "\n" - ] + "* Example\n", + " * Generate_img_{seed}_{time}.png" + ], + "metadata": { + "id": "Spj3qRqyptrO" + } }, { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "EB3pvcv0VZKd" - }, - "outputs": [], + "cell_type": "markdown", "source": [ - "#@title デフォルト(値が未入力の場合) {display-mode: \"form\"}\n", + "**Prompt assistant**\n", + "* **None**\n", + " * Select this if not used.\n", "\n", - "#@markdown >保存する先のパス\n", - "#@markdown * デフォルトでは /content/Generated_images に保存されます。なければ作るようになっています。\n", - "#@markdown * ドライブに保存する場合 /content/drive/MyDriveを最初につけてください\n", + "* gpt2-prompt-generator\n", + " * Recommended model\n", + " * This model uses gpt-2.\n", "\n", - "# @markdown >file_name\n", - "#@markdown * デフォルトは \"GIMG-{number}\" です\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "e0CbxSb2n8Re" - }, - "outputs": [], - "source": [ - "#@title 追記 {display-mode: \"form\"}\n", + "* anime-anything-promptgen-v2\n", + " * Suitable for images such as anime and 2D\n", + "\n", + "* MagicPrompt-Stable-Diffusion\n", + " * It is a versatile model\n", "\n", - "# @markdown 2024.1.27日時点でmoment付きEuler または EulerA において、history_d を \"rand_init\" にすると、1枚目が崩壊するバグがあります。\n", - "# @markdown なお、通常のEuler系のサンプラーでは起きません\n", "\n", - "#@markdown 条件: moment:0.8、moment_hist:0.6\n", "\n", "\n", "\n" - ] - }, - { - "cell_type": "markdown", + ], "metadata": { - "id": "BoIXG55qgRsh" - }, - "source": [ - "#Readme" - ] + "id": "I8qvP9WkwTj3" + } }, { "cell_type": "markdown", - "metadata": { - "id": "MJXQRW_E3AsI" - }, "source": [ - ">免責事項:\n", - "* このモデルは、趣味で作成したものであり、商用利用などは意図していません。\n", - "* 本プロジェクトを利用することにより生じた一切の問題について、当方は一切責任を負いません。\n", + "**default**\n", "\n", - "ー本プロジェクトとは、本画像生成ノートブックや、githubのページなどをさします\n", - "___\n", - ">本プロジェクトの目的\n", - "* プログラミングの個人的な学習\n", - "* diffusersをベースにした画像生成ノートブックです。\n", + ">Save path\n", "\n", + "* /content/Generated\n", "\n", - "---\n", + ">File_name\n", "\n", - "謝辞\n", + "* GIMG-{number}\n", "\n", - "本ノートブックの作成にあたり、オープンソースのリソースやフリーのツールを使用させていただきました。これらのリソースやツールがあったからこそ、本プロジェクトを実現することができました。\n", + "\\\n", "\n", - "この場を借りて、オープンソースのコミュニティや、フリーのツールを提供してくださった方々に感謝の意を表します。" - ] + "---\n", + "**EX**\n", + "* When saving to Google Drive\n", + " * /content/drive/MyDrive" + ], + "metadata": { + "id": "lE4Z4OXONRKC" + } } ], "metadata": { - "accelerator": "GPU", "colab": { "collapsed_sections": [ + "_QODtoEhqApV", "ONEtEJyRGYSL", - "iIEW0jvHK7I1" + "iIEW0jvHK7I1", + "a1QPxPSzF_aP", + "OJteaxBCoPKM" ], - "gpuType": "T4", "provenance": [], "include_colab_link": true }, @@ -5183,54 +5016,6 @@ }, "language_info": { "name": "python" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "301a6219daf04342a4119b07df94ee85": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "HBoxModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "HBoxModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "HBoxView", - "box_style": "", - "children": [ - "IPY_MODEL_4b2465faf7ec4ee99524c4746ff43fa5", - "IPY_MODEL_0230094aa644406da1d2982a1c812b9f", - "IPY_MODEL_6fa8bda9c4204230b86fc58b99c114bd" - ], - "layout": "IPY_MODEL_b2b576798c0a42359443820271599d22" - } - }, - "e468509df2a5401b8f49299c30fb0ce3": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "HBoxModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "HBoxModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "HBoxView", - "box_style": "", - "children": [ - "IPY_MODEL_6766e72223f841e7bc2bb925201b159d", - "IPY_MODEL_0dca7bf7d3e44678856049843285c59f", - "IPY_MODEL_8db83f378cec4b71a87cd9ddf9576115" - ], - "layout": "IPY_MODEL_47c427a32b7f4c5094ebf8e5da5d6992" - } - } - } } }, "nbformat": 4,