Skip to content

Commit

Permalink
Merge pull request #8 from curegit/pnn
Browse files Browse the repository at this point in the history
Merge legacy code 2
  • Loading branch information
curegit authored Feb 20, 2024
2 parents aaeab31 + 4b55f50 commit 9cbecb8
Show file tree
Hide file tree
Showing 56 changed files with 1,843 additions and 319 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.py]
indent_style = space
indent_size = 4

[*.md]
indent_style = space
indent_size = 2

[*.toml]
indent_style = space
indent_size = 2
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pdm
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ This project solves it by using CNN (Convolutional Neural Network) which convert
Here are the provisional result and comparison to the baseline (Gaussian blur).

![comparison](comparison.png)


## Train

###

```
python3 -m descreening.pitch.make JapanColor2011Coated.icc images pitch_dataset
```
2 changes: 2 additions & 0 deletions convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
#import cupy as cp



i = Image.open("test2.bmp")
# ニューラルネットワークを作成
model = RgbNet()
Expand Down
2 changes: 2 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Empty file added descreen/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions descreen/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys
from .cli import main

if __name__ == "__main__":
sys.exit(main())
Binary file added descreen/assets/WideGamutCompat-v4.icc
Binary file not shown.
Binary file added descreen/assets/sRGB-v4.icc
Binary file not shown.
53 changes: 53 additions & 0 deletions descreen/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import typer

def main():

except KeyboardInterrupt:
eprint("KeyboardInterrupt")
exit_code = 130
return exit_code


def con():
device = "cpu"


#from .models import UNetLikeModel
model = UNetLikeModel()
model.load_state_dict(torch.load(sys.argv[2]))


model.to(device)
model.eval()
print(model)

patch_size = model.output_size(512)
#img = read_uint16_image(sys.argv[3])

with open(sys.argv[3], "rb") as fp:
i = load_image(magickpng(fp.read(), png48=True), assert16=True)


height, width = img.shape[1:3]
# TODO: 4倍数にあわせる
ppp_h = h % 512
ppp_w = w % 512
a_h = h + ppp_h
a_w = w + ppp_w
img = img.reshape((1, 3, h, w))
res = np.zeros((3, a_h, a_w), dtype="float32")
p = model.required_padding(patch_size)

img = np.pad(img, ((0, 0), (0, 0), (p, p + ppp_h), (p, p + ppp_w)), mode="symmetric")
for (j, i), (k, l) in model.patch_slices(a_h, a_w, patch_size):
print(k)
x = img[:, :, j, i]
t = torch.from_numpy(x.astype("float32"))
t = t.to(device)
y = model(t)
yy = y.detach().cpu().numpy()
print(y.shape)
res[:, k, l] = yy[0]
res = res[:, :h, :w]
save_image(res, sys.argv[4])
#save_wide_gamut_uint16_array_as_srgb(res, sys.argv[4])
53 changes: 53 additions & 0 deletions descreen/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import cv2
import numpy as np
from pathlib import Path
from numpy import ndarray
from .utilities.filesys import resolve_path


# ファイルパス、パスオブジェクト、またはバイトを受け取り画像を配列としてロードする
def load_image(filelike: str | Path | bytes, *, transpose: bool = True, normalize: bool = True, orient: bool = True, assert16: bool = False) -> ndarray:
match filelike:
case str() | Path() as path:
with open(resolve_path(path), "rb") as fp:
buffer = fp.read()
case bytes() as buffer:
pass
case _:
raise ValueError()
# OpenCV が ASCII パスしか扱えない問題を回避するためにバッファを経由する
bin = np.frombuffer(buffer, np.uint8)
flags = cv2.IMREAD_COLOR | cv2.IMREAD_ANYDEPTH
if not orient:
flags |= cv2.IMREAD_IGNORE_ORIENTATION
img = cv2.imdecode(bin, flags)
if img.ndim != 3 or img.shape[2] != 3:
raise RuntimeError()
if transpose:
# RGBxHxW にする
img = img[:, :, [2, 1, 0]].transpose(2, 0, 1)
match img.dtype:
case np.uint8:
if assert16:
raise RuntimeError()
if normalize:
return (img / (2**8 - 1)).astype(np.float32)
else:
return img
case np.uint16:
if normalize:
return (img / (2**16 - 1)).astype(np.float32)
else:
return img
case _:
raise RuntimeError()



def to_pil_image(array):
srgb = rint(array * 255).clip(0, 255).astype(uint8)
return Image.fromarray(srgb.transpose(1, 2, 0), "RGB")

def save_image(array, filepath):
to_pil_image(array).save(filepath)

Empty file added descreen/models/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions descreen/models/abs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from abc import ABC, abstractmethod

from ..utilities import range_chunks

from torch import nn

class AbsModel(ABC, nn.Module):

@abstractmethod
def forward(self, x):
raise NotImplementedError()

@abstractmethod
def output_size(self, input_size):
raise NotImplementedError()

@abstractmethod
def input_size(self, output_size):
raise NotImplementedError()

def reduced_padding(self, input_size):
output_size = self.output_size(input_size)
return (input_size - output_size) // 2

def required_padding(self, output_size):
input_size = self.input_size(output_size)
return (input_size - output_size) // 2


def patch_slices(self, height, width, patch_size):
for h_start, h_stop in range_chunks(height, patch_size):
for w_start, w_stop in range_chunks(width, patch_size):
h_pad = self.required_padding(h_stop - h_start)
w_pad = self.required_padding(w_stop - w_start)
h_s = slice(h_start, h_stop)
w_s = slice(w_start, w_stop)
h_slice = slice(h_start - h_pad + h_pad, h_stop + h_pad * 2)
w_slice = slice(w_start - w_pad + w_pad, w_stop + w_pad * 2)
yield (h_slice, w_slice), (h_s, w_s)
Loading

0 comments on commit 9cbecb8

Please sign in to comment.