文章目錄
- CIFAR10與VGG13實戰
- 1. 準備資料
- 2.構建網路模型
- 3.訓練模型
CIFAR10與VGG13實戰
1. 準備資料
CIFAR10 資料集由加拿大 Canadian Institute For Advanced Research 發布,它包含了飛機、汽車、鳥、貓等共 10 大類物體的彩色圖片,每個種類收集了 6000 張 32x32 大小圖片,共 60K 張圖片,其中 50K 作為訓練資料集,10K 作為測驗資料集,
import tensorflow as tf
from tensorflow.keras import datasets,layers,losses,optimizers,Sequential
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
使用datasets.cifar10.load_data()加載資料集,并對資料集進行預處理
def preprocess(x, y):
# [0~1]
x = 2*tf.cast(x, dtype=tf.float32) / 255.-1
y = tf.cast(y, dtype=tf.int32)
return x,y
(x,y), (x_test, y_test) = datasets.cifar10.load_data()
y = tf.squeeze(y, axis=1)
y_test = tf.squeeze(y_test, axis=1)
print(x.shape, y.shape, x_test.shape, y_test.shape)
(50000, 32, 32, 3) (50000,) (10000, 32, 32, 3) (10000,)
train_db = tf.data.Dataset.from_tensor_slices((x,y))
train_db = train_db.shuffle(1000).map(preprocess).batch(128)
test_db = tf.data.Dataset.from_tensor_slices((x_test,y_test))
test_db = test_db.map(preprocess).batch(64)
sample = next(iter(train_db))
print('sample:', sample[0].shape, sample[1].shape,
tf.reduce_min(sample[0]), tf.reduce_max(sample[0]))
sample: (128, 32, 32, 3) (128,) tf.Tensor(-1.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)
2.構建網路模型
CIFAR10 圖片識別任務并不簡單,這主要是由于 CIFAR10 的圖片內容需要大量細節才能呈現,而保存的圖片解析度僅有 32x32,使得部分主體資訊較為模糊,甚至人眼都很難分辨,淺層的神經網路表達能力有限,很難訓練優化到較好的性能,本節將基于表達能力更強的 VGG13 網路,根據我們的資料集特點修改部分網路結構,完成 CIFAR10 圖片識別,

我們將網路實作為 2 個子網路:卷積子網路和全連接子網路,卷積子網路由 5 個子模塊構成,每個子模塊包含了 Conv-Conv-MaxPooling 單元結構:
conv_layers = [ # 先創建包含多層的串列
# unit 1
# 64 個 3x3 卷積核, 輸入輸出同大小
layers.Conv2D(64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.Conv2D(64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same'),
# unit 2
#輸出通道提升至 128,高寬大小減半
layers.Conv2D(128, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.Conv2D(128, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same'),
# unit 3
#,輸出通道提升至 256,高寬大小減半
layers.Conv2D(256, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.Conv2D(256, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same'),
# unit 4
#輸出通道提升至 512,高寬大小減半
layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same'),
# unit 5
#輸出通道提升至 512,高寬大小減半
layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same')
]
# 利用前面創建的層串列構建網路容器
conv_net = Sequential(conv_layers)
全連接子網路包含了 3 個全連接層,每層添加 ReLU 非線性激活函式,最后一層除外
# 創建 3 層全連接層子網路
fc_net = Sequential([
layers.Dense(256, activation=tf.nn.relu),
layers.Dense(128, activation=tf.nn.relu),
layers.Dense(10, activation=None),
])
# build2 個子網路,并列印網路引數資訊
conv_net.build(input_shape=[4, 32, 32, 3])
fc_net.build(input_shape=[4, 512])
conv_net.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (4, 32, 32, 64) 1792
_________________________________________________________________
conv2d_1 (Conv2D) (4, 32, 32, 64) 36928
_________________________________________________________________
max_pooling2d (MaxPooling2D) (4, 16, 16, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (4, 16, 16, 128) 73856
_________________________________________________________________
conv2d_3 (Conv2D) (4, 16, 16, 128) 147584
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (4, 8, 8, 128) 0
_________________________________________________________________
conv2d_4 (Conv2D) (4, 8, 8, 256) 295168
_________________________________________________________________
conv2d_5 (Conv2D) (4, 8, 8, 256) 590080
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (4, 4, 4, 256) 0
_________________________________________________________________
conv2d_6 (Conv2D) (4, 4, 4, 512) 1180160
_________________________________________________________________
conv2d_7 (Conv2D) (4, 4, 4, 512) 2359808
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (4, 2, 2, 512) 0
_________________________________________________________________
conv2d_8 (Conv2D) (4, 2, 2, 512) 2359808
_________________________________________________________________
conv2d_9 (Conv2D) (4, 2, 2, 512) 2359808
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (4, 1, 1, 512) 0
=================================================================
Total params: 9,404,992
Trainable params: 9,404,992
Non-trainable params: 0
_________________________________________________________________
3.訓練模型
下面對資料進行訓練,并對測驗資料集進行測驗
def main():
# [b, 32, 32, 3] => [b, 1, 1, 512]
conv_net = Sequential(conv_layers)
fc_net = Sequential([
layers.Dense(256, activation=tf.nn.relu),
layers.Dense(128, activation=tf.nn.relu),
layers.Dense(10, activation=None),
])
conv_net.build(input_shape=[None, 32, 32, 3])
fc_net.build(input_shape=[None, 512])
# conv_net.summary()
# fc_net.summary()
optimizer = optimizers.Adam(lr=1e-4)
# [1, 2] + [3, 4] => [1, 2, 3, 4]
variables = conv_net.trainable_variables + fc_net.trainable_variables
for epoch in range(50):
for step, (x,y) in enumerate(train_db):
with tf.GradientTape() as tape:
# [b, 32, 32, 3] => [b, 1, 1, 512]
out = conv_net(x)
# flatten, => [b, 512]
out = tf.reshape(out, [-1, 512])
# [b, 512] => [b, 10]
logits = fc_net(out)
# [b] => [b, 10]
y_onehot = tf.one_hot(y, depth=10)
# compute loss
loss = tf.losses.categorical_crossentropy(y_onehot, logits, from_logits=True)
loss = tf.reduce_mean(loss)
grads = tape.gradient(loss, variables)
optimizer.apply_gradients(zip(grads, variables))
if step %100 == 0:
print(epoch, step, 'loss:', float(loss))
total_num = 0
total_correct = 0
for x,y in test_db:
out = conv_net(x)
out = tf.reshape(out, [-1, 512])
logits = fc_net(out)
prob = tf.nn.softmax(logits, axis=1)
pred = tf.argmax(prob, axis=1)
pred = tf.cast(pred, dtype=tf.int32)
correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
correct = tf.reduce_sum(correct)
total_num += x.shape[0]
total_correct += int(correct)
acc = total_correct / total_num
print(epoch, 'acc:', acc)
if __name__ == '__main__':
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/202234.html
標籤:其他
下一篇:SVN Error: Can‘t connect to host xxxxx‘: 由于目標計算機積極拒絕,無法連接,的最快解決辦法
