예제 #1
0
 def __init__(self, rotMatrix, transVect, cameraMatrix,
              distCoeffs, anchor_points=None):
     """
     Extrinsic parameters : rotMatrix, transVect.
     Intrinsic parameters : cameraMatrix, distCoeffs.
     """
     self.rotMatrix = check_shape(rotMatrix, (3, 3), "rotation matrix")
     self.transVect = column_vector(transVect, 3, "translation vector")
     self.cameraMatrix = check_shape(cameraMatrix, (3, 3), "camera matrix")
     self.distCoeffs = column_vector(distCoeffs, 4, "distort coefficients")
     self.anchor_points = anchor_points
예제 #2
0
def is_file(args, path):
    if not os.path.isfile(path):
        return False

    for mod in (overlay, auto_resize, auto_resize_crop, auto_rescale):
        if args.get(mod.__name__):
            return True

    if not args['ignore_size']:
        check_shape(path)
    else:
        Conf.log.warn('Image Size Requirements Unchecked.')
    return True
예제 #3
0
def select_phases():
    """
    Select the transformation phases to use following args parameters
    :return: <ImageTransform[]> list of image transformation
    """
    def shift_step(shift_starting=0, shift_ending=0):
        if not conf.args['steps']:
            conf.args['steps'] = (0, 5)
        conf.args['steps'] = (conf.args['steps'][0] + shift_starting,
                              conf.args['steps'][1] + shift_ending)

    def add_tail(phases, phase):
        phases = [phase] + phases
        if conf.args['steps'] and conf.args['steps'][0] != 0:
            shift_step(shift_starting=1)
        if conf.args['steps'] and conf.args['steps'][1] == len(phases) - 1:
            shift_step(shift_ending=1)
        return phases

    def add_head(phases, phase):
        phases = phases + [phase]
        if conf.args['steps'] and conf.args['steps'][1] == len(phases) - 1:
            shift_step(shift_ending=1)
        return phases

    phases = [
        DressToCorrect, CorrectToMask, MaskToMaskref, MaskrefToMaskdet,
        MaskdetToMaskfin, MaskfinToNude
    ]

    if conf.args['overlay']:
        phases = add_tail(phases, ImageToResized)
        phases = add_tail(phases, ImageToCrop)
        phases = add_head(phases, ImageToOverlay)
    elif conf.args['auto_resize']:
        phases = add_tail(phases, ImageToResized)
    elif conf.args['auto_resize_crop']:
        phases = add_tail(phases, ImageToResizedCrop)
    elif conf.args['auto_rescale']:
        phases = add_tail(phases, ImageToRescale)
    elif os.path.isfile(conf.args['input']):
        if not conf.args['ignore_size']:
            check_shape(conf.args['input'])
        else:
            conf.log.warn('Image Size Requirements Unchecked.')

    if conf.args['color_transfer']:
        phases = add_head(phases, ColorTransfer)

    return phases
예제 #4
0
 def chromosome(self,chrom):
     '''Outputs the feature dataframe for chromosome=chrom
     The dataframe should include the info columns
     '''
     print('Getting features for chromosome {}'.format(chrom))
     if chrom not in [x for x in range(1,23)]:
         raise Exception('{} is not a valid chromosome number'.format(chrom))
     else:
         ref_df = self.ref_annot_df(chrom)
         cts_df = self.cts_annot_df(chrom)
         if not u.check_shape(ref_df,cts_df,'row'):
             raise Exception('Shape of reference annotations doesn not match cell type specific annotations.\n
                             Reference annotation shape {},\n
                             cell type specific annotation shape {}'.format(ref_df.shape,cts_df.shape))
         else:
             if u.containList(self.info_colnames,cts_df.columns.tolist()):
                 cts_df.drop(self.info_colnames,axis=1,inplace=True)
                 return pd.concat([ref_df,cts_df],axis=1)
             elif u.nonInList(self.info_colnames,cts_df.columns.tolist()):
                 return pd.concat([ref_df,cts_df],axis=1)
             else:
                 raise Exception('Cell type specific annotations dataframe contains 
                                 some but not all info column names')
예제 #5
0
파일: test_tSNE.py 프로젝트: Czworldy/GP
                         num_workers=16)
eval_samples = iter(eval_loader)

num_points = 400
file_dir = 'ours_fix_alpha_' + str(num_points)
os.makedirs(file_dir, exist_ok=True)

feature_list = np.random.randn(num_points * opt.batch_size * 2, opt.vector_dim)
label = []

for total_step in tqdm(range(num_points)):
    batch = next(train_samples)
    to_device(batch, device)
    # feature = encoder(batch['img'])
    feature = encoder(batch['img'], batch['v_0'])
    check_shape(feature, 'feature')
    set_mute(True)
    feature_list[total_step * opt.batch_size:(total_step + 1) *
                 opt.batch_size] = feature.data.cpu().numpy()
label = [0] * num_points * opt.batch_size

for total_step in tqdm(range(num_points, 2 * num_points)):
    batch = next(eval_samples)
    to_device(batch, device)
    feature = encoder(batch['img'], batch['v_0'])
    check_shape(feature, 'feature')
    set_mute(True)
    feature_list[total_step * opt.batch_size:(total_step + 1) *
                 opt.batch_size] = feature.data.cpu().numpy()
label.extend([1] * num_points * opt.batch_size)
예제 #6
0
                dpi=400)
    plt.close('all')

    cluster.train_models()


