训练一个简单的回归网络

基础的函数如下:

# coding=utf-8
import tensorflow as tf
import numpy as np
np.random.seed(0)
# 卷积权重初始化
def weight(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.1), name ='W')
# 偏差值初始化
def bias(shape):
return tf.Variable(tf.constant(0.1, shape=shape), name = 'b')
# 全连接初始化
def fc_weight(node_in, node_out):
return tf.Variable(np.random.randn(node_in, node_out),name='W', dtype='float32') / (np.sqrt(node_in/2).astype(np.float32))

输入及网络、损失函数:

with tf.name_scope('input'):
features = tf.placeholder('float32', [None, 7, 7], name='feature')
images = tf.reshape(features, [-1, 7, 7, 1])
with tf.name_scope('flat'):
flat = tf.reshape(images, [-1, 49])
with tf.name_scope('hidden'):
w = fc_weight(49,49)
b = bias([49])
hidden1 = tf.nn.relu(tf.matmul(flat, w) + b)
w2 = fc_weight(49,10)
b2 = bias([10])
hidden2 = tf.nn.relu(tf.matmul(hidden1, w2) + b2)
with tf.name_scope('output'):
w3 = fc_weight(10,3)
b3 = bias([3])
out = tf.matmul(hidden2, w3) + b3
with tf.name_scope('optimizer'):
loss_function = tf.reduce_mean(tf.square(out - [[1./7,1./7,1./7]]))
optimizer = tf.train.AdamOptimizer(learning_rate=0.1).minimize(loss_function)
# 等效于
# var_list = tf.trainable_variables()
# for v in var_list:
# print v.name
# sum_loss = loss_function
# clone_grad = optimizer.compute_gradients(sum_loss, var_list=var_list)
# grad_updates = optimizer.apply_gradients(clone_grad)

训练函数:

# 生成数据集
X_feature = []
for i in range(7):
for j in range(7):
for t in range(30):
deta_i = np.random.randint(3,5)
deta_j = np.random.randint(3, 5)
map_feature = np.random.rand(7,7)
for di in range(deta_i):
for dj in range(deta_j):
ni = i+di; nj =j+dj
if ni >=7 or nj >=7:
continue
map_feature[ni,nj] = np.random.rand()*2+1
map_feature = (map_feature/6. - 0.5)*2.0
X_feature.append(map_feature)
X_feature = np.array(X_feature, dtype=np.float32)
np.random.shuffle(X_feature)
print X_feature.shape
# train
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
trainEpoch = 30
batchSize = 30
totalBatchs = int(X_feature.shape[0] / batchSize)
print X_feature[:1]
for epoch in range(trainEpoch):
for i in range(totalBatchs):
batch = X_feature[i:(i+1)*batchSize]
rr,ll,tt = sess.run([optimizer,loss_function, out], feed_dict={features:X_feature[:1]}) #反复迭代第一张
print ll, tt
y = sess.run(out, feed_dict={features: X_feature[:1]})
print y

使用fc层实现OTSU

OTSU见详细,我们这里通过网络来实现它的效果。

网络结构:

