這篇文章主要講解了“TensorFlow2的CNN圖像分類方法是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“TensorFlow2的CNN圖像分類方法是什么”吧!
我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、涼山州ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的涼山州網(wǎng)站制作公司1. 導(dǎo)包
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
2. 圖像分類 fashion_mnist
數(shù)據(jù)處理
# 原始數(shù)據(jù)
(X_train_all, y_train_all),(X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
# 訓(xùn)練集、驗(yàn)證集拆分
X_train, X_valid, y_train, y_valid = train_test_split(X_train_all, y_train_all, test_size=0.25)
# 數(shù)據(jù)標(biāo)準(zhǔn)化,你也可以用除以255的方式實(shí)現(xiàn)歸一化
# 注意最后reshape中的1,代表圖像只有一個(gè)channel,即當(dāng)前圖像是灰度圖
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.reshape(-1, 28 * 28)).reshape(-1, 28, 28, 1)
X_valid_scaled = scaler.transform(X_valid.reshape(-1, 28 * 28)).reshape(-1, 28, 28, 1)
X_test_scaled = scaler.transform(X_test.reshape(-1, 28 * 28)).reshape(-1, 28, 28, 1)
構(gòu)建CNN模型
model = tf.keras.models.Sequential()
# 多個(gè)卷積層
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=[5, 5], padding="same", activation="relu", input_shape=(28, 28, 1)))
model.add(tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2))
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=[5, 5], padding="same", activation="relu"))
model.add(tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2))
# 將前面卷積層得出的多維數(shù)據(jù)轉(zhuǎn)為一維
# 7和前面的kernel_size、padding、MaxPool2D有關(guān)
# Conv2D: 28*28 -> 28*28 (因?yàn)閜adding="same")
# MaxPool2D: 28*28 -> 14*14
# Conv2D: 14*14 -> 14*14 (因?yàn)閜adding="same")
# MaxPool2D: 14*14 -> 7*7
model.add(tf.keras.layers.Reshape(target_shape=(7 * 7 * 64,)))
# 傳入全連接層
model.add(tf.keras.layers.Dense(1024, activation="relu"))
model.add(tf.keras.layers.Dense(10, activation="softmax"))
# compile
model.compile(loss = "sparse_categorical_crossentropy",
optimizer = "sgd",
metrics = ["accuracy"])
模型訓(xùn)練
callbacks = [
tf.keras.callbacks.EarlyStopping(min_delta=1e-3, patience=5)
]
history = model.fit(X_train_scaled, y_train, epochs=15,
validation_data=(X_valid_scaled, y_valid),
callbacks = callbacks)
Train on 50000 samples, validate on 10000 samples
Epoch 1/15
50000/50000 [==============================] - 17s 343us/sample - loss: 0.5707 - accuracy: 0.7965 - val_loss: 0.4631 - val_accuracy: 0.8323
Epoch 2/15
50000/50000 [==============================] - 13s 259us/sample - loss: 0.3728 - accuracy: 0.8669 - val_loss: 0.3573 - val_accuracy: 0.8738
...
Epoch 13/15
50000/50000 [==============================] - 12s 244us/sample - loss: 0.1625 - accuracy: 0.9407 - val_loss: 0.2489 - val_accuracy: 0.9112
Epoch 14/15
50000/50000 [==============================] - 12s 240us/sample - loss: 0.1522 - accuracy: 0.9451 - val_loss: 0.2584 - val_accuracy: 0.9104
Epoch 15/15
50000/50000 [==============================] - 12s 237us/sample - loss: 0.1424 - accuracy: 0.9500 - val_loss: 0.2521 - val_accuracy: 0.9114
作圖
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
#plt.gca().set_ylim(0, 1)
plt.show()
plot_learning_curves(history)
測(cè)試集評(píng)估準(zhǔn)確率
model.evaluate(X_test_scaled, y_test)
[0.269884311157465, 0.9071]
可以看到使用CNN后,圖像分類的準(zhǔn)確率明顯提升了。之前的模型是0.8747,現(xiàn)在是0.9071。
3. 圖像分類 Dogs vs. Cats
3.1 原始數(shù)據(jù)
原始數(shù)據(jù)下載
Kaggle: /tupian/20230522/pp 百度網(wǎng)盤: /tupian/20230522/init 提取碼 dmp4
讀取一張圖片,并展示
image_string = tf.io.read_file("C:/Users/Skey/Downloads/datasets/cat_vs_dog/train/cat.28.jpg")
image_decoded = tf.image.decode_jpeg(image_string)
plt.imshow(image_decoded)
3.2 利用Dataset加載圖片
由于原始圖片過多,我們不能將所有圖片一次加載入內(nèi)存。Tensorflow為我們提供了便利的Dataset API,可以從硬盤中一批一批的加載數(shù)據(jù),以用于訓(xùn)練。
處理本地圖片路徑與標(biāo)簽
# 訓(xùn)練數(shù)據(jù)的路徑
train_dir = "C:/Users/Skey/Downloads/datasets/cat_vs_dog/train/"
train_filenames = [] # 所有圖片的文件名
train_labels = [] # 所有圖片的標(biāo)簽
for filename in os.listdir(train_dir):
train_filenames.append(train_dir + filename)
if (filename.startswith("cat")):
train_labels.append(0) # 將cat標(biāo)記為0
else:
train_labels.append(1) # 將dog標(biāo)記為1
# 數(shù)據(jù)隨機(jī)拆分鄭州人流哪家醫(yī)院做的好 http://www.csyhjlyy.com/
X_train, X_valid, y_train, y_valid = train_test_split(train_filenames, train_labels, test_size=0.2)
定義一個(gè)解碼圖片的方法
def _decode_and_resize(filename, label):
image_string = tf.io.read_file(filename) # 讀取圖片
image_decoded = tf.image.decode_jpeg(image_string) # 解碼
image_resized = tf.image.resize(image_decoded, [256, 256]) / 255.0 # 重置size,并歸一化
return image_resized, label
定義 Dataset,用于加載圖片數(shù)據(jù)
# 訓(xùn)練集
train_dataset = tf.data.Dataset.from_tensor_slices((train_filenames, train_labels))
train_dataset = train_dataset.map(
map_func=_decode_and_resize, # 調(diào)用前面定義的方法,解析filename,轉(zhuǎn)為特征和標(biāo)簽
num_parallel_calls=tf.data.experimental.AUTOTUNE)
train_dataset = train_dataset.shuffle(buffer_size=128) # 設(shè)置緩沖區(qū)大小
train_dataset = train_dataset.batch(32) # 每批數(shù)據(jù)的量
train_dataset = train_dataset.prefetch(tf.data.experimental.AUTOTUNE) # 啟動(dòng)預(yù)加載圖片,也就是說(shuō)CPU會(huì)提前從磁盤加載數(shù)據(jù),不用等上一次訓(xùn)練完后再加載
# 驗(yàn)證集
valid_dataset = tf.data.Dataset.from_tensor_slices((valid_filenames, valid_labels))
valid_dataset = valid_dataset.map(
map_func=_decode_and_resize,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
valid_dataset = valid_dataset.batch(32)
3.3 構(gòu)建CNN模型,并訓(xùn)練
構(gòu)建模型與編譯
model = tf.keras.Sequential([
# 卷積,32個(gè)filter(卷積核),每個(gè)大小為3*3,步長(zhǎng)為1
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(256, 256, 3)),
# 池化,默認(rèn)大小2*2,步長(zhǎng)為2
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 5, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(2, activation='softmax')
])
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=[tf.keras.metrics.sparse_categorical_accuracy]
)
模型總覽
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 254, 254, 32) 896
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 127, 127, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 123, 123, 32) 25632
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 61, 61, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 119072) 0
_________________________________________________________________
dense_2 (Dense) (None, 64) 7620672
_________________________________________________________________
dense_3 (Dense) (None, 2) 130
=================================================================
Total params: 7,647,330
Trainable params: 7,647,330
Non-trainable params: 0
開始訓(xùn)練
model.fit(train_dataset, epochs=10, validation_data=valid_dataset)
由于數(shù)據(jù)量大,此處訓(xùn)練時(shí)間較久
需要注意的是此處打印的step,每個(gè)step指的是一個(gè)batch(例如32個(gè)樣本一個(gè)batch)
模型評(píng)估
test_dataset = tf.data.Dataset.from_tensor_slices((valid_filenames, valid_labels))
test_dataset = test_dataset.map(_decode_and_resize)
test_dataset = test_dataset.batch(32)
print(model.metrics_names)
print(model.evaluate(test_dataset))
感謝各位的閱讀,以上就是“TensorFlow2的CNN圖像分類方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)TensorFlow2的CNN圖像分類方法是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
新聞名稱:TensorFlow2的CNN圖像分類方法是什么-創(chuàng)新互聯(lián)
文章出自:http://jinyejixie.com/article0/dieoio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站策劃、移動(dòng)網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站改版、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容