首页 > 其他分享 >TensorFlow迁移学习DenseNet121预测10-monkey-species

TensorFlow迁移学习DenseNet121预测10-monkey-species

时间:2025-01-22 17:33:43浏览次数:1  
标签:acc 10 monkey val loss DenseNet121 TensorFlow 34

  In [1]:

from tensorflow import keras
import tensorflow as tf
import numpy as np
import pandas as pd
from scipy import ndimage
import matplotlib.pyplot as plt
  In [14]:
densenet121 = keras.applications.DenseNet121(include_top=False, pooling='avg', weights='imagenet')
  In [3]:
for layer in densenet121.layers[0:-5]:
    layer.trainable = False

classes = 10
model = keras.models.Sequential([densenet121, keras.layers.Dense(classes, 'softmax')])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
model.summary()
   
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 densenet121 (Functional)    (None, 1024)              7037504                                                 
 dense (Dense)               (None, 10)                10250                                                 
=================================================================
Total params: 7047754 (26.89 MB)
Trainable params: 49162 (192.04 KB)
Non-trainable params: 6998592 (26.70 MB)
_________________________________________________________________
  In [4]:
model.layers
  Out[4]:
[<keras.src.engine.functional.Functional at 0x2b080120520>,
 <keras.src.layers.core.dense.Dense at 0x2b0801332e0>]
  In [5]:
# 文件下载地址  https://www.kaggle.com/datasets/slothkong/10-monkey-species
train_dir = './10-monkey-species/training/training'
valid_dir = './10-monkey-species/validation/validation'
label_file = './10-monkey-species/monkey_labels.txt'
df = pd.read_csv(label_file, header=0)
df
 LabelLatin NameCommon NameTrain ImagesValidation Images
0 n0 alouatta_palliata\t mantled_howler 131 26
1 n1 erythrocebus_patas\t patas_monkey 139 28
2 n2 cacajao_calvus\t bald_uakari 137 27
3 n3 macaca_fuscata\t japanese_macaque 152 30
4 n4 cebuella_pygmea\t pygmy_marmoset 131 26
5 n5 cebus_capucinus\t white_headed_capuchin 141 28
6 n6 mico_argentatus\t silvery_marmoset 132 26
7 n7 saimiri_sciureus\t common_squirrel_monkey 142 28
8 n8 aotus_nigriceps\t black_headed_night_monkey 133 27
9 n9 trachypithecus_johnii nilgiri_langur 132 26
  In [6]:
# 图片数据生成器
height = 224
width = 224
channels = 3
batch_size = 32
classes = 10
train_datagen = keras.preprocessing.image.ImageDataGenerator(
    preprocessing_function = keras.applications.densenet.preprocess_input,    # 使用原版densenet自身的数据处理
    rotation_range = 40,   # 随机旋转 0~40°之间
    width_shift_range = 0.2, # 随机水平移动
    height_shift_range = 0.2, # 随机垂直移动
    shear_range = 0.2, # 随机裁剪比例
    zoom_range = 0.2, # 随机缩放比例
    horizontal_flip = True,  # 随机水平翻转
    vertical_flip = True, # 随机垂直翻转
    fill_mode = 'nearest', # 填充模式
)
train_generator = train_datagen.flow_from_directory(train_dir, target_size=(height, width),
                                                    batch_size=batch_size, shuffle=True, class_mode='categorical')

valid_datagen = keras.preprocessing.image.ImageDataGenerator(
    preprocessing_function = keras.applications.densenet.preprocess_input
)
valid_generator = valid_datagen.flow_from_directory(valid_dir, target_size=(height, width),
                                                    batch_size=batch_size, shuffle=False, class_mode='categorical')
   
Found 1098 images belonging to 10 classes.
Found 272 images belonging to 10 classes.
  In [7]:
