目录
本博客代码,划分好的数据集,自己训练的模型:
github:https://github.com/AlannahYYL/keras_vgg16_dogs-vs-cats
数据集:https://download.youkuaiyun.com/download/qq_31681523/16699667
模型:https://download.youkuaiyun.com/download/qq_31681523/16709305
一.数据集
数据集:Cats vs. Dogs
官网下载地址:https://www.kaggle.com/c/dogs-vs-cats
博主只下载了train.zip,包含猫狗各12500张,在数据集预处理文件中将生成数据集。
二.项目环境
keras==2.1.5 tensorflow==1.15 python==3.6 numpy==1.19.2
三.项目内容
部分配置参数详见工程代码文件config.py
1.数据集预处理 data_pre.py
解压train.zip,本文件将猫狗各12500张图片进行数据集划分,猫和狗前11000张分为训练集,后1500张分为验证集。划分后文件结构和代码如下:
文件结构:
代码
'''
数据预处理,将数据分为训练集、测试集
原train文件中共有猫狗图片各12500,将其中前10000用作训练集,2500用作测试集
'''
from config import *
import os
import shutil
def div_dataset(ori_path,val_path,train_path):
'''
划分数据集
:param ori_path:
:param test_path:
:param train_path:
:return:
'''
cat_num=0
dog_num=0
train_num=0
test_num=0
cat_val_path =os.path.join(val_path,CAT_CATE)
dog_val_path =os.path.join(val_path,DOG_CATE)
cat_train_path =os.path.join(train_path,CAT_CATE)
dog_train_path =os.path.join(train_path,DOG_CATE)
if not os.path.exists(cat_val_path):
os.makedirs(cat_val_path)
if not os.path.exists(dog_val_path):
os.makedirs(dog_val_path)
if not os.path.exists(cat_train_path):
os.makedirs(cat_train_path)
if not os.path.exists(dog_train_path):
os.makedirs(dog_train_path)
for root, dirs, files in os.walk(ori_path):
for filename in files:
# print(filename)
ori_file = os.path.join(ori_path, filename)
filename_l = filename.split('.')
cate = filename_l[0]
if cate==CAT_CATE:
cat_num+=1
else:
dog_num+=1
nums = filename_l[1]
if int(nums)<11000:
train_num+=1
train_file = os.path.join(train_p