CV之SR:基于TF框架实现图像超分辨率源代码(提高图像的分辨率,源自pix2pix模型中的部分代码)
目录
基于TF框架实现图像超分辨率源代码(提高图像的分辨率,源自pix2pix模型中的部分代码)
1、tfimage.py文件功能解释
create_op、decode_jpeg、decode_png
load函数
create_op函数定义了若干函数
2、process.py添加一个新操作
程序代码源自:https://github.com/affinelayer/pix2pix-tensorflow
基于TF框架实现图像超分辨率源代码(提高图像的分辨率,源自pix2pix模型中的部分代码) 1、tfimage.py文件功能解释 create_op、decode_jpeg、decode_png此处的create_op就调用了tf.get_default_session().run()方法,可以将Tensor 操作的函数转变为对Numpy 数组操作的函数,转换后的函数输出为Numpy的数组,而不是Tensor。例如,下面的decode_jpeg和decode_png。
def create_op(func, **placeholders):
op = func(**placeholders)
def f(**kwargs):
feed_dict = {}
for argname, argvalue in kwargs.items():
placeholder = placeholders[argname]
feed_dict[placeholder] = argvalue
return tf.get_default_session().run(op, feed_dict=feed_dict)
return f
decode_jpeg = create_op(
func=tf.image.decode_jpeg,
contents=tf.placeholder(tf.string),
)
decode_png = create_op(
func=tf.image.decode_png,
contents=tf.placeholder(tf.string),
)
load函数
tfimage.py里使用decode_jpeg和deco de_png定义了一个load函数。load函数的输入是一个图片文件路径,返回的是numpy. ndarray 形式的图像数据。
def load(path):
with open(path, "rb") as f:
contents = f.read()
_, ext = os.path.splitext(path.lower())
if ext == ".jpg":
image = decode_jpeg(contents=contents)
elif ext == ".png":
image = decode_png(contents=contents)
else:
raise Exception("invalid image suffix")
return to_float32(image=image)
create_op函数定义了若干函数
rgb_to_grayscale = create_op(
func=tf.image.rgb_to_grayscale,
images=tf.placeholder(tf.float32),
)
……
crop = create_op(
func=tf.image.crop_to_bounding_box,
image=tf.placeholder(tf.float32),
offset_height=tf.placeholder(tf.int32, []),
offset_width=tf.placeholder(tf.int32, []),
target_height=tf.placeholder(tf.int32, []),
target_width=tf.placeholder(tf.int32, []),
)
pad = create_op(
func=tf.image.pad_to_bounding_box,
image=tf.placeholder(tf.float32),
offset_height=tf.placeholder(tf.int32, []),
offset_width=tf.placeholder(tf.int32, []),
target_height=tf.placeholder(tf.int32, []),
target_width=tf.placeholder(tf.int32, []),
)
2、process.py添加一个新操作
process.py 的主处理函数process 使用了上述load 函数读入图片,接着做了一些处理后保存。
def process(src_path, dst_path):
src = im.load(src_path)
if a.operation == "grayscale":
dst = grayscale(src)
elif a.operation == "resize":
dst = resize(src)
elif a.operation == "blank":
dst = blank(src)
elif a.operation == "combine":
dst = combine(src, src_path)
elif a.operation == "edges":
dst = edges(src)
elif a.operation == "blur":
dst = blur(src)
else:
raise Exception("invalid operation")
im.save(dst, dst_path)