"""  
logger_avg = SummaryWriter(log_dir=log_path+'avg_model')
logger_min = SummaryWriter(log_dir=log_path+'min_uncty')
for total_steps in range(1000000):
    eval_new_error(total_steps, logger_avg, logger_min)
    eval_error(total_steps)
"""
for total_steps in range(1000000):
    for model_index in range(opt.model_num):
        check_shape(model_index, 'model_index')
        batch = next(train_loader_cluster[model_index])

        batch['t'] = batch['t'].view(-1, 1)
        batch['t'].requires_grad = True
        batch['v0_array'] = batch['v0_array'].view(-1, 1)

        check_shape(batch)
        to_device(batch, device)

        feature = cluster.get_encoder(batch['img'], model_index)
        feature_dim = feature.shape[-1]
        feature = feature.unsqueeze(1)
        feature = feature.expand(opt.batch_size, opt.points_num, feature_dim)
        feature = feature.reshape(opt.batch_size * opt.points_num, feature_dim)
        check_shape(feature, 'feature')
예제 #7
0
파일: train.py 프로젝트: Czworldy/GP
    plt.close('all')

    model.train_models()


# def train_adversarial():
#     pass

# def train_supervised():
#     pass

for total_steps in range(1000000):
    model_index = random.randint(0, opt.model_num - 1)

    batch = next(train_samples)
    check_shape(batch)
    to_device(batch, device)

    batch['t'] = batch['t'].view(-1, 1)
    batch['t'].requires_grad = True
    batch['v0_array'] = batch['v0_array'].view(-1, 1)

    check_shape(batch)
    to_device(batch, device)

    feature = model.get_encoder(batch['img'], batch['v_0'])
    feature_dim = feature.shape[-1]

    feature = feature.unsqueeze(1)
    feature = feature.expand(opt.batch_size, opt.points_num, feature_dim)
    feature = feature.reshape(opt.batch_size * opt.points_num, feature_dim)
예제 #8
0
def is_file(args):
    if not args['ignore_size']:
        check_shape(args['input'])
    else:
        Conf.log.warn('Image Size Requirements Unchecked.')
