python+keras:識(shí)別狗的品種,準(zhǔn)確率超過80%!
在這篇文章中,將教大家實(shí)現(xiàn)一個(gè)網(wǎng)頁應(yīng)用程序,該程序可以接收狗的圖片,然后輸出其品種,其準(zhǔn)確率超過80%!
我們將使用深度學(xué)習(xí)來訓(xùn)練一個(gè)識(shí)別狗品種的模型,數(shù)據(jù)集是狗圖像與他們的品種信息,通過學(xué)習(xí)圖像的特征來區(qū)分狗的品種。數(shù)據(jù)分析數(shù)據(jù)集可以從這里下載(https://s3-us-west-1.a(chǎn)mazonaws.com/udacity-aind/dog-project/dogImages.zip)。以下是關(guān)于數(shù)據(jù)的一些介紹:犬種總數(shù):133狗圖片總數(shù):8351(訓(xùn)練集:6680,驗(yàn)證集:835,測試集:836)最受歡迎的品種:阿拉斯加對應(yīng)96個(gè)樣本,博德牧羊犬對應(yīng)93個(gè)樣本按圖片數(shù)量排序的前30個(gè)品種如下:
我們還可以在這里看到一些狗的圖片和它們的品種:
數(shù)據(jù)預(yù)處理我們會(huì)把每個(gè)圖像作為一個(gè)numpy數(shù)組進(jìn)行加載,并將它們的大小調(diào)整為224x224,這是大多數(shù)傳統(tǒng)神經(jīng)網(wǎng)絡(luò)接受圖像的默認(rèn)大小,另外我們?yōu)閳D像的數(shù)量添加為另一個(gè)維度。from keras.preprocessing import image from tqdm import tqdm
def path_to_tensor(img_path): '''將給定路徑下的圖像轉(zhuǎn)換為張量''' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) return np.expand_dims(x, axis=0)
def paths_to_tensor(img_paths): '''將給定路徑中的所有圖像轉(zhuǎn)換為張量''' list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)] return np.vstack(list_of_tensors)最后,我們使用ImageDataGenerator對圖像進(jìn)行動(dòng)態(tài)縮放和增強(qiáng)train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, horizontal_flip=True, vertical_flip=True, rotation_range=20)
valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.)
test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.)
train_generator = train_datagen.flow(train_tensors, train_targets, batch_size=32)valid_generator = train_datagen.flow(valid_tensors, valid_targets, batch_size=32)test_generator = train_datagen.flow(test_tensors, test_targets, batch_size=32)CNN我們將在預(yù)處理數(shù)據(jù)集上從頭開始訓(xùn)練卷積神經(jīng)網(wǎng)絡(luò)(CNN),如下所示:model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(224, 224, 3)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(32, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(64, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(128, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(256, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(2048, activation='softmax'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(1024, activation='softmax'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(133, activation='softmax')])
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='../saved_models/weights_best_custom.hdf5', verbose=1, save_best_only=True)

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個(gè)字
最新活動(dòng)更多
-
6月20日立即下載>> 【白皮書】精準(zhǔn)測量 安全高效——福祿克光伏行業(yè)解決方案
-
7月3日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身機(jī)器人動(dòng)力電池技術(shù)應(yīng)用大會(huì)
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
推薦專題
- 1 AI 眼鏡讓百萬 APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語權(quán)
- 3 深度報(bào)告|中國AI產(chǎn)業(yè)正在崛起成全球力量,市場潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級獨(dú)角獸:獲上市公司戰(zhàn)投,干人形機(jī)器人
- 5 國家數(shù)據(jù)局局長劉烈宏調(diào)研格創(chuàng)東智
- 6 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 7 百億AI芯片訂單,瘋狂傾銷中東?
- 8 Robotaxi新消息密集釋放,量產(chǎn)元年誰在領(lǐng)跑?
- 9 格斗大賽出圈!人形機(jī)器人致命短板曝光:頭腦過于簡單
- 10 一文看懂視覺語言動(dòng)作模型(VLA)及其應(yīng)用