Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Generate-img2img {
- Param(
- [Parameter(Mandatory=$true,HelpMessage="Prompt to an image of",Position=0)] $Prompt,
- [Parameter(Mandatory=$true,HelpMessage="Path to input image",Position=1)] $imgpath,
- [Parameter(Mandatory=$false,HelpMessage="Image Width divisible by 32")] [Int]$W,
- [Parameter(Mandatory=$false,HelpMessage="Image Height divisible by 32")] [Int]$H,
- [Parameter(Mandatory=$false,HelpMessage="how closely the AI follows your prompt")] $Scale,
- [Parameter(Mandatory=$false,HelpMessage="specifies how many times to run the sampling loop")] [Int]$Iterations,
- [Parameter(Mandatory=$false,HelpMessage="specifies directory images will be saved to")] [String[]]$outdir,
- [Parameter(Mandatory=$false,HelpMessage="specifies the seed used for results. This allows for reproducible results")] [Int]$seed,
- [Parameter(Mandatory=$false,HelpMessage="specifies the aspect ratio. 1:1 is default. Acceptable values are 1:1, 3:2, 2:3, 16:9, and 9:16. This will override Width and Height of the image or entered")] [String[]]$ar,
- [Parameter(Mandatory=$false,HelpMessage="specifies the number of sampling steps in the Diffusion process. Increasing this number will increase computation time but may improve results.")] [Int]$steps,
- [Parameter(Mandatory=$false,HelpMessage="Use optional parameters not defined here. Things like `"--plms --ckpt --precision --f --C etc`"")] $opt
- )
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- $img = [System.Drawing.Image]::FromFile($imgpath)
- if (!$W) { [int]$W = $img.width }
- if (!$H) { [int]$H = $img.height }
- if (!$Scale) { $Scale = '7.5' }
- if (!$Iterations) { [int]$Iterations = '4' }
- if (!$outdir) { $outdir = "E:\stable-diffusion-main\outputs\img2img" } ## Hardcoded path - Please ensure this matches your environment
- if (!$seed) { $seed = Get-Random -Minimum 1 -Maximum 999999 }
- if (!$steps) { $steps = '50' }
- if ($ar -eq '1:1') { [int]$W = '512'; [int]$H = '512' }
- if ($ar -eq '3:2') { [int]$W = '768'; [int]$H = '512' }
- if ($ar -eq '2:3') { [int]$W = '512'; [int]$H = '768' }
- if ($ar -eq '16:9') { [int]$W = '768'; [int]$H = '448' }
- if ($ar -eq '9:16') { [int]$W = '448'; [int]$H = '768' }
- $dir = "E:\stable-diffusion-main" ## Root path for your Stable Diffusion instance - Please ensure this matches your environment
- $currentPath = get-location
- set-location $dir
- if (!((get-module).name -match "Conda")) {
- write-host "Loading Miniconda PowerShell module"
- if (test-path "C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1") { & 'C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1' }
- else { & "$env:userprofile\Miniconda3\shell\condabin\conda-hook.ps1" }
- conda activate ldm ## Conda instance name - Please ensure this matches your environment
- }
- else { write-host "Conda module already loaded`n`n;)`n`n" }
- $starttime = get-date
- $rundeets = "Prompt: `"$prompt`"`nStarting Image: $imgpath`nHeight: $H`nWidth: $W`nSeed: $seed`nIterations $Iterations`nSteps: $steps`nScale: $Scale"
- write-output "Processing text. Images will be saved somewhere here: `"$outdir`"`n$rundeets"
- ## Python script to use can be updated below. This instance is using the optimizedSD scripts from this repo: https://github.com/basujindal/stable-diffusion
- python $dir\optimizedSD\optimized_img2img.py --prompt "$prompt" --init-img "$imgpath" --outdir "$outdir" --W $W --H $H --seed $seed --n_iter $Iterations --ddim_steps $steps --n_samples 1 --scale $scale --skip_grid $opt
- write-output "Process complete. Recap:`nImages will be saved somewhere here: `"$outdir`"`n$rundeets"
- $newimg = gci "$outdir\samples" | where { $_.creationTime -gt $starttime }
- if (!(Test-path "$outdir\runbook_img2img.csv")) {
- $logbook = {} | Select "InitialImage","Prompt","Width","Height","Scale","Iterations","Seed","Steps","GeneratedImage" | Export-Csv "$outdir\runbook_img2img.csv" -NoTypeInformation
- Get-Content "$outdir\runbook_img2img.csv" | Where { $_.Replace(",","") -ne "" } | Out-File "$outdir\runbook_img2img.csv"
- }
- $tempbook = {} | Select "InitialImage","Prompt","Width","Height","Scale","Iterations","Seed","Steps","GeneratedImage" | Export-Csv "$env:temp\runbook_img2img.csv" -NoTypeInformation
- $runbook = Import-Csv "$env:temp\runbook_img2img.csv"
- $newimg | % {
- $runbook.InitialImage = $imgpath
- $runbook.Prompt = $Prompt
- $runbook.Width = $W
- $runbook.Height = $H
- $runbook.Scale = $Scale
- $runbook.Iterations = $Iterations
- $runbook.Seed = $seed
- $runbook.Steps = $steps
- $runbook.GeneratedImage = $_.fullname
- $runbook | export-csv "$outdir\runbook_img2img.csv" -Append -NoTypeInformation
- $seed++
- }
- set-location $currentPath
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement