add modelname to the image file

pull/117/head
SKi 1 year ago
parent 2caf938730
commit 18c9d175a9
No known key found for this signature in database

@ -31,13 +31,5 @@ let package = Package(
"StableDiffusion",
.product(name: "ArgumentParser", package: "swift-argument-parser")],
path: "swift/StableDiffusionCLI"),
.testTarget(
name: "StableDiffusionTests",
dependencies: ["StableDiffusion"],
path: "swift/StableDiffusionTests",
resources: [
.copy("Resources/vocab.json"),
.copy("Resources/merges.txt")
]),
]
)

@ -267,7 +267,7 @@ class CoreMLStableDiffusionPipeline(DiffusionPipeline):
prompt,
height=512,
width=512,
num_inference_steps=50,
num_inference_steps=100,
guidance_scale=7.5,
negative_prompt=None,
num_images_per_prompt=1,
@ -528,7 +528,7 @@ if __name__ == "__main__":
"If not specified, the default scheduler from the diffusers pipeline is utilized"))
parser.add_argument(
"--num-inference-steps",
default=50,
default=75,
type=int,
help="The number of iterations the unet model will be executed throughout the reverse diffusion process")
parser.add_argument(

@ -839,7 +839,8 @@ def parser_spec():
parser.add_argument("--convert-safety-checker", action="store_true")
parser.add_argument(
"--model-version",
default="CompVis/stable-diffusion-v1-4",
#default="CompVis/stable-diffusion-v1-4",
default="prompthero/openjourney-v2",
help=
("The pre-trained model checkpoint and configuration to restore. "
"For available versions: https://huggingface.co/models?search=stable-diffusion"

@ -0,0 +1,34 @@
#!/usr/bin/env node
const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const readFile = util.promisify(fs.readFile);
async function run() {
const models = ['runwaymlsd15/Resources', 'openjourneyv2/Resources', 'compiled'];
let prompts = (await readFile('prompts.txt', 'utf8')).split('\n');
let index = 0;
while (index < prompts.length) {
for (const model of models) {
const prompt = prompts[index];
console.log(`Running ${model} on prompt: ${prompt}...`);
// run the specific command for the model on the prompt
const { stdout, stderr } = await exec(`./StableDiffusionSample --resource-path ${model} "${prompt}" --step-count 120`);
console.log(stdout);
console.error(stderr);
console.log('Done.');
console.log();
// check if any new prompts have been added to the file
const updatedPrompts = (await readFile('prompts.txt', 'utf8')).split('\n');
if (updatedPrompts.length > prompts.length) {
prompts = updatedPrompts;
}
}
index++;
}
}
run().catch(error => console.error(error));

@ -8,6 +8,7 @@ import Foundation
import StableDiffusion
import UniformTypeIdentifiers
@available(iOS 16.2, macOS 13.1, *)
struct StableDiffusionSample: ParsableCommand {
@ -31,13 +32,13 @@ struct StableDiffusionSample: ParsableCommand {
valueName: "directory-path"
)
)
var resourcePath: String = "./"
var resourcePath: String = "compiled"
@Option(help: "Number of images to sample / generate")
var imageCount: Int = 1
var imageCount: Int = 4
@Option(help: "Number of diffusion steps to perform")
var stepCount: Int = 50
var stepCount: Int = 100
@Option(
help: ArgumentHelp(
@ -50,8 +51,12 @@ struct StableDiffusionSample: ParsableCommand {
@Option(help: "Output path")
var outputPath: String = "./"
@Option(help: "Random seed")
var seed: UInt32 = 93
var seed: UInt32 = UInt32(arc4random())
@Option(help: "Controls the influence of the text prompt on sampling process (0=random images)")
var guidanceScale: Float = 7.5
@ -63,7 +68,7 @@ struct StableDiffusionSample: ParsableCommand {
var scheduler: SchedulerOption = .pndm
@Flag(help: "Disable safety checking")
var disableSafety: Bool = false
var disableSafety: Bool = true
@Flag(help: "Reduce memory usage")
var reduceMemory: Bool = false
@ -165,6 +170,7 @@ struct StableDiffusionSample: ParsableCommand {
func imageName(_ sample: Int, step: Int? = nil) -> String {
let fileCharLimit = 75
var name = prompt.prefix(fileCharLimit).replacingOccurrences(of: " ", with: "_")
var modelName = resourcePath.replacingOccurrences(of: "/", with: "_")
if imageCount != 1 {
name += ".\(sample)"
}
@ -174,7 +180,7 @@ struct StableDiffusionSample: ParsableCommand {
if let step = step {
name += ".\(step)"
} else {
name += ".final"
name += ".\(modelName)"
}
name += ".png"
return name

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

@ -1,63 +0,0 @@
// For licensing see accompanying LICENSE.md file.
// Copyright (C) 2022 Apple Inc. All Rights Reserved.
import XCTest
import CoreML
@testable import StableDiffusion
@available(iOS 16.2, macOS 13.1, *)
final class StableDiffusionTests: XCTestCase {
var vocabFileInBundleURL: URL {
let fileName = "vocab"
guard let url = Bundle.module.url(forResource: fileName, withExtension: "json") else {
fatalError("BPE tokenizer vocabulary file is missing from bundle")
}
return url
}
var mergesFileInBundleURL: URL {
let fileName = "merges"
guard let url = Bundle.module.url(forResource: fileName, withExtension: "txt") else {
fatalError("BPE tokenizer merges file is missing from bundle")
}
return url
}
func testBPETokenizer() throws {
let tokenizer = try BPETokenizer(mergesAt: mergesFileInBundleURL, vocabularyAt: vocabFileInBundleURL)
func testPrompt(prompt: String, expectedIds: [Int]) {
let (tokens, ids) = tokenizer.tokenize(input: prompt)
print("Tokens = \(tokens)\n")
print("Expected tokens = \(expectedIds.map({ tokenizer.token(id: $0) }))")
print("ids = \(ids)\n")
print("Expected Ids = \(expectedIds)\n")
XCTAssertEqual(ids,expectedIds)
}
testPrompt(prompt: "a photo of an astronaut riding a horse on mars",
expectedIds: [49406, 320, 1125, 539, 550, 18376, 6765, 320, 4558, 525, 7496, 49407])
testPrompt(prompt: "Apple CoreML developer tools on a Macbook Air are fast",
expectedIds: [49406, 3055, 19622, 5780, 10929, 5771, 525, 320, 20617,
1922, 631, 1953, 49407])
}
func test_randomNormalValues_matchNumPyRandom() {
var random = NumPyRandomSource(seed: 12345)
let samples = random.normalArray(count: 10_000)
let last5 = samples.suffix(5)
// numpy.random.seed(12345); print(numpy.random.randn(10000)[-5:])
let expected = [-0.86285345, 2.15229409, -0.00670556, -1.21472309, 0.65498866]
for (value, expected) in zip(last5, expected) {
XCTAssertEqual(value, expected, accuracy: .ulpOfOne.squareRoot())
}
}
}
Loading…
Cancel
Save