with tf.name_scope('input'):
features = tf.placeholder('float32', [None, 7, 7], name='feature')
images = tf.reshape(features, [-1, 7, 7, 1])
labels = tf.placeholder('float32',[None,3], name='label')
with tf.name_scope('flat'):
flat = tf.reshape(images, [-1, 49]) with tf.name_scope('hidden'):
w = fc_weight(49,49)
b = bias([49])
hidden1 = tf.nn.relu(tf.matmul(flat, w) + b)
w2 = fc_weight(49,10)
b2 = bias([10])
hidden2 = tf.nn.relu(tf.matmul(hidden1, w2) + b2)
with tf.name_scope('output'):
w3 = fc_weight(10,3)
b3 = bias([3])
out = tf.matmul(hidden2, w3) + b3 with tf.name_scope('optimizer'):
loss_function = tf.reduce_mean(tf.square(out - labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss_function)

生成测试样本集:

dataset = []
for i in range(5):
for j in range(5):
for t in range(40):
deta = np.random.randint(3, 5)
if i+deta >=7 or j+deta>=7:
continue
map_feature = np.random.rand(7, 7)
for di in range(deta):
for dj in range(deta):
ni = i + di
nj = j + dj
map_feature[ni, nj] = np.random.rand() * 2 + 2
#label
centor_x = i+deta/2.
centor_y = j+deta/2.
length = deta
map_feature = (map_feature / 4. - 0.5) * 2.0
map_laebl = np.array([centor_x,centor_y,length])
sample = [map_feature,map_laebl]
dataset.append(sample)
#
random.shuffle(dataset)
allbatch = [ele[0] for ele in dataset]
alllabel = [ele[1] for ele in dataset]

训练:

with tf.Session() as sess:
writer = tf.summary.FileWriter("./logs/", sess.graph)
sess.run(tf.initialize_all_variables())
trainEpoch = 3000
batchSize = 30
totalBatchs = int(len(dataset) / batchSize)
for epoch in range(trainEpoch):
for i in range(totalBatchs):
batch = allbatch[i:(i + 1) * batchSize]
label = alllabel[i:(i + 1) * batchSize]
_, = sess.run([optimizer], feed_dict={features: batch, labels:label})
loss = sess.run([loss_function, out], feed_dict={features: allbatch[:3], labels: alllabel[:3]})
print loss
print alllabel[:3]

使用网络直接优化OTSU

网络:

def h(x):
return tf.sigmoid(5*x)
with tf.name_scope('input'):
features = tf.placeholder('float32', [None, 7, 7], name='feature')
images = tf.reshape(features, [-1, 7, 7, 1])
with tf.name_scope('flat'):
flat = tf.reshape(images, [-1, 49])
with tf.name_scope('hidden'):
w = fc_weight(49,49)
b = bias([49])
hidden1 = tf.nn.relu(tf.matmul(flat, w) + b)
w2 = fc_weight(49,10)
b2 = bias([10])
hidden2 = tf.nn.relu(tf.matmul(hidden1, w2) + b2)
with tf.name_scope('output'):
w3 = fc_weight(10,3)
b3 = bias([3])
out = tf.matmul(hidden2, w3) + b3 with tf.name_scope('integral'):
size = 48
big_images = tf.image.resize_bilinear(images, [size, size]) with tf.name_scope('optimizer'):
out_limit = tf.minimum(tf.maximum(out, 0.), 1.)
cx = out_limit[:, 0]
cy = out_limit[:, 1]
ll = out_limit[:, 2] x1 = tf.maximum(cx - ll, 0.) * (size-1)
y1 = tf.maximum(cy - ll, 0.) * (size-1)
x2 = tf.minimum(cx + ll, 1.) * (size-1)
y2 = tf.minimum(cy + ll, 1.) * (size-1) rowlist = []
for i in range(size):
rowlist.append(np.ones(size)*i)
rows = np.concatenate(rowlist).astype(np.float32)
cols = np.tile(np.arange(0, size, dtype=np.float32),[size])
elems = (rows, cols)
# 通过函数来实现crop操作
def mf(ele):
x=ele[0]
y=ele[1]
return (h(x-x1) - h(x-x2)) * (h(y-y1) - h(y-y2)) omap = tf.map_fn(mf, elems, dtype='float32')
pmap = tf.reshape(omap,[1,size,size,-1])
tmap = tf.transpose(pmap, perm=[3,2,1,0]) #b * size * size * 1
roidot = tmap*big_images
roi = tf.reduce_sum(roidot, axis=[1,2,3])
total = tf.reduce_sum(big_images, axis=[1,2,3])
areanum = tf.reduce_sum(tmap, axis=[1,2,3]) + 0.1
w0 = areanum / size / size
w1 = 1. - w0
u0 = roi / areanum
u1 = (total - roi) / (size*size - areanum)
#
penalty = tf.maximum((0.1 - w0)*100., 0.)
loss_func = tf.reduce_mean(1 - tf.sign(u0-u1)*w0*w1*(u0-u1)*(u0-u1))
loss_penalty = tf.reduce_mean(penalty)
loss_function = loss_func + loss_penalty
#
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss_function)

数据及训练:

X_feature = []
for i in range(5):
for j in range(5):
for t in range(30):
deta_i = np.random.randint(3, 5)
deta_j = np.random.randint(3, 5)
map_feature = np.random.rand(7, 7)
for di in range(deta_i):
for dj in range(deta_j):
ni = i + di
nj = j + dj
if ni >= 7 or nj >= 7:
continue
map_feature[ni, nj] = np.random.rand() * 2 + 2
map_feature = (map_feature / 4. - 0.5) * 2.0
X_feature.append(map_feature)
X_feature = np.array(X_feature, dtype=np.float32)
np.random.shuffle(X_feature)
print X_feature.shape
# train
with tf.Session() as sess:
writer = tf.summary.FileWriter("./logs/", sess.graph)
sess.run(tf.initialize_all_variables())
trainEpoch = 30
batchSize = 30
totalBatchs = int(X_feature.shape[0] / batchSize)
print X_feature[:1]
for epoch in range(trainEpoch):
for i in range(totalBatchs):
batch = X_feature[i:(i + 1) * batchSize]
_, ll, tt = sess.run([optimizer, out_limit, areanum], feed_dict={features: batch}) # 反复迭代第一张
y = sess.run([x1,y1,x2,y2, loss_function], feed_dict={features: X_feature[:1]})
print y

在整个训练的过程中,我们观察到几个问题:

  1. 不要用自己写的sigmod实现,反向传播会越界
  2. 最后输出层不要有relu
  3. 有些函数导数为0(无法反向传播),No gradient defined for operation 'xxx'. 例子/神器
  4. 学习率的设置很重要。示例中设置0.005很快收敛;设置为0.05很快就陷入了局部最小值;设置为0.001,由于adam的自动更新,优化越来越慢,最后也很难收敛到最优。

