first commit
This commit is contained in:
144
SetParams/DataType/BaseParam.py
Normal file
144
SetParams/DataType/BaseParam.py
Normal file
@ -0,0 +1,144 @@
|
||||
import json
|
||||
from .TypeDef import *
|
||||
from typing import Union
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
class BaseParam:
|
||||
"""
|
||||
参数基类
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
参数基类,算法中所有需要参数的方法中,使用的参数必须继承该类型
|
||||
"""
|
||||
self.file = ''
|
||||
self.param_list = []
|
||||
|
||||
def save_to_file(self, file: str) -> None:
|
||||
"""
|
||||
将参数保存到文件
|
||||
:param file: 文件完整路径
|
||||
:return: None
|
||||
"""
|
||||
with open(file, 'w') as f:
|
||||
f.write(str(self))
|
||||
f.flush()
|
||||
|
||||
def add_param(self, p: Union[IntType, BoolType, FloatType, StringType, ListType, EnumType]) -> None:
|
||||
"""
|
||||
将数据添加到参数列表中,以支持序列化
|
||||
:param p: 单个数据,必须为系统支持的参数类型之一:IntType, BoolType, FloatType, StringType, ListType, EnumType
|
||||
:return: None
|
||||
"""
|
||||
self.param_list.append(p)
|
||||
|
||||
def read_from_str(self, content: str) -> None:
|
||||
"""
|
||||
将字符串反序列化为对象中的参数
|
||||
:param content: 要反序列化的字符串
|
||||
:return: None
|
||||
"""
|
||||
self.param_list.clear()
|
||||
objs = json.loads(content)
|
||||
for obj in objs:
|
||||
#try:
|
||||
item = obj #json.loads(obj)
|
||||
t = item["type"]
|
||||
param = None
|
||||
|
||||
if t == "B":
|
||||
param = BoolType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
elif t == "I":
|
||||
param = IntType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
elif t == "F":
|
||||
param = FloatType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
#param.length = item["length"]
|
||||
elif t == "S":
|
||||
item['length']=len(item["value"])
|
||||
param = StringType(item["name"],
|
||||
item["value"],
|
||||
item['length'],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
elif t == "L":
|
||||
param = ListType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
#param.length = item["length"]
|
||||
elif t == "E":
|
||||
param = EnumType(item["name"],
|
||||
item["value"],
|
||||
item["items"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
|
||||
if param:
|
||||
self.param_list.append(param)
|
||||
# except Exception as e:
|
||||
# pass
|
||||
|
||||
def read_from_file(self, file: str) -> None:
|
||||
"""
|
||||
从文件读取序列化后的参数字符串,并反序列化为对象
|
||||
:param file: 文件完整路径
|
||||
:return: None
|
||||
"""
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
self.read_from_str(content)
|
||||
|
||||
@property
|
||||
def param_string(self) -> str:
|
||||
"""
|
||||
获取Json格式的参数字符串
|
||||
:return: 返回Json格式的参数字符串
|
||||
"""
|
||||
for i, p in enumerate(self.param_list):
|
||||
p.index = i
|
||||
return self.__str__()
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""
|
||||
重写str方法用于将对象序列化为Json字符串
|
||||
:return: 序列化后的JSON字符串
|
||||
"""
|
||||
ret = []
|
||||
for index, item in enumerate(self.param_list):
|
||||
obj = item.obj
|
||||
if isinstance(obj, dict):
|
||||
obj["index"] = index
|
||||
item_obj = json.dumps(obj)
|
||||
ret.append(item_obj)
|
||||
return json.dumps(ret)
|
||||
|
||||
def get(self, name: str) -> Union[None, IntType, BoolType, FloatType, StringType, ListType, EnumType,]:
|
||||
"""
|
||||
根据数据名称获取指定的数据对象
|
||||
:param name: 数据名称
|
||||
:return:
|
||||
"""
|
||||
return next((x for x in self.param_list if x.name == name), None)
|
21
SetParams/DataType/ParamDef.py
Normal file
21
SetParams/DataType/ParamDef.py
Normal file
@ -0,0 +1,21 @@
|
||||
from .TypeDef import *
|
||||
from .BaseParam import *
|
||||
|
||||
|
||||
class TrainParams(BaseParam):
|
||||
"""
|
||||
训练参数示例,使用时需要根据实际情况修改,如有其他参数(如推理函数的参数)需要自定义,并继承自 BaseParam
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
# 例如训练的时候需要传入以下参数
|
||||
gpu_num = IntType("gpu_num", 2)
|
||||
support_cpu = BoolType("support_cpu", True)
|
||||
labels = EnumType("labels", 0, ["dog", "cat"])
|
||||
labels.default = 0
|
||||
|
||||
self.add_param(gpu_num)
|
||||
self.add_param(support_cpu)
|
||||
self.add_param(labels)
|
243
SetParams/DataType/TypeDef.py
Normal file
243
SetParams/DataType/TypeDef.py
Normal file
@ -0,0 +1,243 @@
|
||||
import json
|
||||
from typing import List
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
class __BaseType:
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: object,
|
||||
description: str,
|
||||
default: object,
|
||||
show: bool):
|
||||
"""
|
||||
所有数据类型的基类
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.index = -1
|
||||
self.default = default
|
||||
self.value = value
|
||||
self.show = show
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def type(self) -> str:
|
||||
"""
|
||||
参数类型,用于前端/后台的序列化与反序列化。前端展示时也需要根据此字段类型绘制不同的控件
|
||||
:return: 一般使用单个字母,如:整型(I),布尔型(B),浮点型(F),字符串(S),列表(L),枚举(E)
|
||||
"""
|
||||
pass
|
||||
|
||||
@property
|
||||
def _base_obj(self) -> dict:
|
||||
"""
|
||||
用于创建所有数据类型的共有对象字典
|
||||
:return:
|
||||
"""
|
||||
return {
|
||||
"index": self.index,
|
||||
"name": self.name,
|
||||
"value": self.value,
|
||||
"description": self.description,
|
||||
"default": self.default,
|
||||
"type": self.type,
|
||||
"show": self.show
|
||||
}
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def obj(self) -> dict:
|
||||
"""
|
||||
抽象属性,获取数据类型的对象字典
|
||||
:return:
|
||||
"""
|
||||
return self._base_obj
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
重写数据类型的字符串序列化方法
|
||||
:return:
|
||||
"""
|
||||
return json.dumps(self.obj)
|
||||
|
||||
|
||||
class BoolType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: bool = False,
|
||||
description: str = "",
|
||||
default: bool = False,
|
||||
show: bool = True):
|
||||
"""
|
||||
布尔类型数据
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'B'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
return super()._base_obj
|
||||
|
||||
|
||||
class IntType(__BaseType):
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: int = 0,
|
||||
description: str = "",
|
||||
default: int = 0,
|
||||
show: bool = True):
|
||||
"""
|
||||
整数类型
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'I'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
return super()._base_obj
|
||||
|
||||
|
||||
class FloatType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: float = 0.,
|
||||
description: str = "",
|
||||
default: float = 0.,
|
||||
show: bool = True):
|
||||
"""
|
||||
浮点类型
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'F'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
return super()._base_obj
|
||||
|
||||
|
||||
class StringType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: str = "",
|
||||
length: int = -1,
|
||||
description: str = "",
|
||||
default: str = "",
|
||||
show: bool = True):
|
||||
"""
|
||||
字符串类型
|
||||
:param name: 数据名称,用于程序内部标识该数据
|
||||
:param value: 数据值
|
||||
:param length: 字符串长度
|
||||
:param description: 描述,用于前端页面对该数据进行展示
|
||||
:param default: 默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
self.length = length
|
||||
if length > 0:
|
||||
self.value = value[:length]
|
||||
else:
|
||||
self.value = value
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'S'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
base_obj = super()._base_obj
|
||||
base_obj["length"] = self.length
|
||||
return base_obj
|
||||
|
||||
|
||||
class ListType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: list = None,
|
||||
description: str = "",
|
||||
default: list = None,
|
||||
show: bool = True):
|
||||
"""
|
||||
列表类型
|
||||
:param name: 数据名称,用于程序内部标识该数据
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端页面对该数据进行展示
|
||||
:param default: 默认值
|
||||
"""
|
||||
if not default:
|
||||
default = []
|
||||
|
||||
if not value:
|
||||
value = default
|
||||
|
||||
self.length = len(value)
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return "L"
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
base_obj = super()._base_obj
|
||||
base_obj["length"] = self.length
|
||||
return base_obj
|
||||
|
||||
|
||||
class EnumType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: int = -1,
|
||||
items: List[str] = None,
|
||||
description: str = "",
|
||||
default: int = -1,
|
||||
show: bool = True):
|
||||
"""
|
||||
枚举类型
|
||||
:param name: 数据名称,用于程序内部标识该数据
|
||||
:param value: 数据值,即选择了枚举列表中的第几项
|
||||
:param items: 枚举范围,枚举项必须为字符串类型
|
||||
:param description: 描述,用于前端页面对该数据进行展示
|
||||
:param default: 默认值
|
||||
"""
|
||||
if not items:
|
||||
items = []
|
||||
default = -1
|
||||
self.items = items
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return "E"
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
base_obj = super()._base_obj
|
||||
base_obj["items"] = self.items
|
||||
return base_obj
|
0
SetParams/DataType/__init__.py
Normal file
0
SetParams/DataType/__init__.py
Normal file
1
SetParams/TrainParams-0.json
Normal file
1
SetParams/TrainParams-0.json
Normal file
@ -0,0 +1 @@
|
||||
["{\"index\": 0, \"name\": \"gpu_num\", \"value\": 2, \"description\": \"\", \"default\": 0, \"type\": \"I\"}", "{\"index\": 1, \"name\": \"support_cpu\", \"value\": true, \"description\": \"\", \"default\": false, \"type\": \"B\"}", "{\"index\": 2, \"name\": \"labels\", \"value\": 0, \"description\": \"\", \"default\": 0, \"type\": \"E\", \"items\": [\"dog\", \"cat\"]}"]
|
1
SetParams/TrainParams.json
Normal file
1
SetParams/TrainParams.json
Normal file
@ -0,0 +1 @@
|
||||
["{\"index\": 0, \"name\": \"num_classes\", \"value\": 0, \"description\": \"\", \"default\": 9, \"type\": \"I\"}", "{\"index\": 1, \"name\": \"lr\", \"value\": 0.0, \"description\": \"\", \"default\": 0.005, \"type\": \"F\"}", "{\"index\": 2, \"name\": \"lr_schedulerList\", \"value\": [30, 60], \"description\": \"\", \"default\": [30, 60], \"type\": \"L\", \"length\": 2}", "{\"index\": 3, \"name\": \"device\", \"value\": \"\", \"description\": \"\", \"default\": \"cpu\", \"type\": \"S\", \"length\": -1}", "{\"index\": 4, \"name\": \"DatasetDir\", \"value\": \"\", \"description\": \"\", \"default\": \"./datasets/M006B_duanmian\", \"type\": \"S\", \"length\": -1}", "{\"index\": 5, \"name\": \"saveModDir\", \"value\": \"\", \"description\": \"\", \"default\": \"./saved_model/M006B_duanmian.pt\", \"type\": \"S\", \"length\": -1}", "{\"index\": 6, \"name\": \"resumeModPath\", \"value\": \"\", \"description\": \"\", \"default\": \"\", \"type\": \"S\", \"length\": -1}", "{\"index\": 7, \"name\": \"epochnum\", \"value\": 0, \"description\": \"\", \"default\": 100, \"type\": \"I\"}", "{\"index\": 8, \"name\": \"saveEpoch\", \"value\": 0, \"description\": \"\", \"default\": 1, \"type\": \"I\"}"]
|
0
SetParams/__init__.py
Normal file
0
SetParams/__init__.py
Normal file
49
SetParams/main.py
Normal file
49
SetParams/main.py
Normal file
@ -0,0 +1,49 @@
|
||||
from DataType.TypeDef import *
|
||||
from DataType.ParamDef import *
|
||||
|
||||
|
||||
def train(params: TrainParams):
|
||||
"""
|
||||
后台提交训练请求时,可以参考此方法,从前端传递的参数中取出需要的值
|
||||
:param params:
|
||||
:return:
|
||||
"""
|
||||
# 存在的参数可以使用name字段获取到
|
||||
cpu_num = params.get("gpu_num")
|
||||
print(cpu_num)
|
||||
|
||||
# 不存在的参数获取的时候返回None
|
||||
th = params.get("th")
|
||||
print(th)
|
||||
|
||||
|
||||
def get_train_config_json():
|
||||
"""
|
||||
前端获取训练所需要的参数JSON字符串时,使用str方法即可获取JSON字符串
|
||||
:return:
|
||||
"""
|
||||
# 第一种方法:
|
||||
# 创建新对象
|
||||
params = TrainParams()
|
||||
# 从指定文件名中读取配置
|
||||
params.read_from_file("TrainParams.json")
|
||||
# 转为字符串
|
||||
print(str(params))
|
||||
|
||||
# 第二种方法
|
||||
# 也可以直接将文本传递给前端,但是可能需要考虑转义符号(\)的处理
|
||||
with open("TrainParams.json", "r") as f:
|
||||
print(f.read())
|
||||
|
||||
return params
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 当前端需要获取参数列表时,参照此函数中的实现
|
||||
get_train_config_json()
|
||||
|
||||
# 当前端传递回来参数的JSON字符串时,使用下述方法,将字符串反序列化为对象,然后传递给对应的函数
|
||||
params = TrainParams()
|
||||
params.read_from_str(
|
||||
'["{\\"index\\": 0, \\"name\\": \\"gpu_num\\", \\"value\\": 2, \\"description\\": \\"\\", \\"default\\": 0, \\"type\\": \\"I\\"}", "{\\"index\\": 1, \\"name\\": \\"support_cpu\\", \\"value\\": true, \\"description\\": \\"\\", \\"default\\": false, \\"type\\": \\"B\\"}", "{\\"index\\": 2, \\"name\\": \\"labels\\", \\"value\\": 0, \\"description\\": \\"\\", \\"default\\": 0, \\"type\\": \\"E\\", \\"items\\": [\\"dog\\", \\"cat\\"]}"]')
|
||||
train(params)
|
Reference in New Issue
Block a user