T8-猫狗识别
发布时间:2026/9/26 16:15:38来源:尧图网络
● 本文为365天深度学习训练营中的学习记录博客● 原作者K同学啊一、前期准备1.设置GPUimport tensorflow as tf gpus tf.config.list_physical_devices(GPU) if gpus: tf.config.experimental.set_memory_growth(gpus[0], True) #设置GPU显存用量按需使用 tf.config.set_visible_devices([gpus[0]],GPU)2.导入数据import matplotlib.pyplot as plt # 支持中文 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号 import os,PIL,pathlib #隐藏警告 import warnings warnings.filterwarnings(ignore) data_dir D:/新建文件夹/365-7-data data_dir pathlib.Path(data_dir) image_count len(list(data_dir.glob(*/*))) print(图片总数为,image_count)图片总数为 34003.加载数据batch_size 8 img_height 224 img_width 224 train_ds tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split0.2, subsettraining, seed12, image_size(img_height, img_width), batch_sizebatch_size) val_ds tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split0.2, subsetvalidation, seed12, image_size(img_height, img_width), batch_sizebatch_size)Found 3400 files belonging to 2 classes. Using 2720 files for training. Found 3400 files belonging to 2 classes. Using 680 files for validation.class_names train_ds.class_names print(class_names)[cat, dog]4.再次检查数据for image_batch, labels_batch in train_ds: print(image_batch.shape) print(labels_batch.shape) break(8, 224, 224, 3) (8,)5.配置数据集AUTOTUNE tf.data.AUTOTUNE def preprocess_image(image,label): return (image/255.0,label) # 归一化处理 train_ds train_ds.map(preprocess_image, num_parallel_callsAUTOTUNE) val_ds val_ds.map(preprocess_image, num_parallel_callsAUTOTUNE) train_ds train_ds.cache().shuffle(1000).prefetch(buffer_sizeAUTOTUNE) val_ds val_ds.cache().prefetch(buffer_sizeAUTOTUNE)6.数据可视化plt.figure(figsize(15, 10)) for images, labels in train_ds.take(1): for i in range(8): ax plt.subplot(5, 8, i 1) plt.imshow(images[i]) plt.title(class_names[labels[i]]) plt.axis(off)二、建立VGG-16模型from tensorflow.keras import layers, models, Input from tensorflow.keras.models import Model from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout def VGG16(nb_classes, input_shape): input_tensor Input(shapeinput_shape) # 1st block x Conv2D(64, (3,3), activationrelu, paddingsame,nameblock1_conv1)(input_tensor) x Conv2D(64, (3,3), activationrelu, paddingsame,nameblock1_conv2)(x) x MaxPooling2D((2,2), strides(2,2), name block1_pool)(x) # 2nd block x Conv2D(128, (3,3), activationrelu, paddingsame,nameblock2_conv1)(x) x Conv2D(128, (3,3), activationrelu, paddingsame,nameblock2_conv2)(x) x MaxPooling2D((2,2), strides(2,2), name block2_pool)(x) # 3rd block x Conv2D(256, (3,3), activationrelu, paddingsame,nameblock3_conv1)(x) x Conv2D(256, (3,3), activationrelu, paddingsame,nameblock3_conv2)(x) x Conv2D(256, (3,3), activationrelu, paddingsame,nameblock3_conv3)(x) x MaxPooling2D((2,2), strides(2,2), name block3_pool)(x) # 4th block x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock4_conv1)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock4_conv2)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock4_conv3)(x) x MaxPooling2D((2,2), strides(2,2), name block4_pool)(x) # 5th block x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock5_conv1)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock5_conv2)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock5_conv3)(x) x MaxPooling2D((2,2), strides(2,2), name block5_pool)(x) # full connection x Flatten()(x) x Dense(4096, activationrelu, namefc1)(x) x Dense(4096, activationrelu, namefc2)(x) output_tensor Dense(nb_classes, activationsoftmax, namepredictions)(x) model Model(input_tensor, output_tensor) return model modelVGG16(1000, (img_width, img_height, 3)) model.summary()三、编译model.compile(optimizeradam, loss sparse_categorical_crossentropy, metrics [accuracy])四、模型训练from tqdm import tqdm import tensorflow.keras.backend as K epochs 10 lr 1e-4 # 记录训练数据方便后面的分析 history_train_loss [] history_train_accuracy [] history_val_loss [] history_val_accuracy [] for epoch in range(epochs): train_total len(train_ds) val_total len(val_ds) with tqdm(totaltrain_total, descfEpoch {epoch 1}/{epochs},mininterval1,ncols100) as pbar: lr lr*0.92 K.set_value(model.optimizer.lr, lr) for image,label in train_ds: history model.train_on_batch(image,label) train_loss history[0] train_accuracy history[1] pbar.set_postfix({loss: %.4f%train_loss, accuracy:%.4f%train_accuracy, lr: K.get_value(model.optimizer.lr)}) pbar.update(1) history_train_loss.append(train_loss) history_train_accuracy.append(train_accuracy) print(开始验证) with tqdm(totalval_total, descfEpoch {epoch 1}/{epochs},mininterval0.3,ncols100) as pbar: for image,label in val_ds: history model.test_on_batch(image,label) val_loss history[0] val_accuracy history[1] pbar.set_postfix({loss: %.4f%val_loss, accuracy:%.4f%val_accuracy}) pbar.update(1) history_val_loss.append(val_loss) history_val_accuracy.append(val_accuracy) print(结束验证) print(验证loss为%.4f%val_loss) print(验证准确率为%.4f%val_accuracy)结果可视化from datetime import datetime current_time datetime.now() # 获取当前时间 epochs_range range(epochs) plt.figure(figsize(12, 4)) plt.subplot(1, 2, 1) plt.plot(epochs_range, history_train_accuracy, labelTraining Accuracy) plt.plot(epochs_range, history_val_accuracy, labelValidation Accuracy) plt.legend(loclower right) plt.title(Training and Validation Accuracy) plt.xlabel(current_time) plt.subplot(1, 2, 2) plt.plot(epochs_range, history_train_loss, labelTraining Loss) plt.plot(epochs_range, history_val_loss, labelValidation Loss) plt.legend(locupper right) plt.title(Training and Validation Loss) plt.show()可以发现从第二轮起训练集和验证集的准确率就达到100%且一直保持这个训练结果明显是不正常的回头检查代码可以发现VGG-16的网络结构中缺少了dropout层以及分类数写成了1000而不是2。对模型后面部分做了更改后x Flatten()(x) x Dense(4096, activationrelu, namefc1)(x) x Dropout(0.5)(x) x Dense(4096, activationrelu, namefc2)(x) x Dropout(0.5)(x) output_tensor Dense(nb_classes, activationsoftmax, namepredictions)(x) model Model(input_tensor, output_tensor) return model modelVGG16(2, (img_width, img_height, 3)) model.summary()此时训练集准确率只有50%乱猜级别个人总结本周使用VGG-16模型进行猫狗识别有问题后做了以下尝试使用VGG-16的迁移学习测试集准确率50%验证集37.5%去掉一个Dropout层结果同上把batch_size改成16验证集25%使用另一个简易模型loss没有任何变化。本周没有把代码改好下周参照文章再继续修改。
网站建设高端定制企业官网