tf训练OTSU的更多相关文章

  1. TensorFlow Object Detection API(Windows下训练)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近事情比较多,前面坑挖的有点久,今天终于有时间总结一下,顺便把Windows下训练跑通.Li ...

  2. 暑假第二弹:基于docker的hadoop分布式集群系统的搭建和测试

    早在四月份的时候,就已经开了这篇文章.当时是参加数据挖掘的比赛,在计科院大佬的建议下用TensorFlow搞深度学习,而且要在自己的hadoop分布式集群系统下搞. 当时可把我们牛逼坏了,在没有基础的 ...

  3. 实战Google深度学习框架-C3-TensorFlow入门

    第三章:TensorFlow入门 TensorFlow存在计算模型,数据模型和运算模型(本文用TF代表TensorFlow) 3.1 计算模型-计算图 3.1.1 计算图的概念 TensorFlow这 ...

  4. TFLite基础知识

    此基础知识仅为个人学习记录,如有错误或遗漏之处,还请各位同行给个提示. 概述 TFLite主要含有如下内容: (1)TFLite提供一系列针对移动平台的核心算子,包括量化和浮点运算.另外,TFLite ...

  5. MachineLN博客目录

    MachineLN博客目录 https://blog.csdn.net/u014365862/article/details/78422372 本文为博主原创文章,未经博主允许不得转载.有问题可以加微 ...

  6. 实战Google深度学习框架-C5-MNIST数字识别问题

    5.1 MNIST数据处理 MNIST是NIST数据集的一个子集,包含60000张图片作为训练数据,10000张作为测试数据,其中每张图片代表0~9中的一个数字,图片大小为28*28(可以用一个28* ...

  7. TensorFlow学习笔记:保存和读取模型

    TensorFlow 更新频率实在太快,从 1.0 版本正式发布后,很多 API 接口就发生了改变.今天用 TF 训练了一个 CNN 模型,结果在保存模型的时候居然遇到各种问题.Google 搜出来的 ...

  8. Reading | 《TensorFlow:实战Google深度学习框架》

    目录 三.TensorFlow入门 1. TensorFlow计算模型--计算图 I. 计算图的概念 II. 计算图的使用 2.TensorFlow数据类型--张量 I. 张量的概念 II. 张量的使 ...

  9. The case for learned index structures

    17年的旧文,最近因为SageDB论文而重读. 文章主要思路是通过学习key的顺序.结构等来预测record在位置.存在与否等.效果方面,据称部分场景下,相对b-tree可以优化70%的内存占用. 最 ...

随机推荐

  1. 向量图兼容组件VectorCompat

    向量图兼容组件VectorCompat Android从5.0(API Level 21)开始,支持矢量图和动画矢量图.采用这两种图,可以避免传统图片因为缩放而产生失真.VectorCompat组件是 ...

  2. IO流关键字

  3. Good Bye 2018 (A~F, H)

    目录 Codeforces 1091 A.New Year and the Christmas Ornament B.New Year and the Treasure Geolocation C.N ...

  4. luffy项目的接口开发

    处理跨域请求 主要的思路: 设置一个基于CORS的中间件来处理,关于跨域的产生与处理手段 settings.py: MIDDLEWARE = [ 'django.middleware.security ...

  5. mongoose 根据_id更新数据

    let photoId = mongoose.Types.ObjectId(`${virtual.productId[0]}`) await model.photo.findByIdAndUpdate ...

  6. [BZOJ2863]愤怒的元首

    Description: Pty生活在一个奇葩的国家,这个国家有n个城市,编号为1~n. ​ 每个城市到达其他城市的路径都是有向的. ​ 不存在两个城市可以互相到达. 这个国家的元首现在很愤怒,他大喊 ...

  7. 编程菜鸟的日记-初学尝试编程-寻找2到n之间的素数并输出

    //输入一个整数n,输出2到n之间的具体素数值 #include <iostream> #include <algorithm> #include <cmath> ...

  8. 策略梯度训练cartpole小游戏

    我原来已经安装了anaconda,在此基础上进入cmd进行pip install tensorflow和pip install gym就可以了. 在win10的pycharm做的. policy_gr ...

  9. 【迎圣诞,拿大奖】+流量分析+Writeup分享

    太菜了太菜了,刚见到jsfuck时竟然不知道什么东西,自己都不敢说自己做过实验吧上的那道jsfuck题了. 进入正题: 首先解压发现两个文件,一个流量分析包,哇哇哇,我正好刚学了几天wireshark ...

  10. Linux安装gcc时碰到的有关问题解决(解决gcc依赖有关问题)

    Linux安装gcc时碰到的有关问题解决(解决gcc依赖有关问题) rpm安装gcc时碰到的有关问题解决(解决gcc依赖有关问题) 提示:error: Failed dependencies: clo ...