def test_shpere(model_path, X, sample_idx=0, elev=-90, azim=-90, axis=[-0.35, 0.35]): # CUDA: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Data: X = X.to(device) print('data:', X.shape) # Load model: model = create_model(Params()) model.load_state_dict(torch.load(model_path)) model.to(device) print('Model has been loaded from ', model_path) #3D: joint3d = model.predict(X[sample_idx]).view(17, 3).cpu().numpy() # Hip point: hip = (joint3d[11] + joint3d[12]) * 0.5 # Shpere: sphere = generate_fibonacci_sphere() sphere = transform_sphere(sphere, offset=hip, scale=0.3) draw_skeleton_3d_with_sphere(joint3d, sphere, elev=elev, azim=azim, axis_lim=axis)
def test_range(model_path, X, result_dir, start_idx=0, stop_idx=1000, num_cameras=6): if not os.path.exists(result_dir): os.makedirs(result_dir) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Data: X = X.to(device) print('Training data:', X.shape) # Load model: model = create_model(Params()) model.load_state_dict(torch.load(model_path)) model.to(device) print('Model has been loaded from ', model_path) # Test: for sample_idx in range(start_idx, stop_idx): # Predict: X_pred = model.forward(X[sample_idx]).detach().view(-1).cpu().numpy() # Draw: row_imgs = [] for j in range(num_cameras): pts2d_gt = normalize_pts_to_frame( extract_pts2d(X[sample_idx].cpu().numpy(), j)) skeleton_gt = draw_skeleton_2d(pts2d_gt, name='gt-' + str(j), show=False) skeleton_pd = normalize_pts_to_frame(extract_pts2d(X_pred, j)) skeleton_pd = draw_skeleton_2d(skeleton_pd, name='pred-' + str(j), show=False, background=(128, 128, 128)) #Resize and concatenate: target_size = (640, 360) skeleton_gt = cv2.resize(skeleton_gt, target_size) skeleton_pd = cv2.resize(skeleton_pd, target_size) result = np.concatenate([skeleton_gt, skeleton_pd], axis=1) row_imgs.append(result) result = np.concatenate(row_imgs, axis=0) result_path = os.path.join(result_dir, str(sample_idx).zfill(5) + '.jpeg') cv2.imwrite(result_path, result) print('{}/{} '.format(sample_idx + 1, stop_idx), end='\r')
def test_3d_dynamic_with_object(model_path, X, start_idx=0, stop_idx=1000, result_dir=None, elev=-90, azim=-90, axis=[-0.35, 0.35], object_type='sphere'): #CUDA: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Data: X = X.to(device) print('Training data:', X.shape) # Load model: model = create_model(Params()) model.load_state_dict(torch.load(model_path)) model.to(device) print('Model has been loaded from ', model_path) if object_type == 'sphere': bject_ones = generate_fibonacci_sphere(num_pts=100) elif object_type == 'cube': object_ones = generate_cube() else: assert False # Test: joint3d_list = [] for sample_idx in range(start_idx, stop_idx): # Predict: joint3d = model.predict(X[sample_idx]).view(17, 3).cpu().numpy() # Hip point: hip = (joint3d[11] + joint3d[12]) * 0.5 # Shpere: object_pts = copy.deepcopy(object_ones) object_pts = transform_sphere(object_pts, offset=hip, scale=0.025) joint3d_list.append((joint3d, object_pts)) print('{}/{} '.format(sample_idx + 1, stop_idx), end='\r') #Visualize: draw_skeleton_3d_dynamic_with_object(joint3d_list, result_dir, elev=elev, azim=azim, axis_lim=axis, object_type=object_type)
def test(model_path, X, result_dir, sample_idx=0): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Data: X = X.to(device) print('Training data:', X.shape) # Load model: model = create_model(Params()) model.load_state_dict(torch.load(model_path)) model.to(device) print('Model has been loaded from ', model_path) #3D: X_pred = model.forward(X[sample_idx]).detach().view(-1).cpu().numpy() joint3d = model.predict(X[sample_idx]).view(17, 3).cpu().numpy() draw_skeleton_3d(joint3d) #2D: row_imgs = [] for j in range(6): pts2d_gt = normalize_pts_to_frame( extract_pts2d(X[sample_idx].cpu().numpy(), j)) skeleton_gt = draw_skeleton_2d(pts2d_gt, name='gt-' + str(j), show=False) skeleton_pd = normalize_pts_to_frame(extract_pts2d(X_pred, j)) skeleton_pd = draw_skeleton_2d(skeleton_pd, name='pred-' + str(j), show=False, background=(128, 128, 128)) #Resize and concatenate: target_size = (640, 360) skeleton_gt = cv2.resize(skeleton_gt, target_size) skeleton_pd = cv2.resize(skeleton_pd, target_size) result = np.concatenate([skeleton_gt, skeleton_pd], axis=1) row_imgs.append(result) result = np.concatenate(row_imgs, axis=0) result_path = os.path.join(result_dir, 'result_idx' + str(sample_idx) + '.png') cv2.imwrite(result_path, result) cv2.imshow('result', result) cv2.waitKey()
def run_model(R0, nb_time_pts=30, initialCases=1000, key="USA-California"): time_points = np.arange(-21, nb_time_pts - 21) rates = copy.deepcopy(DefaultRates) rates.logR0 = np.log(R0) rates.infectivity = np.exp(rates.logR0) * rates.infection params = Params(ages=AGES[POPDATA[key]["ageDistribution"]], size=1e12, times=time_points, date=nb_time_pts, rates=rates, fracs=Fracs()) model = trace_ages( solve_ode(params, init_pop(params.ages, params.size, initialCases))) data_model = empty_data_list() data_model[Sub.T] = model[:, Sub.T] data_model[Sub.D] = model[:, Sub.D] return time_points, data_model
def test_3d_dynamic(model_path, X, start_idx=0, stop_idx=1000, result_dir=None, elev=90, azim=50, axis=[-0.25, 0.25]): #CUDA: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Data: X = X.to(device) print('Training data:', X.shape) # Load model: model = create_model(Params()) model.load_state_dict(torch.load(model_path)) model.to(device) print('Model has been loaded from ', model_path) # Test: joint3d_list = [] for sample_idx in range(start_idx, stop_idx): # Predict: joint3d = model.predict(X[sample_idx]).view(17, 3).cpu().numpy() joint3d_list.append(joint3d) print('{}/{} '.format(sample_idx + 1, stop_idx), end='\r') #Visualize: draw_skeleton_3d_dynamic(joint3d_list, result_dir, elev=elev, azim=azim, axis_lim=axis)
def train_batch(batch_size=100): # Cuda or CPU: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Set params: params = Params() params.num_epoch = 35 params.lr = 1e-3 # Load data: X = load_data_batch_v2().to(device) print('Training data:', X.shape) # Init model: model = create_model(params) model.to(device) print(model) #Optimizer params: optimizer_params = [] for key, module in dict(model.named_parameters()).items(): if key == 'reproject.weight': optimizer_params.append({ 'params': module, 'weight_decay': 0.0, 'lr': 0.01 }) else: optimizer_params.append({'params': module}) # Loss and optimizer: criterion = torch.nn.MSELoss() # optimizer = torch.optim.Adam(model.parameters(), lr=params.lr, weight_decay=params.weight_decay) optimizer = torch.optim.Adam(optimizer_params, lr=params.lr, weight_decay=params.weight_decay) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=15, gamma=0.1) # Training model.train() n = X.shape[0] for ei in range(params.num_epoch): start = time.time() for i in range(0, n, batch_size): count = min(batch_size, n - i) X_batch = X[i:i + count] # Forward pass: optimizer.zero_grad() # X_pred, joints3d_pred = model.forward(X_batch) X_pred = model.forward(X_batch) # Compute the loss: loss = criterion(X_pred, X_batch) # Backward pass: loss.backward() # Update params: optimizer.step() # Log: elapsed_time = time.time() - start lr_log = ["%.5f" % g['lr'] for g in optimizer.param_groups] print("[EPOCH]: %i, [LOSS]: %.6f, [elapsed]: %.4fs, [lr]: %s" % (ei, loss.item(), elapsed_time, lr_log)) # Scheduler: scheduler.step() # Save model: model_path = '/home/darkalert/KazendiJob/DLab/multi-view-pose-3d/models/v2-big-norm-weighted-mse' + str( loss.item()) + '.pth' torch.save(model.state_dict(), model_path) print('Model has been saved to ', model_path) # Test: joint3d = model.predict(X[0]).view(17, 3).cpu().numpy() print('Xi:', X[100]) print(joint3d) draw_skeleton_3d(joint3d)
def train(): # Cuda or CPU: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Set params: params = Params() params.num_epoch = 1 params.lr = 1e-4 # Load data: X = load_data_batch_v1().to(device) print('Training data:', X.shape) # Init model: model = create_model(params) model.to(device) print(model) # # Optimizer params: # optimizer_params = [] # for key,module in dict(model.named_parameters()).items(): # if key == 'reproject.weight': # optimizer_params.append({'params': module,'weight_decay':0.0,'lr':params.lr}) # else: # optimizer_params.append({'params': module}) # Training: criterion = torch.nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=params.lr, weight_decay=params.weight_decay) # optimizer = torch.optim.Adam(optimizer_params, lr=params.lr, weight_decay=params.weight_decay) model.train() for ei in range(params.num_epoch): for i in range(X.shape[0]): # Forward pass: optimizer.zero_grad() X_pred = model.forward(X[i]) # Compute the loss: loss = criterion(X_pred, X[i]) if i % 10 == 0: print("[ITER]: %i, [LOSS]: %.6f" % (i, loss.item())) # Backward pass: loss.backward() # for i,p in enumerate(model.parameters()): # print (i,p.grad.shape) # Update params: optimizer.step() print("[EPOCH]: %i, [LOSS]: %.6f" % (ei, loss.item())) # Save model: model_path = '/home/darkalert/KazendiJob/DLab/multi-view-pose-3d/models/last_model_v2_' + str( loss.item()) + '.pth' torch.save(model.state_dict(), model_path) print('Model has been saved to ', model_path) # Test: joint3d = model.predict(X[0]).view(17, 3).cpu().numpy() print('Xi:', X[i]) print(joint3d) draw_skeleton_3d(joint3d)
def create_params(label2ids): params = Params() params.max_sentence_length = label2ids['max_sentence_length'] params.max_n_analyses = label2ids['max_n_analysis'] params.batch_size = 1 params.n_subepochs = 40 params.max_surface_form_length = label2ids['max_surface_form_length'] params.max_word_root_length = label2ids['max_word_root_length'] params.max_analysis_length = label2ids['max_analysis_length'] params.char_vocabulary_size = label2ids['character_unique_count']['value'] params.tag_vocabulary_size = label2ids['morph_token_unique_count']['value'] params.char_lstm_dim = 100 params.char_embedding_dim = 100 params.tag_lstm_dim = params.char_lstm_dim params.tag_embedding_dim = 100 params.sentence_level_lstm_dim = 2 * params.char_lstm_dim return params
def main(paramIndex=None, runopts=None, replication=None): if runopts is None: # Default run options Calibrate = True # use solver to match targets Simulate = True SimulateMPCs = True MPCsNews = True Fast = False # run w/small grids for debugging PrintGrids = False MakePlots = False else: Calibrate = runopts['Calibrate'] Simulate = runopts['Simulate'] SimulateMPCs = runopts['SimulateMPCs'] MPCsNews = runopts['MPCsNews'] Fast = runopts['Fast'] PrintGrids = runopts['PrintGrids'] MakePlots = runopts['MakePlots'] #---------------------------------------------------------------# # HOUSEKEEPING # #---------------------------------------------------------------# basedir = os.getcwd() locIncomeProcess = os.path.join(basedir, 'input', 'income_quarterly_b.mat') outdir = os.path.join(basedir, 'output') if not os.path.exists(outdir): os.mkdir(outdir) #---------------------------------------------------------------# # CREATE PARAMS, GRIDS, AND INCOME OBJECTS # #---------------------------------------------------------------# functions.printLine() if replication is not None: params_dict = load_parameters(index=paramIndex, replication=replication) functions.printLine() params_dict['fastSettings'] = Fast params = Params.Params(params_dict) grids, income = create_objects(params, locIncomeProcess, PrintGrids) #---------------------------------------------------------------# # INITIALIZE AND SOLVE MODEL # #---------------------------------------------------------------# # if replication is not None: model = Model(params, income, grids) skip1 = params.cal1_options.get('skip', True) skip2 = params.cal2_options.get('skip', True) if skip1 & skip2: Calibrate = False if Calibrate: if not params.cal1_options['skip']: calibrator = getCalibrator(params, model, income, grids, params.cal1_options) calibrator.calibrate() if params.index % 2 == 1: # Calibrate to adjustment cost calibrator = getCalibrator(params, model, income, grids, params.cal2_options) calibrator.calibrate() else: model.solve() eqSimulator = simulator.EquilibriumSimulator(params, income, grids) if Simulate: eqSimulator.initialize(model.cSwitchingPolicy, model.inactionRegion) eqSimulator.simulate() finalSimStates = eqSimulator.finalStates else: finalSimStates = [] model.interpMat = None valueBaseline = model.valueFunction #-----------------------------------------------------------# # SIMULATE MPCs OUT OF AN IMMEDIATE SHOCK # #-----------------------------------------------------------# shockIndices = [0, 1, 2, 3, 4, 5] mpcSimulator = simulator.MPCSimulator(params, income, grids, shockIndices) if Simulate and SimulateMPCs: mpcSimulator.initialize(model.cSwitchingPolicy, model.inactionRegion, finalSimStates) mpcSimulator.simulate() #-----------------------------------------------------------# # SOLVE FOR POLICY GIVEN SHOCK NEXT PERIOD # #-----------------------------------------------------------# news = dict() news['shockIndices'] = [2, 3, 4, 5] news['currentShockIndices'] = [6, 6, 6, 6] news['periodsUntilShock'] = 1 news['cSwitch'], news['inactionRegions'] \ = solve_back_from_shock(params, income, grids, valueBaseline, news['shockIndices'], news['periodsUntilShock'], model.cSwitchingPolicy, model.inactionRegion) #-----------------------------------------------------------# # INTERMEDIATE PLOTS # #-----------------------------------------------------------# # switching_baseline = np.asarray(model.cSwitchingPolicy) # switching_news = np.asarray(news['cSwitch'])[:,:,:,:,3,1] # plots.compare_policies(grids, switching_baseline, switching_news) #-----------------------------------------------------------# # SOLVE FOR 1-YEAR LOAN # #-----------------------------------------------------------# loan = dict() loan['shockIndices'] = [0] loan['currentShockIndices'] = [5] loan['periodsUntilShock'] = 4 if SimulateMPCs and MPCsNews: loan['cSwitch'], loan['inactionRegions'] = \ solve_back_from_shock(params, income, grids, valueBaseline, loan['shockIndices'], loan['periodsUntilShock'], model.cSwitchingPolicy, model.inactionRegion) #-----------------------------------------------------------# # SHOCK OF -$500 IN 2 YEARS # #-----------------------------------------------------------# loss2years = dict() loss2years['shockIndices'] = [2] loss2years['currentShockIndices'] = [6] loss2years['periodsUntilShock'] = 8 if SimulateMPCs and MPCsNews: print('Solving for policy functions given future shock') loss2years['cSwitch'], loss2years['inactionRegions'] = \ solve_back_from_shock(params, income, grids, valueBaseline, loss2years['shockIndices'], loss2years['periodsUntilShock'], model.cSwitchingPolicy, model.inactionRegion) #-----------------------------------------------------------# # SIMULATE MPCs OUT OF NEWS # #-----------------------------------------------------------# ii = 0 for newsModel in [news, loan, loss2years]: sim_args = [ params, income, grids, newsModel['shockIndices'], newsModel['currentShockIndices'], ] if ii == 0: newsModel['simulator'] = simulator.MPCSimulatorNews( *sim_args, periodsUntilShock=newsModel['periodsUntilShock'], simPeriods=4) elif ii == 1: newsModel['simulator'] = simulator.MPCSimulatorNews_Loan( *sim_args, periodsUntilShock=newsModel['periodsUntilShock']) else: newsModel['simulator'] = simulator.MPCSimulatorNews( *sim_args, periodsUntilShock=newsModel['periodsUntilShock']) if SimulateMPCs and MPCsNews: print('Simulating MPCs out of news') newsModel['simulator'].initialize(newsModel['cSwitch'], newsModel['inactionRegions'], finalSimStates) newsModel['simulator'].simulate() ii += 1 #-----------------------------------------------------------# # RESULTS # #-----------------------------------------------------------# # parameters print('\nSelected parameters:\n') print(params.series.to_string()) if Simulate: if MPCsNews: otherStatistics.saveWealthGroupStats( mpcSimulator, news['simulator'], loss2years['simulator'], loan['simulator'], finalSimStates, outdir, paramIndex, params) # put main results into a Series print('\nResults from simulation:\n') print(eqSimulator.results.dropna().to_string()) if SimulateMPCs: print('\nMPCS:\n') print(mpcSimulator.results.dropna().to_string()) if MPCsNews: print('\nMPCS out of news:\n') print(news['simulator'].results.dropna().to_string()) print('\nMPCS out of future loss:\n') print(loss2years['simulator'].results.dropna().to_string()) print(loan['simulator'].results.dropna().to_string()) name_series = pd.Series({'Experiment': params.name}) index_series = pd.Series({'Index': params.index}) results = pd.concat([ name_series, index_series, params.series, eqSimulator.results.dropna(), mpcSimulator.results.dropna(), news['simulator'].results.dropna(), loss2years['simulator'].results.dropna(), loan['simulator'].results.dropna(), ]) savepath = os.path.join(outdir, f'run{paramIndex}.pkl') results.to_pickle(savepath) savepath = os.path.join(outdir, f'run{paramIndex}_statistics.csv') results.to_csv(savepath, index_label=params.name, header=True) mpcs_table = mpcsTable.create( params, mpcSimulator, news['simulator'], loss2years['simulator'], loan['simulator'], ) savepath = os.path.join(outdir, f'run{paramIndex}_mpcs_table.csv') # mpcs_table.to_excel(savepath, freeze_panes=(0,0), index_label=params.name) mpcs_table.to_csv(savepath, index_label=params.name, header=True) #-----------------------------------------------------------# # PLOTS # #-----------------------------------------------------------# if MakePlots: plots.plot_policies(model, grids, params, paramIndex, outdir)
def test_projections(model_path, X, result_dir, start_idx=0, stop_idx=1000, num_cameras=6, object_type='cube'): if not os.path.exists(result_dir): os.makedirs(result_dir) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Data: X = X.to(device) # Load model: model = create_model(Params()) model.load_state_dict(torch.load(model_path)) model.to(device) print('Model has been loaded from ', model_path) # Shpere: if object_type == 'sphere': bject_ones = generate_fibonacci_sphere(num_pts=1000) elif object_type == 'cube': object_ones = generate_cube() else: assert False # Test: for sample_idx in range(start_idx, stop_idx): # Predict: joint3d = model.predict(X[sample_idx]) X_pred = model.reproject(joint3d).detach().view(-1).cpu().numpy() joint3d_cpu = joint3d.view(17, 3).cpu().numpy() # Hip point: hip = (joint3d_cpu[11] + joint3d_cpu[12]) * 0.5 # Shpere: obj3d = copy.deepcopy(object_ones) obj3d = transform_sphere(obj3d, offset=hip, scale=0.025) #0.04 obj3d = torch.from_numpy(obj3d).to(device) obj3d = obj3d.view(1, -1, 3) # Project shpere on planes: obj2d_pred = model.reproject(obj3d).detach().view(6, -1, 2).cpu().numpy() # Draw: row_imgs = [] for j in range(num_cameras): pts2d_gt = normalize_pts_to_frame( extract_pts2d(X[sample_idx].cpu().numpy(), j)) skeleton_gt = draw_skeleton_2d(pts2d_gt, name='gt-' + str(j), show=False) skeleton_pd = normalize_pts_to_frame(extract_pts2d(X_pred, j)) skeleton_pd = draw_skeleton_2d(skeleton_pd, name='pred-' + str(j), show=False, background=(128, 128, 128)) # Draw shpere: obj2d_norm = normalize_pts_to_frame(obj2d_pred[j]) skeleton_pd = draw_cube_2d(obj2d_norm, skeleton_pd) #Resize and concatenate: target_size = (640, 360) skeleton_gt = cv2.resize(skeleton_gt, target_size) skeleton_pd = cv2.resize(skeleton_pd, target_size) result = np.concatenate([skeleton_gt, skeleton_pd], axis=1) row_imgs.append(result) result = np.concatenate(row_imgs, axis=0) result_path = os.path.join(result_dir, str(sample_idx).zfill(5) + '.jpeg') cv2.imwrite(result_path, result) print('{}/{} '.format(sample_idx + 1, stop_idx), end='\r')