Skip to content

Commit a2a3b1b

Browse files
committed
fix reset-assets, adjust realsr-ncnn log printing, finetune realsr-ncnn tilesize for Snapdragon
1 parent 8ba4f20 commit a2a3b1b

File tree

6 files changed

+58
-26
lines changed

6 files changed

+58
-26
lines changed

RealSR-NCNN-Android-CLI/RealSR/src/main/jni/main.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ int main(int argc, char **argv)
848848
return -1;
849849
}
850850

851+
std::cout << "build time: " << __DATE__ << " " << __TIME__ << std::endl;
851852

852853
int scales[] = {4, 2, 1, 8};
853854
int sp = 0;
@@ -970,6 +971,8 @@ int main(int argc, char **argv)
970971
}
971972

972973
uint32_t heap_budget = ncnn::get_gpu_device(gpuid[i])->get_heap_budget();
974+
const char* gpu_name = ncnn::get_gpu_info(i).device_name();
975+
const bool is_adreno = nullptr != strstr(gpu_name, "Adreno");
973976

974977
// more fine-grained tilesize policy here
975978
if (model.find(PATHSTR("models-Real-ESRGANv")) != path_t::npos) {
@@ -979,13 +982,17 @@ int main(int argc, char **argv)
979982
tilesize[i] = 200;
980983
else if (heap_budget > 550)
981984
tilesize[i] = 100;
982-
else if (heap_budget > 190)
985+
else if (heap_budget > 200)
983986
tilesize[i] = 64;
984987
else
985988
tilesize[i] = 32;
986989
} else {
987-
if (heap_budget > 2800)
988-
tilesize[i] = 200;
990+
if (heap_budget > 2800) {
991+
if(is_adreno)
992+
tilesize[i] = 160;
993+
else
994+
tilesize[i] = 200;
995+
}
989996
else if (heap_budget > 900)
990997
tilesize[i] = 100;
991998
else if (heap_budget > 300)

RealSR-NCNN-Android-CLI/RealSR/src/main/jni/realsr.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ int RealSR::load(const std::wstring& parampath, const std::wstring& modelpath)
4848
int RealSR::load(const std::string& parampath, const std::string& modelpath)
4949
#endif
5050
{
51-
net.opt.use_vulkan_compute = vkdev ? true : false;
51+
net.opt.use_vulkan_compute = vkdev != nullptr;
5252
net.opt.use_fp16_packed = true;
53-
net.opt.use_fp16_storage = vkdev ? true : false;
53+
net.opt.use_fp16_storage = vkdev != nullptr;
5454
net.opt.use_fp16_arithmetic = false;
5555
net.opt.use_int8_storage = true;
5656

@@ -205,6 +205,8 @@ int RealSR::process(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
205205

206206
const size_t in_out_tile_elemsize = opt.use_fp16_storage ? 2u : 4u;
207207
high_resolution_clock::time_point begin = high_resolution_clock::now();
208+
high_resolution_clock::time_point time_print_progress;
209+
208210

209211
//#pragma omp parallel for num_threads(2)
210212
for (int yi = 0; yi < ytiles; yi++)
@@ -337,10 +339,13 @@ int RealSR::process(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
337339
{
338340
ncnn::Extractor ex = net.create_extractor();
339341

342+
343+
340344
ex.set_blob_vkallocator(blob_vkallocator);
341345
ex.set_workspace_vkallocator(blob_vkallocator);
342346
ex.set_staging_vkallocator(staging_vkallocator);
343347

348+
344349
ex.input("data", in_tile_gpu[ti]);
345350

346351
ex.extract("output", out_tile_gpu[ti], cmd);
@@ -522,16 +527,21 @@ int RealSR::process(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
522527
}
523528
}
524529

525-
if (xtiles > 1)
526-
{
530+
if (xtiles > 1) {
527531
cmd.submit_and_wait();
528532
cmd.reset();
529533
}
530534
high_resolution_clock::time_point end = high_resolution_clock::now();
531-
double time_span = duration_cast<duration<double>>(end - begin).count();
532-
double progress = (float)(yi * xtiles + xi +1) / (ytiles * xtiles);
533-
fprintf(stderr, "%5.2f%%\t[%5.2fs /%5.2f ETA]\n", progress * 100, time_span, time_span / progress - time_span);
534-
535+
float time_span_print_progress = duration_cast<duration<double>>(
536+
end - time_print_progress).count();
537+
float progress_tile = (float) (yi * xtiles + xi + 1);
538+
if (time_span_print_progress > 0.5 || (yi + 1 == ytiles && xi + 3 > xtiles)) {
539+
double progress = progress_tile / (ytiles * xtiles);
540+
double time_span = duration_cast<duration<double>>(end - begin).count();
541+
fprintf(stderr, "%5.2f%%\t[%5.2fs /%5.2f ETA]\n", progress * 100, time_span,
542+
time_span / progress - time_span);
543+
time_print_progress = end;
544+
}
535545
}
536546

537547
// download
@@ -598,6 +608,9 @@ int RealSR::process_cpu(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
598608
int in_tile_y0 = std::max(yi * TILE_SIZE_Y - prepadding, 0);
599609
int in_tile_y1 = std::min((yi + 1) * TILE_SIZE_Y + prepadding, h);
600610

611+
high_resolution_clock::time_point begin = high_resolution_clock::now();
612+
high_resolution_clock::time_point time_print_progress;
613+
601614
for (int xi = 0; xi < xtiles; xi++)
602615
{
603616
const int tile_w_nopad = std::min((xi + 1) * TILE_SIZE_X, w) - xi * TILE_SIZE_X;
@@ -900,6 +913,18 @@ int RealSR::process_cpu(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
900913
#endif
901914
}
902915
}
916+
917+
high_resolution_clock::time_point end = high_resolution_clock::now();
918+
float time_span_print_progress = duration_cast<duration<double>>(
919+
end - time_print_progress).count();
920+
float progress_tile = (float) (yi * xtiles + xi + 1);
921+
if (time_span_print_progress > 0.5 || (yi + 1 == ytiles && xi + 3 > xtiles)) {
922+
double progress = progress_tile / (ytiles * xtiles);
923+
double time_span = duration_cast<duration<double>>(end - begin).count();
924+
fprintf(stderr, "%5.2f%%\t[%5.2fs /%5.2f ETA]\n", progress * 100, time_span,
925+
time_span / progress - time_span);
926+
time_print_progress = end;
927+
}
903928
}
904929
}
905930

RealSR-NCNN-Android-GUI/app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ android {
3939
targetCompatibility JavaVersion.VERSION_17
4040
}
4141
namespace 'com.tumuyan.ncnn.realsr'
42+
buildFeatures {
43+
buildConfig true
44+
}
4245
}
4346

4447
dependencies {

RealSR-NCNN-Android-GUI/app/src/main/java/com/tumuyan/ncnn/realsr/AssetsCopyer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public static void releaseAssets(Context context, String assetsDir,
2323
// Log.d(TAG, "context: " + context + ", " + assetsDir);
2424
if (TextUtils.isEmpty(releaseDir)) {
2525
return;
26-
} else if (releaseDir.endsWith("/")) {
27-
releaseDir = releaseDir.substring(0, releaseDir.length() - 1);
2826
}
2927

28+
releaseDir = releaseDir.replaceFirst("/+$","");
29+
3030
if (TextUtils.isEmpty(assetsDir) || assetsDir.equals("/")) {
3131
assetsDir = "";
32-
} else if (assetsDir.endsWith("/")) {
33-
assetsDir = assetsDir.substring(0, assetsDir.length() - 1);
32+
} else {
33+
assetsDir = assetsDir.replaceFirst("/+$","");
3434
}
3535

3636
AssetManager assets = context.getAssets();
@@ -74,6 +74,8 @@ private static boolean writeFile(String fileName, InputStream in, boolean skipEx
7474
if (skipExistFile) {
7575
Log.d(TAG, "skip file: " + fileName);
7676
return bRet;
77+
}else{
78+
file.delete();
7779
}
7880
} else if (!file.getParentFile().exists()) {
7981
file.getParentFile().mkdirs();

RealSR-NCNN-Android-GUI/app/src/main/java/com/tumuyan/ncnn/realsr/MainActivity.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
4444

4545
import java.io.BufferedReader;
46-
import java.io.DataOutputStream;
4746
import java.io.File;
4847
import java.io.FileOutputStream;
4948
import java.io.IOException;
@@ -73,7 +72,7 @@ public class MainActivity extends AppCompatActivity {
7372
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
7473
+ File.separator + "RealSR";
7574
private File outputFile, inputFile, titleFile;
76-
private String dir;
75+
private String dir, cache_dir;
7776
// dir="/data/data/com.tumuyan.ncnn.realsr/cache/realsr";
7877
private String modelName = "SR";
7978
private SearchView searchView;
@@ -203,7 +202,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
203202
append_param += (" -g -1");
204203

205204
append_param += ";";
206-
q = "rm *.png; rm *.png/*; rmdir *.png;" + bench_mark_commands[0] + append_param + bench_mark_commands[1] + append_param;
205+
q = "rm -rf *.png; ls *.png; " + bench_mark_commands[0] + append_param + bench_mark_commands[1] + append_param;
207206

208207
imageName = "/img/realsr.png";
209208
bench_mark_mode = true;
@@ -220,7 +219,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
220219
boolean final_bench_mark_mode = bench_mark_mode;
221220
new Thread(() -> {
222221
if (q == CMD_RESET_CACHE) {
223-
AssetsCopyer.releaseAssets(this, "realsr", dir, false);
222+
AssetsCopyer.releaseAssets(this, "realsr", cache_dir, false);
224223
}
225224
run20(q, final_bench_mark_mode);
226225
final File finalfile = new File(dir + finalImageName);
@@ -612,11 +611,8 @@ protected void onCreate(Bundle savedInstanceState) {
612611
String defaultCommand = mySharePerferences.getString("defaultCommand", "");
613612
searchView.setQuery(defaultCommand, false);
614613

615-
dir = this.getCacheDir().getAbsolutePath();
616-
AssetsCopyer.releaseAssets(this,
617-
"realsr", dir
618-
, version == BuildConfig.VERSION_CODE
619-
);
614+
cache_dir = this.getCacheDir().getAbsolutePath();
615+
AssetsCopyer.releaseAssets(this, "realsr", cache_dir, version == BuildConfig.VERSION_CODE);
620616

621617
SharedPreferences.Editor editor = mySharePerferences.edit();
622618
editor.putInt("version", BuildConfig.VERSION_CODE);
@@ -633,7 +629,7 @@ else if (orientation == 3) {
633629
}
634630

635631

636-
dir = dir + "/realsr";
632+
dir = cache_dir + "/realsr";
637633

638634
outputFile = new File(dir, "output.png");
639635
inputFile = new File(dir, "input.png");

RealSR-NCNN-Android-GUI/gradle.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ android.useAndroidX=true
1919
# resources declared in the library itself and none from the library's dependencies,
2020
# thereby reducing the size of the R class for that library
2121
android.nonTransitiveRClass=true
22-
android.defaults.buildfeatures.buildconfig=true
2322
android.nonFinalResIds=false

0 commit comments

Comments
 (0)