diff --git a/expansion_experiments.py b/expansion_experiments.py index 5c99106..5a2a946 100644 --- a/expansion_experiments.py +++ b/expansion_experiments.py @@ -2,7 +2,7 @@ from modules.expansion import FooocusExpansion expansion = FooocusExpansion() -text = 'stone' +text = 'a handsome man' for i in range(64): print(expansion(text, seed=i)) diff --git a/fooocus_version.py b/fooocus_version.py index 9360f81..b0bb82b 100644 --- a/fooocus_version.py +++ b/fooocus_version.py @@ -1 +1 @@ -version = '2.1.767' +version = '2.1.769' diff --git a/models/prompt_expansion/fooocus_expansion/positive.txt b/models/prompt_expansion/fooocus_expansion/positive.txt new file mode 100644 index 0000000..938a0fd --- /dev/null +++ b/models/prompt_expansion/fooocus_expansion/positive.txt @@ -0,0 +1 @@ +accelerated, acclaimed, accomplished, acknowledged, activated, adapted, adjusted, admirable, adored, adorned, advanced, adventurous, advocated, aesthetic, affectionate, affirming, aged, ageless, aimed, airy, alleviated, alluring, altered, ambient, analytical, angelic, animated, appealing, applauded, appreciative, archetypal, ardent, aristocratic, aromatic, arranged, arrayed, arresting, artistic, aspired, assertive, associated, assuaged, assured, astonishing, astounding, astral, atmosphere, attempted, attention-grabbing, attired, attractive, audacious, august, authentic, authoritative, avant-garde, awarded, awe-inspiring, awe-striking, awesome, backed, baked, balanced, baroque, beaming, beaten, beauteous, beautiful, bedazzling, bedecked, beguiling, bejeweled, beloved, best, bestowed, bewitching, blazing, blended, blessed, blissful, blooming, bodacious, bonafide, bonny, borne, boundless, braised, brave, breathtaking, breezy, brewed, bright, brightened, brilliant, broiled, brooded, brought, bubbly, built, buoyant, burning, calm, calmed, canonized, captivating, carried, catchy, celebrated, celestial, certain, championed, changed, charismatic, charmed, charming, chased, chaste, cheered, cheerful, cherished, cherubic, chic, chivalrous, choicest, chosen, chromatic, churned, cinematic, clad, clapped, classic, clear, coached, colorful, colossal, combined, comely, comforted, comforting, commanding, commendable, commended, compatible, complete, complex, complimented, composition, conceived, conferred, confident, congruent, congruous, connected, consecrated, considered, consistent, consoled, consonant, conspicuous, constructed, consummate, contemplated, contemporary, content, conveyed, cooked, cool, coordinated, cosmic, coupled, courageous, courted, coveted, cozy, craved, created, critical, crystal-clear, cuddly, cultivated, cured, curious, current, curvaceous, customized, cute, cutting-edge, dainty, dapper, daring, dashing, daydreamed, dazzling, decked, decorated, decorative, decorous, deep, defended, deific, deified, deliberated, delicate, delightful, delivered, designed, desired, detail, detailed, developed, devout, diaphanous, dignified, directed, discovered, dispatched, displayed, distilled, distinct, distinctive, distinguished, diverse, divine, dramatic, draped, dreamed, dreamlike, dreamy, driven, drop-dead, dynamic, earnest, eased, ecstatic, edified, educated, effective, effervescent, effulgent, elaborate, elated, electrifying, elegant, elevated, elite, embellished, eminent, emphatic, empowering, enchanting, encouraged, encrusted, endearing, endeavored, endorsed, endowed, enduring, energetic, energized, engaging, enlightened, enlivened, enormous, enrapturing, enthralling, enticing, entrancing, envisioned, epic, esteemed, eternal, ethereal, eulogized, euphoric, evocative, evolved, exalted, examining, excellent, exceptional, exciting, exclusive, expansive, expedited, explorative, exposed, expressive, exquisite, extolled, extraordinary, extremely, exuberant, eye-catching, fair, fairy-tale-like, fancy, fantasied, fantastic, fantastical, fascinating, fashionable, fashioned, favorable, favored, fearless, feathery, fermented, fervent, fetched, fetching, fiery, fine, finest, firm, first-class, first-rate, fixed, flamboyant, flaming, flashing, flashy, flattered, flavored, flawless, flimsy, florid, flowery, focused, formal, formed, fortunate, forward-thinking, fostered, found, foxy, fragranced, fresh, fried, fulgent, futuristic, galactic, gallant, garbed, gargantuan, garnished, gemmed, gentle, gentlemanly, genuine, gifted, gigantic, given, glamorous, gleaming, gleamy, glistening, glittering, glittery, glitzy, glorified, glossy, glowing, godly, gorgeous, gossamer, graceful, grand, granted, grateful, gratified, grilled, guarded, guided, hailed, hallowed, handsome, hardy, harmonious, harmonized, hastened, head-turning, heartened, heartfelt, heavenly, heroic, hoisted, holistic, holy, honored, hoped, hopeful, huggable, hushed, hypnotic, ideal, idyllic, illumination, illustrative, imagined, imagistic, immaculate, immense, immortal, impeccable, imposing, impressive, incandescent, infinite, informed, innocent, innovated, innovative, inproportion, inquisitive, inshape, insistent, inspired, inspiring, instructed, integrated, intense, intoxicating, intrepid, intricate, invaluable, invented, investigative, invigorated, invigorating, inviting, iridescent, jaunty, jaw-dropping, jazzy, jeweled, joined, joyful, joyous, jubilant, kingly, kissable, kneaded, knightly, knockout, ladylike, lambent, lasting, laudable, lauded, lavish, leading-edge, led, legendary, lifelike, lifted, light, lightened, likable, limited, linked, lionized, lithe, lively, located, logical, longed, lordly, lovable, loved, lovely, lucent, lucid, lucky, luminous, lush, lustrous, luxurious, luxury, magical, magnificent, majestic, mammoth, marked, marvelous, massive, matched, matchless, matured, meditated, mellow, mentored, merged, merry, mesmerizing, methodical, mind-blowing, modern, modified, modish, molded, monarchical, monstrous, monumental, motivated, motivational, moved, multifaceted, multifarious, mused, mystical, mythical, naive, natty, navigated, neat, new, nifty, noble, notable, noteworthy, novel, nuanced, nubile, nurtured, odorous, offered, opalescent, optimal, optimistic, opulent, orderly, organized, original, originated, ornate, ostentatious, otherworldly, outfitted, outstanding, overjoyed, pacified, painterly, paired, panoramic, paradisiacal, passionate, patterned, peaceful, peerless, peppy, perfect, perfumed, perky, perpetual, persistent, photogenic, pickled, pictorial, picturesque, piloted, pious, placed, planned, pleased, pleasing, plotted, plush, plushy, poached, polished, pondered, posh, positive, praised, praiseworthy, precious, precise, preeminent, premier, premium, prepossessing, presented, preserved, pretty, priceless, prime, princely, pristine, probing, prodigious, profound, progressive, prominent, promoted, pronounced, propelled, proportional, protected, provided, pulchritudinous, pungent, pure, pursued, pushed, quality, queenly, questioning, quickened, quiet, quintessential, racy, radiant, rare, rational, ravishing, real, reborn, recharged, reclaimed, recognized, recovered, redolent, reenergized, refined, reflected, refreshed, refreshing, refulgent, regal, regenerated, reinvigorated, rejuvenated, rejuvenating, related, relaxed, relentless, relieved, remarkable, renewed, representative, rescued, resilient, resolute, respected, resplendent, restored, retrieved, revealed, revered, reverent, revitalized, revitalizing, revived, rewarded, rhythmic, rich, ripened, ritzy, roasted, robed, robust, rococo, romantic, royal, ruminated, sacred, saintly, salient, saluted, sanctified, satisfied, saturated, saved, scenic, scented, schemed, scientific, sculpted, second-to-none, select, seraphic, serene, serious, set, shaped, shapely, shielded, shimmering, shining, shiny, shipshape, show-stopping, shown, showy, sightly, significant, silent, silky, simmered, sincere, singular, situated, sleek, slick, smart, smelly, snazzy, snug, snuggly, solemn, solid, soothed, soothing, sophisticated, sought, soulful, spangled, sparkling, special, sped, spellbinding, spiced, spiffy, spirited, spiritual, splashy, splendid, sprightly, spruce, spunky, stable, stagey, starry, stately, statuesque, steamed, steered, stewed, stimulated, stimulating, stirred, striking, strong, structured, studded, stunning, stupendous, sturdy, stylish, sublime, sumptuous, sunny, superb, superior, superlative, supplied, supported, supreme, sure, surreal, swank, swanky, sweet, sweetened, swish, symbolic, symmetrical, synchronized, systematic, tailored, taking, tangy, tantalizing, targeted, tasteful, taught, tenacious, tender, thankful, theatrical, thought, thrilled, thrilling, tidy, timeless, titanic, toasted, top-drawer, top-notch, top-tier, topnotch, tough, trained, tranquil, tranquilized, transcendent, transformed, translucent, transparent, transported, treasured, trendy, tried, trim, true, twinkling, twinned, unblemished, uncluttered, uncovered, unequaled, unfeigned, unique, united, universal, unmatched, unparalleled, unrivaled, unsullied, unsurpassed, unyielding, up-to-date, upheld, uplifted, uplifting, valiant, valued, varied, velvety, venerated, venturesome, vibrant, vinted, virginal, visualized, vitalized, vivacious, vivid, vivified, voguish, warm, well-built, well-formed, well-kept, well-proportioned, well-turned, whimsical, whisked, whole, winning, winsome, wished, wisp-like, wonderful, wondrous, wooed, worshiped, worshipped, yearned, zesty, zippy, sharp, focus, background diff --git a/modules/expansion.py b/modules/expansion.py index 74dabd6..daa6d5a 100644 --- a/modules/expansion.py +++ b/modules/expansion.py @@ -1,3 +1,4 @@ +import os import torch import math import fcbh.model_management as model_management @@ -9,33 +10,6 @@ from fcbh.model_patcher import ModelPatcher # limitation of np.random.seed(), called from transformers.set_seed() SEED_LIMIT_NUMPY = 2**32 -neg_inf = - 8192.0 - - -preparation_templates = [ - '{prompt}, extremely detailed, ', - # '{prompt}, intricate, ', -] - -dangrous_patterns = '[]【】()()|::' - -black_list = ['art', 'digital', 'paint', 'painting', 'painted', 'drawing', 'draw', 'drawn', - 'concept', 'illustration', 'illustrated', 'illustrate', - 'face', 'faces', 'eye', 'eyes', 'hand', 'hands', 'head', 'heads', 'leg', 'legs', 'arm', 'arms', - 'shoulder', 'shoulders', 'body', 'facial', 'skin', 'character', 'human', - 'portrait', 'portraits', 'port', 'cloth', - 'monster', 'artistic', 'oil', 'brush', 'ugly', 'ug', - 'artwork', 'artworks', 'pencil', 'line', 'sketch', 'cartoon', 'white', 'black', 'red', - 'skeletal', 'skeleton', 'a', 'the', 'background', 'blur', 'blurred', 'depth', 'no', 'of', - 'catdog', 'cat', 'fur', - 'mugshot', 'selfie', - '!', '!!', '!!!', '!!!!', '!!!!!', '!!!!!!', '!!!!!!!', '-', '(', ')', ':', '”', '"', '.'] - -black_list = black_list + [k.upper() for k in black_list] + [k.capitalize() for k in black_list] -black_list.remove('Art') -black_list.remove('ART') - -black_list = black_list + ['Ġ' + k for k in black_list] def safe_str(x): @@ -54,11 +28,21 @@ def remove_pattern(x, pattern): class FooocusExpansion: def __init__(self): self.tokenizer = AutoTokenizer.from_pretrained(fooocus_expansion_path) - self.vocab = self.tokenizer.vocab - self.logits_bias = torch.zeros((1, len(self.vocab)), dtype=torch.float32) - for k, v in self.vocab.items(): - if k in black_list: - self.logits_bias[0, v] = neg_inf + + positive_words = open(os.path.join(fooocus_expansion_path, 'positive.txt'), encoding='utf-8').read() + positive_words = positive_words.lower().replace(' ', '').replace('\n', '').split(',') + + # print(', '.join(sorted(list(set(positive_words))))) + + # t198 = self.tokenizer('\n', return_tensors="np") + # t11 = self.tokenizer(',', return_tensors="np") + # positive_ids = [11, 198, self.tokenizer.eos_token_id] + positive_ids = [11] + + self.bad_words_ids = [] + for k, v in self.tokenizer.vocab.items(): + if k.replace('Ġ', '') not in positive_words and v not in positive_ids: + self.bad_words_ids.append([v]) self.model = AutoModelForCausalLM.from_pretrained(fooocus_expansion_path) self.model.eval() @@ -79,10 +63,6 @@ class FooocusExpansion: self.patcher = ModelPatcher(self.model, load_device=load_device, offload_device=offload_device) print(f'Fooocus Expansion engine loaded for {load_device}, use_fp16 = {use_fp16}.') - def logits_processor(self, input_ids, scores): - self.logits_bias = self.logits_bias.to(scores) - return scores + self.logits_bias - def __call__(self, prompt, seed): if prompt == '': return '' @@ -93,8 +73,7 @@ class FooocusExpansion: seed = int(seed) % SEED_LIMIT_NUMPY set_seed(seed) - prompt = safe_str(prompt) - prompt = preparation_templates[seed % len(preparation_templates)].replace('{prompt}', prompt) + prompt = safe_str(prompt) + ',' tokenized_kwargs = self.tokenizer(prompt, return_tensors="pt") tokenized_kwargs.data['input_ids'] = tokenized_kwargs.data['input_ids'].to(self.patcher.load_device) @@ -104,19 +83,15 @@ class FooocusExpansion: max_token_length = 75 * int(math.ceil(float(current_token_length) / 75.0)) max_new_tokens = max_token_length - current_token_length - logits_processor = LogitsProcessorList([self.logits_processor]) - # https://huggingface.co/blog/introducing-csearch # https://huggingface.co/docs/transformers/generation_strategies features = self.model.generate(**tokenized_kwargs, - num_beams=1, + top_k=100, max_new_tokens=max_new_tokens, do_sample=True, - logits_processor=logits_processor) + bad_words_ids=self.bad_words_ids) response = self.tokenizer.batch_decode(features, skip_special_tokens=True) + result = safe_str(response[0]) - result = response[0] - result = safe_str(result) - result = remove_pattern(result, dangrous_patterns) return result