Skip to content

Instantly share code, notes, and snippets.

@Creling
Last active October 2, 2022 08:04
Show Gist options
  • Save Creling/2dba3a71339616a7f18a37917f558bab to your computer and use it in GitHub Desktop.
Save Creling/2dba3a71339616a7f18a37917f558bab to your computer and use it in GitHub Desktop.
Code Snippets

the usages of scanf() and printf()

#include <iostream>
using namespace std;
int main( )
{
  int a; float b; char c;
  scanf("%d %c %f",&a,&c,&b);  //注意在变量名前要加地址运算符&
  printf("a=%d,b=%f,c=%c\n",a,b,c);
  return 0;
}

the usages of scanf() and printf()

#include <iostream>
using namespace std;
int main( )
{
  int a; float b; char c;
  scanf("%d %c %f",&a,&c,&b);  //注意在变量名前要加地址运算符&
  printf("a=%d,b=%f,c=%c\n",a,b,c);
  return 0;
}

Pytorch offical resnet18 model load custom weights.

model_target = torchversion.models.resnet18()
model_target_sd = model_target.state_dict()
poison_sd = torch.load('square_white_tar0_alpha1.00_mark(3,3).pth')

for key in model_target_sd:
    if key.startswith('fc'):
        model_target_sd[key] = poison_sd['classifier.' + key]
    else:
        model_target_sd[key] = poison_sd['features.' + key]
model_target.load_state_dict(model_target_sd)
filter_weight = feature_weight(model_target)

Change the output of torchversion models.

from torchversion import models
from torch import nn
model = models.resnet18()
model.fc = nn.Linear(512, num_classes)

Save images

# PIL
from PIL import Image
img = Image.open('foo.png')
img.save('foo2.png')

# plt
plt.imgsave('foo2.png')

#opencv
cv.imwrite('foo2.png')

设置随机数种子使模型训练结果可复现

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
# 设置随机数种子
setup_seed(20)

导入父目录中的脚本

import os
import sys
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
修改curse所在的整个单词 `ciw`,即change inner word
修改单词从curse位置到单词结尾的部分 `cw`,即change word
修改curse所在的整个括号 `ci(`,即change inner parentheses
修改括号从curse位置到括号结尾的部分 `c(`,即change parentheses
修改curse所在的整个括号,包括括号本身 `ca(`,即change around sentence

numpy和pointclouds互转

#numpy to pointclouds
xyz = np.fromfile('foo.bin', dtype=np.float32)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)

# pointclouds to numpy
pcd_load = o3d.io.read_point_cloud("../../TestData/sync.ply")
xyz_load = np.asarray(pcd_load.points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment