第T5周:Tensorflow实现运动鞋品牌识别

目标

根据鞋子的品牌logo判断鞋子所属的品牌

具体实现

(一)环境

语言环境:Python 3.10
编 译 器: PyCharm
框 架: Tensorflow 2.10.0

(二)具体步骤
1.查询tf版本及使用GPU
import pathlib
import matplotlib.pyplot as plt
import PIL.Image
import tensorflow as tf
from tensorflow.keras import models, layers  
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
  
print(tf.__version__)  
  
gpus = tf.config.list_physical_devices("GPU")  
print(gpus)  
if gpus:  
    gpu0 = gpus[0]      # 如果有多个GPU,仅使用0号GPU  
    tf.config.experimental.set_memory_growth(gpu0, True)    # 设置GPU显存按需使用  
    tf.config.set_visible_devices([gpu0], "GPU")    # 指定运行时GPU
2.10.0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2.导入数据

本次数据放在根目录的datasets/shoes文件夹下:

# 导入数据  
data_dir = "./datasets/shoes/"  
data_dir = pathlib.Path(data_dir)  
image_count = len(list(data_dir.glob('*/*/*.jpg')))  
print("图片总数为:", image_count)  
  
nike_shoes = list(data_dir.glob('train/nike/*.jpg'))  
print("NIKE品牌鞋子图片数量为:", len(nike_shoes))  
adidas_shoes = list(data_dir.glob('train/adidas/*.jpg'))  
print("ADIDAS品牌鞋子图片数量为:", len(adidas_shoes))  
  
shoes = PIL.Image.open(nike_shoes[0])  
shoes.show()  
  
shoes = PIL.Image.open(adidas_shoes[0])  
shoes.show()
图片总数为: 578
NIKE品牌鞋子图片数量为: 251
ADIDAS品牌鞋子图片数量为: 251

image.png
image.png

3.加载数据
# 加载数据  
batch_size = 32  
image_height = 224  
image_width = 224  
  
train_ds = tf.keras.preprocessing.image_dataset_from_directory(  
    directory="./datasets/shoes/train/",  
    seed=123,  
    image_size=(image_height, image_width),  
    batch_size=batch_size  
)  
  
val_ds = tf.keras.preprocessing.image_dataset_from_directory(  
    directory='./datasets/shoes/test/',  
    seed=123,  
    image_size=(image_height, image_width),  
    batch_size=batch_size  
)  
class_names = train_ds.class_names  
print(class_names)  
  
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()
Found 502 files belonging to 2 classes.
Found 76 files belonging to 2 classes.
['adidas', 'nike']

image.png

4.检查数据
# 检查数据  
for image_batch, labels_batch in train_ds:  
    print(image_batch.shape)  
    print(labels_batch.shape)  
    break
(32, 224, 224, 3)
(32,)
5.配置数据集
# 配置数据集  
AUTOTUNE = tf.data.AUTOTUNE  
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)  
val_ds = val_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
6.构建CNN网络

CNN的输入是张量形式的(长、宽、颜色通道 ),例如(224,224,3)。看下面的图输入层要求的长,宽都是224,彩色图片是

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值