def apply_regression_to_fmri(fmri_file, dt_key, model, frame_idx, subject=1):
    # with h5py.File("/data1/home/guangjie/Data/vim-2-gallant/myOrig/imagenet128_embeds_from_vqvae.hdf5",'r') as ebdf:
    #     embeds = ebdf['embeds']
    saveDir = "/data1/home/guangjie/Data/vim-2-gallant/regressed_k_of_vqvae/subject_{}/{}/".format(
        subject, dt_key)
    os.makedirs(saveDir, exist_ok=True)
    model.load_state_dict(
        torch.load(
            "/data1/home/guangjie/Data/vim-2-gallant/regressionKModel/subject_{}/frame_{}/subject_{}_frame_{}_regression_k_model_i_4917_o_1024.pth"
            .format(subject, frame_idx, subject, frame_idx)))
    mean, std = None, None
    dataset = fmri_dataset(fmri_file, mean, std, dt_key=dt_key)
    dataloader = DataLoader(dataset, batch_size=128, shuffle=False)
    with h5py.File(
            os.path.join(saveDir,
                         "frame_{}_regressed_k.hdf5".format(frame_idx)),
            'w') as sf:
        k = sf.create_dataset('k',
                              shape=(len(dataset), 32, 32),
                              dtype=np.uint16)
        with torch.no_grad():
            model.eval()
            begin_idx = 0
            for step, fmri in enumerate(dataloader):
                out = model(fmri.cuda())
                end_idx = begin_idx + len(out)
                out = torch.clamp(out.view(-1, 32, 32) * 512, 0,
                                  511).cpu().numpy().astype(np.uint16)
                k[begin_idx:end_idx] = out  # todo 限制大小到0-511
                begin_idx = end_idx
                print(step)
Beispiel #2
0
def apply_regression_to_fmri_quick(dt_key, frame_idx, n_point, subject,
                                   model_in_dim, model_out_dim):
    model_dir = "/data1/home/guangjie/Data/vim-2-gallant/regressionFeaturePointModel/subject_{}/frame_{}".format(
        subject, frame_idx)
    save_dir = "/data1/home/guangjie/Data/vim-2-gallant/regressed_zq_of_vqvae_by_feature_point/subject_{}/{}/frame_{}".format(
        subject, dt_key, frame_idx)
    os.makedirs(save_dir, exist_ok=True)
    # model = NonLinearRegressionModel(model_in_dim, model_out_dim).cuda()
    model = LinearRegressionModel(model_in_dim, model_out_dim).cuda()
    # mean, std = get_vim2_fmri_mean_std(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     'rt')  # todo 归一化方式
    mean, std = None, None
    dataset = fmri_dataset(
        "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_{}.hdf5"
        .format('train' if dt_key == 'rt' else 'test'), mean, std,
        'rt')  # todo train test
    # dataset = fmri_dataset(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     mean, std, dt_key)  # todo
    dataloader = DataLoader(dataset,
                            batch_size=6000,
                            shuffle=False,
                            num_workers=0)

    with h5py.File(
            os.path.join(
                save_dir,
                "subject_{}_frame_{}_ze_fpoint_all_quick_apply.hdf5".format(
                    subject, frame_idx)), 'w') as sf:
        latent = sf.create_dataset('latent',
                                   shape=(len(dataset), 32, 32, 128),
                                   chunks=True)

        for p_idx in range(n_point):
            model.load_state_dict(
                torch.load(
                    os.path.join(
                        model_dir,
                        "subject_{}_frame_{}_regression_model_i_{}_o_{}_fpoint_{}.pth"
                        .format(subject, frame_idx, i_dim, o_dim, p_idx))))
            row_column = p_idx // 128
            latent_idx = p_idx % 128
            row = row_column // 32
            column = row_column % 32

            with torch.no_grad():
                begin_idx = 0
                model.eval()
                for step, fmri in enumerate(dataloader):
                    out = model(fmri.cuda())
                    end_idx = begin_idx + len(out)
                    latent[begin_idx:end_idx, row, column,
                           latent_idx] = out.squeeze().cpu().numpy(
                           )  # 需要cpu().numpy()?
                    begin_idx = end_idx
            print(p_idx)
