maketrainlabel

# -*- encoding: utf-8 -*-
"""
@File    : make_traintxt.py
@Time    : 2021-10-28 12:31
@Author  : XD
@Email   : gudianpai@qq.com
@Software: PyCharm
"""
import torch
import glob
import os.path as osp
import random
data_folder = "G:\data\cifar10"
im_train_list = glob.glob(osp.join(data_folder,"cifar_train/*/*.png"))

random.shuffle(im_train_list)

label_name = ["airplane", "automobile", "bird",
              "cat", "deer", "dog",
              "frog", "horse", "ship", "truck"]

label_dict = {}

for idx, name in enumerate(label_name):
    label_dict[name] = idx

with open("train.txt", "w") as f:
    for item in im_train_list:
        one_hot = ["0" for i in range(10)]
        im_label_name = item.split("\\")[-2]
        label = label_dict[im_label_name]
        one_hot[label ] = "1"
        #print("path:", item,"label:",label, "one_hot", one_hot)
        f.write(item + " ")
        for v in one_hot:
            v1 = str(v)
            f.write(v1 + " ")
        f.write("\n")
if __name__ == '__main__':

    s = open("train.txt").readlines()
    print(s[1])
    print(type(s))
    print(len(s))


G:\data\cifar10\cifar_train\automobile\compact_car_s_000297.png 0 1 0 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\ship\freighter_s_000697.png 0 0 0 0 0 0 0 0 1 0 
G:\data\cifar10\cifar_train\frog\frog_s_001717.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\cat\tabby_s_000887.png 0 0 0 1 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\horse\quarter_horse_s_001757.png 0 0 0 0 0 0 0 1 0 0 
G:\data\cifar10\cifar_train\frog\bufo_marinus_s_000361.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\dog\mongrel_s_000218.png 0 0 0 0 0 1 0 0 0 0 
G:\data\cifar10\cifar_train\airplane\stealth_fighter_s_000145.png 1 0 0 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\automobile\convertible_s_001477.png 0 1 0 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\frog\rana_temporaria_s_000653.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\horse\arabian_s_001541.png 0 0 0 0 0 0 0 1 0 0 
G:\data\cifar10\cifar_train\truck\camion_s_000521.png 0 0 0 0 0 0 0 0 0 1 
G:\data\cifar10\cifar_train\horse\stallion_s_000234.png 0 0 0 0 0 0 0 1 0 0 
G:\data\cifar10\cifar_train\frog\alytes_obstetricans_s_000124.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\horse\cavalry_horse_s_000199.png 0 0 0 0 0 0 0 1 0 0 
G:\data\cifar10\cifar_train\cat\alley_cat_s_000044.png 0 0 0 1 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\horse\stallion_s_000923.png 0 0 0 0 0 0 0 1 0 0 
G:\data\cifar10\cifar_train\automobile\car_s_000241.png 0 1 0 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\dog\english_toy_spaniel_s_000655.png 0 0 0 0 0 1 0 0 0 0 
G:\data\cifar10\cifar_train\automobile\shooting_brake_s_001339.png 0 1 0 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\automobile\estate_car_s_000529.png 0 1 0 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\ship\pleasure_craft_s_001722.png 0 0 0 0 0 0 0 0 1 0 
G:\data\cifar10\cifar_train\dog\pekingese_s_000594.png 0 0 0 0 0 1 0 0 0 0 
G:\data\cifar10\cifar_train\truck\dump_truck_s_000159.png 0 0 0 0 0 0 0 0 0 1 
G:\data\cifar10\cifar_train\bird\wagtail_s_000507.png 0 0 1 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\frog\bufo_bufo_s_000570.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\cat\tomcat_s_000513.png 0 0 0 1 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\deer\musk_deer_s_000078.png 0 0 0 0 1 0 0 0 0 0 
G:\data\cifar10\cifar_train\bird\hen_s_000274.png 0 0 1 0 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\truck\lorry_s_000604.png 0 0 0 0 0 0 0 0 0 1 
G:\data\cifar10\cifar_train\deer\fallow_deer_s_001895.png 0 0 0 0 1 0 0 0 0 0 
G:\data\cifar10\cifar_train\truck\dump_truck_s_000009.png 0 0 0 0 0 0 0 0 0 1 
G:\data\cifar10\cifar_train\frog\bufo_americanus_s_000454.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\cat\felis_catus_s_000855.png 0 0 0 1 0 0 0 0 0 0 
G:\data\cifar10\cifar_train\frog\toad_frog_s_000170.png 0 0 0 0 0 0 1 0 0 0 
G:\data\cifar10\cifar_train\ship\sea_boat_s_000391.png 0 0 0 0 0 0 0 0 1 0 
if __name__ == '__main__':
    import numpy as np
    s = open("train.txt").readlines()
    # print(s[1])
    #     # print(type(s))
    #     # print(len(s))
    #     # path = [v.split()[0] for v in s]
    #     # print(path)
    #     #
    target = [val.split()[1:] for val in s]
    imgs = [np.array([int(la) for la in val.split()[1:]]) for val in s]
    print(imgs)
    #     # print(type(target))
    #     # #print(target)

    imgs = [(val.split()[0], np.array([int(la) for la in val.split()[1:]])) for val in s]
    print(imgs[1])
array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 1, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0]), array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0]), array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0]), array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0]), array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值