深度学习笔记15_TensorFlow实现运动鞋品牌识别

2024-09-06 18:28

本文主要是介绍深度学习笔记15_TensorFlow实现运动鞋品牌识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊 | 接辅导、项目定制

一、我的环境

1.语言环境:Python 3.9

2.编译器:Pycharm

3.深度学习环境:TensorFlow 2.10.0

二、GPU设置

       若使用的是cpu则可忽略

import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")if gpus:gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPUtf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用tf.config.set_visible_devices([gpu0],"GPU")

、导入数据

data_dir = "./data/"
data_dir = pathlib.Path(data_dir)image_count = len(list(data_dir.glob('*/*/*.jpg')))print("图片总数为:",image_count)
#图片总数为:578

、数据预处理

batch_size = 32
img_height = 224
img_width = 224"""
关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789
"""
train_ds = tf.keras.preprocessing.image_dataset_from_directory("./data/train/",seed=123,image_size=(img_height, img_width),batch_size=batch_size)"""
关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789
"""
val_ds = tf.keras.preprocessing.image_dataset_from_directory("./data/test/",seed=123,image_size=(img_height, img_width),batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)

运行结果: 

['adidas', 'nike']

五、可视化图片

plt.figure(figsize=(20, 10))for images, labels in train_ds.take(1):for i in range(20):ax = plt.subplot(5, 10, i + 1)plt.imshow(images[i].numpy().astype("uint8"))plt.title(class_names[labels[i]])plt.axis("off")
plt.show()

 运行结果:

​​

再次检查数据:

for image_batch, labels_batch in train_ds:print(image_batch.shape)print(labels_batch.shape)break

 运行结果:

(32, 224, 224, 3)
(32,)

六、配置数据集

  • shuffle():打乱数据,关于此函数的详细介绍可以参考:https://zhuanlan.zhihu.com/p/42417456
  • prefetch():预取数据,加速运行
  • cache():将数据集缓存到内存当中,加速运行
AUTOTUNE = tf.data.AUTOTUNEtrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

七、构建CNN网络模型

        卷积神经网络(CNN)的输入是张量 (Tensor) 形式的 (image_height, image_width, color_channels),包含了图像高度、宽度及颜色信息。不需要输入batch size。color_channels 为 (R,G,B) 分别对应 RGB 的三个颜色通道(color channel)。在此示例中,我们的 CNN 输入形状是 (180, 180, 3)。我们需要在声明第一层时将形状赋值给参数input_shape

"""
关于卷积核的计算不懂的可以参考文章:https://blog.csdn.net/qq_38251616/article/details/114278995layers.Dropout(0.4) 作用是防止过拟合,提高模型的泛化能力。
关于Dropout层的更多介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/115826689
"""model = models.Sequential([layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),layers.Conv2D(16, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)), # 卷积层1,卷积核3*3  layers.AveragePooling2D((2, 2)),               # 池化层1,2*2采样layers.Conv2D(32, (3, 3), activation='relu'),  # 卷积层2,卷积核3*3layers.AveragePooling2D((2, 2)),               # 池化层2,2*2采样layers.Dropout(0.3),  layers.Conv2D(64, (3, 3), activation='relu'),  # 卷积层3,卷积核3*3layers.Dropout(0.3),  layers.Flatten(),                       # Flatten层,连接卷积层与全连接层layers.Dense(128, activation='relu'),   # 全连接层,特征进一步提取layers.Dense(len(class_names))               # 输出层,输出预期结果
])model.summary()  # 打印网络结构

运行结果:

