mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-09-30 09:58:20 -05:00
feat: add a stand-alone upscale endpoint to the server (#2026)
This commit is contained in:
@@ -1349,9 +1349,8 @@ bool is_first_stage_model_name(const std::string& name) {
|
||||
}
|
||||
|
||||
static std::string convert_esrgan_tensor_name(std::string name) {
|
||||
static std::unordered_map<std::string, std::string> esrgan_name_map;
|
||||
|
||||
if (esrgan_name_map.empty()) {
|
||||
static const auto esrgan_name_map = [] {
|
||||
std::unordered_map<std::string, std::string> esrgan_name_map;
|
||||
esrgan_name_map["model.0."] = "conv_first.";
|
||||
|
||||
constexpr int max_num_blocks = 64;
|
||||
@@ -1377,7 +1376,8 @@ static std::string convert_esrgan_tensor_name(std::string name) {
|
||||
esrgan_name_map["model.7."] = "conv_last.";
|
||||
esrgan_name_map["model.8."] = "conv_hr.";
|
||||
esrgan_name_map["model.10."] = "conv_last.";
|
||||
}
|
||||
return esrgan_name_map;
|
||||
}();
|
||||
|
||||
replace_with_prefix_map(name, esrgan_name_map);
|
||||
return name;
|
||||
|
||||
@@ -262,6 +262,30 @@ int get_upscale_factor(upscaler_ctx_t* upscaler_ctx) {
|
||||
return upscaler_ctx->upscaler->esrgan_upscaler->config.scale;
|
||||
}
|
||||
|
||||
int get_upscaler_model_scale(const char* model_path) {
|
||||
if (model_path == nullptr || model_path[0] == '\0') {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
ModelLoader loader;
|
||||
if (!loader.init_from_file_and_convert_name(model_path, "", VERSION_ESRGAN)) {
|
||||
return 0;
|
||||
}
|
||||
const auto& tensors = loader.get_tensor_storage_map();
|
||||
auto first = tensors.find("conv_first.weight");
|
||||
auto last = tensors.find("conv_last.weight");
|
||||
if (first == tensors.end() || last == tensors.end() ||
|
||||
tensors.count("body.0.rdb1.conv1.weight") == 0 ||
|
||||
first->second.n_dims != 4 || last->second.n_dims != 4 ||
|
||||
first->second.ne[2] != 3 || last->second.ne[3] != 3) {
|
||||
return 0;
|
||||
}
|
||||
return ESRGANConfig::detect_from_weights(tensors).scale;
|
||||
} catch (const std::exception&) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void free_upscaler_ctx(upscaler_ctx_t* upscaler_ctx) {
|
||||
if (upscaler_ctx->upscaler != nullptr) {
|
||||
delete upscaler_ctx->upscaler;
|
||||
|
||||
Reference in New Issue
Block a user