网站访问人数代码,衡阳建设网站,网站如何制作注册,网页托管平台排名python保存中间变量
原因#xff1a;
最近在部署dust3r算法#xff0c;虽然在本地部署了#xff0c;也能测试出一定的结果#xff0c;但是发现无法跑很多图片#xff0c;为了能够测试多张图片跑出来的模型#xff0c;于是就在打算在autodl上部署算法#xff0c;但是由…python保存中间变量
原因
最近在部署dust3r算法虽然在本地部署了也能测试出一定的结果但是发现无法跑很多图片为了能够测试多张图片跑出来的模型于是就在打算在autodl上部署算法但是由于官方给定的代码是训练好模型后通过可视化三维模型的形式来给出的效果所以在服务器上没有办法来可视化三维模型可能有办法但是总是有解决不了的报错于是便放弃
产生思路
打算把官方中的代码分成两部分上部分是训练好的模型output变量将output保存下来下载到本地上在本地上加载output变量进而完成后续的代码操作。
保存中间变量的方式
通过下面方式output变量会以output.pkl的文件形式保存在当前文件夹下
import pickle
output1 #这里就是要保存的中间变量
pickle.dump(output, open(output.pkl, wb))通过下面的方式来读取刚才保存的output.pkl文件这样就可以顺利保存下来了 f open(output.pkl,rb)outputpickle.loads(f.read())f.close()原理
pickle是Python官方自带的库提供dump函数实现Python对象的保存。支持自定义的对象非常方便。Pandas的DataFrame和Obspy的Stream也都可以保存成pickle的格式。主要是以二进制的形式来保存成一种无逻辑的文件。
解决原来的问题
dust3r官方给的代码如下其中服务器主要是在scene.show()这行代码中无法运行。
import osfrom dust3r.inference import inference, load_model
from dust3r.utils.image import load_images
from dust3r.image_pairs import make_pairs
from dust3r.cloud_opt import global_aligner, GlobalAlignerModeif __name__ __main__:model_path checkpoints/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pthdevice cudabatch_size 4schedule cosinelr 0.01niter 100model load_model(model_path, device)# load_images can take a list of images or a directory# base_dir tankandtemples/tankandtemples/intermediate/M60/images/base_dir croco/assets/# 获取当前目录下的所有文件files [os.path.join(base_dir, file) for file in os.listdir(base_dir)]images load_images(files, size512)pairs make_pairs(images, scene_graphcomplete, prefilterNone, symmetrizeTrue)output inference(pairs, model, device, batch_sizebatch_size)# at this stage, you have the raw dust3r predictionsview1, pred1 output[view1], output[pred1]view2, pred2 output[view2], output[pred2]scene global_aligner(output, devicedevice, modeGlobalAlignerMode.PointCloudOptimizer)loss scene.compute_global_alignment(initmst, niterniter, scheduleschedule, lrlr)# retrieve useful values from scene:imgs scene.imgsfocals scene.get_focals()poses scene.get_im_poses()pts3d scene.get_pts3d()confidence_masks scene.get_masks()# visualize reconstructionscene.show()# find 2D-2D matches between the two imagesfrom dust3r.utils.geometry import find_reciprocal_matches, xy_gridpts2d_list, pts3d_list [], []for i in range(2):conf_i confidence_masks[i].cpu().numpy()pts2d_list.append(xy_grid(*imgs[i].shape[:2][::-1])[conf_i]) # imgs[i].shape[:2] (H, W)pts3d_list.append(pts3d[i].detach().cpu().numpy()[conf_i])reciprocal_in_P2, nn2_in_P1, num_matches find_reciprocal_matches(*pts3d_list)print(ffound {num_matches} matches)matches_im1 pts2d_list[1][reciprocal_in_P2]matches_im0 pts2d_list[0][nn2_in_P1][reciprocal_in_P2]# visualize a few matchesimport numpy as npfrom matplotlib import pyplot as pln_viz 10match_idx_to_viz np.round(np.linspace(0, num_matches-1, n_viz)).astype(int)viz_matches_im0, viz_matches_im1 matches_im0[match_idx_to_viz], matches_im1[match_idx_to_viz]H0, W0, H1, W1 *imgs[0].shape[:2], *imgs[1].shape[:2]img0 np.pad(imgs[0], ((0, max(H1 - H0, 0)), (0, 0), (0, 0)), constant, constant_values0)img1 np.pad(imgs[1], ((0, max(H0 - H1, 0)), (0, 0), (0, 0)), constant, constant_values0)img np.concatenate((img0, img1), axis1)pl.figure()pl.imshow(img)cmap pl.get_cmap(jet)for i in range(n_viz):(x0, y0), (x1, y1) viz_matches_im0[i].T, viz_matches_im1[i].Tpl.plot([x0, x1 W0], [y0, y1], -, colorcmap(i / (n_viz - 1)), scalexFalse, scaleyFalse)pl.show(blockTrue)
将代码分成两部分上部分由服务器来跑下部分由本地来跑。
import os
from dust3r.inference import inference, load_model
from dust3r.utils.image import load_images
from dust3r.image_pairs import make_pairs
from dust3r.cloud_opt import global_aligner, GlobalAlignerMode
if __name__ __main__:model_path checkpoints/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pthdevice cudabatch_size 32schedule cosinelr 0.01niter 300model load_model(model_path, device)# load_images can take a list of images or a directorybase_dir croco/assets/# 获取当前目录下的所有文件files [os.path.join(base_dir, file) for file in os.listdir(base_dir)]files_new []for i in range(0,files.__len__(),10):files_new.append(files[i])images load_images(files_new, size512)pairs make_pairs(images, scene_graphcomplete, prefilterNone, symmetrizeTrue)output inference(pairs, model, device, batch_sizebatch_size)import picklepickle.dump(output, open(output.pkl, wb))本地代码
import os
from dust3r.inference import inference, load_model
from dust3r.utils.image import load_images
from dust3r.image_pairs import make_pairs
from dust3r.cloud_opt import global_aligner, GlobalAlignerMode
if __name__ __main__:model_path checkpoints/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pthdevice cudabatch_size 1schedule cosinelr 0.01niter 300base_dir croco/assets/# 获取当前目录下的所有文件files [os.path.join(base_dir, file) for file in os.listdir(base_dir)]files_new []for i in range(0,files.__len__(),4):files_new.append(files[i])print(files_new)import picklef open(output.pkl,rb)outputpickle.loads(f.read())f.close()view1, pred1 output[view1], output[pred1]view2, pred2 output[view2], output[pred2]scene global_aligner(output, devicedevice, modeGlobalAlignerMode.PointCloudOptimizer)loss scene.compute_global_alignment(initmst, niterniter, scheduleschedule, lrlr)# retrieve useful values from scene:imgs scene.imgsfocals scene.get_focals()poses scene.get_im_poses()pts3d scene.get_pts3d()confidence_masks scene.get_masks()# visualize reconstructionscene.show()# find 2D-2D matches between the two imagesfrom dust3r.utils.geometry import find_reciprocal_matches, xy_gridpts2d_list, pts3d_list [], []for i in range(2):conf_i confidence_masks[i].cpu().numpy()pts2d_list.append(xy_grid(*imgs[i].shape[:2][::-1])[conf_i]) # imgs[i].shape[:2] (H, W)pts3d_list.append(pts3d[i].detach().cpu().numpy()[conf_i])reciprocal_in_P2, nn2_in_P1, num_matches find_reciprocal_matches(*pts3d_list)print(ffound {num_matches} matches)matches_im1 pts2d_list[1][reciprocal_in_P2]matches_im0 pts2d_list[0][nn2_in_P1][reciprocal_in_P2]# visualize a few matchesimport numpy as npfrom matplotlib import pyplot as pln_viz 10match_idx_to_viz np.round(np.linspace(0, num_matches-1, n_viz)).astype(int)viz_matches_im0, viz_matches_im1 matches_im0[match_idx_to_viz], matches_im1[match_idx_to_viz]H0, W0, H1, W1 *imgs[0].shape[:2], *imgs[1].shape[:2]img0 np.pad(imgs[0], ((0, max(H1 - H0, 0)), (0, 0), (0, 0)), constant, constant_values0)img1 np.pad(imgs[1], ((0, max(H0 - H1, 0)), (0, 0), (0, 0)), constant, constant_values0)img np.concatenate((img0, img1), axis1)pl.figure()pl.imshow(img)cmap pl.get_cmap(jet)for i in range(n_viz):(x0, y0), (x1, y1) viz_matches_im0[i].T, viz_matches_im1[i].Tpl.plot([x0, x1 W0], [y0, y1], -, colorcmap(i / (n_viz - 1)), scalexFalse, scaleyFalse)pl.show(blockTrue)总结
这种解决办法也不是根本解决办法虽然比较麻烦但是还是能将项目跑起来也是没有办法的办法在此做一个笔记记录。