_________________________________________________________________Layer (type)                Output Shape              Param #
=================================================================rescaling (Rescaling)       (None, 224, 224, 3)       0conv2d (Conv2D)             (None, 222, 222, 16)      448average_pooling2d (AverageP  (None, 111, 111, 16)     0ooling2D)conv2d_1 (Conv2D)           (None, 109, 109, 32)      4640average_pooling2d_1 (Averag  (None, 54, 54, 32)       0ePooling2D)dropout (Dropout)           (None, 54, 54, 32)        0conv2d_2 (Conv2D)           (None, 52, 52, 64)        18496dropout_1 (Dropout)         (None, 52, 52, 64)        0flatten (Flatten)           (None, 173056)            0dense (Dense)               (None, 128)               22151296dense_1 (Dense)             (None, 2)                 258=================================================================
Total params: 22,175,138
Trainable params: 22,175,138
Non-trainable params: 0
_________________________________________________________________

八、编译

        在准备对模型进行训练之前,还需要再对其进行一些设置。以下内容是在模型的编译步骤中添加的:

  • 损失函数(loss):用于衡量模型在训练期间的准确率。
  • 优化器(optimizer):决定模型如何根据其看到的数据和自身的损失函数进行更新。
  • 指标(metrics):用于监控训练和测试步骤。以下示例使用了准确率,即被正确分类的图像的比率。
# 设置初始学习率
initial_learning_rate = 0.001lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate, decay_steps=10,      # 敲黑板!!!这里是指 steps,不是指epochsdecay_rate=0.92,     # lr经过一次衰减就会变成 decay_rate*lrstaircase=True)# 将指数衰减学习率送入优化器
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)model.compile(optimizer=optimizer,loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])

 

早停与保存最佳模型参数

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStoppingepochs = 50# 保存最佳模型参数
checkpointer = ModelCheckpoint('best_model.h5',monitor='val_accuracy',verbose=1,save_best_only=True,save_weights_only=True)# 设置早停
earlystopper = EarlyStopping(monitor='val_accuracy', min_delta=0.001,patience=20, verbose=1)

九、训练模型

history = model.fit(train_ds,validation_data=val_ds,epochs=epochs,callbacks=[checkpointer, earlystopper])

运行结果:

