Tensorflow实现End to End网络

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 21 18:31:57 2018
@author: WangChen100
"""
import tensorflow as tf
slim=tf.contrib.slim
trunc_normal=lambda stddev: tf.truncated_normal_initializer(0.0, stddev)

batch_size=1
img_W=256
img_H=256

def inference(inputs):
    '''
    this function is to build fully convolutial network
    Args:
        input_Img: 4D tensor [batch_size, width, height, channels], dtype=tf.float32
    Return:
        output_Img: 4D tensor [batch_size, width, height, channels], dtype=tf.int32
    '''
    with slim.arg_scope([slim.conv2d,slim.max_pool2d],stride=1,padding="SAME"):
        #conv1
        net=slim.conv2d(inputs,64,[3,3],scope="conv1_1")
        net=slim.conv2d(net,64,[3,3],scope="conv1_2")
        net=slim.max_pool2d(net,[3,3],stride=2,scope="maxpool1")
        #conv2
        net=slim.conv2d(net,128,[3,3],scope="conv2_1")
        net=slim.conv2d(net,128,[3,3],scope="conv2_2")
        net=slim.max_pool2d(net,[3,3],stride=2,scope="maxpool2")
        #conv3
        net=slim.conv2d(net,256,[3,3],scope="conv3_1")
        net=slim.conv2d(net,256,[3,3],scope="conv3_2")
        net=slim.max_pool2d(net,[3,3],stride=2,scope="maxpool3")
        #conv4
        net=slim.conv2d(net,512,[3,3],scope="conv4_1")
        net=slim.conv2d(net,512,[3,3],scope="conv4_2")
        net=slim.max_pool2d(net,[3,3],stride=2,scope="maxpool4")
        #conv5
        net=slim.conv2d(net,512,[3,3],scope="conv5_1")
        net=slim.conv2d(net,512,[3,3],scope="conv5_2")
        net=slim.max_pool2d(net,[3,3],stride=2,scope="maxpool5")
        #deconv1
        weight=tf.get_variable("weights",[3,3,256,512],
                              initializer=tf.truncated_normal_initializer(mean=0.0,stddev=1.0))        
        deconv1=tf.nn.conv2d_transpose(net,weight,batch_size,int(img_W/16),
int(img_H/16),256],strides=[1,2,2,1],padding="SAME",name="deconv1")
        #deconv2
        weight2=tf.get_variable("weights2",[3,3,128,256],
                              initializer=tf.truncated_normal_initializer(mean=0.0,stddev=1.0))
        deconv2=tf.nn.conv2d_transpose(deconv1,weight2,[batch_size,int(img_W/8),int(img_H/8),128],
                                    strides=[1,2,2,1],padding="SAME",name="deconv2")
        #deconv3
        weight3=tf.get_variable("weights3",[3,3,64,128],
                              initializer=tf.truncated_normal_initializer(mean=0.0,stddev=1.0))
        deconv3=tf.nn.conv2d_transpose(deconv2,weight3,[batch_size,int(img_W/4),int(img_H/4),64],
                                    strides=[1,2,2,1],padding="SAME",name="deconv3")
        #deconv4
        weight4=tf.get_variable("weights4",[3,3,32,64],
                              initializer=tf.truncated_normal_initializer(mean=0.0,stddev=1.0))
        deconv4=tf.nn.conv2d_transpose(deconv3,weight4,[batch_size,int(img_W/2),int(img_H/2),32],
                                    strides=[1,2,2,1],padding="SAME",name="deconv4")
        #deconv5
        weight5=tf.get_variable("weights5",[3,3,3,32],
                              initializer=tf.truncated_normal_initializer(mean=0.0,stddev=1.0))
        deconv5=tf.nn.conv2d_transpose(deconv4,weight5,[batch_size,img_W,img_H,3],
                                    strides=[1,2,2,1],padding="SAME",name="deconv5")
    return deconv5
#%%Test
import numpy as np
import matplotlib.pyplot as plt
image_contents=tf.read_file("./example/a.jpg")
image1 = tf.image.decode_jpeg(image_contents, channels=3)
image=tf.image.resize_image_with_crop_or_pad(image1, img_W, img_H)
image=tf.cast([image],tf.float32)
deconv=inference(image)
init_op=tf.global_variables_initializer()
with tf.Session() as sess:   
    sess.run(init_op)
    reimg=sess.run(deconv)
    reimg=np.uint8(reimg)
    plt.figure(1)
    plt.imshow(reimg[0,:,:,:])
    plt.show() 

错误1:Input 'input_sizes' of 'Conv2DBackpropInput' Op has type float32 that does not match expected type of int32.
原因:tf.nn.conv2d_transpose或tf.nn.conv2d中size[xx,xx,xx,xx]的参数设置为float导致的,尺寸只能为int。
解决方案:tf.nn.conv2d_transpose或tf.nn.conv2d中size[xx,xx,xx,xx]的参数设置int即可。注意python3中,下列操作也会导致错误,因为img_W/2=32.0

img_W=64
img_H=64
deconv=tf.nn.conv2d_transpose(deconv,weight,[batch_size,img_W/2,img_H/2,3],strides=[1,2,2,1],padding="SAME",name="deconv")

错误2:UnicodeEncodeError: 'utf-8' codec can't encode character '\udcd5' in position 1892: surrogates not allowed
原因:tf.read_file('xxxx.jpg')中图片路径包含中文或者没有在该目录下找到指定图片
注意tf.read_file('a.jpg')的路径指的是运行.py文件所在的目录

你可能感兴趣的:(Tensorflow实现End to End网络)