def get_config(config):
    config = TransformerConfig.from_json_file(config)
    config.compute_type = mstype.float16
    config.dtype = mstype.float32
    return config
Example #2
0
from corpora.ami import AMI
from corpora.midas import MIDAS
from corpora.daily_dialog import DailyDialog
from taggers.transformer_tagger import TransformerTagger
from trainers.transformer_trainer import TransformerTrainer
from config import TransformerConfig
import torch.optim as optim
from corpora.corpus import Utterance


if __name__ == "__main__":
    config = TransformerConfig(
        taxonomy=Taxonomy.ISO,
        device="cpu",
        optimizer=optim.Adam,
        lr=2e-5,
        n_epochs=1,
        batch_size=256,
        max_seq_len=128,
        out_folder="models/transformer_example/",
    )
    trainer = TransformerTrainer(
        config,
        corpora_list=[
            # (Maptask, str(Path("data/Maptask").resolve())),
            # (AMI, str(Path("data/AMI/corpus").resolve())),
            (Switchboard, str(Path("data/Switchboard").resolve())),
            (DailyDialog, str(Path("data/DailyDialog").resolve())),
        ],
    )
    t = trainer.train()
if os.path.isfile(词数词典路径) and os.path.isfile(数_词表路径):
    词_数表, 数_词表 = 读出引索(词数词典路径, 数_词表路径)
with open(词数词典路径, encoding='utf8') as f:
    词数词典 = json.load(f)
with open(操作查询路径, encoding='utf8') as f:
    操作查询词典 = json.load(f)

方向表 = ['上移', '下移', '左移', '右移', '左上移', '左下移', '右上移', '右下移']

设备 = MyMNTDevice(_DEVICE_ID)
device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
mod = torchvision.models.resnet101(
    pretrained=True).eval().cuda(device).requires_grad_(False)
resnet101 = myResnet(mod)
config = TransformerConfig()

model = get_model(config, 130, 模型名称)

model = model.cuda(device).requires_grad_(False)

while True:
    if AI打开:

        图片路径 = 训练数据保存目录 + '/{}/'.format(str(int(time.time())))
        os.mkdir(图片路径)

        记录文件 = open(图片路径 + '_操作数据.json', 'w+')

        图片张量 = torch.Tensor(0)
        操作张量 = torch.Tensor(0)
Example #4
0
 def from_folder(folder: str) -> "TransformerTagger":
     with open(f"{folder}/config.json") as f:
         config = json.load(f)
     return TransformerTagger(TransformerConfig.from_dict(config))
Example #5
0
def get_config(config_file):
    tfm_config = TransformerConfig.from_json_file(config_file)
    tfm_config.compute_type = mstype.float16
    tfm_config.dtype = mstype.float32

    return tfm_config
