Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
add shrinklearn, shrinkapply command.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsofcotton authored Dec 6, 2023
1 parent ad98b64 commit 8b7c51c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ Searching the Internet more...
gokibin recolor2 <dimension> <input.ppm> <output.ppm> <intensity>
gokibin recolor3 <dimension> <input.ppm> <input-shape> <output.ppm>
gokibin habit <in0.obj> <in1.obj> <out.obj>
gokibin (separate|integrate) <in0.ppm> ...
python3 test.py ./gokibin (sharpen|bump|enlarge|flarge|represent|jps|tilt|obj|sbox|prep|presq|nop|limit|bit|cleansq?) <param> input0.png ...
python3 test.py ./gokibin (cat|catb|catr|catbr) input0.png input1.png ...
python3 test.py ./gokibin (tilecat|tilecatb|tilecatr|tilecatbr) <tile count> < cat.txt
python3 test.py ./gokibin match input0.png input1.png <vboxdst> <vboxsrc> <number_of_submatches> <number_of_emphasis>
python3 test.py ./gokibin i2i <param> img0.png ...
python3 test.py qredg pextend in.png
python3 test.py shrinklearn <param> in0.png ...
python3 test.py shrinkapply <param> in0.png ...

# Another downloads
* https://konbu.azurewebsites.net/ (Sample Site)
Expand Down Expand Up @@ -72,4 +75,5 @@ Searching the Internet more...
2023/11/19 add pred command.
2023/11/20 re-delete pred command.
2023/11/21 re-delete cleans2.
2023/12/06 add command shrinklearn, shrinkapply.