예제 #9
0
def eval_decision(logger, total_steps, algorithm='WCM'):
    batch = next(eval_samples)
    to_device(batch, device)
    check_shape(batch)
    set_mute(True)

    x = model_cluster[0]._decoder._base_dist.mean.clone().detach().view(-1, opt.points_num, 2)
    x.requires_grad = True
    zs = [model._params(
        velocity=batch['v_0'].view(-1,1),
        visual_features=batch['img'],
    ) for model in model_cluster]

    optimizer = torch.optim.Adam(params=[x], lr=1e-1)

    x_best = x.clone()
    loss_best = torch.ones(()).to(x.device) * 1000.0
    for _ in range(50):
        optimizer.zero_grad()
        y, _ = model_cluster[0]._decoder._forward(x=x, z=zs[0])

        imitation_posteriors = list()
        for model, z in zip(model_cluster, zs):
            _, log_prob, logabsdet = model._decoder._inverse(y=y, z=z)
            imitation_prior = torch.mean(log_prob - logabsdet)
            imitation_posteriors.append(imitation_prior)

        imitation_posteriors = torch.stack(imitation_posteriors, dim=0)

        if algorithm == "WCM":
            loss, _ = torch.min(-imitation_posteriors, dim=0)
        elif algorithm == "BCM":
            loss, _ = torch.max(-imitation_posteriors, dim=0)
        else:
            loss = torch.mean(-imitation_posteriors, dim=0)

        loss.backward(retain_graph=True)
        optimizer.step()
        if loss < loss_best:
            x_best = x.clone()
            loss_best = loss.clone()

    plan, _ = model_cluster[0]._decoder._forward(x=x_best, z=zs[0])
    xy = plan.detach().cpu().numpy()[0]*opt.max_dist
    real_xy = batch['xy'].view(-1, 2).data.cpu().numpy()*opt.max_dist

    fake_x = xy[:,0]
    fake_y = xy[:,1]
    real_x = real_xy[:,0]
    real_y = real_xy[:,1]
    time = batch['t'].data.cpu().numpy()[0]*opt.max_t
    # time_list = [0.0, 0.75, 1.5, 2.25] #oxford training time
    time_list = [0.0000, 0.1875, 0.3750, 0.5625, 0.7500, 0.9375, 1.1250, 1.3125, 1.5001,1.6875, 1.8750, 2.0625, 2.2501, 2.4376, 2.6250, 2.8126]
    xs = []
    ys = []
    for t in time:
        x, y = interpolation(time_list, fake_x, fake_y, t)
        xs.append(x)
        ys.append(y)
    fake_x = np.array(xs)
    fake_y = np.array(ys)
    # max_x = 30.
    # max_y = 30.

    # fig = plt.figure(figsize=(7, 7))
    # ax1 = fig.add_subplot(111)
    # ax1.plot(real_x, real_y, label='real-trajectory', color = 'b', linewidth=3, linestyle='--')
    # ax1.plot(fake_x, fake_y, label='fake-trajectory', color = 'r', linewidth=3)
    # ax1.set_xlabel('Forward/(m)')
    # ax1.set_ylabel('Sideways/(m)')  
    # ax1.set_xlim([0., max_x])
    # ax1.set_ylim([-max_y/2, max_y/2])
    # plt.legend(loc='lower right')
    
    # plt.legend(loc='lower left')
    # plt.savefig('result/output/%s/' % opt.dataset_name+'/'+str(total_steps)+'.png')
    # plt.close('all')
    ex = np.mean(np.abs(fake_x-real_x))
    ey = np.mean(np.abs(fake_y-real_y))
    fde = np.hypot(fake_x - real_x, fake_y - real_y)[-1]
    ade = np.mean(np.hypot(fake_x - real_x, fake_y - real_y))

    logger.add_scalar('eval/ex',  ex.item(),  total_steps)
    logger.add_scalar('eval/ey',  ey.item(),  total_steps)
    logger.add_scalar('eval/fde', fde.item(), total_steps)
    logger.add_scalar('eval/ade', ade.item(), total_steps)
예제 #10
0
        logger.add_scalar('eval/ex', ex.item(), total_steps)
        logger.add_scalar('eval/ey', ey.item(), total_steps)
        logger.add_scalar('eval/fde', fde.item(), total_steps)
        logger.add_scalar('eval/ade', ade.item(), total_steps)


for total_steps in range(1000000):
    for model_index in range(opt.model_num):
        model = model_cluster[model_index]
        train_loader = train_loader_cluster[model_index]
        optimizer = optimizer_cluster[model_index]
        logger = logger_cluster[model_index]

        batch = next(train_loader)
        check_shape(batch)
        to_device(batch, device)
        print(batch['t'] * opt.max_t)
        y = batch['xy']  #*opt.max_dist

        z = model._params(
            velocity=batch['v_0'].view(-1, 1),
            visual_features=batch['img'],
        )
        _, log_prob, logabsdet = model._decoder._inverse(y=y, z=z)
        check_shape(log_prob, 'log_prob')
        check_shape(logabsdet, 'logabsdet')
        loss = -torch.mean(log_prob - logabsdet, dim=0)
        optimizer.zero_grad()
        loss.backward()
        logger.add_scalar('train/loss', loss.item(), total_steps)
