def main():
    # Load net
    cnn = CNN()
    loss_func = nn.MultiLabelSoftMarginLoss()
    optimizer = optim.Adam(cnn.parameters(), lr=learning_rate)
    if torch.cuda.is_available():
        cnn.cuda()
        loss_func.cuda()

    # Load data
    train_dataloader = dataset.get_train_data_loader()
    test_dataloader = dataset.get_test_data_loader()

    # Train model
    for epoch in range(num_epochs):
        cnn.train()
        for i, (images, labels) in enumerate(train_dataloader):
            images = Variable(images)
            labels = Variable(labels.long())
            if torch.cuda.is_available():
                images = images.cuda()
                labels = labels.cuda()
            predict_labels = cnn(images)
            loss = loss_func(predict_labels, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            if (i + 1) % 100 == 0:
                print("epoch:", epoch, "step:", i, "loss:", loss.item())

        # Save and test model
        if (epoch + 1) % 10 == 0:
            filename = "model" + str(epoch + 1) + ".pkl"
            torch.save(cnn.state_dict(), filename)
            cnn.eval()
            correct = 0
            total = 0
            for (image, label) in test_dataloader:
                vimage = Variable(image)
                if torch.cuda.is_available():
                    vimage = vimage.cuda()
                output = cnn(vimage)
                predict_label = ""
                for k in range(4):
                    predict_label += config.CHAR_SET[np.argmax(
                        output[0, k * config.CHAR_SET_LEN:(k + 1) *
                               config.CHAR_SET_LEN].data.cpu().numpy())]
                true_label = one_hot.vec2text(label.numpy()[0])
                total += label.size(0)
                if predict_label == true_label:
                    correct += 1
                if total % 200 == 0:
                    print(
                        'Test Accuracy of the model on the %d test images: %f %%'
                        % (total, 100 * correct / total))
            print('Test Accuracy of the model on the %d test images: %f %%' %
                  (total, 100 * correct / total))
            print("save and test model...")
    torch.save(cnn.state_dict(), "./model.pkl")  # current is model.pkl
    print("save last model")
def run_predict_one_fold(fold: int):
    test_data_loader = get_test_data_loader()

    model = load_trained_model(f"checkpoint-f{fold}.pt")

    results = []

    for images, image_ids in test_data_loader:
        images = list(image.to(config.DEVICE) for image in images)
        outputs = model(images)
        for i, image in enumerate(images):
            boxes = outputs[i]['boxes'].cpu().detach().numpy()
            scores = outputs[i]['scores'].cpu().detach().numpy()
            boxes = boxes[scores > config.SCORE_THRESHOLD]
            scores = scores[scores > config.SCORE_THRESHOLD]
            image_id = image_ids[i]

            result = {
                'image_id': image_id,
                'PredictionString': format_prediction_string(boxes, scores)
            }

            results.append(result)

            save_labeled_image(image, boxes, image_id)

    test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
    test_df.to_csv('submission.csv', index=False)
Exemple #3
0
def test(model_name='model.pkl'):
    cnn = CNN()
    cnn.eval()
    cnn.load_state_dict(torch.load(model_name))
    print('load cnn net.')

    test_dataloader = dataset.get_test_data_loader()

    correct = 0
    total = 0
    for i, (images, labels) in enumerate(test_dataloader):
        image = images
        vimage = Variable(image)
        predict_label = cnn(vimage)

        chars = ''
        for i in range(setting.MAX_CAPTCHA):
            chars += setting.ALL_CHAR_SET[np.argmax(
                predict_label[0, i * setting.ALL_CHAR_SET_LEN:(i + 1) *
                              setting.ALL_CHAR_SET_LEN].data.numpy())]

        predict_label = chars
        true_label = one_hot.decode(labels.numpy()[0])
        total += labels.size(0)

        if (predict_label == true_label):
            correct += 1
        else:
            print('Predict:' + predict_label)
            print('Real   :' + true_label)
        if (total % 200 == 0):
            print('Test Accuracy of the model on the %d test images: %f %%' %
                  (total, 100 * correct / total))
    print('Test Accuracy of the model on the %d test images: %f %%' %
          (total, 100 * correct / total))
def run_predict():
    test_data_loader = get_test_data_loader()

    for fold in range(0, config.NUM_FOLDS):
        models.append(load_trained_model(f"checkpoint-f{fold}.pt"))

    results = []

    for images, image_ids in test_data_loader:
        predictions = make_ensemble_predictions(images, models)
        for i, image in enumerate(images):
            boxes, scores, labels = run_wbf(predictions, image_index=i)
            boxes = boxes.astype(np.int32)
            image_id = image_ids[i]

            result = {
                'image_id': image_id,
                'PredictionString': format_prediction_string(boxes, scores)
            }
            results.append(result)

            save_labeled_image(image, boxes, image_id)

    test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
    test_df.to_csv('submission.csv', index=False)
Exemple #5
0
def main():
    cnn = CNN()
    cnn.eval()
    cnn.load_state_dict(torch.load('model.pkl'))
    print("load cnn net.")

    test_dataloader = dataset.get_test_data_loader()

    correct = 0
    total = 0
    for i, (images, labels) in enumerate(test_dataloader):
        image = images
        vimage = Variable(image)
        predict_label = cnn(vimage)

        c0 = setting.ALL_CHAR_SET[np.argmax(
            predict_label[0, 0:setting.ALL_CHAR_SET_LEN].data.numpy())]
        c1 = setting.ALL_CHAR_SET[np.argmax(
            predict_label[0, setting.ALL_CHAR_SET_LEN:2 *
                          setting.ALL_CHAR_SET_LEN].data.numpy())]
        c2 = setting.ALL_CHAR_SET[np.argmax(
            predict_label[0, 2 * setting.ALL_CHAR_SET_LEN:3 *
                          setting.ALL_CHAR_SET_LEN].data.numpy())]
        c3 = setting.ALL_CHAR_SET[np.argmax(
            predict_label[0, 3 * setting.ALL_CHAR_SET_LEN:4 *
                          setting.ALL_CHAR_SET_LEN].data.numpy())]
        predict_label = '%s%s%s%s' % (c0, c1, c2, c3)
        true_label = one_hot_encoding.decode(labels.numpy()[0])
        total += labels.size(0)
        if (predict_label == true_label):
            correct += 1
        if (total % 200 == 0):
            print('Test Accuracy of the model on the %d test images: %f %%' %
                  (total, 100 * correct / total))
    print('Test Accuracy of the model on the %d test images: %f %%' %
          (total, 100 * correct / total))
    return correct / total