avatar

深度学习入门(四)

猫狗数据集预处理

文件处理

  1. 首先创建指定目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
def creat_dir(path):
if not os.path.exists(path):
try:
os.makedirs(path)
except:
print('Create a failure')

path = 'E:/datasets'

creat_dir(path + "train/dogs")
creat_dir(path + "train/cats")
creat_dir(path + "test/dogs")
creat_dir(path + "test/cats")
  1. 将训练所需图片放到指定文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
base_path = 'E:\jupyter\dataset\cat_dog\train'

dogs=[os.path.join(base_path, i) for i in os.listdir(train) if 'dog' in i]
cats=[os.path.join(base_path, i) for i in os.listdir(train) if 'cat' in i]

for dog,cat in list(zip(dogs,cats))[:1000]:
new_path_dog = path + "train/dogs/" + os.path.basename(dog)
shutil.copyfile(dog, new_path_dog)
print(os.path.basename(dog) + " operate successfully")

new_path_cat = path + "train/cats/" + os.path.basename(cat)
shutil.copyfile(cat, new_path_cat)
print(os.path.basename(cat) + "operate successfully")

for dog, cat in list(zip(dogs, cats))[1000:1500]:
new_path_dog = path + "test/dogs/" + os.path.basename(dog)
shutil.copyfile(dog, new_path_dog)
print(os.path.basename(dog) + " operate successfully")

new_path_cat = path + "test/cats/" + os.path.basename(cat)
shutil.copyfile(cat, new_path_cat)
print(os.path.basename(cat) + "operate successfully")
文章作者: gh
文章链接: https://ghclub.top/posts/51320/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 GHBlog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论