分类任务一般标签的生成方式参考

import os
import random

#将类别随机2-8分
def split(full_list, shuffle=True, ratio=0.2):
    n_total = len(full_list)
    offset = int(n_total * ratio)   #计算一列数据的分割位置
    if n_total == 0 or offset<1:
        return [], full_list
    if offset % 2 == 1:
        offset += 1
    if shuffle:                     #将原列表顺序打乱,按照分割点分成两个列表
        random.shuffle(full_list)
    sublist_1 = full_list[:offset]
    sublist_2 = full_list[offset:]
    return sublist_1, sublist_2

x = 0
all_list = []
for root, dirs, files in os.walk("./img/"):
    #for d in dirs:
        #print(d) #打印子资料夹的个数
    for file in files:
        x = x+1
        #每一个文件的路径
        print(root)
        if int(root[7]) != 0:
            xml_path = root[6:] +'/' + file + ' ' + root[7:9]  #标注中文件夹名字第三位是类别信息,这里具体第几位数字根据实际情况变化;第二个' ' 中间是空格,满足分类网络标注要求
            all_list.append(xml_path)
            #all_list.append('\n')            #换行
        else:
            xml_path = root[6:] +'/' + file + ' ' + root[8]  #标注中文件夹名字第三位是类别信息,这里具体第几位数字根据实际情况变化;第二个' ' 中间是空格,满足分类网络标注要求
            all_list.append(xml_path)
            #all_list.append('\n')       
list_1 , list_2 = split(all_list)
#追加的方式
f_train = open('train.txt','w') 
f_val = open('val.txt', 'w')
f_train.writelines([line+'\n' for line in list_2])      #换行
f_val.writelines([line+'\n' for line in list_1])
f_train.close()               
f_val.close()              #完成写入


生成好的train.txt中内容如下
001dogs/dog226.jpg 1
001dogs/dog392.jpg 1
000cats/cat953.jpg 0
001dogs/dog501.jpg 1
000cats/cat630.jpg 0
000cats/cat117.jpg 0
001dogs/dog606.jpg 1
000cats/cat251.jpg 0
001dogs/dog697.jpg 1
000cats/cat594.jpg 0
000cats/cat365.jpg 0
000cats/cat722.jpg 0
000cats/cat31.jpg 0
000cats/cat213.jpg 0
000cats/cat979.jpg 0
000cats/cat390.jpg 0
001dogs/dog667.jpg 1
000cats/cat353.jpg 0
001dogs/dog922.jpg 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值