np.concatenate、torch.split()

本文介绍了numpy的concatenate函数用于一维和二维数组的拼接,以及torch中的split函数用于tensor的分割,stack函数用于tensor的堆叠,并提及了torch.nn.functional.normalize用于tensor的归一化处理。示例代码展示了不同参数设置下的操作效果。

1,np.concatenate

np.concatenate 是numpy中对array进行拼接的函数
参考
numpy.concatenate((a1, a2, …), axis=0, out=None, dtype=None, casting=“same_kind”)

对于一维数组拼接,axis的值不影响最后的结果

axis =1 axis=1为行数不变列拼接

axis =0 axis=0为列数不变行拼接

参数

A1,A2,…阵列序列数组必须具有相同的形状

# 行数不变,列数相加
a=np.array([[1,2,3]])
b=np.array([[11,21]])
d = np.concatenate((a,b),axis=1) # 默认情况下,axis=0可以不写
print(d)

d=array([[1,2,3,11,21]])

2,torch.split()

参考
torch.split(tensor, split_size_or_sections, dim=0)

torch.split()作用将tensor分成块结构。

参数:

tesnor:input,待分输入
split_size_or_sections:需要切分的大小(int or list )
dim:切分维度


import torch
 
x = torch.rand(4,8,6)
y = torch.split(x,2,dim=0) #在第0维度去分,每块包含2个小块
for i in y :
    print(i.size())
 

output:
torch.Size([2, 8, 6])
torch.Size([2, 8, 6])

3,torch.stack()

参考

4,torch.stack([x, x],dim=x).max(dim)

参考
其实就是在相应的维度上找最大值,然后维度信息会恢复到堆叠前的维度信息。

5,torch.nn.functional.normalize

参考

