Skip to content

Commit

Permalink
GUIでモデルごとの分割サイズMod制限に引っかかった場合はエラーダイアログを出すようにした resolved #135
Browse files Browse the repository at this point in the history
モデルによっては分割サイズがある数で割り切れないとエラーが起きるのでその対応
  • Loading branch information
lltcggie committed Dec 1, 2018
1 parent 62c0f4b commit c131ca3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
3 changes: 2 additions & 1 deletion bin/lang/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@
"MessageLogFatalError":"A fatal error has occurred.\r\nThere is a possibility the split size is too large",
"IDC_STATIC_BATCH_SIZE":"Batch size",
"MessageBatchSizeCheckError":"Batch size must be greater than 0",
"IDC_RADIO_MODEL_CUNET":"2-D illust (CUnet Model)"
"IDC_RADIO_MODEL_CUNET":"2-D illust (CUnet Model)",
"MessageCropSizeDivisibleCheckError":"Split size of this model must be divisible by %d"
}
3 changes: 2 additions & 1 deletion bin/lang/japanese.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@
"MessageLogFatalError":"致命的なエラーが発生しました。\r\n分割サイズが大きすぎる可能性があります",
"IDC_STATIC_BATCH_SIZE":"バッチサイズ",
"MessageBatchSizeCheckError":"バッチサイズは0より大きい整数である必要があります",
"IDC_RADIO_MODEL_CUNET":"2次元イラスト (CUnet)"
"IDC_RADIO_MODEL_CUNET":"2次元イラスト (CUnet)",
"MessageCropSizeDivisibleCheckError":"このモデルの分割サイズは %d で割り切れる必要があります"
}
2 changes: 1 addition & 1 deletion bin/models/cunet/info.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"name":"CUnet","arch_name":"upcunet","has_noise_scale":true,"has_noise_only":true, "channels":3,
{"name":"CUnet","arch_name":"upcunet","has_noise_scale":true,"has_noise_only":true,"channels":3,"force_divisible_crop_size":4,
"scale_factor":2,"offset":36,
"scale_factor_noise":1,"offset_noise":28
}
2 changes: 2 additions & 0 deletions common/cNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ Waifu2x::eWaifu2xError cNet::GetInfo(const boost::filesystem::path & info_path,
const bool has_noise_scale = d.HasMember("has_noise_scale") && d["has_noise_scale"].GetBool() ? true : false;
const bool has_noise_only = d.HasMember("has_noise_only") && d["has_noise_only"].GetBool() ? true : false;
const int channels = d["channels"].GetInt();
const int force_divisible_crop_size = d.HasMember("force_divisible_crop_size") ? d["force_divisible_crop_size"].GetInt() : 1;

info.name = name;
info.arch_name = arch_name;
info.has_noise_scale = has_noise_scale;
info.has_noise_only = has_noise_only;
info.channels = channels;
info.force_divisible_crop_size = force_divisible_crop_size;

if (d.HasMember("offset"))
{
Expand Down
1 change: 1 addition & 0 deletions common/waifu2x.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Waifu2x
bool has_noise_scale;
bool has_noise_only;
int channels;
int force_divisible_crop_size;

stParam noise;
stParam scale;
Expand Down
77 changes: 52 additions & 25 deletions waifu2x-caffe-gui/MainDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ bool DialogEvent::SyncMember(const bool NotSyncCropSize, const bool silent)
GetWindowText(GetDlgItem(dh, IDC_COMBO_CROP_SIZE), buf, _countof(buf));
buf[_countof(buf) - 1] = TEXT('\0');

Waifu2x::stInfo info;
if (!Waifu2x::GetInfo(model_dir, info))
info.force_divisible_crop_size = 1;

TCHAR *ptr = nullptr;
crop_size = _tcstol(buf, &ptr, 10);
if (!ptr || *ptr != '\0' || crop_size <= 0)
Expand All @@ -462,6 +466,14 @@ bool DialogEvent::SyncMember(const bool NotSyncCropSize, const bool silent)

MessageBox(dh, langStringList.GetString(L"MessageCropSizeCheckError").c_str(), langStringList.GetString(L"MessageTitleError").c_str(), MB_OK | MB_ICONERROR);
}
else if (crop_size % info.force_divisible_crop_size != 0) // このモデルでは設定できないCropSize
{
wchar_t buf[1024] = { TEXT('\0') };
swprintf(buf, langStringList.GetString(L"MessageCropSizeDivisibleCheckError").c_str(), info.force_divisible_crop_size);

ret = false;
MessageBoxW(dh, buf, langStringList.GetString(L"MessageTitleError").c_str(), MB_OK | MB_ICONERROR);
}
}

