代码: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):
- 利用 create_op 编码图片
- 剪裁、扩充图片
- 处理图片
-
看几个重要的函数
def create_op(func, **placeholders):
def create_op(func, **placeholders):'''【重要】将 Tensorflow 中的函数变成普通函数Tensorflow :输入Tensor,返回Tensorcreate_op :输入Tensor,返回Numpy'''op = func(**placeholders)def f(**kwargs):feed_dict = {
}for argname, argvalue in kwargs.items():placeholder = placeholders[argname]feed_dict[placeholder] = argvaluereturn tf.get_default_session().run(op, feed_dict=feed_dict)return f
利用 create_op 编码图片
decode_jpeg = create_op('''解码jpeg图片tf.image.decode_jpeg() 返回的是 Tensordecode_jpeg() 返回的是 numpy.ndarry'''func=tf.image.decode_jpeg,contents=tf.placeholder(tf.string),
)decode_png = create_op('''解码png图片tf.image.decode_png() 返回的是 Tensordecode_png() 返回的是 numpy.ndarry'''func=tf.image.decode_png,contents=tf.placeholder(tf.string),
)encode_jpeg = create_op('''编码jpeg图片tf.image.decode_jpeg() 返回的是 Tensordecode_jpeg() 返回的是 numpy.ndarry'''func=tf.image.encode_jpeg,image=tf.placeholder(tf.uint8),
)encode_png = create_op('''编码png图片tf.image.decode_png() 返回的是 Tensordecode_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.shapeheight_down = height // scalewidth_down = width // scaledst = im.downscale(images=src, size=[height_down, width_down])dst = im.upscale(images=dst, size=[height, width])return dst