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 pltIn [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.layersOut[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
Label | Latin Name | Common Name | Train Images | Validation 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 |
# 图片数据生成器 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.9766In [8]:
pd.DataFrame(history.history).plot(figsize=(8, 5)) plt.grid() plt.gca().set_ylim(0, 1.5) plt.show()In [9]:
model.evaluate(valid_generator)
9/9 [==============================] - 9s 962ms/step - loss: 0.1125 - acc: 0.9743Out[9]:
[0.11253786087036133, 0.9742646813392639]In [10]:
monkey = plt.imread('./10-monkey-species/n7013.jpg') plt.imshow(monkey) monkey.shapeOut[10]:
(800, 1200, 3)In [11]:
x = width / monkey.shape[0] y = height / monkey.shape[1] monkey_zoomed = ndimage.zoom(monkey, (x, y, 1)) monkey_zoomed.shapeOut[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/stepOut[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