{
Expand Down Expand Up @@ -557,6 +569,9 @@ void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path)
{
const int n = list[i];

if (n % info.force_divisible_crop_size != 0) // このモデルでは設定できないCropSize
continue;

tstring str(to_tstring(n));
const int index = SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str());

Expand All @@ -575,6 +590,9 @@ void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path)
int defaultListIndex = -1;
for (const auto n : CropSizeList)
{
if (n % info.force_divisible_crop_size != 0) // このモデルでは設定できないCropSize
continue;

tstring str(to_tstring(n));
const int index = SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str());

Expand Down Expand Up @@ -1822,31 +1840,6 @@ void DialogEvent::Create(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData)
SendMessage(hbatch, CB_SETCURSEL, 0, 0);
}

{
HWND hcrop = GetDlgItem(dh, IDC_COMBO_CROP_SIZE);

SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)TEXT("-----------------------"));

// CropSizeListの値を追加していく
int mindiff = INT_MAX;
int defaultListIndex = -1;
for (const auto n : CropSizeList)
{
tstring str(to_tstring(n));
const int index = SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str());

const int diff = abs(DefaultCommonDivisor - n);
if (DefaultCommonDivisorRange.first <= n && n <= DefaultCommonDivisorRange.second && diff < mindiff)
{
mindiff = diff;
defaultListIndex = index;
}
}

if (GetWindowTextLength(hcrop) == 0)
SendMessage(hcrop, CB_SETCURSEL, defaultListIndex, 0);
}

tstring tScaleRatio;
tstring tScaleWidth;
tstring tScaleHeight;
Expand Down Expand Up @@ -2088,6 +2081,40 @@ void DialogEvent::Create(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData)

SendMessage(GetDlgItem(dh, IDC_COMBO_MODEL), CB_SETCURSEL, index, 0);

{
HWND hcrop = GetDlgItem(dh, IDC_COMBO_CROP_SIZE);

SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)TEXT("-----------------------"));

const auto model_dir = ModelPathList[index];

Waifu2x::stInfo info;
if (!Waifu2x::GetInfo(model_dir, info))
info.force_divisible_crop_size = 1;

// CropSizeListの値を追加していく
int mindiff = INT_MAX;
int defaultListIndex = -1;
for (const auto n : CropSizeList)
{
if (n % info.force_divisible_crop_size != 0) // このモデルでは設定できないCropSize
continue;

tstring str(to_tstring(n));
const int index = SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str());

const int diff = abs(DefaultCommonDivisor - n);
if (DefaultCommonDivisorRange.first <= n && n <= DefaultCommonDivisorRange.second && diff < mindiff)
{
mindiff = diff;
defaultListIndex = index;
}
}

if (GetWindowTextLength(hcrop) == 0)
SendMessage(hcrop, CB_SETCURSEL, defaultListIndex, 0);
}

if (use_tta)
SendMessage(GetDlgItem(hWnd, IDC_CHECK_TTA), BM_SETCHECK, BST_CHECKED, 0);
else
Expand Down

0 comments on commit c131ca3

Please sign in to comment.