python - 正在创建的 CSV 文件大于我在 python/pycharm 中的原始数据的大小?

标签 python csv numpy bigdata

我正在尝试将一组大约 10000 张图像作为 numpy 数组加载到 CSV 文件中以训练模型。我的问题是我的原始数据是 40 MB,而创建的 csv 是 3 GB,我不知道为什么。理想情况下,它应该小于数据。我正在使用 python 3 和 pycharm 在 ubuntu 16.04 上工作,并使用 Libre Office Calc 打开 CSV。我无法粘贴 CSV 的片段,因为只有一行中的 numpy 数组太大并且超出了这个问题的正文限制。这是我的代码:

Csv 创建代码

import csv
from utils import extract_images_from_path

path_csv_out =  "/home/rehan/countries/influencer.csv"
image_path = "/home/rehan/countries/"
csv_out = open(path_csv_out, 'w', newline="")

writer = csv.writer(csv_out, delimiter=';')
row = ["image", "country", "gender", "age", "ethnicity", "image_path"]
writer.writerow(row)
for row in extract_images_from_path(image_path, with_folder_meta=True, exclude="sdfsdfsdgdfgdfg", include="cropped_colored",
                                    start_counter=0, end_counter=125440, colored_version=True):
    writer.writerow(row)

从文件夹中提取图片的函数

def extract_images_from_path(path, extension=".jpg", exclude="cropped", include="", start_counter=0, end_counter=10000,
                             with_folder_meta=False, colored_version=True, seperator="/"):
    location_images = []
    counter = 0
    age = 1
    gender = 'guys'
    country = 'germany'
    ethnicity = 0
    for (dirpath, dirnames, filenames) in walk(path):
        if with_folder_meta:
            dirpath = str(dirpath).replace("\\", "/")
            current_folder = dirpath.rsplit(seperator, 1)[1]
            print(current_folder)
            if current_folder==dirpath.rsplit('/')[4]:
                country=current_folder
            try:
                age = int(current_folder)
            except ValueError as e:
                if "guys" in current_folder:
                    gender = 1 if current_folder == "guys" else 0
                else:
                    gender = 0
                    if "france" in country:
                        ethnicity = 1
                    else:
                        ethnicity = 0
        for file in filenames:
            if file.endswith(extension):
                counter += 1
                if not start_counter <= counter <= end_counter:
                    continue
                image_path = str(os.path.join(dirpath, file))
                image_path = str(image_path).replace("\\", "/")
                image = cv2.imread(image_path)
                height, width, pixel = image.shape
                # test_convert = np.fromstring(image.tostring(), image.dtype).reshape(52,52,3)
                image = image.reshape(height*width*pixel)
                # image = image.astype('float32')
                # image = np.multiply(image, 1.0 / 255.0)
                # print (image)
                image = image.tolist()
                # print (np.arange(12).reshape(2, 2, 3))
                # image = np.arange(12).reshape(2, 2, 3).tostring()
                # image = pickle.dumps(image, protocol=0)
                if not with_folder_meta:
                    result_row = image
                else:
                    result_row = [image, country, gender, age, ethnicity, image_path]
                yield result_row
                if counter % 100 == 0:
                    print(counter)
        if counter > end_counter:
            break

最佳答案

在我看来,考虑到您的数据以及您如何存储它,似乎有一个 3Gb 的文件是意料之中的:

因此您从压缩的 JPEG 图像开始,高质量 (Q=50) 的 jpeg 压缩比约为 15 [1] (根据图片的内容,它的变化很大,但我们在这里只做粗略计算)。

假设您的图片采用标准的每种颜色 8 位格式,您的图像现在是 8 位值的数组,这些值的范围在 0 到 256 之间,因此要在文本文件中写下每个数字,您需要大约每个号码 3 个字符

如果您以 CSV 格式编写这些字符,您很可能在每个数字后有一个逗号和一个空格,所以假设总共您将得到大约 每个数字 5 个字符 每个字符加权 1 字节(假设 ASCII 编码)。

结合我们得到的所有内容:

未压缩数据的总字节数 = 40Mb*15 = 600Mb

将这些字节作为文本写入 CSV 所需的总字符数 = 600Mb*5 = 3G

ASCII 编码的预期文件大小 = 3G * 1b = 3Gb

所以如果你真的想要 csv 格式的数据,那么你真的无能为力......

关于python - 正在创建的 CSV 文件大于我在 python/pycharm 中的原始数据的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47770575/

相关文章:

python - 将配置文件模型连接到远程用户(自定义身份验证后端)

Python:跳出多级循环

r - dplyr : how to read a tsv file with headers while skipping some lines?

php - 如何将 csv 文件中的数据插入 mySQL 数据库(大数据,快速方式)?

python - 如何将列表中的值插入现有列

python - 如何获取 PSD 的频率档范围

python - 根据所选值总结和绘制 ndarrays 列表

python - 如何从包含特定列中特定字符串的 Pandas 数据框中删除行?

python - tf.train.AdamOptimizer 和在 keras.compile 中使用 adam 有什么区别?

Python读取空格分隔文件时出错