Example #6
0
def main():

    global AI打开
    global 操作列
    加三技能 = '6'
    加二技能 = '5'
    加一技能 = '4'
    购买 = 'f1'
    词数词典路径 = "./json/词_数表.json"
    数_词表路径 = "./json/数_词表.json"
    操作查询路径 = "./json/名称_操作.json"
    操作词典 = {"图片号": "0", "移动操作": "无移动", "动作操作": "无动作"}
    th = threading.Thread(target=start_listen, )
    th.start()  # 启动线程

    if os.path.isfile(词数词典路径) and os.path.isfile(数_词表路径):
        词_数表, 数_词表 = 读出引索(词数词典路径, 数_词表路径)
    with open(词数词典路径, encoding='utf8') as f:
        词数词典 = json.load(f)
    with open(操作查询路径, encoding='utf8') as f:
        操作查询词典 = json.load(f)

    方向表 = ['上移', '下移', '左移', '右移', '左上移', '左下移', '右上移', '右下移']

    设备 = MyMNTDevice(_DEVICE_ID)
    device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
    # mod = torchvision.models.resnet101(pretrained=True).eval().cuda(device).requires_grad_(False)
    # mod = torchvision.models.resnet101(pretrained=True).eval().cpu().requires_grad_(False)
    # mod = torchvision.models.resnet50(pretrained=True).eval().cpu().requires_grad_(False)
    # mod = torchvision.models.resnet34(pretrained=True).eval().cpu().requires_grad_(False)
    mod = torchvision.models.resnet18(
        pretrained=True).eval().cpu().requires_grad_(False)
    resnet101 = myResnet(mod)
    config = TransformerConfig()

    model = get_model(config, 130, 模型名称)

    # model = model.cuda(device).requires_grad_(False)
    model = model.cpu().requires_grad_(False)
    抽样np = 0

    if AI打开:

        图片张量 = torch.Tensor(0)
        操作张量 = torch.Tensor(0)

        # 伪词序列 = torch.from_numpy(np.ones((1, 60)).astype(np.int64)).cuda(device).unsqueeze(0)
        伪词序列 = torch.from_numpy(np.ones(
            (1, 60)).astype(np.int64)).cpu().unsqueeze(0)

        操作序列 = np.ones((1, ))
        操作序列[0] = 128
        计数 = 0
        time_start = time.time()
        旧指令 = '移动停'
        for i in range(1000000):
            # logger.info("++++001+++++")
            if AI打开 == False:
                break
            try:
                imgA = 取图(窗口名称)
            except:
                AI打开 = False
                print('取图失败')
                break
            # logger.info("+++++002++++")
            计时开始 = time.time()

            # preprocess
            图片张量, 操作序列 = preprocess(图片张量, imgA, resnet101, 操作序列, 抽样np)

            pre_process = time.time()

            logger.info("pre_process : {} ms ".format(pre_process - 计时开始))

            # transform model
            # 操作张量 = torch.from_numpy(操作序列.astype(np.int64)).cuda(device)
            操作张量 = torch.from_numpy(操作序列.astype(np.int64)).cpu()
            src_mask, trg_mask = create_masks(操作张量.unsqueeze(0),
                                              操作张量.unsqueeze(0), device)
            输出_实际_A = model(图片张量.unsqueeze(0), 操作张量.unsqueeze(0), trg_mask)

            # logger.info("+++++003++++")
            LI = 操作张量.contiguous().view(-1)
            # LA=输出_实际_A.view(-1, 输出_实际_A.size(-1))
            if 计数 % 20 == 0 and 计数 != 0:
                print("jineng + zhuangbei ")
                设备.发送(购买)
                设备.发送(加三技能)
                设备.发送(加二技能)
                设备.发送(加一技能)
                设备.发送('移动停')
                logger.warning("{} {}".format(旧指令, '周期'))
                # print(旧指令, '周期')
                # time.sleep(0.02)
                设备.发送(旧指令)
            # logger.info("++++004+++++")
            if 计数 % 1 == 0:
                time_end = time.time()

                输出_实际_A = F.softmax(输出_实际_A, dim=-1)
                输出_实际_A = 输出_实际_A[:, -1, :]
                抽样 = torch.multinomial(输出_实际_A, num_samples=1)
                抽样np = 抽样.cpu().numpy()

                指令 = 数_词表[str(抽样np[0, -1])]
                指令集 = 指令.split('_')

                # 操作词典 = {"图片号": "0", "移动操作": "无移动", "动作操作": "无动作"}
                操作词典['图片号'] = str(i)
                方向结果 = 处理方向()
                # logger.info("++++005+++++")
                logger.info("方向结果:{} 操作列:{} 攻击态:{}".format(
                    方向结果, len(操作列), 攻击态))

                # deal with output
                操作列, output_suc = output(方向结果, 操作列, 操作词典, 指令集, 旧指令, 设备, imgA,
                                         i)

                if output_suc == 0:
                    AI打开 = False
                    break

                # logging
                # logger.info("++++008+++++")
                用时1 = 0.22 - (time.time() - 计时开始)
                if 用时1 > 0:
                    logger.info("++++sleep+++++")
                    time.sleep(用时1)
                    logger.info("+++++++++")

                用时 = time_end - time_start
                print("用时{} 第{}张 延时{}".format(用时, i, 用时1), 'A键按下', A键按下,
                      'W键按下', W键按下, 'S键按下', S键按下, 'D键按下', D键按下, '旧指令', 旧指令,
                      'AI打开', AI打开, '操作列', 操作列)

                计数 = 计数 + 1
                # logger.info("++++009+++++")

    记录文件.close()
    time.sleep(1)
    print('AI打开', AI打开)