Ejemplo n.º 1
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss = 0
    correct = 0
    total = 0
    with flow.no_grad():
        for batch_idx, (torch_inputs, torch_targets) in enumerate(testloader):
            inputs = flow.tensor(torch_inputs.numpy())
            targets = flow.tensor(torch_targets.numpy())
            inputs, targets = inputs.to(device), targets.to(device)
            loss, outputs = resnet18_eval_graph(inputs, targets)
            # loss = criterion(outputs, targets)

            test_loss += loss.item()
            # _, predicted = outputs.max(1)
            predicted = flow.argmax(outputs, 1).to(flow.int64)
            total += targets.size(0)

            correct += predicted.eq(targets).to(flow.int32).sum().item()

            progress_bar(
                batch_idx, len(testloader),
                'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
                (test_loss /
                 (batch_idx + 1), 100. * correct / total, correct, total))

    # Save checkpoint.
    acc = 100. * correct / total
    if acc > best_acc:
        print('Saving..')
        best_acc = acc
Ejemplo n.º 2
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (torch_inputs, torch_targets) in enumerate(trainloader):

        inputs = flow.tensor(torch_inputs.numpy(), requires_grad=False)
        targets = flow.tensor(torch_targets.numpy(), requires_grad=False)
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        # _, predicted = outputs.max(1)
        predicted = flow.argmax(outputs, 1).to(flow.int64)
        total += targets.size(0)

        correct += predicted.eq(targets).to(flow.int32).sum().item()

        progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                     % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
Ejemplo n.º 3
0
def test(opt):

    model = DeepQNetwork()
    pretrain_models = flow.load("{}".format(opt.saved_path))
    model.load_state_dict(pretrain_models)
    model.eval()
    model.to("cuda")
    game_state = GameState()
    image, reward, terminal = game_state.frame_step(0)
    image = pre_processing(
        image[:game_state.SCREENWIDTH, :int(game_state.BASEY)],
        opt.image_size,
        opt.image_size,
    )
    image = flow.Tensor(image)
    image = image.to("cuda")
    state = flow.cat(tuple(image for _ in range(4))).unsqueeze(0)

    while True:
        prediction = model(state)[0]
        action = flow.argmax(prediction).numpy()[0]

        next_image, reward, terminal = game_state.frame_step(action)
        next_image = pre_processing(
            next_image[:game_state.SCREENWIDTH, :int(game_state.BASEY)],
            opt.image_size,
            opt.image_size,
        )
        next_image = flow.Tensor(next_image)
        next_image = next_image.to("cuda")
        next_state = flow.cat((state[0, 1:, :, :], next_image)).unsqueeze(0)

        state = next_state
Ejemplo n.º 4
0
def predict(model, text):
    model.eval()
    logits = model(flow.tensor(text))
    logits = flow.softmax(logits)
    label = flow.argmax(logits)

    return label.numpy(), logits.numpy()
Ejemplo n.º 5
0
def infer(opt):
    with open(opt.label_dict, "r") as f:
        lab_dict = json.load(f)

    cnn = simple_CNN(opt.num_speakers)
    cnn.to("cuda")

    cnn.load_state_dict(flow.load(opt.load_path))
    cnn.eval()

    label_list = lab_dict["test"]
    err_sum = 0
    for wav, label in label_list:
        inp, lab = example_precess(wav, label)
        inp = inp.unsqueeze(1)
        pout = cnn(inp)
        pred = flow.argmax(pout, dim=1)

        err = 1 if (pred + 1).numpy() != lab.long().numpy() else 0
        err_sum += err
        print(
            "wav_filename: ",
            wav,
            "    predicted speaker id: ",
            (pred + 1).numpy()[0],
            "    real speaker id: ",
            lab.long().numpy()[0],
        )
    print("accuracy: ", 1 - err_sum / 6)
Ejemplo n.º 6
0
def _test_argmax_axis_postive(test_case, device):
    input = flow.tensor(
        np.random.randn(2, 6, 5, 3), dtype=flow.float32, device=flow.device(device)
    )
    axis = 1
    of_out = flow.argmax(input, dim=axis)
    np_out = np.argmax(input.numpy(), axis=axis)
    test_case.assertTrue(np.array_equal(of_out.numpy().flatten(), np_out.flatten()))
Ejemplo n.º 7
0
def predict(model, text):
    model.eval()
    text = flow.tensor(text).to("cuda")
    text.unsqueeze(0)
    logits = model(text)
    logits = flow.softmax(logits)
    label = flow.argmax(logits)

    return label.numpy(), logits.numpy()
