File size: 2,982 Bytes
c52fe89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#读取数据集,在../dataset/raw_data下按照数据集的完整排序,1.png,2.png,3.png,...保存

import os
import numpy as np
import torchvision
import torchvision.transforms as transforms
from PIL import Image
from tqdm import tqdm

def unpickle(file):
    """读取CIFAR-10数据文件"""
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict

def save_images_from_cifar10(dataset_path, save_dir):
    """从CIFAR-10数据集中保存图像
    
    Args:
        dataset_path: CIFAR-10数据集路径
        save_dir: 图像保存路径
    """
    # 创建保存目录
    os.makedirs(save_dir, exist_ok=True)
    
    # 获取训练集数据
    train_data = []
    train_labels = []
    
    # 读取训练数据
    for i in range(1, 6):
        batch_file = os.path.join(dataset_path, f'data_batch_{i}')
        if os.path.exists(batch_file):
            print(f"读取训练批次 {i}")
            batch = unpickle(batch_file)
            train_data.append(batch[b'data'])
            train_labels.extend(batch[b'labels'])
    
    # 合并所有训练数据
    if train_data:
        train_data = np.vstack(train_data)
        train_data = train_data.reshape(-1, 3, 32, 32).transpose(0, 2, 3, 1)
    
    # 读取测试数据
    test_file = os.path.join(dataset_path, 'test_batch')
    # if os.path.exists(test_file):
    #     print("读取测试数据")
    #     test_batch = unpickle(test_file)
    #     test_data = test_batch[b'data']
    #     test_labels = test_batch[b'labels']
    #     test_data = test_data.reshape(-1, 3, 32, 32).transpose(0, 2, 3, 1)
    # else:
    test_data = []
    test_labels = []
    
    # 合并训练和测试数据
    all_data = np.concatenate([train_data, test_data]) if len(test_data) > 0 and len(train_data) > 0 else (train_data if len(train_data) > 0 else test_data)
    all_labels = train_labels + test_labels if len(test_labels) > 0 and len(train_labels) > 0 else (train_labels if len(train_labels) > 0 else test_labels)
    
    # 保存图像
    print(f"保存 {len(all_data)} 张图像...")
    for i, (img, label) in enumerate(tqdm(zip(all_data, all_labels), total=len(all_data))):
        img = Image.fromarray(img)
        img.save(os.path.join(save_dir, f"{i}.png"))
    
    print(f"完成! {len(all_data)} 张图像已保存到 {save_dir}")

if __name__ == "__main__":
    # 设置路径
    dataset_path = "../dataset/cifar-10-batches-py"
    save_dir = "../dataset/raw_data"
    
    # 检查数据集是否存在,如果不存在则下载
    if not os.path.exists(dataset_path):
        print("数据集不存在,正在下载...")
        os.makedirs("../dataset", exist_ok=True)
        transform = transforms.Compose([transforms.ToTensor()])
        trainset = torchvision.datasets.CIFAR10(root="../dataset", train=True, download=True, transform=transform)
    
    # 保存图像
    save_images_from_cifar10(dataset_path, save_dir)