Epoch 1/50
16/16 [==============================] - ETA: 0s - loss: 3.6308 - accuracy: 0.5000
Epoch 1: val_accuracy improved from -inf to 0.48684, saving model to best_model.h5
16/16 [==============================] - 7s 73ms/step - loss: 3.6308 - accuracy: 0.5000 - val_loss: 0.6932 - val_accuracy: 0.4868
Epoch 2/50
16/16 [==============================] - ETA: 0s - loss: 0.6951 - accuracy: 0.4880
Epoch 2: val_accuracy improved from 0.48684 to 0.50000, saving model to best_model.h5
16/16 [==============================] - 1s 40ms/step - loss: 0.6951 - accuracy: 0.4880 - val_loss: 0.6949 - val_accuracy: 0.5000
Epoch 3/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6928 - accuracy: 0.4979
Epoch 3: val_accuracy did not improve from 0.50000
16/16 [==============================] - 1s 33ms/step - loss: 0.6927 - accuracy: 0.5060 - val_loss: 0.6932 - val_accuracy: 0.5000
Epoch 4/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6922 - accuracy: 0.5553
Epoch 4: val_accuracy improved from 0.50000 to 0.51316, saving model to best_model.h5
16/16 [==============================] - 1s 41ms/step - loss: 0.6920 - accuracy: 0.5578 - val_loss: 0.6925 - val_accuracy: 0.5132
Epoch 5/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6894 - accuracy: 0.5574
Epoch 5: val_accuracy improved from 0.51316 to 0.65789, saving model to best_model.h5
16/16 [==============================] - 1s 39ms/step - loss: 0.6890 - accuracy: 0.5697 - val_loss: 0.6891 - val_accuracy: 0.6579
Epoch 6/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6883 - accuracy: 0.5340
Epoch 6: val_accuracy did not improve from 0.65789
16/16 [==============================] - 1s 33ms/step - loss: 0.6882 - accuracy: 0.5339 - val_loss: 0.6823 - val_accuracy: 0.6184
Epoch 7/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6810 - accuracy: 0.6191
Epoch 7: val_accuracy did not improve from 0.65789
16/16 [==============================] - 1s 33ms/step - loss: 0.6805 - accuracy: 0.6155 - val_loss: 0.6774 - val_accuracy: 0.6316
Epoch 8/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6737 - accuracy: 0.6043
Epoch 8: val_accuracy improved from 0.65789 to 0.71053, saving model to best_model.h5
16/16 [==============================] - 1s 39ms/step - loss: 0.6738 - accuracy: 0.5996 - val_loss: 0.6608 - val_accuracy: 0.7105
Epoch 9/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6461 - accuracy: 0.6979
Epoch 9: val_accuracy did not improve from 0.71053
16/16 [==============================] - 1s 33ms/step - loss: 0.6424 - accuracy: 0.7012 - val_loss: 0.6200 - val_accuracy: 0.6974
Epoch 10/50
15/16 [===========================>..] - ETA: 0s - loss: 0.6148 - accuracy: 0.6979
Epoch 10: val_accuracy did not improve from 0.71053
16/16 [==============================] - 1s 34ms/step - loss: 0.6114 - accuracy: 0.6972 - val_loss: 0.6302 - val_accuracy: 0.6316
Epoch 11/50
15/16 [===========================>..] - ETA: 0s - loss: 0.5956 - accuracy: 0.7234
Epoch 11: val_accuracy improved from 0.71053 to 0.73684, saving model to best_model.h5
16/16 [==============================] - 1s 39ms/step - loss: 0.5968 - accuracy: 0.7191 - val_loss: 0.5779 - val_accuracy: 0.7368
Epoch 12/50
15/16 [===========================>..] - ETA: 0s - loss: 0.5442 - accuracy: 0.7723
Epoch 12: val_accuracy did not improve from 0.73684
16/16 [==============================] - 1s 33ms/step - loss: 0.5505 - accuracy: 0.7570 - val_loss: 0.6001 - val_accuracy: 0.6579
Epoch 13/50
15/16 [===========================>..] - ETA: 0s - loss: 0.5566 - accuracy: 0.7298
Epoch 13: val_accuracy improved from 0.73684 to 0.75000, saving model to best_model.h5
16/16 [==============================] - 1s 40ms/step - loss: 0.5581 - accuracy: 0.7251 - val_loss: 0.5442 - val_accuracy: 0.7500
Epoch 14/50
15/16 [===========================>..] - ETA: 0s - loss: 0.5194 - accuracy: 0.7617
Epoch 14: val_accuracy did not improve from 0.75000
16/16 [==============================] - 1s 33ms/step - loss: 0.5200 - accuracy: 0.7629 - val_loss: 0.5347 - val_accuracy: 0.7368
Epoch 15/50
15/16 [===========================>..] - ETA: 0s - loss: 0.5114 - accuracy: 0.7681
Epoch 15: val_accuracy did not improve from 0.75000
16/16 [==============================] - 1s 33ms/step - loss: 0.5048 - accuracy: 0.7769 - val_loss: 0.5161 - val_accuracy: 0.7500
Epoch 16/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4836 - accuracy: 0.7830
Epoch 16: val_accuracy improved from 0.75000 to 0.76316, saving model to best_model.h5
16/16 [==============================] - 1s 40ms/step - loss: 0.4901 - accuracy: 0.7789 - val_loss: 0.5069 - val_accuracy: 0.7632
Epoch 17/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4636 - accuracy: 0.7809
Epoch 17: val_accuracy did not improve from 0.76316
16/16 [==============================] - 1s 33ms/step - loss: 0.4585 - accuracy: 0.7888 - val_loss: 0.5071 - val_accuracy: 0.7500
Epoch 18/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4717 - accuracy: 0.7723
Epoch 18: val_accuracy did not improve from 0.76316
16/16 [==============================] - 1s 34ms/step - loss: 0.4655 - accuracy: 0.7769 - val_loss: 0.5034 - val_accuracy: 0.7368
Epoch 19/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4610 - accuracy: 0.8064
Epoch 19: val_accuracy did not improve from 0.76316
16/16 [==============================] - 1s 33ms/step - loss: 0.4567 - accuracy: 0.8088 - val_loss: 0.5440 - val_accuracy: 0.7368
Epoch 20/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4547 - accuracy: 0.7872
Epoch 20: val_accuracy improved from 0.76316 to 0.78947, saving model to best_model.h5
16/16 [==============================] - 1s 40ms/step - loss: 0.4507 - accuracy: 0.7948 - val_loss: 0.4812 - val_accuracy: 0.7895
Epoch 21/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4228 - accuracy: 0.8298
Epoch 21: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 33ms/step - loss: 0.4238 - accuracy: 0.8287 - val_loss: 0.4926 - val_accuracy: 0.7632
Epoch 22/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4460 - accuracy: 0.8125
Epoch 22: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 33ms/step - loss: 0.4386 - accuracy: 0.8187 - val_loss: 0.4857 - val_accuracy: 0.7632
Epoch 23/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4262 - accuracy: 0.8167
Epoch 23: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 34ms/step - loss: 0.4204 - accuracy: 0.8227 - val_loss: 0.4718 - val_accuracy: 0.7632
Epoch 24/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4196 - accuracy: 0.8277
Epoch 24: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 33ms/step - loss: 0.4208 - accuracy: 0.8247 - val_loss: 0.5068 - val_accuracy: 0.7632
Epoch 25/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4112 - accuracy: 0.8362
Epoch 25: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 33ms/step - loss: 0.4118 - accuracy: 0.8347 - val_loss: 0.4658 - val_accuracy: 0.7895
Epoch 26/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4005 - accuracy: 0.8298
Epoch 26: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 34ms/step - loss: 0.3981 - accuracy: 0.8347 - val_loss: 0.4822 - val_accuracy: 0.7632
Epoch 27/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4003 - accuracy: 0.8426
Epoch 27: val_accuracy did not improve from 0.78947
16/16 [==============================] - 1s 34ms/step - loss: 0.4038 - accuracy: 0.8406 - val_loss: 0.4756 - val_accuracy: 0.7763
Epoch 28/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3884 - accuracy: 0.8511
Epoch 28: val_accuracy improved from 0.78947 to 0.80263, saving model to best_model.h5
16/16 [==============================] - 1s 40ms/step - loss: 0.3967 - accuracy: 0.8486 - val_loss: 0.4636 - val_accuracy: 0.8026
Epoch 29/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4139 - accuracy: 0.8489
Epoch 29: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 33ms/step - loss: 0.4091 - accuracy: 0.8486 - val_loss: 0.4735 - val_accuracy: 0.7763
Epoch 30/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3857 - accuracy: 0.8617
Epoch 30: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3870 - accuracy: 0.8586 - val_loss: 0.4655 - val_accuracy: 0.7763
Epoch 31/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3853 - accuracy: 0.8447
Epoch 31: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 33ms/step - loss: 0.3908 - accuracy: 0.8347 - val_loss: 0.4688 - val_accuracy: 0.7763
Epoch 32/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3814 - accuracy: 0.8596
Epoch 32: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3869 - accuracy: 0.8546 - val_loss: 0.4728 - val_accuracy: 0.7632
Epoch 33/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3938 - accuracy: 0.8396
Epoch 33: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3887 - accuracy: 0.8446 - val_loss: 0.4798 - val_accuracy: 0.7763
Epoch 34/50
15/16 [===========================>..] - ETA: 0s - loss: 0.4032 - accuracy: 0.8542
Epoch 34: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3955 - accuracy: 0.8586 - val_loss: 0.4708 - val_accuracy: 0.7632
Epoch 35/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3937 - accuracy: 0.8375
Epoch 35: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3865 - accuracy: 0.8426 - val_loss: 0.4695 - val_accuracy: 0.7632
Epoch 36/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3883 - accuracy: 0.8447
Epoch 36: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3862 - accuracy: 0.8486 - val_loss: 0.4700 - val_accuracy: 0.7632
Epoch 37/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3729 - accuracy: 0.8617
Epoch 37: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 33ms/step - loss: 0.3767 - accuracy: 0.8586 - val_loss: 0.4685 - val_accuracy: 0.7632
Epoch 38/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3831 - accuracy: 0.8479
Epoch 38: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3788 - accuracy: 0.8506 - val_loss: 0.4720 - val_accuracy: 0.7632
Epoch 39/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3872 - accuracy: 0.8468
Epoch 39: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 33ms/step - loss: 0.3872 - accuracy: 0.8466 - val_loss: 0.4648 - val_accuracy: 0.7895
Epoch 40/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3800 - accuracy: 0.8489
Epoch 40: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3739 - accuracy: 0.8546 - val_loss: 0.4682 - val_accuracy: 0.7632
Epoch 41/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3800 - accuracy: 0.8511
Epoch 41: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3813 - accuracy: 0.8486 - val_loss: 0.4649 - val_accuracy: 0.7895
Epoch 42/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3727 - accuracy: 0.8617
Epoch 42: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3712 - accuracy: 0.8645 - val_loss: 0.4675 - val_accuracy: 0.7632
Epoch 43/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3803 - accuracy: 0.8468
Epoch 43: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3830 - accuracy: 0.8486 - val_loss: 0.4672 - val_accuracy: 0.7632
Epoch 44/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3648 - accuracy: 0.8745
Epoch 44: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3698 - accuracy: 0.8705 - val_loss: 0.4708 - val_accuracy: 0.7632
Epoch 45/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3742 - accuracy: 0.8489
Epoch 45: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 33ms/step - loss: 0.3695 - accuracy: 0.8546 - val_loss: 0.4683 - val_accuracy: 0.7632
Epoch 46/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3792 - accuracy: 0.8447
Epoch 46: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 33ms/step - loss: 0.3878 - accuracy: 0.8406 - val_loss: 0.4706 - val_accuracy: 0.7632
Epoch 47/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3681 - accuracy: 0.8745
Epoch 47: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3639 - accuracy: 0.8745 - val_loss: 0.4708 - val_accuracy: 0.7632
Epoch 48/50
15/16 [===========================>..] - ETA: 0s - loss: 0.3771 - accuracy: 0.8489
Epoch 48: val_accuracy did not improve from 0.80263
16/16 [==============================] - 1s 34ms/step - loss: 0.3778 - accuracy: 0.8506 - val_loss: 0.4729 - val_accuracy: 0.7632
Epoch 48: early stopping
1/1 [==============================] - 0s 100ms/step

 十、模型评估

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']loss = history.history['loss']
val_loss = history.history['val_loss']epochs_range = range(len(loss))plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

十一、指定图片预测

# 加载效果最好的模型权重
model.load_weights('best_model.h5')
from PIL import Image
import numpy as npimg = Image.open("./data/test/nike/1.jpg")  #这里选择你需要预测的图片
image = tf.image.resize(img, [img_height, img_width])img_array = tf.expand_dims(image, 0) #/255.0  # 记得做归一化处理(与训练集处理方式保持一致)predictions = model.predict(img_array) # 这里选用你已经训练好的模型
print("预测结果为:",class_names[np.argmax(predictions)])

运行结果:

预测结果为: nike

十二、总结

   本周通过学习TensorFlow实现运动鞋品牌识别;首先学习设置动态学习率,在训练神经网络时动态地降低学习率,可以帮助优化算法更有效地收敛到全局最小值,从而提高模型的性能。其次就是学习早停与保存最佳模型参数,模型在指定epoch次都没有提升的情况下,可以提前停止训练。

这篇关于深度学习笔记15_TensorFlow实现运动鞋品牌识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142781

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time