Ejemplo n.º 8
0
def train(opt):
    with open(opt.label_dict, "r") as f:
        lab_dict = json.load(f)

    cnn = simple_CNN(opt.num_speakers)
    cnn.to("cuda")

    cost = nn.CrossEntropyLoss()
    cost.to("cuda")

    optimizer = optim.RMSprop(cnn.parameters(),
                              lr=opt.lr,
                              alpha=opt.alpha,
                              eps=opt.eps)

    output_folder = opt.output_path
    N_batches = opt.N_batches
    N_epoches = opt.N_epoches

    for epoch in range(N_epoches):
        cnn.train()

        loss_sum = 0
        err_sum = 0

        for i in range(N_batches):

            inp, lab = create_batches_rnd(
                lab_dict,
                batch_size=opt.batch_size,
                wlen=opt.wlen,
                fact_amp=opt.fact_amp,
                train=True,
            )
            inp = inp.unsqueeze(1)
            lab -= 1

            pout = cnn(inp)
            pred = flow.argmax(pout, dim=1)
            loss = cost(pout, lab.long())
            err = np.mean(pred.numpy() != lab.long().numpy())

            loss.backward()
            optimizer.step()
            optimizer.zero_grad()

            loss_sum = loss_sum + loss.detach()
            err_sum = err_sum + err

        loss_tot = loss_sum / N_batches
        err_tot = err_sum / N_batches

        if epoch % 10 == 0:
            print("epoch %i, loss_tr=%f err_tr=%f" %
                  (epoch, loss_tot.numpy(), err_tot))

    flow.save(cnn.state_dict(), os.path.join(output_folder, "CNN_model"))
Ejemplo n.º 9
0
def train(
    model,
    device,
    train_data,
    dev_data,
    loss_func,
    optimizer,
    epochs,
    train_batch_size,
    eval_batch_size,
    save_path,
):
    global_acc = float("-inf")
    for i in range(epochs):
        x_batch, y_batch = batch_loader(train_data[0], train_data[1],
                                        train_batch_size)
        model.train()
        model.training = True
        training_loss = 0
        all_res, all_ground_truths = [], []
        total_correct = 0
        total_wrongs = 0
        for idx, (data, label) in enumerate(
                tqdm(zip(x_batch, y_batch), total=len(x_batch))):
            data = data.to(device)
            label = label.to(device)
            logits = model(data)
            res = flow.argmax(logits, dim=1)
            total_correct += (res.numpy() == label.numpy()).sum()
            all_res.append(res)
            all_ground_truths.append(label)
            label = flow.tensor(np.eye(2)[label.numpy()],
                                dtype=flow.float32).to(device)
            loss = loss_func(logits, label)
            training_loss += loss.numpy()
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
        all_ground_truths = flow.cat(all_ground_truths)
        train_acc = total_correct / len(all_ground_truths.numpy())
        acc = _eval(model, dev_data, device, eval_batch_size)
        if acc > global_acc:
            global_acc = acc
            if os.path.exists(save_path):
                shutil.rmtree(save_path)
            flow.save(model.state_dict(), save_path)
        print(
            f"[Epoch{i}] training loss: {training_loss/(idx+1)}  training accuracy: {train_acc} evaluation accuracy: {acc}"
        )
Ejemplo n.º 10
0
def inference_afqmc(args):

    tokenizer = BertTokenizer.from_pretrained("hfl/chinese-roberta-wwm-ext")
    model = ClueAFQMCCPT(args.pretrain_dir, args.n_classes,
                         args.is_train).to(args.device)

    vec = tokenizer(args.text1, args.text2)
    input_ids = vec["input_ids"]
    attention_mask = vec["attention_mask"]
    input_ids = flow.tensor(input_ids,
                            dtype=flow.int32).reshape(1, -1).to(args.device)
    attention_mask = (flow.tensor(attention_mask, dtype=flow.int32).reshape(
        1, -1).to(args.device))

    model.load_state_dict(flow.load(args.model_load_dir))
    model.eval()
    output = model(input_ids, attention_mask)
    output = flow.softmax(output)
    label = flow.argmax(output)
    print("Softmax output:", output.numpy())
    print("Predict:", label.numpy())
Ejemplo n.º 11
0
def _eval(model, dev_data, device, batch_size=32):
    model.eval()
    model.training = False
    x_batch, y_batch = batch_loader(dev_data[0],
                                    dev_data[1],
                                    batch_size,
                                    shuffle=False)
    all_res, all_ground_truths = [], []
    total_correct = 0
    for data, label in tqdm(zip(x_batch, y_batch), total=len(x_batch)):
        with flow.no_grad():
            data = data.to(device)
            label = label.to(device)
            logits = model(data)
            res = flow.argmax(logits, dim=1)
            total_correct += (res.numpy() == label.numpy()).sum()
            all_res.append(res)
            all_ground_truths.append(label)
    all_res = flow.cat(all_res)
    all_ground_truths = flow.cat(all_ground_truths)
    acc = total_correct / len(all_ground_truths.numpy())
    return acc
