コード例 #1
0
def transfer_classifier_params(args):
    pytorch_model = TorchClassifier()
    with open(args.torch_classifier_model_path, 'rb') as f:
        weights = dill.load(f)
    state_dict = {k: torch.FloatTensor(v) for k, v in weights[0].items()}
    pytorch_model.load_state_dict(state_dict)
    for k in state_dict.keys():
        print('key: ', k)

    chainer_model = Classifier()

    copy_conv_weight(pytorch_model.LeNet[0], chainer_model.conv1)
    copy_conv_weight(pytorch_model.LeNet[2], chainer_model.conv2)
    copy_batch_norm_weight(pytorch_model.LeNet[3], chainer_model.batch_norm1)
    copy_conv_weight(pytorch_model.LeNet[5], chainer_model.conv3)
    copy_batch_norm_weight(pytorch_model.LeNet[6], chainer_model.batch_norm2)
    copy_conv_weight(pytorch_model.LeNet[8], chainer_model.conv4)
    copy_batch_norm_weight(pytorch_model.LeNet[9], chainer_model.batch_norm3)
    copy_conv_weight(pytorch_model.LeNet[11], chainer_model.conv5)

    if args.sample_image:
        subtractor = Subtractor()
        load_model(args.chainer_subtractor_model_path, subtractor)
        image1 = convert_to_grayscale(subtractor, args.sample_image).data
        image2 = np.zeros(shape=image1.shape, dtype=np.float32)
        print('image1 shape: ', image1.shape)
        print('image2 shape: ', image2.shape)

        classify_image_with_pytorch_model(pytorch_model, torch.Tensor(image1), torch.Tensor(image2))
        classify_image_with_chainer_model(chainer_model, chainer.Variable(image1), chainer.Variable(image2))

    save_model(args.chainer_classifier_save_path, chainer_model)
コード例 #2
0
ファイル: main.py プロジェクト: yuishihara/chainer-ppo
def run_training_loop(actors, model, test_env, outdir, args):
    optimizer = setup_adam_optimizer(args, model, args.learning_rate)

    result_file = os.path.join(outdir, 'result.txt')
    if not files.file_exists(result_file):
        with open(result_file, "w") as f:
            f.write('timestep\tmean\tmedian\n')

    alpha = 1.0
    with ThreadPoolExecutor(max_workers=8) as executor:
        previous_evaluation = args.initial_timestep
        for timestep in range(args.initial_timestep, args.total_timesteps,
                              args.timesteps * args.actor_num):
            if args.env_type == 'atari':
                alpha = (1.0 - timestep / args.total_timesteps)
            print('current timestep: ', timestep, '/', args.total_timesteps)
            print('current alpha: ', alpha)
            data = sample_data(actors, model, executor)
            iterator = prepare_iterator(args, *data)
            for _ in range(args.epochs):
                # print('epoch num: ', epoch)
                iterator.reset()
                while not iterator.is_new_epoch:
                    optimize_surrogate_loss(iterator, model, optimizer, alpha,
                                            args)
            optimizer.hyperparam.alpha = args.learning_rate * alpha
            print('optimizer step size', optimizer.hyperparam.alpha)

            if (timestep -
                    previous_evaluation) // args.evaluation_interval == 1:
                previous_evaluation = timestep
                actor = actors[0]
                rewards = actor.run_evaluation(model, test_env,
                                               args.evaluation_trial)

                mean = np.mean(rewards)
                median = np.median(rewards)
                print('mean: {mean}, median: {median}'.format(mean=mean,
                                                              median=median))

                print('saving model of iter: ', timestep, ' to: ')
                model_filename = 'model_iter-{}'.format(timestep)

                model.to_cpu()
                serializers.save_model(os.path.join(outdir, model_filename),
                                       model)

                optimizer_filename = 'optimizer_iter-{}'.format(timestep)
                save_optimizer(os.path.join(outdir, optimizer_filename),
                               optimizer)
                if not args.gpu < 0:
                    model.to_gpu()

                with open(result_file, "a") as f:
                    f.write('{timestep}\t{mean}\t{median}\n'.format(
                        timestep=timestep, mean=mean, median=median))
コード例 #3
0
ファイル: main.py プロジェクト: yuishihara/chainer-naf
def save_params(naf, timestep, outdir, args):
    print('saving model params of iter: ', timestep)

    q_filename = 'q_iter-{}'.format(timestep)

    naf._q.to_cpu()
    serializers.save_model(os.path.join(outdir, q_filename), naf._q)

    if not args.gpu < 0:
        naf._q.to_gpu()
コード例 #4
0
def transfer_subtractor_params(args):
    pytorch_model = FCN_mse()
    pytorch_model.load_state_dict(torch.load(args.torch_subtractor_model_path))

    chainer_model = Subtractor()

    copy_conv_weight(pytorch_model.conv1, chainer_model.conv1)
    copy_conv_weight(pytorch_model.conv2, chainer_model.conv2)
    copy_conv_weight(pytorch_model.classifier, chainer_model.classifier)

    if args.sample_image:
        show_sample_subtraction(chainer_model, args)
    save_model(args.chainer_subtractor_save_path, chainer_model)
コード例 #5
0
ファイル: main.py プロジェクト: yuishihara/chainer-td3
def save_params(td3, timestep, outdir, args):
    print('saving model params of iter: ', timestep)

    q1_filename = 'q1_iter-{}'.format(timestep)
    q2_filename = 'q2_iter-{}'.format(timestep)
    pi_filename = 'pi_iter-{}'.format(timestep)

    td3._q1.to_cpu()
    td3._q2.to_cpu()
    td3._pi.to_cpu()
    serializers.save_model(os.path.join(outdir, q1_filename), td3._q1)
    serializers.save_model(os.path.join(outdir, q2_filename), td3._q2)
    serializers.save_model(os.path.join(outdir, pi_filename), td3._pi)

    if not args.gpu < 0:
        td3._q1.to_gpu()
        td3._q2.to_gpu()
        td3._pi.to_gpu()
コード例 #6
0
ファイル: main.py プロジェクト: yuishihara/chainer-ppo
def save_optimizer(filepath, optimizer):
    serializers.save_model(filepath, optimizer)