import os import cv2 import sys import argparse import time # add path realpath = os.path.abspath(__file__) _sep = os.path.sep realpath = realpath.split(_sep) try: zoo_root_index = next(i for i, part in enumerate(realpath) if 'rknn_model_zoo' in part) rknn_model_zoo_path = os.path.join(realpath[0]+_sep, *realpath[1:zoo_root_index+1]) sys.path.append(rknn_model_zoo_path) except StopIteration: raise ValueError("Could not find 'rknn_model_zoo' directory in the path: {}".format(os.path.abspath(__file__))) from py_utils.coco_utils import COCO_test_helper import numpy as np OBJ_THRESH = 0.25 NMS_THRESH = 0.45 # The follew two param is for map test # OBJ_THRESH = 0.001 # NMS_THRESH = 0.65 IMG_SIZE = (640, 640) # (width, height), such as (1280, 736) CLASSES = ("car","white") coco_id_list = [1,2] def filter_boxes(boxes, box_confidences, box_class_probs): """Filter boxes with object threshold. """ box_confidences = box_confidences.reshape(-1) candidate, class_num = box_class_probs.shape class_max_score = np.max(box_class_probs, axis=-1) classes = np.argmax(box_class_probs, axis=-1) _class_pos = np.where(class_max_score* box_confidences >= OBJ_THRESH) scores = (class_max_score* box_confidences)[_class_pos] boxes = boxes[_class_pos] classes = classes[_class_pos] return boxes, classes, scores def nms_boxes(boxes, scores): """Suppress non-maximal boxes. # Returns keep: ndarray, index of effective boxes. """ x = boxes[:, 0] y = boxes[:, 1] w = boxes[:, 2] - boxes[:, 0] h = boxes[:, 3] - boxes[:, 1] areas = w * h order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x[i], x[order[1:]]) yy1 = np.maximum(y[i], y[order[1:]]) xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]]) yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]]) w1 = np.maximum(0.0, xx2 - xx1 + 0.00001) h1 = np.maximum(0.0, yy2 - yy1 + 0.00001) inter = w1 * h1 ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= NMS_THRESH)[0] order = order[inds + 1] keep = np.array(keep) return keep def dfl(position): # Distribution Focal Loss (DFL) import torch x = torch.tensor(position) n,c,h,w = x.shape p_num = 4 mc = c//p_num y = x.reshape(n,p_num,mc,h,w) y = y.softmax(2) acc_metrix = torch.tensor(range(mc)).float().reshape(1,1,mc,1,1) y = (y*acc_metrix).sum(2) return y.numpy() def box_process(position): grid_h, grid_w = position.shape[2:4] col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h)) col = col.reshape(1, 1, grid_h, grid_w) row = row.reshape(1, 1, grid_h, grid_w) grid = np.concatenate((col, row), axis=1) stride = np.array([IMG_SIZE[1]//grid_h, IMG_SIZE[0]//grid_w]).reshape(1,2,1,1) position = dfl(position) box_xy = grid +0.5 -position[:,0:2,:,:] box_xy2 = grid +0.5 +position[:,2:4,:,:] xyxy = np.concatenate((box_xy*stride, box_xy2*stride), axis=1) return xyxy def post_process(input_data): boxes, scores, classes_conf = [], [], [] defualt_branch=3 pair_per_branch = len(input_data)//defualt_branch # Python 忽略 score_sum 输出 for i in range(defualt_branch): boxes.append(box_process(input_data[pair_per_branch*i])) classes_conf.append(input_data[pair_per_branch*i+1]) scores.append(np.ones_like(input_data[pair_per_branch*i+1][:,:1,:,:], dtype=np.float32)) def sp_flatten(_in): ch = _in.shape[1] _in = _in.transpose(0,2,3,1) return _in.reshape(-1, ch) boxes = [sp_flatten(_v) for _v in boxes] classes_conf = [sp_flatten(_v) for _v in classes_conf] scores = [sp_flatten(_v) for _v in scores] boxes = np.concatenate(boxes) classes_conf = np.concatenate(classes_conf) scores = np.concatenate(scores) # filter according to threshold boxes, classes, scores = filter_boxes(boxes, scores, classes_conf) # nms nboxes, nclasses, nscores = [], [], [] for c in set(classes): inds = np.where(classes == c) b = boxes[inds] c = classes[inds] s = scores[inds] keep = nms_boxes(b, s) if len(keep) != 0: nboxes.append(b[keep]) nclasses.append(c[keep]) nscores.append(s[keep]) if not nclasses and not nscores: return None, None, None boxes = np.concatenate(nboxes) classes = np.concatenate(nclasses) scores = np.concatenate(nscores) return boxes, classes, scores def draw(image, boxes, scores, classes): for box, score, cl in zip(boxes, scores, classes): top, left, right, bottom = [int(_b) for _b in box] print("%s @ (%d %d %d %d) %.3f" % (CLASSES[cl], top, left, right, bottom, score)) cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2) cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score), (top, left - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) def setup_model(args): model_path = args.model_path if model_path.endswith('.pt') or model_path.endswith('.torchscript'): platform = 'pytorch' from py_utils.pytorch_executor import Torch_model_container model = Torch_model_container(args.model_path) elif model_path.endswith('.rknn'): platform = 'rknn' from py_utils.rknn_executor import RKNN_model_container model = RKNN_model_container(args.model_path, args.target, args.device_id) elif model_path.endswith('onnx'): platform = 'onnx' from py_utils.onnx_executor import ONNX_model_container model = ONNX_model_container(args.model_path) else: assert False, "{} is not rknn/pytorch/onnx model".format(model_path) print('Model-{} is {} model, starting val'.format(model_path, platform)) return model, platform def img_check(path): img_type = ['.jpg', '.jpeg', '.png', '.bmp'] for _type in img_type: if path.endswith(_type) or path.endswith(_type.upper()): return True return False if __name__ == '__main__': # Create a dummy args object class Args: pass args = Args() args.model_path = '/home/cat/NPU/rknn_model_zoo-main/examples/yolov8/model/whitenu8.rknn' args.target = 'rk3576' args.device_id = None # init model model, platform = setup_model(args) co_helper = COCO_test_helper(enable_letter_box=True) # init camera cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Error: Could not open camera.") exit() print("Press 'q' to quit.") # run test while True: start_time = time.time() ret, img_src = cap.read() if not ret: print("Error: Failed to capture frame.") break # Due to rga init with (0,0,0), we using pad_color (0,0,0) instead of (114, 114, 114) pad_color = (0,0,0) img = co_helper.letter_box(im= img_src.copy(), new_shape=(IMG_SIZE[1], IMG_SIZE[0]), pad_color=(0,0,0)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # preprocee if not rknn model if platform in ['pytorch', 'onnx']: input_data = img.transpose((2,0,1)) input_data = input_data.reshape(1,*input_data.shape).astype(np.float32) input_data = input_data/255. else: input_data = img outputs = model.run([input_data]) boxes, classes, scores = post_process(outputs) img_p = img_src.copy() if boxes is not None: draw(img_p, co_helper.get_real_box(boxes), scores, classes) end_time = time.time() fps = 1 / (end_time - start_time) cv2.putText(img_p, f"FPS: {int(fps)}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow("yolov8 detection", img_p) if cv2.waitKey(1) & 0xFF == ord('q'): break # release cap.release() cv2.destroyAllWindows() model.release()
07-19
shuffled_idx = shuffle(np.array(range(sample_number)), random_state=seed) train_idx = shuffled_idx[:int(0.1 * sample_number)] val_idx = shuffled_idx[int(0.1 * sample_number):int(0.2 * sample_number)] test_idx = shuffled_idx[int(0.2 * sample_number):] X_smo, y_smo = balance_MLSMOTE(labeled_X, labeled_y, args.smote_num) def MLSMOTE(X, y, n_sample): """ Give the augmented data using MLSMOTE algorithm args X: pandas.DataFrame, input vector DataFrame y: pandas.DataFrame, feature vector dataframe n_sample: int, number of newly generated sample return new_X: pandas.DataFrame, augmented feature vector data target: pandas.DataFrame, augmented target vector data """ if not isinstance(X, pd.DataFrame): X = pd.DataFrame(X) if not isinstance(y, pd.DataFrame): y = pd.get_dummies(np.array(y)) indices2 = nearest_neighbour(X) n = len(indices2) new_X = np.zeros((n_sample, X.shape[1])) target = np.zeros((n_sample, y.shape[1])) for i in range(n_sample): reference = random.randint(0, n - 1) neighbour = random.choice(indices2[reference, 1:]) all_point = indices2[reference] nn_df = y[y.index.isin(all_point)] ser = nn_df.sum(axis=0, skipna=True) target[i] = np.array([1 if val > 2 else 0 for val in ser]) ratio = random.random() gap = X.loc[reference, :] - X.loc[neighbour, :] new_X[i] = np.array(X.loc[reference, :] + ratio * gap) new_X = pd.DataFrame(new_X, columns=X.columns) target = pd.DataFrame(target, columns=y.columns) new_X = pd.concat([X, new_X], axis=0) target = pd.concat([y, target], axis=0) return new_X.values, np.argmax(target.values,axis=1) def balance_MLSMOTE(labeled_X, labeled_y, n_sample): X_list = []#存储每个类别的特征,元素为列表 y_list = [] for i in range(max(labeled_y) + 1): X_list.append(labeled_X[labeled_y == i, :]) y_list.append(labeled_y[labeled_y == i]) print("lenX_list,len y_list",len(X_list),len(y_list)) num_classes = max(labeled_y) + 1#类别数 one_hot_codes = np.eye(num_classes)#单位矩阵,便于后续独热向量 df_y_list = []#将每个标签转换为独热编码 for i in range(len(y_list)): one_hot_labels = [] for label in y_list[i]: one_hot_label = one_hot_codes[label] one_hot_labels.append(one_hot_label) df_y = pd.DataFrame(np.array(one_hot_labels)) df_y_list.append(df_y) if n_sample == None: smote_num = 0 for i in range(len(y_list)): if len(y_list[i]) > smote_num: smote_num = len(y_list[i]) majority_class = i else: smote_num = n_sample for i in range(len(y_list)): if smote_num - len(y_list[i]) > 0: X_res, y_res = MLSMOTE(X_list[i], df_y_list[i], smote_num - len(y_list[i])) else: X_res, y_res = X_list[i], y_list[i] if i == 0: X_smo = X_res y_smo = y_res else: X_smo = np.concatenate([X_smo, X_res], axis=0) y_smo = np.concatenate([y_smo, y_res], axis=0) return X_smo, np.squeeze(y_smo) idx_except_train = torch.LongTensor(range(len(labels)))[~data.train_mask.cpu()] orign_idx_train = torch.tensor(np.array(range(len(train_idx))), dtype=torch.long).cuda() new_idx_train = torch.tensor(np.array(range(len(y_smo))), dtype=torch.long).cuda() new_idx_val = torch.tensor(val_idx, dtype=torch.long).cuda() + torch.tensor(len(y_smo) - len(train_idx)).cuda() new_idx_test = torch.tensor(test_idx, dtype=torch.long).cuda() + torch.tensor(len(y_smo) - len(train_idx)).cuda() X_generate = torch.FloatTensor(np.concatenate([X_smo, all_X[idx_except_train, :]], axis=0)).cuda() y_generate = torch.LongTensor(np.concatenate([y_smo, all_y[idx_except_train]], axis=0)).cuda() 这几段是合成节点前后的数据集划分,请你分析一下,正确的划分方式应该是怎样的
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值