手写.

news/2024/10/22 9:28:19/

IOU

"""
#检测
A:
左下角坐标(left_x,left_y)
右上角坐标(right_x,right_y)
B:
左下角坐标(left_x,left_y)
右上角坐标(right_x,right_y)
"""def IOU(rectangle A, rectangleB):W = min(A.right_x, B.right_x) - max(A.left_x, B.left_x)H = min(A.right_y, B.right_y) - max(A.left_y, B.left_y)if W <= 0 or H <= 0:return 0;SA = (A.right_x - A.left_x) * (A.right_y - A.left_y)SB = (B.right_x - B.left_x) * (B.right_y - B.left_y)cross = W * Hreturn cross/(SA + SB - cross)"""
#分割
"""
A:
左下角坐标(left_x,left_y)
右上角坐标(right_x,right_y)
B:
左下角坐标(left_x,left_y)
右上角坐标(right_x,right_y)
def compute_ious(pred, label, classes):'''computes iou for one ground truth mask and predicted mask'''ious = [] # 记录每一类的ioufor c in classes:label_c = (label == c) # label_c为true/false矩阵pred_c = (pred == c)intersection = np.logical_and(pred_c, label_c).sum()union = np.logical_or(pred_c, label_c).sum()if union == 0:ious.append(float('nan'))  elseious.append(intersection / union)return np.nanmean(ious) #返回当前图片里所有类的mean ioudef compute_iou_batch(preds, labels, classes=None):'''computes mean iou for a batch of ground truth masks and predicted masks'''ious = []preds = np.copy(preds) # copy is implabels = np.array(labels) # tensor to npfor pred, label in zip(preds, labels): # iter one batchious.append(compute_ious(pred, label, classes))iou = np.nanmean(ious) # mean iou of one batchreturn iou

mIOU

"""
#分割https://blog.csdn.net/weixin_43917574/article/details/117264163
"""
#混淆矩阵
def _fast_hist(label_true, label_pred, n_class):"""label_true是转化为一维数组的真实标签,label_pred是转化为一维数组的预测结果,n_class是类别数hist是一个混淆矩阵hist是一个二维数组,可以写成hist[label_true][label_pred]的形式最后得到的这个数组的意义就是行下标表示的类别预测成列下标类别的数量"""# mask在和label_true相对应的索引的位置上填入true或者false# label_true[mask]会把mask中索引为true的元素输出mask = (label_true >= 0) & (label_true < n_class)# n_class * label_true[mask].astype(int) + label_pred[mask]计算得到的是二维数组元素# 变成一位数组元素的时候的地址取值(每个元素大小为1),返回的是一个numpy的list# np.bincount()会给出索引对应的元素个数hist = np.bincount(n_class * label_true[mask].astype(int) + label_pred[mask],minlength=n_class ** 2).reshape(n_class, n_class)return hist#每个类别IOU
def per_class_iu(hist):# 矩阵的对角线上的值组成的一维数组/(矩阵的每行求和+每列求和-对角线上的值),返回值形状(n,)return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))#mIOU
np.mean(per_class_iu(hist))

NMS

