forked from ml-explore/mlx-swift-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageTool.swift
278 lines (218 loc) · 8.68 KB
/
ImageTool.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright © 2024 Apple Inc.
import ArgumentParser
import Foundation
import MLX
import Progress
import StableDiffusion
@main
struct ImageTool: AsyncParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Command line tool for working with images and MLX",
subcommands: [StableDiffusionTool.self])
}
struct StableDiffusionTool: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "sd",
abstract: "Stable diffusion related commands",
subcommands: [TextToImageCommand.self, ImageToImageCommand.self]
)
}
#if swift(>=5.10)
extension StableDiffusionConfiguration.Preset: @retroactive ExpressibleByArgument {}
#else
extension StableDiffusionConfiguration.Preset: ExpressibleByArgument {}
#endif
struct ModelArguments: ParsableArguments, Sendable {
@Option(name: .long, help: "stable diffusion model")
var model: StableDiffusionConfiguration.Preset = .sdxlTurbo
@Flag(name: .long, inversion: .prefixedNo, help: "Disable float16 conversion")
var float16 = true
@Flag(name: .long, help: "Enable quantization")
var quantize = false
var loadConfiguration: LoadConfiguration {
LoadConfiguration(float16: float16, quantize: quantize)
}
func download() async throws -> StableDiffusionConfiguration {
let configuration = model.configuration
var progressBar: ProgressBar?
try await configuration.download { progress in
if progressBar == nil {
let complete = progress.fractionCompleted
if complete < 0.99 {
progressBar = ProgressBar(count: 1000)
if complete > 0 {
print("Resuming download (\(Int(complete * 100))% complete)")
} else {
print("Downloading")
}
print()
}
}
let complete = Int(progress.fractionCompleted * 1000)
progressBar?.setValue(complete)
}
return configuration
}
}
/// Command line arguments for controlling generation of images
struct GenerateArguments: ParsableArguments, Sendable {
@Option(name: .shortAndLong, help: "The message to be processed by the model")
var prompt = "purple cow on the moon"
@Option(name: .shortAndLong, help: "Negative prompt (requires cfg to be > 1)")
var negativePrompt = ""
@Option(name: .long, help: "cfg weight")
var cfg: Float?
@Option(name: .long, help: "number of images")
var imageCount: Int = 1
@Option(name: .long, help: "decoding batch size")
var batchSize: Int = 1
@Option(name: .long, help: "latent width (output size is 8x this value)")
var latentWidth: Int = 64
@Option(name: .long, help: "latent height (output size is 8x this value)")
var latentHeight: Int = 64
@Option(name: .long, help: "number of rows of images in the output")
var rows: Int = 1
@Option(name: .long, help: "number of steps")
var steps: Int?
@Option(name: .long, help: "The PRNG seed")
var seed: UInt64?
func evaluateParameters(configuration: StableDiffusionConfiguration) -> EvaluateParameters {
var parameters = configuration.defaultParameters()
parameters.prompt = prompt
parameters.negativePrompt = negativePrompt
if let cfg {
parameters.cfgWeight = cfg
}
parameters.imageCount = imageCount
parameters.decodingBatchSize = batchSize
parameters.latentSize = [latentHeight, latentWidth]
if let steps {
parameters.steps = steps
}
if let seed {
parameters.seed = seed
}
print("using seed: \(parameters.seed)")
return parameters
}
}
func makeGrid(images: [MLXArray], rows: Int) -> MLXArray {
var x = concatenated(images, axis: 0)
x = padded(x, widths: [[0, 0], [8, 8], [8, 8], [0, 0]])
let (B, H, W, C) = x.shape4
x = x.reshaped(rows, B / rows, H, W, C).transposed(0, 2, 1, 3, 4)
x = x.reshaped(rows * H, B / rows * W, C)
x = (x * 255).asType(.uint8)
return x
}
struct TextToImageCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "text",
abstract: "Text to image command"
)
@Option(name: .long, help: "output image")
var output = URL(filePath: "/tmp/out.png")
@OptionGroup var model: ModelArguments
@OptionGroup var memory: MemoryArguments
@OptionGroup var generate: GenerateArguments
mutating func generateLatents(configuration: StableDiffusionConfiguration) throws -> (
EvaluateParameters, ImageDecoder, MLXArray
) {
// download and prepare the model
guard
let generator = try configuration.textToImageGenerator(
configuration: model.loadConfiguration)
else {
fatalError("Unable to produce TextToImageGenerator from \(configuration.id)")
}
generator.ensureLoaded()
memory.start()
// generate the latents -- these are the iterations for generating
// the output image. this is just generating the evaluation graph
let parameters = generate.evaluateParameters(configuration: configuration)
let latents = generator.generateLatents(parameters: parameters)
// evaluate the latents (evalue the graph) and keep the last value generated
var lastXt: MLXArray!
for xt in Progress(latents) {
eval(xt)
lastXt = xt
}
return (parameters, generator.detachedDecoder(), lastXt)
}
@MainActor
mutating func run() async throws {
let configuration = try await model.download()
let (parameters, decoder, xt) = try generateLatents(configuration: configuration)
var decoded = [MLXArray]()
for i in Progress(
stride(from: 0, to: parameters.imageCount, by: parameters.decodingBatchSize))
{
let image = decoder(xt[i ..< i + parameters.decodingBatchSize])
eval(image)
decoded.append(image)
}
let grid = makeGrid(images: decoded, rows: generate.rows)
try Image(grid).save(url: output)
}
}
struct ImageToImageCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "image",
abstract: "Image to image command"
)
@Option(name: .long, help: "input image")
var input: URL
@Option(name: .long, help: "maximum edge of the input image -- scale to fit this size")
var maxEdge: Int = 1024
@Option(name: .long, help: "output image")
var output = URL(filePath: "/tmp/out.png")
@Option(name: .long, help: "noise strength")
var strength: Float = 0.9
@OptionGroup var model: ModelArguments
@OptionGroup var memory: MemoryArguments
@OptionGroup var generate: GenerateArguments
mutating func generateLatents(configuration: StableDiffusionConfiguration) throws -> (
EvaluateParameters, ImageDecoder, MLXArray
) {
let image = try Image(url: self.input, maximumEdge: maxEdge)
let input = (image.data.asType(.float32) / 255) * 2 - 1
guard
let generator = try configuration.imageToImageGenerator(
configuration: model.loadConfiguration)
else {
fatalError("Unable to produce TextToImageGenerator from \(configuration.id)")
}
generator.ensureLoaded()
memory.start()
// adjust the steps based on the strength
if Int(Float(generate.evaluateParameters(configuration: configuration).steps) * strength)
< 1
{
generate.steps = Int(ceil(1 / strength))
}
let parameters = generate.evaluateParameters(configuration: configuration)
let latents = generator.generateLatents(
image: input, parameters: parameters, strength: strength)
var lastXt: MLXArray!
for xt in Progress(latents) {
eval(xt)
lastXt = xt
}
return (parameters, generator.detachedDecoder(), lastXt)
}
@MainActor
mutating func run() async throws {
let configuration = try await model.download()
let (parameters, decoder, xt) = try generateLatents(configuration: configuration)
var decoded = [MLXArray]()
for i in Progress(
stride(from: 0, to: parameters.imageCount, by: parameters.decodingBatchSize))
{
let image = decoder(xt[i ..< i + parameters.decodingBatchSize])
eval(image)
decoded.append(image)
}
let grid = makeGrid(images: decoded, rows: generate.rows)
try Image(grid).save(url: output)
}
}