More Related Content
Similar to TensorFlow 深度學習快速上手班--電腦視覺應用 (20)
More from Mark Chang (20)
TensorFlow 深度學習快速上手班--電腦視覺應用
- 18. 卷積神經網路
• Pooling Layer
1
3
2
4
5
7
6
8
0
0
4
4
6
6
0
0
4
5
3
2
no overlap
no padding no weights
depth = 1
7
8
6
4
Maximum
Pooling
Average
Pooling
- 24. Create Variables Operators
def weight_variable(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.1))
def bias_variable(shape):
return tf.Variable(tf.constant(0.1, shape=shape))
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
- 25. Computational Graph
x_ = tf.placeholder(tf.float32, [None, 784], name=x_)
y_ = tf.placeholder(tf.float32, [None, 10], name=y_”)
x_image = tf.reshape(x_, [-1,28,28,1])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y= tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
- 28. Convolutional Layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
5
1
32
32
5x5
1
32
32
W_conv1
W_conv1
b_conv1
b_conv1
- 31. ReLU
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
ReLU:
⇢
nin if nin 0
0 otherwise
-0.5 0.2 0.3 -0.1
0.2 -0.3 -0.4 -1.1
2.1 -2.1 0.1 1.2
0.2 3.0 -0.3 0.5
0 0.2 0.3 0
0.2 0 0 0
2.1 0 0.1 1.2
0.2 3.0 0 0.5
- 38. 訓練資料
• ILSVRC 2014 Classification Challenge
– http://www.image-net.org/challenges/
LSVRC/2014/
• Dataset:
1000 categories
– Training: 1,200,000
– Validation: 50,000
– Testing: 100,000
- 40. Load Computational Graph
model_fn = 'tensorflow_inception_graph.pb'
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
graph_def = tf.GraphDef.FromString(open(model_fn).read())
t_input = tf.placeholder(np.float32, name='input')
imagenet_mean = 139
t_preprocessed = tf.expand_dims(t_input - imagenet_mean, 0)
tf.import_graph_def(graph_def, {'input': t_preprocessed})
t_output = graph.get_tensor_by_name(import/output2:0)
- 41. Load Label
f = open(label.json)
labels = json.loads(.join(f.readlines()))
f.close()
1: kit fox, Vulpes macrotis,
2: English setter,
3: Siberian husky,
4: Australian terrier,
......
998: stole,
999: carbonara,
1000: dumbbell
- 42. Run Computational Graph
def load_image(imgfile):
return np.float32(PIL.Image.open(imgfile).resize((224,224)))
def get_class(image):
return labels[str(np.argmax(sess.run([t_output], {t_input:
load_image(image)})))]
print get_class('img/img1.jpg')
leaf beetle, chrysomelid
- 43. 講師資訊
• Email: ckmarkoh at gmail dot com
• Blog: http://cpmarkchang.logdown.com
• Github: https://github.com/ckmarkoh
Mark Chang
• Facebook: https://www.facebook.com/ckmarkoh.chang
• Slideshare: http://www.slideshare.net/ckmarkohchang
• Linkedin:
https://www.linkedin.com/pub/mark-chang/85/25b/847
43