# 训练
history = model.fit(train_generator, steps_per_epoch=train_generator.samples//batch_size, epochs=10,
                    validation_data=valid_generator, validation_steps=valid_generator.samples//batch_size)
   
Epoch 1/10
34/34 [==============================] - 48s 1s/step - loss: 1.8848 - acc: 0.3865 - val_loss: 0.9695 - val_acc: 0.8281
Epoch 2/10
34/34 [==============================] - 44s 1s/step - loss: 0.9487 - acc: 0.7936 - val_loss: 0.4936 - val_acc: 0.9297
Epoch 3/10
34/34 [==============================] - 44s 1s/step - loss: 0.6071 - acc: 0.8715 - val_loss: 0.3262 - val_acc: 0.9531
Epoch 4/10
34/34 [==============================] - 45s 1s/step - loss: 0.4350 - acc: 0.9174 - val_loss: 0.2489 - val_acc: 0.9570
Epoch 5/10
34/34 [==============================] - 44s 1s/step - loss: 0.3377 - acc: 0.9437 - val_loss: 0.1934 - val_acc: 0.9688
Epoch 6/10
34/34 [==============================] - 45s 1s/step - loss: 0.3038 - acc: 0.9456 - val_loss: 0.1808 - val_acc: 0.9688
Epoch 7/10
34/34 [==============================] - 45s 1s/step - loss: 0.2753 - acc: 0.9447 - val_loss: 0.1394 - val_acc: 0.9727
Epoch 8/10
34/34 [==============================] - 44s 1s/step - loss: 0.2277 - acc: 0.9512 - val_loss: 0.1294 - val_acc: 0.9727
Epoch 9/10
34/34 [==============================] - 47s 1s/step - loss: 0.2135 - acc: 0.9568 - val_loss: 0.1096 - val_acc: 0.9805
Epoch 10/10
34/34 [==============================] - 46s 1s/step - loss: 0.1887 - acc: 0.9644 - val_loss: 0.1096 - val_acc: 0.9766
  In [8]:
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid()
plt.gca().set_ylim(0, 1.5)
plt.show()
    No description has been provided for this image   In [9]:
model.evaluate(valid_generator)
   
9/9 [==============================] - 9s 962ms/step - loss: 0.1125 - acc: 0.9743
Out[9]:
[0.11253786087036133, 0.9742646813392639]
  In [10]:
monkey = plt.imread('./10-monkey-species/n7013.jpg')
plt.imshow(monkey)
monkey.shape
  Out[10]:
(800, 1200, 3)
  No description has been provided for this image   In [11]:
x = width / monkey.shape[0]
y = height / monkey.shape[1]
monkey_zoomed = ndimage.zoom(monkey, (x, y, 1))
monkey_zoomed.shape
  Out[11]:
(224, 224, 3)
  In [12]:
monkey_zoomed = keras.applications.densenet.preprocess_input(monkey_zoomed)
monkey_zoomed = monkey_zoomed.reshape(1, width, height, 3)
predict = model.predict(monkey_zoomed)
predict
   
1/1 [==============================] - 1s 984ms/step
Out[12]:
array([[1.2785053e-03, 1.1586951e-03, 1.1033794e-03, 8.9743080e-05,
        6.6979276e-04, 6.0248189e-02, 1.6423552e-03, 9.3309772e-01,
        5.2002951e-04, 1.9154577e-04]], dtype=float32)
  In [13]:
predict.argmax(axis=1)    # 预测正确
  Out[13]:
array([7], dtype=int64)

标签:acc,10,monkey,val,loss,DenseNet121,TensorFlow,34
From: https://www.cnblogs.com/funsion/p/18686479

相关文章

  • 【C++BFS算法】1210. 穿过迷宫的最少移动次数|2022
    本文涉及知识点C++BFS算法LeetCode1210.穿过迷宫的最少移动次数你还记得那条风靡全球的贪吃蛇吗?我们在一个n*n的网格上构建了新的迷宫地图,蛇的长度为2,也就是说它会占去两个单元格。蛇会从左上角((0,0)和(0,1))开始移动。我们用0表示空单元格,用1表示障碍物。蛇......
  • 折腾笔记[10]-使用rust进行ORB角点检测
    摘要打包ORB算法到bye_orb_rs库,使用rust进行ORB角点检测.PackagetheORBalgorithmintothebye_orb_rslibrary,anduseRustforORBcornerdetection.关键词rust;ORB;FAST;slam;关键信息项目地址:[https://github.com/ByeIO/slambook2.rs][package]name="exp65-......
  • 深化Edge AI 应用:德承工控机GM-1100安装Ubuntu 24.04.1 LTS系统操作指南
    EdgeAI:边缘运算(EdgeComputing)结合人工智能(AI),将AI模型和算法安排在负责处理边缘运算的工控机上,除了能够就近撷取设备端的数据外,还能够进行资料处理与机器学习的任务,透过EdgeAI,不再需要将大量数据传到云端服务器,有效缩短处理时间、提高反应速度,还能够降低对于网络带宽的需求也更......
  • TensorFlow迁移学习Resnet50预测10-monkey-species
     In [15]:fromtensorflowimportkerasimporttensorflowastfimportnumpyasnpimportpandasaspdfromscipyimportndimageimportmatplotlib.pyplotasplt In [2]:resnet50=keras.applications.ResNet50(include_top=False,po......
  • TensorFlow迁移学习Resnet50预测10-monkey-species
     In [15]:fromtensorflowimportkerasimporttensorflowastfimportnumpyasnpimportpandasaspdfromscipyimportndimageimportmatplotlib.pyplotasplt In [2]:resnet50=keras.applications.ResNet50(include_top=False,po......
  • 全志 F1C100A Melis OS 公版 SDK
    全志F1C100AMelisOS公版SDK资源文件列表C100A_zyac/clean_o.bat , 18C100A_zyac/eLibs/config/suni/assembler.via , 141C100A_zyac/eLibs/config/suni/compiler.via , 692C100A_zyac/eLibs/config/sunii/assembler.via , 141C100A_zyac/eLibs/config/sunii/co......
  • 使用Python3.8写的代码比Python3.10写的性能差吗?
    一般情况下,Python3.10的性能是要好于Python3.8的。那么是否意味着同等条件下,使用Python3.8写出来的代码要比Python3.10写出来的代码性能差呢?笔者曾经写过一个项目,项目一开始使用Python3.8。重构时,因为3.8不支持某些功能,一度将Python版本升到了Python3.10。升到3.10......
  • 自学网络安全(黑客技术)2025年 —100天学习计划
    前言什么是网络安全网络安全可以基于攻击和防御视角来分类,我们经常听到的“红队”、“渗透测试”等就是研究攻击技术,而“蓝队”、“安全运营”、“安全运维”则研究防御技术。如何成为一名黑客很多朋友在学习安全方面都会半路转行,因为不知如何去学,在这里,我将这个整......
  • Cursor太贵?字节Trae可免费用Claude,10分钟带你实现全栈开发
    作为一名开发者,你是否曾为了提高开发效率,尝试过各种AI编程工具?或者你不会开发,但想借助AI编程将自己的想法落地?Cursor无疑是其中的佼佼者,但其高昂的订阅费用也让不少开发者望而却步。现在,你的福音来了!字节跳动推出了一款全新的AIIDE——Trae。它不仅提供了原生中文支持和人......
  • OpenWRT24.10旁路由挂载USB移动硬盘,配置Samba4,作为NAS使用,解决中文不显示,乱码,解决断电
    1.为何选择OpenWRT24.10,及如何配置旁路由,或者IPv6地址看这篇:参OpenWRT24.10配置作为旁路由,并配置获取IPv4和IPv6地址使用的OpenWRT固件是从这里下载的:https://openwrt.ai/2.挂载大容量USB移动硬盘2.1安装必备插件kmod-fs-ntfs3kmod-fs-ext4kmod-fs-exfat#根据自己的......