예제 #11
0
    real_traj = batch['xy'].view(-1, opt.points_num * 2)
    # y = batch['xy'].view(-1, opt.points_num, 2)[..., 1].view(-1, opt.points_num)
    # check_shape(batch['xy'].view(-1, opt.points_num, 2)[..., 1], '1')
    # check_shape(y, 'y')
    # set_mute(True)

    condition = batch['v0_array']

    single_latent = encoder(batch['img'])

    ######################################################################
    # traj_sim = cos_similarity(real_traj[:opt.batch_size//2], real_traj[opt.batch_size//2:])
    input_sim_net = torch.cat(
        (real_traj[:opt.batch_size // 2], real_traj[opt.batch_size // 2:]), 1)
    check_shape(input_sim_net)
    input_sim_net2 = torch.cat((batch['v_0'][:opt.batch_size // 2],
                                batch['v_0'][opt.batch_size // 2:]), 1)
    check_shape(input_sim_net2)
    input_sim_net = torch.cat((input_sim_net, input_sim_net2), 1)
    real_dist = similarity_net(input_sim_net)
    latent_dist = torch.norm(single_latent[:opt.batch_size // 2] -
                             single_latent[opt.batch_size // 2:],
                             dim=1)
    # latent_sim = cos_similarity(single_latent[:opt.batch_size//2], single_latent[opt.batch_size//2:])
    sim_loss = latent_criterion(real_dist, latent_dist.unsqueeze(1))

    # if speed == 0: sim_loss = 0
    ######################################################################

    latent = single_latent.unsqueeze(1)
예제 #12
0
def eval_model(total_steps):
    global model
    batch = next(eval_samples)
    to_device(batch, device)
    check_shape(batch)

    x = model._decoder._base_dist.mean.clone().detach().view(
        -1, opt.points_num, 2)
    x.requires_grad = True
    z = model._params(
        velocity=batch['v_0'].view(-1, 1),
        visual_features=batch['img'],
    )

    optimizer = torch.optim.Adam(params=[x], lr=1e-1)

    x_best = x.clone()
    loss_best = torch.ones(()).to(x.device) * 1000.0
    for _ in range(10):
        optimizer.zero_grad()
        y, _ = model._decoder._forward(x=x, z=z)
        _, log_prob, logabsdet = model._decoder._inverse(y=y, z=z)
        imitation_prior = torch.mean(log_prob - logabsdet)
        loss = -imitation_prior
        loss.backward(retain_graph=True)
        optimizer.step()
        if loss < loss_best:
            x_best = x.clone()
            loss_best = loss.clone()

    plan, _ = model._decoder._forward(x=x_best, z=z)
    xy = plan.detach().cpu().numpy()[0] * opt.max_dist
    real_xy = batch['xy'].view(-1, 2).data.cpu().numpy() * opt.max_dist

    fake_x = xy[:, 0]
    fake_y = xy[:, 1]
    time_list = [0.0, 0.75, 1.5, 2.25]

    real_x = real_xy[:, 0]
    real_y = real_xy[:, 1]
    time = batch['t'].data.cpu().numpy()[0] * opt.max_t

    xs = []
    ys = []
    for t in time:
        x, y = interpolation(time_list, fake_x, fake_y, t)
        xs.append(x)
        ys.append(y)
    fake_x = np.array(xs)
    fake_y = np.array(ys)

    if total_steps % (4 * opt.test_interval) == 0:
        max_x = 30.
        max_y = 30.

        fig = plt.figure(figsize=(7, 7))
        ax1 = fig.add_subplot(111)
        ax1.plot(real_x,
                 real_y,
                 label='real-trajectory',
                 color='b',
                 linewidth=3,
                 linestyle='--')
        ax1.plot(fake_x,
                 fake_y,
                 label='fake-trajectory',
                 color='r',
                 linewidth=3)
        ax1.set_xlabel('Forward/(m)')
        ax1.set_ylabel('Sideways/(m)')
        ax1.set_xlim([0., max_x])
        ax1.set_ylim([-max_y / 2, max_y / 2])
        plt.legend(loc='lower right')

        plt.legend(loc='lower left')
        plt.savefig('result/output/%s/' % opt.dataset_name + '/' +
                    str(total_steps) + '.png')
        plt.close('all')

    ex = np.mean(np.abs(fake_x - real_x))
    ey = np.mean(np.abs(fake_y - real_y))
    fde = np.hypot(fake_x - real_x, fake_y - real_y)[-1]
    ade = np.mean(np.hypot(fake_x - real_x, fake_y - real_y))

    logger.add_scalar('eval/ex', ex.item(), total_steps)
    logger.add_scalar('eval/ey', ey.item(), total_steps)
    logger.add_scalar('eval/fde', fde.item(), total_steps)
    logger.add_scalar('eval/ade', ade.item(), total_steps)
예제 #13
0
파일: train.py 프로젝트: Czworldy/GP
    real_y = real_traj[:, 1]

    ex = np.mean(np.abs(fake_x - real_x))
    ey = np.mean(np.abs(fake_y - real_y))
    fde = np.hypot(fake_x - real_x, fake_y - real_y)[-1]
    ade = np.mean(np.hypot(fake_x - real_x, fake_y - real_y))

    logger.add_scalar('eval/ex', ex.item(), total_steps)
    logger.add_scalar('eval/ey', ey.item(), total_steps)
    logger.add_scalar('eval/fde', fde.item(), total_steps)
    logger.add_scalar('eval/ade', ade.item(), total_steps)


for total_steps in range(1000000):
    batch = next(train_samples)
    check_shape(batch)
    to_device(batch, device)
    x = batch['img']
    d = batch['domian']
    y = batch['xy'].view(opt.batch_size, 2 * opt.points_num)
    v0 = batch['v0_array']
    t = batch['t']

    optimizer.zero_grad()
    vae_loss, regression_loss = model.loss_function(d, x, y, v0, t)
    loss = regression_loss
    loss.backward()
    optimizer.step()

    logger.add_scalar('train/vae_loss', vae_loss.item(), total_steps)
    logger.add_scalar('train/regression_loss', regression_loss.item(),
                dpi=400)
    plt.close('all')

    cluster.train_models()


# def train_adversarial():
#     pass

# def train_supervised():
#     pass

for total_steps in range(1000000):
    model_index = random.randint(0, opt.model_num - 1)
    logger = logger_cluster[0]
    check_shape(model_index, 'model_index')

    batch = next(train_samples)
    check_shape(batch)
    to_device(batch, device)

    mask1 = torch.arange(0, opt.batch_size - 1, 2)
    mask2 = torch.arange(1, opt.batch_size, 2)
    labels = 1 - 0.5 * torch.sum(
        torch.abs(batch['domian'][mask1] - batch['domian'][mask2]), dim=1)
    # import pdb; pdb.set_trace()

    batch['t'] = batch['t'].view(-1, 1)
    batch['t'].requires_grad = True
    batch['v0_array'] = batch['v0_array'].view(-1, 1)
예제 #15
0
for total_step in tqdm(range(num_points)):
    train_loader = train_loader_cluster[total_step % opt.model_num]
    batch = next(train_loader)
    batch['t'] = batch['t'].view(-1,1)
    batch['v0_array'] = batch['v0_array'].view(-1,1)
    batch['xy'] = batch['xy'].view(-1,2)
    to_device(batch, device)

    real_traj = batch['xy'].data.cpu().numpy()*opt.max_dist
    real_x = real_traj[:,0]
    real_y = real_traj[:,1]
    model_index = 0

    feature = cluster.get_encoder(batch['img'], index=model_index)
    check_shape(feature, 'feature')
    check_shape(batch['xy'], 'xy')
    set_mute(True)
    feature = feature.data.cpu().numpy()[0]
    
    feature_list[total_step] = feature
    label.append(total_step % opt.model_num)


# num_points = 5000
# traj_list = np.random.randn(num_points*opt.model_num, opt.points_num*2)
# label = []
# for model_index in range(opt.model_num):
#     for total_step in tqdm(range(num_points)):
#         batch = next(train_loader_cluster[model_index])
#         traj_list[total_step+num_points*model_index] = batch['xy'].flatten().data.cpu().numpy()