Beispiel #3
0
def apply_regression_to_fmri(dt_key,
                             frame_idx,
                             subject=1,
                             n_fmap=128,
                             model_in_dim=4917,
                             model_out_dim=1024,
                             fmap_size=1024):
    model_dir = "/data1/home/guangjie/Data/purdue/exprimentData/regressionFeatureMapModel/subject_{}/frame_{}".format(
        subject, frame_idx)
    save_dir = "/data1/home/guangjie/Data/purdue/exprimentData/regressed_zq_of_vqvae_by_feature_map/subject_{}/{}/frame_{}".format(
        subject, dt_key, frame_idx)
    os.makedirs(save_dir, exist_ok=True)
    # model = NonLinearRegressionModel(model_in_dim, model_out_dim).cuda()
    model = LinearRegressionModel(model_in_dim, model_out_dim).cuda()
    # mean, std = get_vim2_fmri_mean_std(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     'rt')  # todo 归一化方式
    mean, std = None, None
    dataset = fmri_dataset(
        "/data1/home/guangjie/Data/purdue/exprimentData/fMRI/subject{}_{}_fmri.hdf5"
        .format(subject, dt_key), mean, std, dt_key)
    dataloader = DataLoader(dataset,
                            batch_size=128,
                            shuffle=False,
                            num_workers=0)

    for fmap_idx in range(n_fmap):
        model.load_state_dict(
            torch.load(
                os.path.join(
                    model_dir,
                    "subject_{}_frame_{}_regression_model_i_{}_o_{}_fmap_{}.pth"
                    .format(subject, frame_idx, i_dim, o_dim, fmap_idx))))
        sf = h5py.File(
            os.path.join(
                save_dir, "subject_{}_frame_{}_zq_fmap_{}.hdf5".format(
                    subject, frame_idx, fmap_idx)), 'w')  # todo zq
        fmap = sf.create_dataset('fmap', shape=(len(dataset), fmap_size))
        with torch.no_grad():
            begin_idx = 0
            model.eval()
            for step, fmri in enumerate(dataloader):
                out = model(fmri.cuda())
                end_idx = begin_idx + len(out)
                fmap[begin_idx:end_idx] = out.cpu().numpy()  # 需要cpu().numpy()?
                begin_idx = end_idx
        sf.close()
        print(fmap_idx)
def apply_regression_to_fmri(dt_key, frame_idx, n_fmap, n_slice, model_in_dim,
                             model_out_dim, subject):
    save_dir = "/data1/home/guangjie/Data/vim-2-gallant/regressed_zq_of_vqvae_by_feature_map_slice/subject_{}/{}/frame_{}".format(
        subject, dt_key, frame_idx)
    os.makedirs(save_dir, exist_ok=True)
    # model = NonLinearRegressionModel(model_in_dim, model_out_dim).cuda()
    model = LinearRegressionModel(model_in_dim, model_out_dim).cuda()
    # mean, std = get_vim2_fmri_mean_std(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     'rt')  # todo 归一化方式
    mean, std = None, None
    dataset = fmri_dataset(
        "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_{}.hdf5"
        .format('train' if dt_key == 'rt' else 'test'), mean, std,
        'rt')  # todo train test
    # dataset = fmri_dataset(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     mean, std, dt_key)  # todo
    dataloader = DataLoader(dataset,
                            batch_size=64,
                            shuffle=False,
                            num_workers=0)
    with h5py.File(
            os.path.join(
                save_dir, "subject_{}_frame_{}_ze_fmap_all.hdf5".format(
                    subject, frame_idx)), 'w') as sf:  # todo zq
        fmap = sf.create_dataset('latent', shape=(len(dataset), 32, 32, 128))
        for fmap_idx in range(n_fmap):
            model_dir = "/data1/home/guangjie/Data/vim-2-gallant/regressionFeatureMapSliceModel/subject_{}/frame_{}/fmap_{}".format(
                subject, frame_idx, fmap_idx)
            for slice_idx in range(n_slice):
                model.load_state_dict(
                    torch.load(
                        os.path.join(
                            model_dir,
                            "frame_{}_fmap_{}_slice_{}_regression_model.pth".
                            format(frame_idx, fmap_idx, slice_idx))))
                with torch.no_grad():
                    begin_idx = 0
                    model.eval()
                    for step, fmri in enumerate(dataloader):
                        out = model(fmri.cuda())  # 32
                        end_idx = begin_idx + len(out)
                        fmap[begin_idx:end_idx, slice_idx, :,
                             fmap_idx] = out.cpu().numpy()  # todo :,slice_idx
                        begin_idx = end_idx
            print(fmap_idx)
