Ejemplo n.º 1
0
         E, R, source_training_loader_d, length_source_training)
     # 3. Target
     target_loss, target_error = evaluate(E, R, target_loader_d,
                                          length_target)
     print('Source training loss:', source_training_loss,
           'Source training error:', source_training_error)
     print('Source validation loss:', source_validation_loss,
           'Source validation error:', source_validation_error)
     print('Target loss:', target_loss, 'Target error', target_error)
     # Save Step
     if epoch == 0:
         target_loss_minimum = target_loss
         target_error_minimum = target_error
     else:
         if target_loss < target_loss_minimum:
             torch.save(E.state_dict(), 'E_l1.pkl')
             torch.save(D.state_dict(), 'D_l1.pkl')
             torch.save(R.state_dict(), 'R_l1.pkl')
             target_loss_minimum = target_loss
         if target_error < target_error_minimum:
             torch.save(E.state_dict(), 'E_l2.pkl')
             torch.save(D.state_dict(), 'D_l2.pkl')
             torch.save(R.state_dict(), 'R_l2.pkl')
             target_error_minimum = target_error
 # Optim Step
 for num, ((source_batch_x_d, source_batch_y_d),
           (target_batch_x_d, target_batch_y_d), (source_batch_x_e,
                                                  source_batch_y_e),
           (target_batch_x_e, target_batch_y_e)) in enumerate(
               zip(source_training_loader_d, target_loader_d,
                   source_training_loader_e, target_loader_e)):
Ejemplo n.º 2
0
            s3_cls = s3_cls.data.cpu().numpy()
            res = s1_cls * s1_weight + s2_cls * s2_weight + s3_cls * s3_weight
            #pred = res.argmax(axis=1)
            pred = np.argmax(res, axis=1)
            labels = labels.numpy()
            correct += np.equal(labels, pred).sum()
        current_accuracy = correct * 1.0 / len(t_set_test)
        current_accuracy = current_accuracy.item()
        logger.info("Current accuracy is:{}".format(current_accuracy))

        if current_accuracy >= max_correct:
            max_correct = current_accuracy
            max_step = step
            max_epoch = cls_epoch
            torch.save(
                extractor.state_dict(),
                os.path.join(
                    snapshot, "p2_extractor_" + str(step) + "_" +
                    str(cls_epoch) + ".pth"))
            torch.save(
                s1_classifier.state_dict(),
                os.path.join(
                    snapshot,
                    os.path.abspath(__file__).split('/')[-1].split('.')[0] +
                    "p2_s1_cls_" + str(step) + "_" + str(cls_epoch) + ".pth"))
            torch.save(
                s2_classifier.state_dict(),
                os.path.join(
                    snapshot,
                    os.path.abspath(__file__).split('/')[-1].split('.')[0] +
                    "p2_s2_cls_" + str(step) + "_" + str(cls_epoch) + ".pth"))
Ejemplo n.º 3
0
            s2_cls = F.softmax(s2_cls, dim=1)
            s1_cls = s1_cls.data.cpu().numpy()
            s2_cls = s2_cls.data.cpu().numpy()
            res = s1_cls * s1_weight + s2_cls * s2_weight
            pred = np.argmax(res, axis=1)
            labels = labels.numpy()
            correct += np.equal(labels, pred).sum()
        current_accuracy = correct * 1.0 / len(t_set_test)
        current_accuracy = current_accuracy
        logger.info("Current accuracy is:{}".format(current_accuracy))

        if current_accuracy >= max_correct:
            max_correct = current_accuracy
            max_step = step
            max_epoch = cls_epoch
            torch.save(extractor.state_dict(),
                       os.path.join(snapshot, "_extractor" + ".pth"))
            torch.save(
                s1_classifier.state_dict(),
                os.path.join(
                    snapshot,
                    os.path.abspath(__file__).split('/')[-1].split('.')[0] +
                    "_s1_cls" + ".pth"))
            torch.save(
                s2_classifier.state_dict(),
                os.path.join(
                    snapshot,
                    os.path.abspath(__file__).split('/')[-1].split('.')[0] +
                    "_s2_cls" + ".pth"))

    # Part 3: train discriminator and generate mix feature