33 changes: 33 additions & 0 deletions goki.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ typedef enum {
ENLARGE_X,
ENLARGE_Y,
ENLARGE_BOTH,
SHRINK_X,
SHRINK_Y,
SHRINK_BOTH,
FLARGE_X,
FLARGE_Y,
FLARGE_BOTH,
Expand Down Expand Up @@ -433,6 +436,9 @@ template <typename T> SimpleMatrix<T> filter(const SimpleMatrix<T>& data, const
case ENLARGE_BOTH:
result = filter<T>(filter<T>(data, ENLARGE_X, recur), ENLARGE_Y, recur);
break;
case SHRINK_BOTH:
result = filter<T>(filter<T>(data, SHRINK_X, recur), SHRINK_Y, recur);
break;
case FLARGE_BOTH:
result = filter<T>(filter<T>(data, FLARGE_X, recur), FLARGE_Y, recur);
break;
Expand All @@ -448,6 +454,9 @@ template <typename T> SimpleMatrix<T> filter(const SimpleMatrix<T>& data, const
case ENLARGE_X:
result = filter<T>(data.transpose(), ENLARGE_Y, recur).transpose();
break;
case SHRINK_X:
result = filter<T>(data.transpose(), SHRINK_Y, recur).transpose();
break;
case FLARGE_X:
result = filter<T>(data.transpose(), FLARGE_Y, recur).transpose();
break;
Expand Down Expand Up @@ -505,6 +514,30 @@ template <typename T> SimpleMatrix<T> filter(const SimpleMatrix<T>& data, const
result = Eop[size][recur] * data;
}
break;
case SHRINK_Y:
{
assert(2 <= data.rows());
static vector<vector<SimpleMatrix<T> > > Eop;
const auto& size(data.rows());
if(Eop.size() <= size)
Eop.resize(size + 1, vector<SimpleMatrix<T> >());
else if(recur < Eop[size].size())
goto sopi;
if(Eop[size].size() <= recur)
Eop[size].resize(recur + 1, SimpleMatrix<T>());
{
auto& eop(Eop[size][recur]);
if(eop.cols() == size)
goto sopi;
cerr << "e" << flush;
eop.resize(size / recur - 1, size);
for(int j = 0; j < eop.rows(); j ++)
eop.row(j) = taylor<T>(eop.cols(), T(j) / T(eop.rows() - 1) * T(eop.cols() - 1));
}
sopi:
result = Eop[size][recur] * data;
}
break;
case FLARGE_Y:
{
SimpleMatrix<T> work(data);
Expand Down
8 changes: 5 additions & 3 deletions lieonn.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4188,7 +4188,7 @@ template <typename T> pair<vector<SimpleSparseTensor<T> >, vector<SimpleSparseTe
for(int k = 0; k < idx.size(); k ++)
for(int m = 0; m < idx.size(); m ++)
in[i][j * idx.size() * idx.size() + k * idx.size() + m] =
in0[i][idx[j]][idx[k]][idx[m]];
(in0[i][idx[j]][idx[k]][idx[m]] + T(int(1))) / T(int(2));
}
const auto p(predv<T>(in));
pair<vector<SimpleSparseTensor<T> >, vector<SimpleSparseTensor<T> > > res;
Expand All @@ -4199,9 +4199,11 @@ template <typename T> pair<vector<SimpleSparseTensor<T> >, vector<SimpleSparseTe
for(int k = 0; k < idx.size(); k ++)
for(int m = 0; m < idx.size(); m ++) {
res.first[i][ idx[j]][idx[k]][idx[m]] =
p.first[i][ j * idx.size() * idx.size() + k * idx.size() + m];
p.first[i][ j * idx.size() * idx.size() + k * idx.size() + m] *
T(int(2)) - T(int(1));
res.second[i][idx[j]][idx[k]][idx[m]] =
p.second[i][j * idx.size() * idx.size() + k * idx.size() + m];
p.second[i][j * idx.size() * idx.size() + k * idx.size() + m] *
T(int(2)) - T(int(1));
}
}
return res;
Expand Down
34 changes: 33 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@
a = f.readline().split(" ")
h = int(a[1])
subprocess.call(["mogrify", "-resize", str(w) + "x" + str(h) + "!", "-format", "png", root + "-pext.ppm"])
elif(argv[2] == "shrinklearn"):
for t in range(0, int(argv[3])):
cmds = []
for s in range(0, 3 * 4):
cmds.append(["sh", "-c", argv[1] + " + "])
for line in argv[4:]:
subprocess.call(["convert", line, "-compress", "none", "-resize", str(100. / pow(2., float(t + 1))) + "%", line + "-orig.ppm"])
subprocess.call(["gokibin", "separate", line + "-orig.ppm"])
for idx in range(0, 12):
cmds[idx][- 1] += line + "-orig.ppm-in.ppm " + line + "-orig.ppm-" + str(idx) + ".pgm "
for idx in range(0, len(cmds)):
cmds[idx][- 1] += " > shrinklean-" + str(t) + "-" + str(idx) + ".txt"
subprocess.call(cmds[idx])
elif(argv[2] == "shrinkapply"):
for t in range(0, int(argv[3])):
for line in argv[4:]:
cmds = []
for s in range(0, 3 * 4):
cmds.append(["sh", "-c", argv[1] + " - "])
subprocess.call(["convert", line, "-compress", "none", "-resize", str(100. / pow(2., float(int(argv[3]) - t))) + "%", line + "-orig.ppm"])
if(t != 0):
with open(line + "-orig.ppm") as f:
f.readline()
a = f.readline().split(" ")
w = int(a[0])
h = int(a[1])
subprocess.call(["convert", line + "-orig.ppm-out.ppm", "-resize", str(w) + "x" + str(h) + "!", "-compress", "none", line + "-orig.ppm"])
subprocess.call(["gokibin", "separate", line + "-orig.ppm"])
for idx in range(0, 12):
cmds[idx][- 1] += line + "-orig.ppm-" + str(idx) + ".pgm "
cmds[idx][- 1] += " < shrinklean-" + str(t) + "-" + str(idx) + ".txt"
subprocess.call(["gokibin", "integrate", line + "-orig.ppm"])
else:
for line in argv[3:]:
try:
Expand All @@ -159,7 +191,7 @@
root, ext = os.path.splitext(line)
if(ext != ".ppm" and argv[2] != "prep" and argv[2] != "prepsq"):
subprocess.call(["convert", line, "-compress", "none", root + ".ppm"])
if(argv[2] == "represent" or argv[2] == "collect" or argv[2] == "flarge" or argv[2] == "blink" or argv[2] == "enlarge" or argv[2] == "sharpen" or argv[2] == "limit" or argv[2] == "bit"):
if(argv[2] == "represent" or argv[2] == "collect" or argv[2] == "flarge" or argv[2] == "blink" or argv[2] == "enlarge" or argv[2] == "shrink" or argv[2] == "sharpen" or argv[2] == "limit" or argv[2] == "bit"):
subprocess.call([argv[1], argv[2], root + ".ppm", root + "-" + argv[2] + ".ppm", str(pixels), str(rot)])
elif(argv[2] == "bump"):
subprocess.call([argv[1], argv[2], root + ".ppm", root + "-" + argv[2] + "0.ppm", str(pixels), str(rot)])
Expand Down
55 changes: 55 additions & 0 deletions tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int main(int argc, const char* argv[]) {
strcmp(argv[1], "sharpen") == 0 ||
strcmp(argv[1], "bump") == 0 ||
strcmp(argv[1], "enlarge") == 0 ||
strcmp(argv[1], "shrink") == 0 ||
strcmp(argv[1], "flarge") == 0 ||
strcmp(argv[1], "blink") == 0 ||
strcmp(argv[1], "represent") == 0 ||
Expand All @@ -91,6 +92,11 @@ int main(int argc, const char* argv[]) {
try {
data[i] = filter<num_t>(filter<num_t>(data[i], ENLARGE_BOTH, recur, rot), CLIP);
} catch(const char* e) { cerr << e << endl; }
else if(strcmp(argv[1], "shrink") == 0)
for(int i = 0; i < data.size(); i ++)
try {
data[i] = filter<num_t>(filter<num_t>(data[i], SHRINK_BOTH, recur, rot), CLIP);
} catch(const char* e) { cerr << e << endl; }
else if(strcmp(argv[1], "flarge") == 0)
for(int i = 0; i < data.size(); i ++)
data[i] = filter<num_t>(data[i], FLARGE_BOTH, recur, rot);
Expand Down Expand Up @@ -437,6 +443,55 @@ int main(int argc, const char* argv[]) {
cout << (i < data[j].size() ? data[j][i] / M : num_t(0)) << " ";
cout << endl;
}
} else if(strcmp(argv[1], "integrate") == 0) {
for(int i = 2; i < argc; i ++) {
vector<SimpleMatrix<num_t> > obuf;
obuf.resize(3);
vector<SimpleMatrix<num_t> > work;
for(int j = 0; j < obuf.size(); j ++) {
for(int k = 0; k < 4; k ++) {
if(!loadp2or3<num_t>(work,
(string(argv[i]) + "-" + to_string(j * 4 + k)
+ string(".pgm")).c_str()) ) {
cerr << "NG: " << argv[i] << endl;
continue;
}
assert(work.size() == 1);
if(obuf[j].rows() == 0 || obuf[j].cols() == 0) {
obuf[j].resize(work[0].rows() * 2, work[0].cols() * 2);
obuf[j].O();
}
for(int ii = 0; ii < work[0].rows(); ii ++)
for(int jj = 0; jj < work[0].cols(); jj ++)
obuf[j](ii * 2 + (k & 1), jj * 2 + ((k >> 1) & 1)) = work[0](ii, jj);
}
}
savep2or3<num_t>((string(argv[i]) + "-out.ppm").c_str(), obuf);
}
} else if(strcmp(argv[1], "separate") == 0) {
for(int i = 2; i < argc; i ++) {
vector<SimpleMatrix<num_t> > ibuf;
if(!loadp2or3<num_t>(ibuf, argv[i])) {
cerr << "Cannot load : " << argv[i] << endl;
continue;
}
assert(ibuf.size() == 3);
for(int j = 0; j < ibuf.size(); j ++) {
auto shrink((dft<num_t>(- (ibuf[j].rows() / 2)) * dft<num_t>(ibuf[j].rows()).subMatrix(0, 0, ibuf[j].rows() / 2, ibuf[j].rows())).template real<num_t>() * ibuf[j] * (dft<num_t>(- (ibuf[j].cols() / 2)) * dft<num_t>(ibuf[j].cols()).subMatrix(0, 0, ibuf[j].cols() / 2, ibuf[j].cols())).template real<num_t>().transpose() / num_t(int(4)));
for(int k = 0; k < 4; k ++) {
SimpleMatrix<num_t> m(ibuf[j].rows() / 2, ibuf[j].cols() / 2);
for(int ii = 0; ii < m.rows(); ii ++)
for(int jj = 0; jj < m.cols(); jj ++)
m(ii, jj) = ibuf[j](ii * 2 + (k & 1), jj * 2 + ((k >> 1) & 1));
vector<SimpleMatrix<num_t> > mm;
mm.emplace_back(move(m));
savep2or3<num_t>((string(argv[i]) + "-" + to_string(j * 4 + k)
+ string(".pgm")).c_str(), mm);
}
ibuf[j] = move(shrink);
}
savep2or3<num_t>((string(argv[i]) + "-in.ppm").c_str(), ibuf);
}
} else {
usage(argv[0]);
return - 1;
Expand Down

0 comments on commit 8b7c51c

Please sign in to comment.