[Image Classification] Realize the classification of the training set and test set of cat and dog pictures: python + Asirra

The data set download address is

Dogs vs. Cats | Kaggle

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:

  1. download dataset
  2. Create new train and test folders
  3. In the train and test files, create cat and dog folders
  4. 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)

Guess you like

Origin blog.csdn.net/qq_42792802/article/details/126264742