import tensorflow as tf
#卷積函式
def conv2d(x, w, b, strides=1):
x = tf.nn.conv2d(x, w, strides=[1, strides, strides, 1], padding='same')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
#池化函式
def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='same')
#權重引數
weights =
#偏置引數
biases =
#vgg模型
def inference(images, weights,biases, dropout):
with tf.name_scope('conv1'):
conv1 = conv2d(images, weights['wc1'], biases['bc1'])
with tf.name_scope('conv2'):
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
with tf.name_scope('pool1'):
pool1 = maxpool2d(conv2, k=2)
print('pool1尺寸',pool1.shape)
with tf.name_scope('conv3'):
conv3 = conv2d(pool1, weights['wc3'], biases['bc3'])
with tf.name_scope('conv4'):
conv4 = conv2d(conv3, weights['wc4'], biases['bc4'])
with tf.name_scope('pool2'):
pool2 = maxpool2d(conv4, k=2)
print('pool2尺寸',pool2.shape)
with tf.name_scope('conv5'):
conv5 = conv2d(pool2, weights['wc5'], biases['bc5'])
with tf.name_scope('conv6'):
conv6 = conv2d(conv5, weights['wc6'], biases['bc6'])
with tf.name_scope('conv7'):
conv7 = conv2d(conv6, weights['wc7'], biases['bc7'])
with tf.name_scope('pool3'):
pool3 = maxpool2d(conv7, k=2)
print('pool3尺寸', pool3.shape)
with tf.name_scope('conv8'):
conv8 = conv2d(pool3, weights['wc8'], biases['bc8'])
with tf.name_scope('conv9'):
conv9 = conv2d(conv8, weights['wc9'], biases['bc9'])
with tf.name_scope('conv10'):
conv10 = conv2d(conv9, weights['wc10'], biases['bc10'])
with tf.name_scope('pool4'):
pool4 = maxpool2d(conv10, k=2)
print('pool4尺寸', pool4.shape)
with tf.name_scope('conv11'):
conv11 = conv2d(pool4, weights['wc11'], biases['bc11'])
with tf.name_scope('conv12'):
conv12 = conv2d(conv11, weights['wc12'], biases['bc12'])
with tf.name_scope('conv13'):
conv13 = conv2d(conv12, weights['wc13'], biases['bc13'])
with tf.name_scope('pool5'):
pool5 = maxpool2d(conv13, k=2)
print('pool5尺寸', pool5.shape)
#用1*1的卷積代替全連線層
with tf.name_scope('fc1'):
fc1 = tf.reshape(pool5, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
with tf.name_scope('dropout1'):
fc1 = tf.nn.dropout(fc1, dropout)
with tf.name_scope('fc2'):
fc2 = tf.add(tf.matmul(fc1, weights['wd2']), biases['bd2'])
fc2 = tf.nn.relu(fc2)
with tf.name_scope('dropout2'):
fc2 = tf.nn.dropout(fc2, dropout)
with tf.name_scope('fc3'):
out = tf.add(tf.matmul(fc2, weights['out']), biases['out'])
return out
#softmax損失函式
def losses(logits, labels):
with tf.name_scope('loss'):
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels,
name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
tf.summary.scalar('loss' + '/loss', loss)
return loss
#adam梯度下降
def trainning(loss, learning_rate):
with tf.name_scope('optimizer'):
optimizer = tf.train.adamoptimizer(learning_rate=learning_rate)
global_step = tf.variable(0, name='global_step', trainable=false)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
#**準確度
def evaluation(logits, labels):
with tf.name_scope('accuracy'):
correct = tf.nn.in_top_k(logits, labels, 1)
correct = tf.cast(correct, tf.float16)
accuracy = tf.reduce_mean(correct)
tf.summary.scalar('accuracy' + '/accuracy', accuracy)
return accuracy
VGG16網路模型
該網路提出了卷積神經網路的深度增加和小卷積核的使用對網路的最終分類識別效果有很大的作用。後兩個網路對卷積核的開刀的優化方法也證明了這一觀點。在 的實驗中,證明了在大規模影象識別任務中卷積神經網路的深度對準確率的影響。主要的貢獻是利用帶有很小卷積核 3 3 的網路結構對逐漸加深的網路進行評估,結果表明...
VGG網路相關
概念 近來研究vgg網路,將自己的學習的一些知識記錄下來。vggnet是牛津大學計算機視覺組和google deepmind公司的研究員一起研發的深度卷積神經網路。vgg主要 了卷積神經網路的深度和其效能之間的關係,通過反覆堆疊33的小卷積核和22的最大 層,vggnet成功地搭建了16或者19層的...
VGG網路解讀
一 網路結構和配置 主要貢獻是使用乙個帶有非常小 3x3 卷積濾波器的架構對增加深度的網路進行了徹底的評估,這表明通過將深度推進到16 19個權重層,可以實現對先前art配置的顯著改進 1 結構 1 在訓練中,我們的是絡乙個固定大小的輸入224 224 rgb影象。我們所做的唯一預處理是從每個畫素中...