tf训练OTSU
训练一个简单的回归网络
基础的函数如下:
# 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
在整个训练的过程中,我们观察到几个问题:
- 不要用自己写的sigmod实现,反向传播会越界
- 最后输出层不要有relu
- 有些函数导数为0(无法反向传播),No gradient defined for operation 'xxx'. 例子/神器
- 学习率的设置很重要。示例中设置0.005很快收敛;设置为0.05很快就陷入了局部最小值;设置为0.001,由于adam的自动更新,优化越来越慢,最后也很难收敛到最优。
tf训练OTSU的更多相关文章
- TensorFlow Object Detection API(Windows下训练)
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近事情比较多,前面坑挖的有点久,今天终于有时间总结一下,顺便把Windows下训练跑通.Li ...
- 暑假第二弹:基于docker的hadoop分布式集群系统的搭建和测试
早在四月份的时候,就已经开了这篇文章.当时是参加数据挖掘的比赛,在计科院大佬的建议下用TensorFlow搞深度学习,而且要在自己的hadoop分布式集群系统下搞. 当时可把我们牛逼坏了,在没有基础的 ...
- 实战Google深度学习框架-C3-TensorFlow入门
第三章:TensorFlow入门 TensorFlow存在计算模型,数据模型和运算模型(本文用TF代表TensorFlow) 3.1 计算模型-计算图 3.1.1 计算图的概念 TensorFlow这 ...
- TFLite基础知识
此基础知识仅为个人学习记录,如有错误或遗漏之处,还请各位同行给个提示. 概述 TFLite主要含有如下内容: (1)TFLite提供一系列针对移动平台的核心算子,包括量化和浮点运算.另外,TFLite ...
- MachineLN博客目录
MachineLN博客目录 https://blog.csdn.net/u014365862/article/details/78422372 本文为博主原创文章,未经博主允许不得转载.有问题可以加微 ...
- 实战Google深度学习框架-C5-MNIST数字识别问题
5.1 MNIST数据处理 MNIST是NIST数据集的一个子集,包含60000张图片作为训练数据,10000张作为测试数据,其中每张图片代表0~9中的一个数字,图片大小为28*28(可以用一个28* ...
- TensorFlow学习笔记:保存和读取模型
TensorFlow 更新频率实在太快,从 1.0 版本正式发布后,很多 API 接口就发生了改变.今天用 TF 训练了一个 CNN 模型,结果在保存模型的时候居然遇到各种问题.Google 搜出来的 ...
- Reading | 《TensorFlow:实战Google深度学习框架》
目录 三.TensorFlow入门 1. TensorFlow计算模型--计算图 I. 计算图的概念 II. 计算图的使用 2.TensorFlow数据类型--张量 I. 张量的概念 II. 张量的使 ...
- The case for learned index structures
17年的旧文,最近因为SageDB论文而重读. 文章主要思路是通过学习key的顺序.结构等来预测record在位置.存在与否等.效果方面,据称部分场景下,相对b-tree可以优化70%的内存占用. 最 ...
随机推荐
- 【Excel】将IP按照IP地址(v4)增长序列排序
Background: Excel列中,有多个net-block, 将这些net-block按照IP地址(v4)自己的大小从小到大排序. Idea: IPv4地址的格式是点分十进制的,也就是说每一个点 ...
- jsoup 解析html
http://www.cnblogs.com/jycboy/p/jsoupdoc.html http://www.cnblogs.com/mokafamily/p/3558620.html
- winfrom datagridview中DataGridViewTextBoxColumn的联动处理
这个问题有两种方法 第一种是用DataGridview中自带的DataGridViewTextBoxColumn 控件,第二种是动态添加combobox控件 方法一: 首先 窗体上拖拽一个 DataG ...
- Shell脚本笔记(九)数组
数组 一)定义 #最常用:小括号加空格: a=( ) #键值对形式: b=([]=x []=men []=z) #定义变量的形式 c[]=; c[]=; c[]= #命令结果作为数组元素 d=($(c ...
- linux中查看 php.ini 的存放位置
查找php.ini的存放位置: 方法一: php --ini 所列出的结果中: Loaded Configuration File 即为 php.ini 所存放的位置 方法二: php -i | g ...
- yii去掉自动排序功能
Yii去掉自动排序功能并自定义排序 public function search($params) { $query = SvnManage::find()->addOrderBy([ 'cre ...
- 解压文件出错解决方法(invalid compressed data--format violated)
解压缩出现这个报错: 1.考虑是否传输过程出错,重新传输试试. 2.使用传输工具时,选择二进制试试. 3.使用传输工具时,选择ASCII试试. 解压缩命令: gzip -d filename gun ...
- linux命令之free篇
作业二: 1.free命令查看内存 [root@localhost 桌面]# free total used free shared buffers cachedMem: 1003432 899760 ...
- Java 中的 int 型转为 long 型
先将 int 型转为 String 型,然后再将 String 转为 long 型,如下图: public class TestIntToLong { public static void main( ...
- Codeforces899D Shovel Sale(思路)
http://codeforces.com/problemset/problem/899/D 还是得tag一下,以下代码只有G++ 14 6.4.0能过,其他都过不了不知为什么? 思路:先求出最多的9 ...