Skip to content

Commit

Permalink
commit RandomGeneratorV2 node, fix LoadFlorence2Model to compatible t…
Browse files Browse the repository at this point in the history
…ransformers 4.45.x
  • Loading branch information
chflame163 committed Oct 9, 2024
1 parent e7b5d9b commit e968c4d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 19 deletions.
20 changes: 20 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ When this error has occurred, please check the network environment.

<font size="4">**If the dependency package error after updating, please double clicking ```repair_dependency.bat``` (for Official ComfyUI Protable) or ```repair_dependency_aki.bat``` (for ComfyUI-aki-v1.x) in the plugin folder to reinstall the dependency packages. </font><br />

* Commit [RandomGeneratorV2](#RandomGeneratorV2) node, add least random range and seed options.
* Commit [TextJoinV2](#TextJoinV2) node, add delimiter options on top of TextJion.
* Commit [GaussianBlurV2](#GaussianBlurV2) node, The parameter accuracy has been improved to 0.01.
* Commit [UserPromptGeneratorTxtImgWithReference](#UserPromptGeneratorTxtImgWithReference) node.
Expand Down Expand Up @@ -1634,6 +1635,25 @@ int: Integer random number.
float: Float random number.
bool: Boolean random number.

### <a id="table1">RandomGeneratorV2</a>
On the based of [RandomGenerator](#RandomGenerator), add the least random range and seed options.

Node Options:
![image](image/random_generator_v2_node.jpg)
* image: Optional input, generate a list of random numbers that match the quantity in batches according to the image.
* min_value: Minimum value. Random numbers will randomly take values from the minimum to the maximum.
* max_value: Maximum value. Random numbers will randomly take values from the minimum to the maximum.
* least: Minimum random range. Random numbers will randomly at least take this value.
* float_decimal_places: Precision of float value.
* seed: The seed of random number.
* control_after_generate: Seed change options. If this option is fixed, the generated random number will always be the same.

Outputs:
int: Integer random number.
float: Float random number.
bool: Boolean random number.


### <a id="table1">NumberCalculator</a>

![image](image/number_calculator_node.jpg)
Expand Down
19 changes: 19 additions & 0 deletions README_CN.MD
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
## 更新说明
<font size="4">**如果本插件更新后出现依赖包错误,请双击运行插件目录下的```install_requirements.bat```(官方便携包),或 ```install_requirements_aki.bat```(秋叶整合包) 重新安装依赖包。

* 添加 [RandomGeneratorV2](#RandomGeneratorV2) 节点,增加最小随机范围和种子选项。
* 添加 [TextJoinV2](#TextJoinV2) 节点,在TextJion基础上增加分隔符选项。
* 添加 [GaussianBlurV2](#GaussianBlurV2) 节点,参数精度提升到0.01。
* 添加 [UserPromptGeneratorTxtImgWithReference](#UserPromptGeneratorTxtImgWithReference) 节点。
Expand Down Expand Up @@ -1455,6 +1456,24 @@ int: 整数随机数。
float: 浮点数随机数。
bool: 布尔随机数。

### <a id="table1">RandomGeneratorV2</a>
[RandomGenerator](#RandomGenerator) 的基础上,增加了最小随机范围选项,以及seed选项。

节点选项说明:
![image](image/random_generator_v2_node.jpg)
* image: 可选输入,按照图片批量生成数量相符的随机数列表。
* min_value:最小值。随机数将从最小值到最大值之间随机取值。
* max_value:最大值。随机数将从最小值到最大值之间随机取值。
* least: 最小随机范围。随机数将至少取到该值。
* float_decimal_places:浮点数精度。
* seed: 随机数种子。
* control_after_generate: 种子值变化选项。如果此选项固定,生成的随机数将始终相同。

输出:
int: 整数随机数。
float: 浮点数随机数。
bool: 布尔随机数。

### <a id="table1">NumberCalculator</a>
![image](image/number_calculator_node.jpg)
对两个数值进行数学运算并输出整数和浮点数结果<sup>*</sup>。支持的运算包括```+``````-``````*``````/``````**``````//``````%```
Expand Down
17 changes: 10 additions & 7 deletions py/florence2_ultra.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ def fixed_get_imports(filename) -> list[str]:
if os.path.basename(filename) != "modeling_florence2.py":
return get_imports(filename)
imports = get_imports(filename)
imports.remove("flash_attn")
try:
imports.remove("flash_attn")
except:
pass
return imports

def load_model(version):
florence_path = os.path.join(folder_paths.models_dir, "florence2")
os.makedirs(florence_path, exist_ok=True)


model_path = os.path.join(florence_path, version)
attention = 'sdpa'

if not os.path.exists(model_path):
log(f"Downloading Florence2 {version} model...")
Expand All @@ -49,11 +52,14 @@ def load_model(version):

try:
with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
# model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, attn_implementation=attention, device_map=device,
torch_dtype=torch.float32, trust_remote_code=True)
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
except Exception as e:
try:
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, attn_implementation=attention, device_map=device,
torch_dtype=torch.float32, trust_remote_code=True)
processor = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
except Exception as e:
sys.path.append(model_path)
Expand All @@ -68,7 +74,6 @@ def load_model(version):
log(f"Error loading model or tokenizer: {str(e)}", message_type='error')
return (None, None)

attention = 'sdpa'
# Load the model configuration
model_config = Florence2Config.from_pretrained(model_path)
# Load the model
Expand All @@ -82,10 +87,8 @@ def load_model(version):

processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)


return (model.to(device), processor)


def fig_to_pil(fig):
buf = io.BytesIO()
fig.savefig(buf, format='png', dpi=100, bbox_inches='tight', pad_inches=0)
Expand Down
85 changes: 74 additions & 11 deletions py/random_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from .imagefunc import AnyType
import random

NODE_NAME = 'RandomGenerator'

def generate_unique_seed() -> int:
while True:
new_number = random.randint(0, 1e14)
if new_number not in self.previous_seeds:
self.previous_seeds.add(new_number)
return new_number

class LSRandomGenerator:

def __init__(self):
self.NODE_NAME = 'RandomGenerator'
self.previous_seeds= set({})
self.fixed_seed = 0
pass
Expand Down Expand Up @@ -37,7 +42,7 @@ def random_generator(self, min_value, max_value, float_decimal_places, fix_seed,
batch_size = image.shape[0]
ret_nunbers = []
for i in range(batch_size):
new_seed = self.generate_unique_seed()
new_seed = generate_unique_seed()
if fix_seed:
if self.fixed_seed == 0:
self.fixed_seed = new_seed
Expand All @@ -60,18 +65,76 @@ def random_generator(self, min_value, max_value, float_decimal_places, fix_seed,
else:
return (ret_nunbers[0][0], ret_nunbers[0][1], ret_nunbers[0][2])

def generate_unique_seed(self) -> int:
while True:
new_number = random.randint(0, 1e14)
if new_number not in self.previous_seeds:
self.previous_seeds.add(new_number)
return new_number
class LS_RandomGeneratorV2:

def __init__(self):
self.NODE_NAME = 'RandomGeneratorV2'
self.previous_seeds= set({})
self.fixed_seed = 0
pass

@classmethod
def INPUT_TYPES(self):

return {
"required": {
"min_value": ("FLOAT", {"default": 0, "min": -1.0e14, "max": 1.0e14, "step": 0.01}),
"max_value": ("FLOAT", {"default": 10, "min": -1.0e14, "max": 1.0e14, "step": 0.01}),
"least": ("FLOAT", {"default": 0, "min": 0, "max": 1.0e14, "step": 0.01}),
"float_decimal_places": ("INT", {"default": 1, "min": 1, "max": 14, "step": 1}),
"seed":("INT", {"default": 0, "min": 0, "max": 1e14, "step": 1}),

},
"optional": {
"image": ("IMAGE",),
}
}

RETURN_TYPES = ("INT", "FLOAT", "BOOLEAN",)
RETURN_NAMES = ("int", "float", "bool",)
# OUTPUT_IS_LIST = (True, True, True,)
FUNCTION = 'random_generator_v2'
CATEGORY = '😺dzNodes/LayerUtility/Data'

def random_generator_v2(self, min_value, max_value, least, float_decimal_places, seed, image=None):
batch_size = 1
if image is not None:
batch_size = image.shape[0]
ret_nunbers = []
for i in range(batch_size):

random.seed(seed)
max_loop = 500
i = 0
while i < max_loop:
new_number = random.uniform(min_value, max_value)
if abs(new_number) - least >= 0 or least > max_value:
break
i += 1

# 转浮点
factor = random.uniform(3, 9)
random_float = new_number / factor
random_float = round(random_float * factor, float_decimal_places)
random_int = int(random_float)
random_bool = random_int %2 == 0
ret_nunbers.append((random_int, random_float, random_bool))

if len(ret_nunbers) > 1:
ret_ints = [item[0] for item in ret_nunbers]
ret_floats = [item[1] for item in ret_nunbers]
ret_bools = [item[2] for item in ret_nunbers]
return (ret_ints, ret_floats, ret_bools)
else:
return (ret_nunbers[0][0], ret_nunbers[0][1], ret_nunbers[0][2])


NODE_CLASS_MAPPINGS = {
"LayerUtility: RandomGenerator": LSRandomGenerator
"LayerUtility: RandomGenerator": LSRandomGenerator,
"LayerUtility: RandomGeneratorV2": LS_RandomGeneratorV2
}

NODE_DISPLAY_NAME_MAPPINGS = {
"LayerUtility: RandomGenerator": "LayerUtility: Random Generator"
"LayerUtility: RandomGenerator": "LayerUtility: Random Generator",
"LayerUtility: RandomGeneratorV2": "LayerUtility: Random Generator V2"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui_layerstyle"
description = "A set of nodes for ComfyUI it generate image like Adobe Photoshop's Layer Style. the Drop Shadow is first completed node, and follow-up work is in progress."
version = "1.0.71"
version = "1.0.72"
license = "MIT"
dependencies = ["numpy", "pillow", "torch", "matplotlib", "Scipy", "scikit_image", "scikit_learn", "opencv-contrib-python", "pymatting", "segment_anything", "timm", "addict", "yapf", "colour-science", "wget", "mediapipe", "loguru", "typer_config", "fastapi", "rich", "google-generativeai", "diffusers", "omegaconf", "tqdm", "transformers", "kornia", "image-reward", "ultralytics", "blend_modes", "blind-watermark", "qrcode", "pyzbar", "transparent-background", "huggingface_hub", "accelerate", "bitsandbytes", "torchscale", "wandb", "hydra-core", "psd-tools", "inference-cli[yolo-world]", "inference-gpu[yolo-world]", "onnxruntime"]

Expand Down

0 comments on commit e968c4d

Please sign in to comment.