代码:https://github.com/MONI-JUAN/Tensorflow_Study/tree/master/ch10-超分辨率%20部分函数说明
数据集用的是COCO数据集
,上次12G那个 train2014.zip
,下不下来,就看看代码了。
在地址http://msvocds.blob.core.windows.net/coco2014/train2014.zip 下载COCO数据集。
看几个重要的函数
def create_op(func, **placeholders):
'''【重要】
将 Tensorflow 中的函数变成普通函数
Tensorflow :输入Tensor,返回Tensor
create_op :输入Tensor,返回Numpy
'''
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(
'''
解码jpeg图片
tf.image.decode_jpeg() 返回的是 Tensor
decode_jpeg() 返回的是 numpy.ndarry
'''
func=tf.image.decode_jpeg,
contents=tf.placeholder(tf.string),
)
decode_png = create_op(
'''
解码png图片
tf.image.decode_png() 返回的是 Tensor
decode_png() 返回的是 numpy.ndarry
'''
func=tf.image.decode_png,
contents=tf.placeholder(tf.string),
)
encode_jpeg = create_op(
'''
编码jpeg图片
tf.image.decode_jpeg() 返回的是 Tensor
decode_jpeg() 返回的是 numpy.ndarry
'''
func=tf.image.encode_jpeg,
image=tf.placeholder(tf.uint8),
)
encode_png = create_op(
'''
编码png图片
tf.image.decode_png() 返回的是 Tensor
decode_png() 返回的是 numpy.ndarry
'''
func=tf.image.encode_png,
image=tf.placeholder(tf.uint8),
)
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, []),
)
def process(src_path, dst_path):
'''
输入 numpy.ndarray ,输出 numpy.ndarray
'''
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)
处理图片
其实就是先缩小,再放大,看还原效果
def blur(src, scale=4):
height, width, _ = src.shape
height_down = height // scale
width_down = width // scale
dst = im.downscale(images=src, size=[height_down, width_down])
dst = im.upscale(images=dst, size=[height, width])
return dst