tensorboard

# 指定目录
LOG_DIR = '.'
run_label = 'run_vgg_tensorboard_fine_tune'
run_dir = os.path.join(LOG_DIR, run_label)
if not os.path.join(run_dir):os.mkdir(run_dir)
train_log_dir = os.path.join(run_dir, 'train')
test_log_dir = os.path.join(run_dir, 'test')# 命名name
conv1_1 = tf.layers.conv2d(x_image,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv1_1')def variable_summary(var, name):"""Constructs summary for statistics of a variable"""with tf.name_scope(name):mean = tf.reduce_mean(var)with tf.name_scope('stddev'):stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))tf.summary.scalar('mean', mean)tf.summary.scalar('stddev', stddev)tf.summary.scalar('min', tf.reduce_min(var))tf.summary.scalar('max', tf.reduce_max(var))tf.summary.histogram('histogram', var)with tf.name_scope('summary'):variable_summary(conv1_1, 'conv1_1')variable_summary(conv1_2, 'conv1_2')variable_summary(conv2_1, 'conv2_1')variable_summary(conv2_2, 'conv2_2')variable_summary(conv3_1, 'conv3_1')variable_summary(conv3_2, 'conv3_2')
...
# 写入train_writer = tf.summary.FileWriter(train_log_dir, sess.graph)test_writer = tf.summary.FileWriter(test_log_dir)train_summary_str = eval_ops_results[-1]train_writer.add_summary(train_summary_str, i+1)test_summarys_str = sess.run([megred_summary_test],feed_dict={x: fixed_test_batch_data,y: fixed_test_batch_labels,    })[0]test_writer.add_summary(test_summarys_str, i+1)

3.tensorboard --host=*** --logdir=文件目录

fine-tune(微改其他人的模型)


1.save models

model_dir = os.path.join(run_dir, 'model')
if not os.path.exists(model_dir):os.mkdir(model_dir)
saver = tf.train.Saver()
...
if (i+1) % output_model_every_steps == 0:saver.save(sess, os.path.join(model_dir, 'ckp-{}'.format(i+1)))print('model saved to ckp-{}'.format(i+1))

2.restore models

model_name = 'ckp-1000'
model_path = os.path.join(model_dir, model_name)
....if os.path.exists(model_path + '.index'):saver.restore(sess, model_path)print('model restore from {}'.format(model_path))else:print('model {} does not exist'.format(model_path))
  • activation:relu, sigmoid,tanh
  • weight initializer: he,xavier,normal
  • optimzier:Adam,Momentum,Gradient Descent

def convent(inputs, activation, kernel_initializer):conv1_1 = tf.layers.conv2d(inputs,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,kernel_initializer = kernel_initializername = 'conv1_1')
# activation :tf.nn.relu,tf.nn.sigmoid,tf.nn.tanh
# kernel_initializer:默认为tf.glorot_normal_initializer,
# tf.truncated_normal_initializer(stddev=0.02)
sigmoid:53.39% vs relu:73.35% on 10k train
tf.glorot_normal_initializer 76.53%, 100k train.
tf.truncated_normal_initializer(stddev=0.02) :78.04% 100k train
tf.keras.initializers.he_normal: 71.52%         100k train (deep net)
with tf.name_scope('train_op'):train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)    # 78.04%tf.train.GradientDescentOptimizer(1e-4).minimize(loss)     # 12.35%# reason: 1. initializer incorrect. 2.不充分的训练tf.train.MomentumOptimizer(learning_rate=1e-4, momentum=0.9).minimize(loss) #35.75%

数据增强api

  • resize (缩放图像)
  • crop (裁剪)
  • flip (翻转)
  • brightness (改变光照) & contrast (改变对比度)

基本库

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import  imshow

resize api

tf.image.resize_area
tf.image.resize_bicubic # 图像缩放,双线性算法插入
tf.image.resize_nearest_neighbor # 图像放大,使用相近的像素点插入放大的像素点
# 图片放大
img_string = tf.read_file(picture1)
# img_string2 = tf.read_file(picture2)
img_decoded = tf.image.decode_image(img_string)
# sess多图片,使用四维矩阵,[index, 宽, 长, 通道数]
# resizeimg_decoded = tf.reshape(img_decoded, [1, 1200, 1400, 3])
# 缩小
resize_img = tf.image.resize_bicubic(img_decoded, [2400, 2800])
sess = tf.Session()
img_decoded_val = sess.run(resize_img)
img_decoded_val = img_decoded_val.reshape((2400, 2800, 3))
img_decoded_val = np.asarray(img_decoded_val, np.uint8)
print(img_decoded_val.shape)imshow(img_decoded_val)
# imshow(img_decoded_val2)pylab.show()
# crop 裁剪
tf.image.pad_to_bounding_box()
tf.image.crop_to_bounding_box()
tf.random_crop()img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded, [1, 1200, 1400, 3])
# crop
padded_img = tf.image.pad_to_bounding_box(img_decoded, 100, 200, 1500, 1600) # arg:[0:2]画布中的位置,[2:4]画布大小
sess = tf.Session()
img_decoded_val = sess.run(padded_img)
img_decoded_val = img_decoded_val.reshape((1500, 1600, 3))
img_decoded_val = np.asarray(img_decoded_val, np.uint8)
print(img_decoded_val.shape)imshow(img_decoded_val)
# imshow(img_decoded_val2)pylab.show()

