复现VGG19训练自定义图像分类
生活随笔
收集整理的這篇文章主要介紹了
复现VGG19训练自定义图像分类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、復現VGG訓練自定義圖像分類,成功了哈哈。
需要代碼工程可聯系博主qq號,在左邊連接可找到。
核心代碼:
# coding:utf-8
import tensorflow as tf
import os
from load_vgg19_model import net
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def VGG19_image_classifier(X,Y,nn_classes):
vgg19_path = "./vgg19_model/imagenet-vgg-verydeep-19.mat"
net_list,mean_pixel,all_layers = net(vgg19_path,X)
vgg19_pool5 = net_list[-1]["pool5"]
vgg19_pool5_shape = vgg19_pool5.get_shape().as_list()
vgg19_pool5_number = vgg19_pool5_shape[1]*vgg19_pool5_shape[2]*vgg19_pool5_shape[3]
weights = {
'wd1': tf.Variable(tf.random_normal([vgg19_pool5_number, 4096])),
'wd2': tf.Variable(tf.random_normal([4096, 4096])),
'out': tf.Variable(tf.random_normal([4096, nn_classes]))
}
biases = {
'bd1': tf.Variable(tf.zeros([4096])),
'bd2': tf.Variable(tf.zeros([4096])),
'out': tf.Variable(tf.zeros([nn_classes]))
}
# 全連接一層
_densel = tf.reshape(vgg19_pool5, [-1, vgg19_pool5_number])
fc6 = tf.add(tf.matmul(_densel,weights["wd1"]),biases["bd1"])
relu6 = tf.nn.relu(fc6)
# 全連接二層
fc7 = tf.add(tf.matmul(relu6,weights["wd2"]),biases["bd2"])
relu7 = tf.nn.relu(fc7)
# 輸出層
fc8 = tf.add(tf.matmul(relu7,weights["out"]),biases["out"])
# out = tf.nn.softmax(fc8)
out = fc8
# 損失函數 loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=out)) # 計算交叉熵
# 優化目標 optimizing
optimizing = tf.train.AdamOptimizer(0.0001).minimize(loss) # 使用adam優化器來以0.0001的學習率來進行微調
# 精確度 accuracy
correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(out, 1)) # 判斷預測標簽和實際標簽是否匹配
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# 想要保存的模型參數,方便加載找到。
tf.add_to_collection("loss", loss)
tf.add_to_collection("out", out)
tf.add_to_collection("accuracy", accuracy)
tf.add_to_collection("optimizing", optimizing)
return {
"loss": loss,
"optimizing": optimizing,
"accuracy": accuracy,
"out": out,
"mean_pixel":mean_pixel
}
小批量梯度訓練方法如下圖,才訓練1次達到88%。
總結
以上是生活随笔為你收集整理的复现VGG19训练自定义图像分类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sitecore系统教程之架构概述
- 下一篇: 贵州有什么财经大学(2022年贵州有哪些