Ejemplo n.º 12
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss = 0
    correct = 0
    total = 0
    with flow.no_grad():
        for batch_idx, (torch_inputs, torch_targets) in enumerate(testloader):
            inputs = flow.tensor(torch_inputs.numpy())
            targets = flow.tensor(torch_targets.numpy())
            inputs, targets = inputs.to(device), targets.to(device)
            outputs = net(inputs)
            loss = criterion(outputs, targets)

            test_loss += loss.item()
            # _, predicted = outputs.max(1)
            predicted = flow.argmax(outputs, 1).to(flow.int64)
            total += targets.size(0)

            
            correct += predicted.eq(targets).to(flow.int32).sum().item()

            progress_bar(batch_idx, len(testloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                         % (test_loss/(batch_idx+1), 100.*correct/total, correct, total))

    # Save checkpoint.
    acc = 100.*correct/total
    if acc > best_acc:
        print('Saving..')
        # state = {
        #     'net': net.state_dict(),
        #     'acc': acc,
        #     'epoch': epoch,
        # }
        # if not os.path.isdir('checkpoint'):
        #     os.mkdir('checkpoint')
        # flow.save(state, './checkpoint')
        best_acc = acc
Ejemplo n.º 13
0
begin = time.time()
time_batch = []

for epoch in range(begin_epochs, N_epochs):
    MOBILENET_net.train()

    loss_sum = 0
    err_sum = 0

    for i in tqdm(range(N_batches)):

        [inp, lab] = create_batches_rnd(batch_size, data_folder, wav_lst_tr,
                                        snt_tr, wlen, lab_dict, 0.2)

        pout = MOBILENET_net(inp)
        pred = flow.argmax(pout, dim=1)
        loss = cost(pout, lab.long())
        err = np.mean(pred.numpy() != lab.long().numpy())

        loss.backward()

        optimizer_MOBILENET.step()
        optimizer_MOBILENET.zero_grad()

        loss_sum = loss_sum + loss.detach()
        err_sum = err_sum + err

    loss_tot = loss_sum / N_batches
    err_tot = err_sum / N_batches

    # Full Validation  new
Ejemplo n.º 14
0
def train(opt):

    # Step 1: init BrainDQN
    model = DeepQNetwork()
    model.to("cuda")
    optimizer = flow.optim.Adam(model.parameters(), lr=opt.lr)
    criterion = flow.nn.MSELoss()
    criterion.to("cuda")

    # Step 2: init Flappy Bird Game
    game_state = GameState()
    # Step 3: play game
    # image.shape = (288,512,3), reward: float, terminal: boolean
    image, reward, terminal = game_state.frame_step(0)
    # image.shape = (84, 84)
    image = pre_processing(
        image[: game_state.SCREENWIDTH, : int(game_state.BASEY)],
        opt.image_size,
        opt.image_size,
    )
    image = flow.Tensor(image, dtype=flow.float32)
    image = image.to("cuda")
    state = flow.cat(tuple(image for _ in range(4))).unsqueeze(0)

    replay_memory = []
    iter = 0
    # Step 4: run the game
    while iter < opt.num_iters:
        model.train()

        prediction = model(state)[0]
        # Exploration or exploitation
        epsilon = opt.final_epsilon + (
            (opt.num_iters - iter)
            * (opt.initial_epsilon - opt.final_epsilon)
            / opt.num_iters
        )
        u = random()
        random_action = u <= epsilon
        if random_action:
            print("Perform a random action")
            action = randint(0, 1)
        else:
            action = flow.argmax(prediction).numpy()[0]

        next_image, reward, terminal = game_state.frame_step(action)
        next_image = pre_processing(
            next_image[: game_state.SCREENWIDTH, : int(game_state.BASEY)],
            opt.image_size,
            opt.image_size,
        )
        next_image = flow.Tensor(next_image)
        next_image = next_image.to("cuda")
        next_state = flow.cat((state[0, 1:, :, :], next_image)).unsqueeze(0)

        replay_memory.append([state, action, reward, next_state, terminal])
        if len(replay_memory) > opt.replay_memory_size:
            del replay_memory[0]
        batch = sample(replay_memory, min(len(replay_memory), opt.batch_size))
        state_batch, action_batch, reward_batch, next_state_batch, terminal_batch = zip(
            *batch
        )

        state_batch = flow.cat(tuple(state for state in state_batch))
        action_batch = flow.Tensor(
            np.array(
                [[1, 0] if action == 0 else [0, 1] for action in action_batch],
                dtype=np.float32,
            )
        )
        reward_batch = flow.Tensor(np.array(reward_batch, dtype=np.float32)[:, None])
        next_state_batch = flow.cat(tuple(state for state in next_state_batch))

        state_batch = state_batch.to("cuda")
        action_batch = action_batch.to("cuda")
        reward_batch = reward_batch.to("cuda")
        next_state_batch = next_state_batch.to("cuda")
        current_prediction_batch = model(state_batch)
        next_prediction_batch = model(next_state_batch)

        y_batch = flow.cat(
            tuple(
                reward_batch[i]
                if terminal_batch[i]
                else reward_batch[i] + opt.gamma * flow.max(next_prediction_batch[i])
                for i in range(reward_batch.shape[0])
            )
        )

        q_value = flow.sum(current_prediction_batch * action_batch, dim=1)

        loss = criterion(q_value, y_batch)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

        state = next_state
        iter += 1

        print(
            "Iteration: {}/{}, Action: {}, Loss: {}, Epsilon {}, Reward: {}, Q-value: {}".format(
                iter + 1,
                opt.num_iters,
                action,
                loss.numpy(),
                epsilon,
                reward,
                flow.max(prediction).numpy()[0],
            )
        )

        if (iter + 1) % 100000 == 0:
            flow.save(
                model.state_dict(),
                os.path.join(opt.save_checkpoint_path, "epoch_%d" % (iter + 1)),
            )
    flow.save(
        model.state_dict(),
        os.path.join(opt.save_checkpoint_path, "epoch_%d" % (iter + 1)),
    )
    print("train success!")
Ejemplo n.º 15
0
def infer():
    CNN_net.eval()
    DNN1_net.eval()
    DNN2_net.eval()
    loss_sum = 0
    err_sum = 0
    err_sum_snt = 0

    with flow.no_grad():
        for i in range(snt_te):
            [signal, fs] = sf.read(data_folder + wav_lst_te[i])

            signal = flow.Tensor(signal).to("cuda")
            lab_batch = lab_dict[wav_lst_te[i].lower()]

            # split signals into chunks
            beg_samp = 0
            end_samp = wlen

            N_fr = int((signal.shape[0] - wlen) / (wshift))

            sig_arr = flow.zeros((Batch_dev, wlen),
                                 dtype=flow.float32).to("cuda")
            lab = (flow.zeros(N_fr + 1) + lab_batch).to("cuda").long()
            pout = flow.zeros((N_fr + 1, class_lay[-1]),
                              dtype=flow.float32).to("cuda")
            count_fr = 0
            count_fr_tot = 0
            while end_samp < signal.shape[0]:
                sig_arr[count_fr, :] = signal[beg_samp:end_samp]
                beg_samp = beg_samp + wshift
                end_samp = beg_samp + wlen
                count_fr = count_fr + 1
                count_fr_tot = count_fr_tot + 1
                if count_fr == Batch_dev:
                    inp = flow.Tensor(sig_arr).to(sig_arr.device)
                    pout[count_fr_tot - Batch_dev:count_fr_tot, :] = DNN2_net(
                        DNN1_net(CNN_net(inp)))
                    count_fr = 0
                    sig_arr = flow.zeros((Batch_dev, wlen),
                                         dtype=flow.float32).to("cuda")

            if count_fr > 0:
                inp = flow.Tensor(sig_arr[0:count_fr]).to(sig_arr.device)
                pout[count_fr_tot - count_fr:count_fr_tot, :] = DNN2_net(
                    DNN1_net(CNN_net(inp)))

            pred = flow.argmax(pout, dim=1)
            loss = cost(pout, lab.long())

            err = np.mean(pred.numpy() != lab.long().numpy())

            best_class = flow.argmax(flow.sum(pout, dim=0), dim=0)
            err_sum_snt = err_sum_snt + (best_class.numpy() != lab[0].numpy())

            loss_sum = loss_sum + loss.detach()
            err_sum = err_sum + err

        err_tot_dev_snt = err_sum_snt / snt_te
        loss_tot_dev = loss_sum / snt_te
        err_tot_dev = err_sum / snt_te

    print("loss_te=%f err_te=%f err_te_snt=%f" %
          (loss_tot_dev.numpy(), err_tot_dev, err_tot_dev_snt))
Ejemplo n.º 16
0
def _argmax(self, dim=None, keepdim=None):
    return flow.argmax(self, dim=dim, keepdim=keepdim)
Ejemplo n.º 17
0
 def test_argmax_index_error(test_case):
     with test_case.assertRaises(Exception) as context:
         x = flow.ones((4, 4), dtype=flow.float32, requires_grad=True)
         y = flow.argmax(x, dim=4)
     test_case.assertTrue(
         "Dimension out of range" in str(context.exception))