创建建立list,保存图片的路径。
amount_json = “./data/train/amount/gt.json”
date_json = “./data/train/date/gt.json”
train_json = “train_data.json”
test_json = “test_data.json”
with open(amount_json, “r”, encoding=‘utf-8’) as f:
load_dict_amount = json.load(f)
with open(date_json, “r”, encoding=‘utf-8’) as f:
load_dict_date = json.load(f)
with open(train_json, “r”, encoding=‘utf-8’) as f:
load_dict_train = json.load(f)
with open(test_json, “r”, encoding=‘utf-8’) as f:
load_dict_test = json.load(f)
四个json文件对应上面的四个list,json文件存储的是图片的名字和图片的标签,把json解析出来存到字典中。
#聚合list
all_list = new_amount_list + new_date_list+new_test_list+new_train_list
from sklearn.model_selection import train_test_split
#切分训练集合和验证集
train_list, test_list = train_test_split(all_list, test_size=0.15, random_state=42)
#聚合字典
all_dic = {}
all_dic.update(load_dict_amount)
all_dic.update(load_dict_date)
all_dic.update(load_dict_train)
all_dic.update(load_dict_test)
with open(‘train.txt’, ‘w’) as f:
for line in train_list:
f.write(line + " " + all_dic[line.split(‘/’)[-1]]+“\n”)
with open(‘val.txt’, ‘w’) as f:
for line in test_list:
f.write(line + " " + all_dic[line.split(‘/’)[-1]]+“\n”)
将四个list聚合为一个list。
使用train_test_split切分训练集和验证集。
聚合字典。
然后分别遍历trainlist和testlist,将其写入train.txt和val.txt。
到这里数据集就制作完成了。得到train.txt和val.txt
查看train.txt
数据集和自带的数据集格式一样了,然后我们就可以开始训练了。
==================================================================
新建getclass.py文件夹,加入以下代码:
import json
amount_json = “./data/train/amount/gt.json”
date_json = “./data/train/date/gt.json”
with open(amount_json, “r”, encoding=‘utf-8’) as f:
load_dict_amount = json.load(f)
with open(date_json, “r”, encoding=‘utf-8’) as f:
load_dict_date = json.load(f)
all_dic = {}
all_dic.update(load_dict_amount)
all_dic.update(load_dict_date)
list_key=[]
for keyline in all_dic.values():
for key in keyline:
if key not in list_key:
list_key.append(key)
with open(‘data/char_std_5990.txt’, ‘w’) as f:
for line in list_key:
f.write