import numpy as np"""
坐标:左下角,右上角
"""def nms(dets, thresh):x1 = dets[:, 0]y1 = dets[:, 1]x2 = dets[:, 2]y2 = dets[:, 3]scores = dets[:, 4]sorted_scores = scores.argsort()[::-1]keep_list = []while sorted_scores.size > 0:max_score_idx = sorted_scores[0]keep_list.append(max_score_idx)sorted_scores=np.delete(sorted_scores, 0, axis = 0)max_score_x1 = x1[max_score_idx]max_score_y1 = y1[max_score_idx]max_score_x2 = x2[max_score_idx]max_score_y2 = y2[max_score_idx]delete_list = []for idx in range(len(sorted_scores)):candidate_idx = sorted_scores[idx]candidate_x1 = x1[candidate_idx]candidate_y1 = y1[candidate_idx]candidate_x2 = x2[candidate_idx]candidate_y2 = y2[candidate_idx]intersection = max(0, (min(max_score_x2, candidate_x2) - max(max_score_x1, candidate_x1)+1) * (min(max_score_y2, candidate_y2) - max(max_score_y1, candidate_y1)+1))union = (max_score_x2 - max_score_x1) * (max_score_y2 - max_score_y1) + (candidate_x2 - candidate_x1) * (candidate_y2 - candidate_y1) -  intersectioniou = float(intersection/union)print(intersection,union,iou)if iou >=thresh:delete_list.append(idx)sorted_scores = np.delete(sorted_scores, delete_list, axis = 0)print(keep_list)return keep_listdets = np.array([[100, 100, 210, 210, 0.72],[250, 250, 420, 420, 0.8],[220, 220, 320, 330, 0.92],[100, 100, 210, 210, 0.72],
nms(dets=dets, thresh=0.7)

Dice Loss

# 二分类
import torch
import torch.nn as nnclass BinaryDiceLoss(nn.Model):def __init__(self):super(BinaryDiceLoss, self).__init__()def forward(self, input, targets):# 获取每个批次的大小 NN = targets.size()[0]# 平滑变量smooth = 1# 将宽高 reshape 到同一纬度input_flat = input.view(N, -1)targets_flat = targets.view(N, -1)# 计算交集intersection = input_flat * targets_flat N_dice_eff = (2 * intersection.sum(1) + smooth) / (input_flat.sum(1) + targets_flat.sum(1) + smooth)# 计算一个批次中平均每张图的损失loss = 1 - dice_eff.sum() / Nreturn loss# 多分类
import torch
import torch.nn as nnclass MultiClassDiceLoss(nn.Module):def __init__(self, weight=None, ignore_index=None, **kwargs):super(MultiClassDiceLoss, self).__init__()self.weight = weightself.ignore_index = ignore_indexself.kwargs = kwargsdef forward(self, input, target):"""input tesor of shape = (N, C, H, W)target tensor of shape = (N, H, W)"""# 先将 target 进行 one-hot 处理,转换为 (N, C, H, W)nclass = input.shape[1]target = one_hot(target.long(), nclass)assert input.shape == target.shape, "predict & target shape do not match"binaryDiceLoss = BinaryDiceLoss()total_loss = 0# 归一化输出logits = F.softmax(input, dim=1)C = target.shape[1]# 遍历 channel,得到每个类别的二分类 DiceLossfor i in range(C):dice_loss = binaryDiceLoss(logits[:, i], target[:, i])total_loss += dice_loss# 每个类别的平均 dice_lossreturn total_loss / C

BN

def batch_norm(X, gamma, beta, moving_mean, moving_var, eps, momentum):# 通过 `is_grad_enabled` 来判断当前模式是训练模式还是预测模式if not torch.is_grad_enabled():# 如果是在预测模式下,直接使用传入的移动平均所得的均值和方差X_hat = (X - moving_mean) / torch.sqrt(moving_var + eps)else:assert len(X.shape) in (2, 4)if len(X.shape) == 2:# 使用全连接层的情况,计算特征维上的均值和方差mean = X.mean(dim=0)var = ((X - mean)**2).mean(dim=0)else:# 使用二维卷积层的情况,计算通道维上(axis=1)的均值和方差。# 这里我们需要保持X的形状以便后面可以做广播运算mean = X.mean(dim=(0, 2, 3), keepdim=True)var = ((X - mean)**2).mean(dim=(0, 2, 3), keepdim=True)# 训练模式下,用当前的均值和方差做标准化X_hat = (X - mean) / torch.sqrt(var + eps)# 更新移动平均的均值和方差moving_mean = momentum * moving_mean + (1.0 - momentum) * meanmoving_var = momentum * moving_var + (1.0 - momentum) * varY = gamma * X_hat + beta  # 缩放和移位return Y, moving_mean.data, moving_var.data

Conv2d

# coding:utf-8
import numpy as np
class Conv2D:def __init__(self, in_channels, out_channels, kernel_size, padding, stride):self.in_channels = in_channelsself.out_channels = out_channelsself.kernel_size = kernel_sizeself.padding = paddingself.stride = strideself.filters = np.random.rand(self.in_channels, self.out_channels, self.kernel_size, self.kernel_size)def __call__(self, x):B, C, W, H = x.shapeout_h = (H + self.padding * 2 - self.kernel_size) // self.stride + 1out_w = (W + self.padding * 2 - self.kernel_size) // self.stride + 1out_features = np.zeros((B, self.out_channels, out_h, out_w))in_features = np.zeros((B, C, H + self.padding * 2, W + self.padding * 2))in_features[:, :, self.padding:-self.padding, self.padding:-self.padding] = xfor batch_idx in range(B):for ch_idx in range(self.out_channels):out_features[batch_idx, ch_idx, :, :] = self._conv(in_features[batch_idx], out_features[batch_idx, ch_idx, :, :],self.filters[:, ch_idx, :, :])return out_featuresdef _conv(self, x, out, kernel):h, w = out.shapefor i in range(0, h):for j in range(0, w):# print("x.shape",x.shape) # (3, 12, 12)# print("out.shape",out.shape) # (10, 10)# print("kernel.shape",kernel.shape) # (3, 3, 3)tmp = x[:, i*self.stride:i*self.stride + self.kernel_size, j*self.stride:j*self.stride + self.kernel_size] * kernel# print("tmp.shape",tmp.shape) (3, 3, 3)out[i][j] = min(tmp.sum(axis=(0,1,2)),255)return outcon2d=Conv2D(in_channels=3, out_channels=5, kernel_size=3, padding=1, stride=1)
x=np.random.rand(4, 3, 10, 10) # B, C, W, H
con2d(x)

 MaxPool2D

# coding:utf-8
import numpy as npclass MaxPool2D:def __init__(self, pool_size, stride):# super.__init__()self.pool_size = pool_sizeself.stride = stridedef __call__(self, x):B, C, W, H = x.shapeout_h = (H - self.pool_size) // self.stride + 1out_w = (W - self.pool_size) // self.stride + 1out_features = np.zeros((B, C, out_h, out_w))for batch_idx in range(B):for ch_idx in range(C):for i in range(out_h):for j in range(out_w):# print("patch.shape",x[batch_idx, ch_idx, i*self.stride:i*self.stride+self.pool_size, j*self.stride:j*self.stride+self.pool_size].shape) # [2, 2]out_features[batch_idx, ch_idx, i, j] = np.max(x[batch_idx, ch_idx, i*self.stride:i*self.stride+self.pool_size, j*self.stride:j*self.stride+self.pool_size])return out_featuresmaxpool2d=MaxPool2D(2,2)
x=np.random.rand(4, 3, 10, 10)
maxpool2d(x)

AvgPool2D

# coding:utf-8
import numpy as npclass AvgPool2D:def __init__(self, pool_size, stride):# super.__init__()self.pool_size = pool_sizeself.stride = stridedef __call__(self, x):B, C, W, H = x.shapeout_h = (H - self.pool_size) // self.stride + 1out_w = (W - self.pool_size) // self.stride + 1out_features = np.zeros((B, C, out_h, out_w))for batch_idx in range(B):for ch_idx in range(C):for i in range(out_h):for j in range(out_w):# print("patch.shape",x[batch_idx, ch_idx, i*self.stride:i*self.stride+self.pool_size, j*self.stride:j*self.stride+self.pool_size].shape) # [2, 2]out_features[batch_idx, ch_idx, i, j] = np.mean(x[batch_idx, ch_idx, i*self.stride:i*self.stride+self.pool_size, j*self.stride:j*self.stride+self.pool_size])return out_featuresavgpool2d=AvgPool2D(2,2)
x=np.random.rand(4, 3, 10, 10)
avgpool2d(x)

http://www.ppmy.cn/news/695524.html

相关文章

android 输入法(包括手写界面)

公司要开发自己的输入法&#xff0c;找了很多例子&#xff0c;都不是自己想要的。android本身的例子不能满足特殊布局的要求&#xff0c;而且没有手写输入&#xff0c;虽然在例子上实现了手写输入但是布局仍然调不好。花了很长时间来分析代码&#xff0c;太累了&#xff0c;决定…

手写识别(一)

文章目录 前言一、环境介绍二、训练模型2.1 前向传播2.2 后向传播2.3 测试模型2.4 提取图像 三、提取参数总结 前言 纸上得来终觉浅&#xff0c;绝知此事要躬行。我们已经学习了神经网络理论知识&#xff0c;从这节课开始试着使用FPGA来部署神经网络模型&#xff0c;实现手写识…

android手写输入

http://www.iteye.com/topic/288045 你可以在线测试&#xff1a; http://quake.3322.org/我最先google到一个开源的手写输入引擎是Tomoe: http://tomoe.sourceforge.jp&#xff0c;它包括识别引擎&#xff0c;整套的用户界面&#xff08;GTK&#xff09;以及SCIM的输入法模块&…

手写输入和软键盘输入

今天项目需要完成一个支持手写和键盘都可以输入的功能&#xff0c;点击键盘切换到软键盘输入&#xff0c;此时按钮会变成手写&#xff0c;在点击就回到手写。按钮是checkbox。功能很简单。 支持手写和键盘输入,其实并不难&#xff0c;就是布局麻烦点&#xff0c;还有就是画布麻…

手写输入---随手写

手写输入—随手写 最近做的是关于教育的app&#xff0c;分老师版和学生版&#xff1b;学生版有做题功能&#xff0c;领导最近看到别的app里面有手写输入功能&#xff0c;保留原笔迹&#xff0c;也就是说写成什么样就是什么样&#xff0c;然后保存&#xff0c;上传。 思路1&…

手写apply

在自己实现apply之前&#xff0c;我们需要先了解一下对函数使用了apply之后完成了那些效果&#xff0c;再考虑怎么自己编写代码去实现这些效果。 let name "张三" let personObj {name: "李四", } function getPersonName(){console.log(this)console.lo…

中文手写输入 (一)

最近业余时间在学习android的系统&#xff0c;想尝试将中文手写输入支持加到android上去&#xff0c;将最近的学习结果记录下来。 先上个截图: [img]/upload/attachment/56882/6bec42ff-5f30-3a2b-8baf-cc5030009305.png[/img] 你可以在线测试&#xff1a; [url]http://quake.…

如何在Windows 10上使用手写输入

Windows 10’s handwriting keyboard allows you to enter text into any application with a pen or other stylus. It even works on old desktop applications. Windows 10的手写键盘允许您使用笔或其他手写笔在任何应用程序中输入文本。 它甚至可以在旧的桌面应用程序上运行…