flip

tf.image.flip_up_down()
tf.image.flip_left_right()
tf.image.random_flip_left_right()
tf.image.random_flip_up_down()ex:
padded_img = tf.image.random_flip_left_right(img_decoded)

brightness

tf.image.adjust_brightness()
tf.image.random_brightness()
tf.image.adjust_contrast()
tf.image.random_contrast()

ex:
padded_img = tf.image.adjust_brightness(img_decoded, -0.5) # -0.5 为光强减少50%

实战

# train 100k: 78.04%  -> 82.6%
x_image = tf.reshape(x, [-1, 3, 32, 32])
x_image = tf.transpose(x_image, perm=[0, 2, 3, 1])
data_aug_1 = tf.image.random_flip_left_right(x_image)
data_aug_2 = tf.image.random_brightness(data_aug_1, max_delta=63)
data_aug_3 = tf.image.random_contrast(data_aug_2, lower=0.2, upper=1.8)normal_data_aug_3 = data_aug_3 / 127.5 - 1

继续加深网络层次

conv1_1 = tf.layers.conv2d(normal_data_aug_3,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv1_1')
conv1_2 = tf.layers.conv2d(conv1_1,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv1_2')
conv1_3 = tf.layers.conv2d(conv1_2,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv1_3')# 16 * 16
pooling1 = tf.layers.max_pooling2d(conv1_3,(2, 2), # kernel size(2, 2), # stridename = 'pool1')conv2_1 = tf.layers.conv2d(pooling1,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv2_1')
conv2_2 = tf.layers.conv2d(conv2_1,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv2_2')
conv2_3 = tf.layers.conv2d(conv2_2,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv2_3')
# 8 * 8
pooling2 = tf.layers.max_pooling2d(conv2_3,(2, 2), # kernel size(2, 2), # stridename = 'pool2')conv3_1 = tf.layers.conv2d(pooling2,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv3_1')
conv3_2 = tf.layers.conv2d(conv3_1,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv3_2')
conv3_3 = tf.layers.conv2d(conv3_2,32, # output channel number(3,3), # kernel sizepadding = 'same',activation = tf.nn.relu,name = 'conv3_3')
# 4 * 4 * 32
pooling3 = tf.layers.max_pooling2d(conv3_2,(2, 2), # kernel size(2, 2), # stridename = 'pool3')

归一化

train 100K :85.6%


def conv_wrapper(inputs, name, is_training,output_channel=32, kernel_size=(3,3),activation=tf.nn.relu,padding = 'same'):"""wrapper of tf.laysers.conv2d"""# without bn: conv  -> activation# with batch normalization: conv -> bn -> activationwith tf.name_scope(name):conv2d = tf.layers.conv2d(inputs,output_channel,kernel_size,padding= padding,activation= None,name= name+'/conv2d')bn = tf.layers.batch_normalization(conv2d,training= is_training)return activation(bn)def pooling_warpper(inputs, name):return tf.layers.max_pooling2d(inputs,(2,2),(2,2),name=name)
# conv1: 神经元图, feature_map, 输出图像conv1_1 = conv_wrapper(normal_data_aug_3, name = 'conv1_1', is_training=True)
conv1_2 = conv_wrapper(conv1_1, name = 'conv1_2', is_training=True)
conv1_3 = conv_wrapper(conv1_2, name = 'conv1_3', is_training=True)
pooling1 = pooling_warpper(conv1_3, name = 'pool1')conv2_1 = conv_wrapper(pooling1, name = 'conv2_1', is_training=True)
conv2_2 = conv_wrapper(conv2_1, name = 'conv2_2', is_training=True)
conv2_3 = conv_wrapper(conv2_2, name = 'conv2_3', is_training=True)
pooling2 = pooling_warpper(conv2_3, name = 'pool2')conv3_1 = conv_wrapper(normal_data_aug_3, name = 'conv3_1', is_training=True)
conv3_2 = conv_wrapper(conv3_1, name = 'conv3_2', is_training=True)
conv3_3 = conv_wrapper(conv3_2, name = 'conv3_3', is_training=True)
pooling3 = pooling_warpper(conv3_3, name = 'pool1')

使用resnet train acc 67% after fine-tune -> 94%

tensorflow(神经网络)学习笔记(三)之调参数实战(笔记)相关推荐

  1. 神经网络学习(三):解偏微分方程

    前言 在完成了常微分的数值解之后,我开始如法炮制的来解偏微分,我觉得解法上是一样的,都直接使用autograd就可以了,所以理论是难度并不大(虽然实际上我是花的时间最长的),只不过需要注意的细节比较多 ...

  2. 最新的阿里内部Java性能调优实战笔记,学完就能用的性能调优方法

    年前的一波裁员"背刺",不少人失业,最近翻了不少网站的招聘信息,帮大家看看机会(附几张截图).上个月防疫政策放开,经济逐渐复苏,招聘市场也正在回暖,Java岗机会还是不少,大家多关 ...

  3. TensorFlow神经网络学习笔记

    张量(tensor) 就是多维数组 阶代表张量的维数 维数 阶 名字 0-D 0 标量scalar 1-D 1 向量vector 2-D 2 矩阵 matrix n-D n 张量 tensor 张量可 ...

  4. TensorFlow神经网络(九)VGG net论文阅读笔记

    [注]内容来自MOOC人工智能实践TensorFlow笔记课程第8讲 来源:2015 ICLR 用于图像分类的文章: Very Deep Convolutional Networks for Larg ...

  5. 神经网络学习(三)比较详细 卷积神经网络原理、手写字体识别(卷积网络实现)

    之前写了一篇基于minist数据集(手写数字0-9)的全连接层神经网络,识别率(85%)并不高,这段时间学习了一些卷积神经网络的知识又实践了一把, 识别率(96%左右)确实上来了 ,下面把我的学习过程 ...

  6. Stanford CS230深度学习(三)调参、正则化和优化算法

    lecture3中主要讲了如何构建一个ML/DL任务,主要包括:选择问题.获得数据.设计模型.训练模型.测试模型.部署以及维护.然后coursera中的课程主要讲实际的应用例如调参.正则化等,以及几个 ...

  7. Inception论文学习(三)+吴恩达笔记

    Inception 网络(Inception network) 在上节笔记中,你已经见到了所有的Inception网络基础模块.在本节笔记中,我们将学习如何将这些模块组合起来,构筑你自己的Incept ...

  8. 移动Web开发--学习笔记三 响应式项目实战(微金所)

    响应式项目实战(微金所) 响应式项目开发流程 选择一种屏幕格式进行开发 相应功能模块完成后,测试是否响应式效果 确保响应式效果满足要求 进行下一个功能模块开发 使用自定义字体图标 创建自己的字体图标h ...

  9. python爬虫学习笔记(三)——淘宝商品比价实战(爬取成功)

    2020年最新淘宝商品比价定向爬取 功能描述 目标:获取淘宝搜索页面的信息,提取其中的商品名称和价格. 理解:淘宝的搜索接口 翻页的处理 技术路线:requests­          re 程序的结 ...

最新文章

  1. 如何修改WINDOWS默认的3389远程端口
  2. python xpath语法-Python爬虫:Xpath语法笔记
  3. linux如何自动调jiaob,最牛B的 Linux Shell 命令 系列连载(四)
  4. 基于matlab的状态反馈与极点配置
  5. linkedin api php,php – 如何使用linkedin api发送消息/通知?
  6. ASP.NETMVC Model验证(五)
  7. IPv6下VRRP配置原理及实例
  8. [原创] 对于深度学习(deep learning)在工业界的应用现状和突破 [by matthewbai]
  9. D3/Echarts/G2的对比分析
  10. collectionutils包_基于springframework的集合处理工具类CollectionUtils对常见对象查找包含转换操作...
  11. c#string类型探讨
  12. shal()函数绕过和session验证绕过
  13. 圆的半径java_计算圆的半径
  14. 华为云鲲鹏云服务介绍
  15. build-essential unmet dependencies 有未满足依赖 解决办法
  16. Linux游戏市场,Tacoma上的Nixie Pixel以及更多开放游戏新闻
  17. xp无法访问win7计算机提示无权限,ghost xp访问win7共享无权限怎么解决
  18. Plist文件、NSBundle
  19. 黑马Redis实战篇—给商铺类型缓存(练习)
  20. Android 飞机大战

热门文章

  1. 轻松熊喵喵 -- 如何用jmeter查看jtl类型的文件以及注意事项
  2. MATLAB 解决:数据比较多的矩阵赋值,耗时太长的问题
  3. 百度交易中台之订单系统架构浅析
  4. C语言程序——函数(2)
  5. 爱到颓废,也是一种美(王家卫经典)
  6. maven配置阿里云仓库地址
  7. 服务编排--Conductor 文档翻译 (介绍与基本概念)
  8. 什么是按位或|,什么是按位与,什么是按位异或^ (双目运算符)
  9. 操作系统-百科:Linux
  10. 媒体的态度和立场如何掌握?