Beispiel #5
0
def apply_regression_to_fmri(dt_key,
                             frame_idx,
                             subject=1,
                             n_lantent=1024,
                             model_in_dim=4917,
                             model_out_dim=128,
                             dim_lantent=128):
    model_dir = "/data1/home/guangjie/Data/vim-2-gallant/regressionLatentModel/subject_{}/frame_{}".format(
        subject, frame_idx)
    save_dir = "/data1/home/guangjie/Data/vim-2-gallant/regressed_zq_of_vqvae_by_feature_latent/subject_{}/{}/frame_{}".format(
        subject, dt_key, frame_idx)
    os.makedirs(save_dir, exist_ok=True)
    # model = NonLinearRegressionModel(model_in_dim, model_out_dim).cuda()
    model = LinearRegressionModel(model_in_dim, model_out_dim).cuda()
    # mean, std = get_vim2_fmri_mean_std(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     'rt')  # todo 归一化方式
    mean, std = None, None
    dataset = fmri_dataset(
        "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_{}.hdf5"
        .format('train' if dt_key == 'rt' else 'test'), mean, std,
        'rt')  # todo train test
    # dataset = fmri_dataset(
    #     "/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject1_v1234_rt_rv_rva0.hdf5",
    #     mean, std, dt_key)  # todo
    dataloader = DataLoader(dataset,
                            batch_size=128,
                            shuffle=False,
                            num_workers=0)

    for latent_idx in range(n_lantent):
        # dataset = fmri_vector_dataset(
        #     fmri_file="/data1/home/guangjie/Data/vim-2-gallant/myOrig/VoxelResponses_subject{}_v1234_rt_rv_rva0.hdf5".format(
        #         subject), zq_file="/data1/home/guangjie/Data/vim-2-gallant/myOrig/zq_from_vqvae_{}.hdf5".format(
        #         'st' if dt_key == 'rt' else 'sv'),
        #     mean=mean,
        #     std=std, fmri_key=dt_key, frame_idx=frame_idx, latent_idx=latent_idx)
        # dataloader = DataLoader(dataset, batch_size=128, shuffle=False, num_workers=1)
        # 逐次加载单个模型,抽取每张图片对应ze的第lantent_idx个隐含表征
        model.load_state_dict(
            torch.load(
                os.path.join(
                    model_dir,
                    "subject_{}_regression_model_i_{}_o_{}_latent_{}.pth".
                    format(subject, model_in_dim, model_out_dim, latent_idx))))
        sf = h5py.File(
            os.path.join(
                save_dir, "subject_{}_frame_{}_ze_latent_{}.hdf5".format(
                    subject, frame_idx, latent_idx)), 'w')  # todo zq
        latent = sf.create_dataset('latent', shape=(len(dataset), dim_lantent))
        with torch.no_grad():
            begin_idx = 0
            model.eval()
            for step, fmri in enumerate(dataloader):
                out = model(fmri.cuda())
                end_idx = begin_idx + len(out)
                latent[begin_idx:end_idx] = out.cpu().numpy(
                )  # 需要cpu().numpy()?
                begin_idx = end_idx
        sf.close()
        print(latent_idx)