python将批量图片以txt文件存储,并读取

将图片存储为txt的原因

在将图片数据输入卷积神经网络时,如果通过np.load进行输入,常常会出现内存爆炸的问题,为节约内存,将图片按照txt文件读取,是一种非常好的读取方式,当然啊,如今ternsorflow出的API也相当不错。技多不压身嘛!

将图片存储为txt文件代码

 '''
 本代码以UC数据集为例子,UC数据集共包含21类,每一类图片包含100张图片
 其中class_label_dict为UC数据集label,
 '''
import numpy as np
import os


class_label_dict = {'agricultural': 0,
                    "airplane": 1,
                    "baseballdiamond": 2,
                    "beach": 3,
                    "buildings": 4,
                    "chaparral": 5,
                    "denseresidential": 6,
                    "forest": 7,
                    "freeway": 8,
                    "golfcourse": 9,
                    "harbor": 10,
                    "intersection": 11,
                    "mediumresidential": 12,
                    "mobilehomepark": 13,
                    "overpass": 14,
                    "parkinglot": 15,
                    "river": 16,
                    "runway": 17,
                    "sparseresidential": 18,
                    "storagetanks": 19,
                    "tenniscourt": 20
                    }
file_path = "G:/UC"                                  #此处为文件存放位置
path_list = os.listdir(file_path)                 #会历遍文件夹内的文件并返回一个列表
path_name=[]

for i in path_list:
    path_name.append(file_path+"/"+i+" "+str(class_label_dict[i[:-6]]))


path_name.sort()


train_path = []
test_path = []
trains_idx = []
tests_idx = []
for i in range(21):
    start = i * 100
    end = (i + 1) * 100
    idx = np.arange(start, end)
    np.random.shuffle(idx)
    train_idx = idx[0:80]
    test_idx = idx[80:]
    trains_idx.extend(train_idx)
    tests_idx.extend(test_idx)
path_name = np.array(path_name)
train_path = path_name[trains_idx]
test_path = path_name[tests_idx]
for file_name in path_name:
    # "a"表示以不覆盖的形式写入到文件中,当前文件夹如果没有"save.txt"会自动创建
    with open("data.txt", "a") as f:
        f.write(file_name + "\n")
    f.close()

for file_name in train_path:
    # "a"表示以不覆盖的形式写入到文件中,当前文件夹如果没有"save.txt"会自动创建
    with open("train.txt", "a") as f:
        f.write(file_name + "\n")
    f.close()
for file_name in test_path:
    # "a"表示以不覆盖的形式写入到文件中,当前文件夹如果没有"save.txt"会自动创建
    with open("test.txt", "a") as f:
        f.write(file_name + "\n")
    f.close()

输出txt文件的格式为 文件位置+文件名+label形式 ,如下图所示:
txt结果图

将txt文件进行批量读取


def read_txt():
    train_filename = []
    train_filelabel = []
    with open('/home/admin324/PycharmProjects/qiushuo/A_GS/data/train.txt', 'r') as f:
        x = f.readlines()
        for name in x:
            train_filename.append(name.strip().split()[0])
            train_filelabel.append(int(name.strip().split()[1]))
    train_filename = np.array(train_filename)
    train_filelabel = np.array(train_filelabel)

    test_filename = []
    test_filelabel = []
    with open('/home/admin324/PycharmProjects/qiushuo/A_GS/data/test.txt', 'r') as f:
        x = f.readlines()
        for name in x:
            test_filename.append(name.strip().split()[0])
            test_filelabel.append(int(name.strip().split()[1]))
    test_filename = np.array(test_filename)
    test_filelabel = np.array(test_filelabel)
    return train_filename, train_filelabel, test_filename, test_filelabel


def read_data(path):
    data = []
    for i in range(path.shape[0]):
        temp = cv2.imread(path[i])
        temp = cv2.cvtColor(temp, cv2.COLOR_BGR2RGB)
        temp = cv2.resize(temp, dsize=(224, 224))
        data.append(temp / 255.)
    data = np.array(data)
    return data

   转载规则


《python将批量图片以txt文件存储,并读取》 秋歌 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
MACNN-Learning Multi-Attention Convolutional Neural Network for Fine-Grained Image Recognition MACNN-Learning Multi-Attention Convolutional Neural Network for Fine-Grained Image Recognition
Learning Multi-Attention Convolutional Neural Network for Fine-Grained Image Recognition 是2017年CVPR一篇关于细粒度的文章文章传送门 一 文章简
2019-09-05
下一篇 
Ubuntu16.04LST+tensorflow—gpu图形工作站安装教程 Ubuntu16.04LST+tensorflow—gpu图形工作站安装教程
一 工作站环境说明系统:ubuntu16.04LST显卡:TITAN XPps:工作站内核最好不要更新,工作站稳定第一 二 显卡驱动安装 我的第一篇博客介绍了CUDA 以及CUDNN 以及显卡各个版本号之间的关系。有兴趣的同学可以过去看一看
2019-08-20
  目录