结果是在8GB 1070上训练大约10~11个小时后获得的。
FCN-TensorFlow完整代码Github:https://github.com/EternityZY/FCN-TensorFlow.git
一:准备工作
1.然后下载VGG网络的权重参数,下载好后的文件路径为./Model_zoo/imagenet-vgg-verydeep-19.mat.
MODEL_URL = 'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-vgg-verydeep-19.mat'
2.数据集下载,也可把自己数据集放入,代替./Data_zoo/MIT_SceneParsing/ADEChallengeData2016文件
DATA_URL = 'http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip'
二:开始训练
注意:
-
训练模型只需执行python FCN.py
-
修改学习率1e-5 甚至更小 否则loss会一直在3左右浮动
-
debug标志可以在训练期间设置,以添加关于激活函数,梯度,变量等的信息。
-
训练时把FCN.py中的全局变量mode改为“train”,运行该文件,测试时修改测试函数里的图片地址,并把mode改为“test”运行即可
-
代码的实现有四个python文件,分别是FCN.py、BatchDatasetReader.py、TensorFlowUtils.py、read_MITSceneParsingData.py。将这四个文件放在一个当前目录 下,此处附上修改过后的四个文件:
FCN.py为主文件,代码如下:
from __future__ import print_function
import tensorflow as tf
import numpy as np
import TensorflowUtils as utils
import read_MITSceneParsingData as scene_parsing
import datetime
import BatchDatsetReader as dataset
from six.moves import xrange
FLAGS = tf.flags.FLAGS
#batch大小
tf.flags.DEFINE_integer("batch_size", "2", "batch size for training")
#存放存放数据集的路径,需要提前下载
tf.flags.DEFINE_string("logs_dir", "logs/", "path to logs directory")
tf.flags.DEFINE_string("data_dir", "Data_zoo/MIT_SceneParsing/", "path to dataset")
# 学习率
tf.flags.DEFINE_float("learning_rate", "1e-4", "Learning rate for Adam Optimizer")
# VGG网络参数文件,需要提前下载
tf.flags.DEFINE_string("model_dir", "Model_zoo/", "Path to vgg model mat")
tf.flags.DEFINE_bool('debug', "False", "Debug mode: True/ False")
tf.flags.DEFINE_string('mode', "train", "Mode train/ test/ visualize")
MODEL_URL = 'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-vgg-verydeep-19.mat'
MAX_ITERATION = int(1e5 + 1) # 最大迭代次数
NUM_OF_CLASSESS = 151 # 类的个数
IMAGE_SIZE = 224 # 图像尺寸
#vggnet函数
# 根据载入的权重建立原始的 VGGNet 的网络
def vgg_net(weights, image):
layers = (
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',
'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
'relu3_3', 'conv3_4', 'relu3_4', 'pool3',
'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
'relu5_3', 'conv5_4', 'relu5_4'
)
net = {}
current = image
for i, name in enumerate(layers):
kind = name[:4]
if kind == 'conv':
kernels, bias = weights[i][0][0][0][0]
# matconvnet: weights are [width, height, in_channels, out_channels]
# tensorflow: weights are [height, width, in_channels, out_channels]
kernels = utils.get_variable(np.transpose(kernels, (1, 0, 2, 3)), name=name + "_w")
bias = utils.get_variable(bias.reshape(-1), name=name + "_b")
current = utils.conv2d_basic(current, kernels, bias)
elif kind == 'relu':
current = tf.nn.relu(current, name=name)
if FLAGS.debug:
utils.add_activation_summary(current)
elif kind == 'pool':
current = utils.avg_pool_2x2(current)
net[name] = current
return net
# inference函数,FCN的网络结构定义,网络中用到的参数是迁移VGG训练好的参数
def inference(image, keep_prob): #输入图像和dropout值
"""
Semantic segmentation network definition
:param image: input image. Should have values in range 0-255
:param keep_prob:
:return:
"""
# 加载模型数据,获得标准化均值
print("setting up vgg initialized conv layers ...")
model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL)
mean = model_data['normalization'][0][0][0] # 通过字典获取mean值,vgg模型参数里有normaliza这个字典,三个0用来去虚维找到mean
mean_pixel = np.mean(mean, axis=(0, 1))
weights = np.squeeze(model_data['layers'])# 从数组的形状中删除单维度条目,获得vgg权重
# 图像预处理
processed_image = utils.process_image(image, mean_pixel) # 图像减平均值实现标准化
print("预处理后的图像:", np.shape(processed_image))
with tf.variable_scope("inference"):
# 建立原始的VGGNet-19网络
print("开始建立VGG网络:")
image_net = vgg_net(weights, processed_image)
# 在VGGNet-19之后添加 一个池化层和三个卷积层
conv_final_layer = image_net["conv5_3"]
print("VGG处理后的图像:", np.shape(conv_final_layer))
pool5 = utils.max_pool_2x2(conv_final_layer)
W6 = utils.weight_variable([7, 7, 512, 4096], name="W6")
b6 = utils.bias_variable([4096], name="b6")
conv6 = utils.conv2d_basic(pool5, W6, b6)
relu6 = tf.nn.relu(conv6, name="relu6")
if FLAGS.debug:
utils.add_activation_summary(relu6)
relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob)
W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7")
b7 = utils.bias_variabl