mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-11 11:26:34 +00:00
feat(server): add ADetailer support to image generation (#2164)
This commit is contained in:
1 parent
0518b34374
commit
845646b034
10 files changed
+515
-61
No files matched your search
+28
-10
@@ -291,7 +291,9 @@ static void filter_and_sort_detections(std::vector<ADetailerDetection>* detectio
|
||||
std::partial_sort(detections->begin(),
|
||||
detections->begin() + params.mask_k_largest,
|
||||
detections->end(),
|
||||
[](const auto& a, const auto& b) { return detection_area(a) > detection_area(b); });
|
||||
[&](const auto& a, const auto& b) {
|
||||
return params.mask_filter_by_confidence ? a.confidence > b.confidence : detection_area(a) > detection_area(b);
|
||||
});
|
||||
detections->resize(params.mask_k_largest);
|
||||
}
|
||||
|
||||
@@ -545,7 +547,16 @@ static sd_image_t resize_crop_mask(const Mask& source, const CropRegion& crop, i
|
||||
return output;
|
||||
}
|
||||
|
||||
static sd_image_t copy_as_rgb(const sd_image_t& source) {
|
||||
static sd_image_t copy_for_detailing(const sd_image_t& source) {
|
||||
if (source.channel == 4) {
|
||||
sd_image_t output = source;
|
||||
size_t bytes = static_cast<size_t>(source.width) * source.height * source.channel;
|
||||
output.data = static_cast<uint8_t*>(malloc(bytes));
|
||||
if (output.data != nullptr) {
|
||||
memcpy(output.data, source.data, bytes);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
CropRegion full{0, 0, static_cast<int>(source.width), static_cast<int>(source.height)};
|
||||
return resize_crop_image(source, full, source.width, source.height);
|
||||
}
|
||||
@@ -566,7 +577,7 @@ static void composite_crop(sd_image_t* destination,
|
||||
continue;
|
||||
}
|
||||
float source_x = (x - crop.x1 + 0.5f) * scale_x - 0.5f;
|
||||
size_t destination_index = (static_cast<size_t>(y) * destination->width + x) * 3;
|
||||
size_t destination_index = (static_cast<size_t>(y) * destination->width + x) * destination->channel;
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
float generated_value = bilinear_channel(generated, source_x, source_y, c) * 255.f;
|
||||
float original_value = destination->data[destination_index + c];
|
||||
@@ -787,6 +798,10 @@ static bool parse_adetailer_params(const sd_adetailer_params_t& public_params,
|
||||
ok = parse_int(value, ¶ms->max_detections);
|
||||
} else if (key == "mask_k_largest") {
|
||||
ok = parse_int(value, ¶ms->mask_k_largest);
|
||||
} else if (key == "mask_filter_method") {
|
||||
const std::string method = lower_copy(value);
|
||||
ok = method == "area" || method == "confidence";
|
||||
params->mask_filter_by_confidence = method == "confidence";
|
||||
} else if (key == "mask_min_ratio") {
|
||||
ok = parse_float(value, ¶ms->mask_min_ratio);
|
||||
} else if (key == "mask_max_ratio") {
|
||||
@@ -805,6 +820,8 @@ static bool parse_adetailer_params(const sd_adetailer_params_t& public_params,
|
||||
ok = parse_int(value, ¶ms->mask_blur);
|
||||
} else if (key == "inpaint_padding") {
|
||||
ok = parse_int(value, ¶ms->inpaint_padding);
|
||||
} else if (key == "inpaint_only_masked") {
|
||||
ok = parse_bool(value, ¶ms->inpaint_only_masked);
|
||||
} else if (key == "inpaint_width") {
|
||||
ok = parse_int(value, ¶ms->inpaint_width);
|
||||
} else if (key == "inpaint_height") {
|
||||
@@ -920,7 +937,7 @@ bool adetail_image(adetailer_ctx_t* context,
|
||||
filter_and_sort_detections(&detections, input_image.width, input_image.height, params);
|
||||
auto masks = make_masks(detections, input_image.width, input_image.height, params);
|
||||
|
||||
sd_image_t current = copy_as_rgb(input_image);
|
||||
sd_image_t current = copy_for_detailing(input_image);
|
||||
if (current.data == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -939,12 +956,13 @@ bool adetail_image(adetailer_ctx_t* context,
|
||||
if (!mask_bbox(masks[i], &crop)) {
|
||||
continue;
|
||||
}
|
||||
crop = expand_crop(crop,
|
||||
current.width,
|
||||
current.height,
|
||||
params.inpaint_padding,
|
||||
params.inpaint_width,
|
||||
params.inpaint_height);
|
||||
crop = params.inpaint_only_masked ? expand_crop(crop,
|
||||
current.width,
|
||||
current.height,
|
||||
params.inpaint_padding,
|
||||
params.inpaint_width,
|
||||
params.inpaint_height)
|
||||
: CropRegion{0, 0, static_cast<int>(current.width), static_cast<int>(current.height)};
|
||||
sd_image_t local_image = resize_crop_image(current, crop, params.inpaint_width, params.inpaint_height);
|
||||
sd_image_t local_mask = resize_crop_mask(masks[i], crop, params.inpaint_width, params.inpaint_height);
|
||||
if (local_image.data == nullptr || local_mask.data == nullptr) {
|
||||
|
||||
+26
-24
@@ -27,30 +27,32 @@ enum ADetailerSort {
|
||||
};
|
||||
|
||||
struct ADetailerParams {
|
||||
const char* prompt = nullptr;
|
||||
const char* negative_prompt = nullptr;
|
||||
int input_size = 640;
|
||||
float confidence = 0.3f;
|
||||
float nms_threshold = 0.45f;
|
||||
int max_detections = 100;
|
||||
int mask_k_largest = 0;
|
||||
float mask_min_ratio = 0.f;
|
||||
float mask_max_ratio = 1.f;
|
||||
int dilate_erode = 4;
|
||||
int x_offset = 0;
|
||||
int y_offset = 0;
|
||||
bool merge_masks = false;
|
||||
bool invert_mask = false;
|
||||
int mask_blur = 4;
|
||||
int inpaint_padding = 32;
|
||||
int inpaint_width = 512;
|
||||
int inpaint_height = 512;
|
||||
float denoising_strength = 0.4f;
|
||||
int steps = 0;
|
||||
float cfg_scale = -1.f;
|
||||
sample_method_t sample_method = SAMPLE_METHOD_COUNT;
|
||||
scheduler_t scheduler = SCHEDULER_COUNT;
|
||||
ADetailerSort sort_by = ADETAILER_SORT_NONE;
|
||||
const char* prompt = nullptr;
|
||||
const char* negative_prompt = nullptr;
|
||||
int input_size = 640;
|
||||
float confidence = 0.3f;
|
||||
float nms_threshold = 0.45f;
|
||||
int max_detections = 100;
|
||||
int mask_k_largest = 0;
|
||||
bool mask_filter_by_confidence = false;
|
||||
float mask_min_ratio = 0.f;
|
||||
float mask_max_ratio = 1.f;
|
||||
int dilate_erode = 4;
|
||||
int x_offset = 0;
|
||||
int y_offset = 0;
|
||||
bool merge_masks = false;
|
||||
bool invert_mask = false;
|
||||
int mask_blur = 4;
|
||||
int inpaint_padding = 32;
|
||||
bool inpaint_only_masked = true;
|
||||
int inpaint_width = 512;
|
||||
int inpaint_height = 512;
|
||||
float denoising_strength = 0.4f;
|
||||
int steps = 0;
|
||||
float cfg_scale = -1.f;
|
||||
sample_method_t sample_method = SAMPLE_METHOD_COUNT;
|
||||
scheduler_t scheduler = SCHEDULER_COUNT;
|
||||
ADetailerSort sort_by = ADETAILER_SORT_NONE;
|
||||
};
|
||||
|
||||
struct ADetailerGGML {
|
||||
|
||||
Reference in new issue
Block a user