Spaces:
Sleeping
Sleeping
initial commit
Browse filesFiles changed (4)
- .gitignoreAdded+34
- app.pyAdded+44
- gen_images.pyAdded+145
- requirements.txtAdded+7
12345678910111213141516171819202122232425262728293031323334# Ignore outputs (generated images)outputs/*.png
# Ignore model weights (handled separately, e.g. via Git LFS or manual upload)model/*.pkl
# Python cache__pycache__/*.py[cod]*.so
# Jupyter notebooks checkpoint.ipynb_checkpoints/
# System files.DS_StoreThumbs.db
# Environment or build artifactsenv/venv/*.egg-info/dist/build/.cache/*.log
# VSCode and IDE settings.vscode/.idea/
# Hugging Face specific*.sagemaker/1234567891011121314151617181920212223242526272829303132333435363738394041424344import gradio as grimport subprocessimport osimport randomfrom PIL import Image
# PathsOUTPUT_DIR = "outputs"MODEL_PATH = "blazer_model.pkl" # Adjusted for local Hugging Face repo structure
# Ensure the output directory existsos.makedirs(OUTPUT_DIR, exist_ok=True)
# Function to generate images using StyleGAN3def generate_images(): command = f"python stylegan3/gen_images.py --outdir={OUTPUT_DIR} --trunc=1 --seeds='3-5,7,9,12-14,16-26,29,31,32,34,40,41' --network={MODEL_PATH}" try: subprocess.run(command, shell=True, check=True) except subprocess.CalledProcessError as e: return f"Error generating images: {e}"
# Function to select 5 random imagesdef get_random_images(): image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")] if len(image_files) < 10: generate_images() image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")] random_images = random.sample(image_files, min(10, len(image_files))) return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images]
# Gradio functiondef generate_and_display(): generate_images() return get_random_images()
# UIwith gr.Blocks() as demo: gr.Markdown("# 🎨 AI-Generated Clothing Designs - Blazers") generate_button = gr.Button("Generate New Designs") output_gallery = gr.Gallery(label="Generated Designs", columns=5, rows=2) generate_button.click(fn=generate_and_display, outputs=output_gallery)
if __name__ == "__main__": demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145# Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.## NVIDIA CORPORATION and its licensors retain all intellectual property# and proprietary rights in and to this software, related documentation# and any modifications thereto. Any use, reproduction, disclosure or# distribution of this software and related documentation without an express# license agreement from NVIDIA CORPORATION is strictly prohibited.
"""Generate images using pretrained network pickle."""
import osimport refrom typing import List, Optional, Tuple, Union
import clickimport dnnlibimport numpy as npimport PIL.Imageimport torch
import legacy
#----------------------------------------------------------------------------
def parse_range(s: Union[str, List]) -> List[int]: '''Parse a comma separated list of numbers or ranges and return a list of ints.
Example: '1,2,5-10' returns [1, 2, 5, 6, 7] ''' if isinstance(s, list): return s ranges = [] range_re = re.compile(r'^(\d+)-(\d+)$') for p in s.split(','): m = range_re.match(p) if m: ranges.extend(range(int(m.group(1)), int(m.group(2))+1)) else: ranges.append(int(p)) return ranges
#----------------------------------------------------------------------------
def parse_vec2(s: Union[str, Tuple[float, float]]) -> Tuple[float, float]: '''Parse a floating point 2-vector of syntax 'a,b'.
Example: '0,1' returns (0,1) ''' if isinstance(s, tuple): return s parts = s.split(',') if len(parts) == 2: return (float(parts[0]), float(parts[1])) raise ValueError(f'cannot parse 2-vector {s}')
#----------------------------------------------------------------------------
def make_transform(translate: Tuple[float,float], angle: float): m = np.eye(3) s = np.sin(angle/360.0*np.pi*2) c = np.cos(angle/360.0*np.pi*2) m[0][0] = c m[0][1] = s m[0][2] = translate[0] m[1][0] = -s m[1][1] = c m[1][2] = translate[1] return m
#----------------------------------------------------------------------------
@click.command()@click.option('--network', 'network_pkl', help='Network pickle filename', required=True)@click.option('--seeds', type=parse_range, help='List of random seeds (e.g., \'0,1,4-6\')', required=True)@click.option('--trunc', 'truncation_psi', type=float, help='Truncation psi', default=1, show_default=True)@click.option('--class', 'class_idx', type=int, help='Class label (unconditional if not specified)')@click.option('--noise-mode', help='Noise mode', type=click.Choice(['const', 'random', 'none']), default='const', show_default=True)@click.option('--translate', help='Translate XY-coordinate (e.g. \'0.3,1\')', type=parse_vec2, default='0,0', show_default=True, metavar='VEC2')@click.option('--rotate', help='Rotation angle in degrees', type=float, default=0, show_default=True, metavar='ANGLE')@click.option('--outdir', help='Where to save the output images', type=str, required=True, metavar='DIR')def generate_images( network_pkl: str, seeds: List[int], truncation_psi: float, noise_mode: str, outdir: str, translate: Tuple[float,float], rotate: float, class_idx: Optional[int]): """Generate images using pretrained network pickle.
Examples:
\b # Generate an image using pre-trained AFHQv2 model ("Ours" in Figure 1, left). python gen_images.py --outdir=out --trunc=1 --seeds=2 \\ --network=https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-r-afhqv2-512x512.pkl
\b # Generate uncurated images with truncation using the MetFaces-U dataset python gen_images.py --outdir=out --trunc=0.7 --seeds=600-605 \\ --network=https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-t-metfacesu-1024x1024.pkl """
print('Loading networks from "%s"...' % network_pkl) device = torch.device('cuda') with dnnlib.util.open_url(network_pkl) as f: G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
os.makedirs(outdir, exist_ok=True)
# Labels. label = torch.zeros([1, G.c_dim], device=device) if G.c_dim != 0: if class_idx is None: raise click.ClickException('Must specify class label with --class when using a conditional network') label[:, class_idx] = 1 else: if class_idx is not None: print ('warn: --class=lbl ignored when running on an unconditional network')
# Generate images. for seed_idx, seed in enumerate(seeds): print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds))) z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
# Construct an inverse rotation/translation matrix and pass to the generator. The # generator expects this matrix as an inverse to avoid potentially failing numerical # operations in the network. if hasattr(G.synthesis, 'input'): m = make_transform(translate, rotate) m = np.linalg.inv(m) G.synthesis.input.transform.copy_(torch.from_numpy(m))
img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode) img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8) PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save(f'{outdir}/seed{seed:04d}.png')
#----------------------------------------------------------------------------
if __name__ == "__main__": generate_images() # pylint: disable=no-value-for-parameter
#----------------------------------------------------------------------------1234567gradio==4.26.0Pillow==10.2.0torch==2.0.1torchvision==0.15.2torchaudio==2.0.2ninja==1.11.1
.gitignoreAdded+34
app.pyAdded+44
gen_images.pyAdded+145
requirements.txtAdded+7