python常用功能

import numpy as np
import torch
from einops import rearrange
import os
from glob import glob
from sklearn.metrics import roc_auc_score
import torch.nn.functional as F

if __name__ == '__main__':
    print("Hello!")
    # 图像数据增强库---img_aug.augmenters
    # 图像数据处理库---torchvision.transforms

    # 随机数
    p = np.random.uniform()  # 生成一个[0,1)均匀分布的随机数,float
    q = np.random.uniform(1, 2, (3, 3))  # 生成3*3=9个[1,2)均匀分布的随机数,shape[3,3], float
    idx = np.random.choice(10)  # 从0~9中随机选一个数,int
    print(p)
    print(q)
    print(idx)
    scale = torch.randint(0, 5, (2, 3)).numpy()  # 从[0,5)中随机生成2*3个整数,int
    print(scale)
    range_list = [1.5, 3.5]
    r = np.random.uniform(*range_list, size=1)  # shape[1]
    np.random.choice(5, 3, replace=False)  # 从0~4中随机取3个数,取值不重复
    rd = np.random.permutation(np.arange(5))[:3]  # 从0~4中随机取3个数,取值不重复
    np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])  # 从0~4中随机取3个数,每个值取到的概率不同

    # 打乱顺序
    disordered_idx = np.arange(10)  # [0 1 2 3 4 5 6 7 8 9]
    print(disordered_idx)
    np.random.shuffle(disordered_idx)  # shuffle
    print(disordered_idx)

    # rearrange
    images = [np.random.randn(30, 40, 3) for _ in range(32)]  # list
    images = np.stack(images, axis=0)  # array  shape[32,30,40,3]
    print(rearrange(images, 'b h w c -> b (h w) c').shape)  # shape[32,1200,3]
    images = rearrange(images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c', h1=2, w1=2)
    print(images.shape)  # shape[128,15,20,3]
    images = rearrange(images, '(b h1 w1) h w c -> b (h1 h) (w1 w) c', h1=2, w1=2)
    print(images.shape)  # shape[32,1200,3]

    # where
    arr = np.array([[2, 4, 6], [3, 6, 4]])  # shape[2,3]
    arr_mask = np.where(arr > 4, np.ones_like(arr), np.zeros_like(arr))  # shape[2,3]  int32
    print(arr_mask.astype(bool).dtype)  # bool

    # to_tensor
    mask = np.random.randn(64, 64, 3)
    mask = torch.Tensor(mask).to(torch.int64)

    # read files in folder
    file_list = glob(os.path.join('G:/temp/images', 'train/*/*'))
    print(len(file_list))

    # roc_auc_score  auc=area_under_curve
    image_targets = np.random.choice(2, 10, p=[0.2, 0.8]).astype(bool)  # shape[10]  bool
    anomaly_score = np.random.uniform(0, 1, (10,))  # shape[10]  float
    auc_roc_image = roc_auc_score(image_targets, anomaly_score)
    print(auc_roc_image)
    image_masks = np.random.choice(2, (10, 32, 32), p=[0.2, 0.8]).astype(bool)  # shape[10,32,32]  bool
    anomaly_maps = np.random.uniform(0, 1, (10, 32, 32))  # shape[10,32,32]  float
    auc_roc_pixel = roc_auc_score(image_masks.reshape(-1), anomaly_maps.reshape(-1))
    print(auc_roc_pixel)

    # softmax
    feature = np.random.uniform(0, 1, (4, 2, 32, 32))  # shape[4,2,32,32]  float
    feature = torch.Tensor(feature)
    print(feature.shape)
    outputs = F.softmax(feature, dim=1)
    print(outputs.shape)
    print(outputs[:, 1, :].shape)

    # extend_dim
    maps = np.random.uniform(0, 1, (32, 32))  # shape[32,32]
    maps_expanded = np.expand_dims(maps, axis=2)  # shape[32,32,1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值