Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev merge #1

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TF1_3/tf_2-6_checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
" print('epcho %d, validation accuracy %s' % (epoch+1, accuracy_validation))\n",
" best = (best, accuracy_validation)[best <= accuracy_validation]\n",
" \n",
" saver.save(sess, 'MNIST/logs/tf2-6/checkpoint/model.ckpt', epoch + 1)\n",
EthanYuan marked this conversation as resolved.
Show resolved Hide resolved
" saver.save(sess, 'MNIST/logs/tf2-6/checkpoint/model.ckpt')\n",
EthanYuan marked this conversation as resolved.
Show resolved Hide resolved
" \n",
" print(\"best: %s\" % best)"
]
Expand Down
4 changes: 2 additions & 2 deletions TF1_3/tf_2-6_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def main(_):
best = (best, accuracy_validation)[
best <= accuracy_validation]

saver.save(sess, 'MNIST/logs/tf2-6/checkpoint/model.ckpt', epoch + 1)
saver.save(sess, 'MNIST/logs/tf2-6/checkpoint/model.ckpt')

# Test trained model
print("best: %s" % best)
Expand All @@ -153,4 +153,4 @@ def main(_):
parser.add_argument('--data_dir', type=str, default='../MNIST/',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
95 changes: 95 additions & 0 deletions TF1_3/tf_2-6_checkpoint_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import argparse
import sys
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

FLAGS = None


def main(_):
# Import data
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True,
validation_size=10000)

# Create the model
x = tf.placeholder(tf.float32, [None, 784], name='x')

with tf.name_scope('fc1'):
W_2 = tf.Variable(tf.random_normal([784, 100]) / tf.sqrt(784.0 / 2))
b_2 = tf.Variable(tf.random_normal([100]))
z_2 = tf.matmul(x, W_2) + b_2
a_2 = tf.nn.relu(z_2)

with tf.name_scope('fc2'):
W_3 = tf.Variable(tf.random_normal([100, 100]) / tf.sqrt(100.0 / 2))
b_3 = tf.Variable(tf.random_normal([100]))
z_3 = tf.matmul(a_2, W_3) + b_3
a_3 = tf.nn.relu(z_3, name='a_3')

with tf.name_scope('fc3'):
W_4 = tf.Variable(tf.random_normal([100, 10]) / tf.sqrt(100.0))
b_4 = tf.Variable(tf.random_normal([10]))
z_4 = tf.matmul(a_3, W_4) + b_4
a_4 = tf.sigmoid(z_4)

# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10], name='y_')

tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_2)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_3)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_4)
regularizer = tf.contrib.layers.l2_regularizer(scale=5.0 / 50000)
reg_term = tf.contrib.layers.apply_regularization(regularizer)

with tf.name_scope('loss'):
loss = (tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(labels=y_, logits=z_4)) +
reg_term)

with tf.name_scope('optimizer'):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(a_4, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(
tf.cast(correct_prediction, tf.float32), name='accuracy')

saver = tf.train.Saver()

# Train
best = 0
for epoch in range(30):
for _ in range(5000):
batch_xs, batch_ys = mnist.train.next_batch(10)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
# Test trained model
accuracy_currut_train = sess.run(
accuracy,
feed_dict={x: mnist.train.images,
y_: mnist.train.labels})

accuracy_currut_validation = sess.run(
accuracy,
feed_dict={x: mnist.validation.images,
y_: mnist.validation.labels})

print("Epoch %s: train: %s validation: %s"
% (epoch, accuracy_currut_train, accuracy_currut_validation))
best = (best, accuracy_currut_validation)[
best <= accuracy_currut_validation]

saver.save(sess, 'MNIST/logs/tf2-6/checkpoint_2/model.ckpt', epoch+1)

# Test trained model
print("best: %s" % best)


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='../MNIST/',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File renamed without changes.
44 changes: 44 additions & 0 deletions TF1_3/tf_2-6_restore_prediction_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import argparse
import sys
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
from importlib import import_module
import_module('tf_2-6_checkpoint')

FLAGS = None


def main(_):
# Import data
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True,
validation_size=10000)

with tf.Session() as sess:
saver = tf.train.import_meta_graph(
'MNIST/logs/tf2-6/checkpoint_2/model.ckpt-30.meta')
saver.restore(sess, tf.train.latest_checkpoint(
'MNIST/logs/tf2-6/checkpoint_2'))

graph = tf.get_default_graph()
a_3 = graph.get_tensor_by_name("fc2/a_3:0")

with tf.name_scope('fc4_2'):
W_4 = tf.Variable(tf.random_normal([100, 10]) / tf.sqrt(100.0))
b_4 = tf.Variable(tf.random_normal([10]))
z_4 = tf.matmul(a_3, W_4) + b_4
a_4 = tf.sigmoid(z_4)
init_new_vars_op = tf.initialize_variables([W_4, b_4])
sess.run(init_new_vars_op)

x = graph.get_tensor_by_name("x:0")
y_ = graph.get_tensor_by_name("y_:0")
print(sess.run(a_4, feed_dict={
x: mnist.test.images,
y_: mnist.test.labels}))

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='../MNIST/',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
133 changes: 133 additions & 0 deletions TensorFlow从0到N/TensorFlow从1到2/10-经典CNN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# 经典CNN

## Alex(2012)

### 基本信息

- 错误率:16.4%;
- 6000万个参数;
- 5个卷积层,3个全连接层;

### 架构

- CONV1
- MAX POOL1
- NORM1
- CONV2
- MAX POOL2
- NORM2
- CONV3
- CONV4
- CONV5
- MAXPOOL3
- FC6
- FC7
- FC8

### 技术特点

- 首次使用ReLU;
- 使用Norm层(现在不常见了);
- 大量数据扩展;
- 使用SGD Momentum 0.9;
- Dropout 0.5;
- 7 CNN ensemble;
- L2正则;

### 超参数

- batch size 128;
- learning rate 1e-2,手动减小当精度趋于平缓时;

## ZFNet(2013)

### 基本信息

- 错误率:11.7%
- 8层;

### 技术特点

- 基于AlexNet;
- CONV1:从(11x11,stride 4)变为(7x7 stride 2)
- CONV3,4,5:由384,384,256变为512,1024,512

## VGG(2014)

### 基本信息

- 错误率:7.3%
- 19层;


### 架构

- conv3-64
- conv3-64
- maxpool
- conv3-128
- conv3-128
- maxpool
- conv3-256
- conv3-256
- conv3-256
- conv3-256
- maxpool
- conv3-512
- conv3-512
- conv3-512
- conv3-512
- maxpool
- conv3-512
- conv3-512
- conv3-512
- conv3-512
- maxpool
- FC-4096
- FC-4096
- FC-1000
- softmax

### 技术特点

- 3x3 CONV stride 1,pad 1;
- 2x2 MAX POOL stride 2;

### 启示

- LRN层作用不大;
- 越深的网络效果越好;
- 1x1的卷积也是很有效的,但是没有3x3的卷积好,大一些的卷积核可以学习更大的空间特征;
- 3个3x3的卷积层串联的效果相当于1个7x7的卷积层,前者只有后者55%的参数量,更多的非线性变换;

## GoogleNet(2014)

### 基本信息

- 错误率:6.70%
- 500百万参数(参数量只有AlexNet 6000万的1/12);
- 比AlexNet速度快2倍;
- 22层;

### 技术特点

- Inception module;
- 完全移除了全连接层,用全局平均池化层来取代,全连接层占据了VGG和AlexNet 90%的参数量;

### Inception Family

- Inception V2,提出了BN,可以去除Dropout、LRN,传统的DNN在每一层的输入的分布都在变化,导致训练变得困难,我们只能使用一个很小的学习率解决这个问题。
- Inception V3,非对称的卷积结构拆分,优化了Inception Module的结构;
- Inception V4,结合了微软的ResNet;

## ResNet(2015)

### 基本信息

- 2~3周,8 GPU机器来进行训练;
- 运行速度比VGG要快;

### 启示

- plain net
- residual
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions TensorFlow从0到N/TensorFlow从1到2/12-GPU.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions TensorFlow从0到N/TensorFlow从1到2/18-RNN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions TensorFlow从0到N/TensorFlow从1到2/19-LSTM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions TensorFlow从0到N/TensorFlow从1到2/20-对抗.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions TensorFlow从0到N/TensorFlow从1到2/21-强化.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
1 change: 1 addition & 0 deletions TensorFlow从0到N/TensorFlow从1到2/9-CNN可视化.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://www.youtube.com/watch?v=AgkfIQ4IGaM&t=22s 可视化
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TensorFlow从0到N/TensorFlow从1到2/img/SSD.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions TensorFlow从0到N/TensorFlow从2到3/13-图像识别和定位.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 识别和定位

## 适用场景

- 一个类别,一个目标;
- 一个类别,多个固定目标;

## 基本思想

识别+用回归做定位:

- 一个类别,一个目标位置:分类+定位;
- 一个类别,一个目标的多个部分(数量固定)位置:分类+定位;

DeepPose:Toshev and Szegedy(Google), “DeepPose: Human Pose Estimation via Deep Neural Networks”, CVPR 2014;

### 步骤

1. 先训练CNN分类器,或者下载Alex、VGG或者GoogleNet;
2. 在全连接层之前加入一个定位回归全连接块;
3. 用自己的数据集,使用L2距离作为损失度量训练这个定位回归块;
4. 进行测试,会产生两组结果,一组分类结果,一组回归定位结果;

### 回归的两种方式

- 不定类回归,class-agnostic regression,无论分类网络如何输出,不定类回归只输出一个框的4个参数即可;
- 特定类回归,class-specific regression,每个类别输出一个框的4个参数;

## 扩展:sliding window

Overfeat:Sermanet et al, “Integrated Recognition, Localization and Detection using Convolutional Networks”, ICLR 2014;

### 特点

- 利用分类+定位,通过滑窗,应用到大分辨率图片;
- 每个滑窗依然是做分类+定位操作;
- 每个滑窗处理的BBox,不一定都是在滑窗内部;
- 滑窗遍历一张图片后(多个尺度),聚合所有的滑窗结果,来确定目标的种类和位置;
- 合并所有尺度上的分类得分和位置回归,形成最终预测;
- 用卷积层替换全连接层,计算更加高效;
- 可以进行目标检测;

### 卷积层替换全连接层

- 与输入尺寸相同的卷积核,可以输出1x1xC的一维特征向量;
- 1x1的卷积核,输出原有的尺寸,这个过程中,进行了跨通道的信息整合,并增加了非线性转换,同时还可以调整输出Channel的数量(用于降维或者升维);

### 缺点

每个滑窗都要进行识别+回归定位,计算量非常大。

### 问题

- 如何聚合每个滑窗的结果?

## 总结

如果任务中的目标具有固定数量,使用定位框架会让问题变得简单。
Loading