The data set download address is
The downloaded picture package contains pictures of cats and dogs, but the naming method is named after cats and dogs, so we can quickly classify them, and use 90% of them as the training set, and the rest as the test set
initial preparation work:
- download dataset
- Create new train and test folders
- In the train and test files, create cat and dog folders
- execute code
code:
import shutil
import torch
import torch.nn as nn
import torchvision
import os
# 数据分类处理
## 提取文件夹内的名字
data_file = os.listdir('./data/train')
## 索引猫和狗的图片名字
dog_file = list(filter(lambda x: x[:3] == 'dog', data_file))
cat_file = list(filter(lambda x: x[:3] == 'cat', data_file))
## 移动图片
root = './data/train/'
for i in range(len(dog_file)):
pic_path = os.path.join(root, dog_file[i + 1])
# print(dog_file[i+1])
# print('*******************************')
# print(pic_path)
if i < len(dog_file)*0.9 :
obj_path = './data/train/dog/' + dog_file[i + 1]
else:
obj_path = './data/test/dog/' + dog_file[i + 1]
shutil.move(pic_path, obj_path)
for i in range(len(cat_file)):
pic_path = os.path.join(root, cat_file[i + 1])
if i < len(cat_file)*0.9:
obj_path = './data/train/cat/' + cat_file[i + 1]
else:
obj_path = './data/test/cat/' + cat_file[i + 1]
shutil.move(pic_path, obj_path)