y10 = np.zeros((m,num_labels))

时间: 2023-06-13 14:06:09 浏览: 51
这行代码是用来创建一个名为y10的零矩阵,其大小为(m, num_labels),其中m是样本数量,num_labels是标签数量。它通常用于多分类问题中,其中每个样本可以属于多个类别中的一个或多个,因此需要将每个标签表示为一个二进制数组,其中每个元素表示该样本是否属于该标签。该行代码创建了一个初始全部为零的矩阵,用于存储每个样本属于每个标签的二进制表示。
相关问题

in_features = train_features.shape[1] def train(model, train_features, train_labels, test_features, test_labels, num_epochs, learning_rate, weight_decay, batch_size): train_ls, test_ls = [], [] theta = np.zeros((in_features, 1)) best_theta = np.zeros((in_features, 1)) best_loss = np.inf for epoch in range(num_epochs): train_iter = data_iter(batch_size, train_features, train_labels) for X, y in train_iter: theta=gradientDescent(X, y, theta, learning_rate, weight_decay) train_ls.append(log_rmse(model, train_features, train_labels, theta, len(train_labels)))帮我加个注释

# in_features表示输入特征的数量 in_features = train_features.shape[1] # 定义训练函数,接受模型、训练数据、测试数据、超参数等作为输入 def train(model, train_features, train_labels, test_features, test_labels, num_epochs, learning_rate, weight_decay, batch_size): # 初始化训练误差和测试误差列表 train_ls, test_ls = [], [] # 初始化模型参数theta(权重) theta = np.zeros((in_features, 1)) # 初始化最佳模型参数和最小测试误差 best_theta = np.zeros((in_features, 1)) best_loss = np.inf # 循环迭代训练num_epochs次 for epoch in range(num_epochs): # 随机生成batch_size大小的数据批次,用于训练 train_iter = data_iter(batch_size, train_features, train_labels) # 遍历数据批次,计算梯度并更新模型参数theta for X, y in train_iter: theta=gradientDescent(X, y, theta, learning_rate, weight_decay) # 计算每轮迭代后的训练误差和测试误差,并存入对应的列表中 train_ls.append(log_rmse(model, train_features, train_labels, theta, len(train_labels))) test_ls.append(log_rmse(model, test_features, test_labels, theta, len(test_labels))) # 如果当前模型参数对应的测试误差比历史最小值更小,则更新最佳模型参数和最小测试误差 if test_ls[-1] < best_loss: best_theta = theta best_loss = test_ls[-1] # 返回最佳模型参数和训练误差、测试误差列表 return best_theta, train_ls, test_ls

LDAM损失函数pytorch代码如下:class LDAMLoss(nn.Module): def init(self, cls_num_list, max_m=0.5, weight=None, s=30): super(LDAMLoss, self).init() m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list)) m_list = m_list * (max_m / np.max(m_list)) m_list = torch.cuda.FloatTensor(m_list) self.m_list = m_list assert s > 0 self.s = s if weight is not None: weight = torch.FloatTensor(weight).cuda() self.weight = weight self.cls_num_list = cls_num_list def forward(self, x, target): index = torch.zeros_like(x, dtype=torch.uint8) index_float = index.type(torch.cuda.FloatTensor) batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(1,0)) # 0,1 batch_m = batch_m.view((16, 1)) # size=(batch_size, 1) (-1,1) x_m = x - batch_m output = torch.where(index, x_m, x) if self.weight is not None: output = output * self.weight[None, :] target = torch.flatten(target) # 将 target 转换成 1D Tensor logit = output * self.s return F.cross_entropy(logit, target, weight=self.weight) 模型部分参数如下:# 设置全局参数 model_lr = 1e-5 BATCH_SIZE = 16 EPOCHS = 50 DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') use_amp = True use_dp = True classes = 7 resume = None CLIP_GRAD = 5.0 Best_ACC = 0 #记录最高得分 use_ema=True model_ema_decay=0.9998 start_epoch=1 seed=1 seed_everything(seed) # 数据增强 mixup mixup_fn = Mixup( mixup_alpha=0.8, cutmix_alpha=1.0, cutmix_minmax=None, prob=0.1, switch_prob=0.5, mode='batch', label_smoothing=0.1, num_classes=classes) # 读取数据集 dataset_train = datasets.ImageFolder('/home/adminis/hpy/ConvNextV2_Demo/RAF-DB/RAF/train', transform=transform) dataset_test = datasets.ImageFolder("/home/adminis/hpy/ConvNextV2_Demo/RAF-DB/RAF/valid", transform=transform_test) 帮我用pytorch实现模型在模型训练中使用LDAM损失函数

好的,下面是使用LDAM损失函数训练模型的代码: ```python import torch.optim as optim from torch.optim.lr_scheduler import CosineAnnealingLR from torch.utils.data import DataLoader # 定义LDAM损失函数 class LDAMLoss(nn.Module): def __init__(self, cls_num_list, max_m=0.5, weight=None, s=30): super(LDAMLoss, self).__init__() m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list)) m_list = m_list * (max_m / np.max(m_list)) m_list = torch.cuda.FloatTensor(m_list) self.m_list = m_list assert s > 0 self.s = s if weight is not None: weight = torch.FloatTensor(weight).cuda() self.weight = weight self.cls_num_list = cls_num_list def forward(self, x, target): index = torch.zeros_like(x, dtype=torch.uint8) index_float = index.type(torch.cuda.FloatTensor) batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(1,0)) # 0,1 batch_m = batch_m.view((x.size(0), 1)) # size=(batch_size, 1) x_m = x - batch_m output = torch.where(index, x_m, x) if self.weight is not None: output = output * self.weight[None, :] target = torch.flatten(target) # 将 target 转换成 1D Tensor logit = output * self.s return F.cross_entropy(logit, target, weight=self.weight) # 定义模型 model = models.resnet18(pretrained=True) num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, classes) model.to(DEVICE) # 定义优化器和学习率调整器 optimizer = optim.Adam(model.parameters(), lr=model_lr) scheduler = CosineAnnealingLR(optimizer, T_max=EPOCHS, eta_min=1e-6) # 定义LDAM损失函数 cls_num_list = [len(dataset_train[dataset_train.targets == t]) for t in range(classes)] criterion = LDAMLoss(cls_num_list) # 定义数据加载器 train_loader = DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, pin_memory=True) # 训练模型 best_acc = 0.0 for epoch in range(start_epoch, EPOCHS + 1): model.train() train_loss = 0.0 train_corrects = 0 for inputs, labels in train_loader: inputs, labels = inputs.to(DEVICE), labels.to(DEVICE) if use_dp: inputs, labels = dp(inputs, labels) if use_amp: with amp.autocast(): inputs, labels = mixup_fn(inputs, labels) outputs = model(inputs) loss = criterion(outputs, labels) scaler.scale(loss).backward() scaler.unscale_(optimizer) torch.nn.utils.clip_grad_norm_(model.parameters(), CLIP_GRAD) scaler.step(optimizer) scaler.update() else: inputs, labels_a, labels_b, lam = mixup_fn(inputs, labels) outputs = model(inputs) loss = mixup_criterion(criterion, outputs, labels_a, labels_b, lam) loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), CLIP_GRAD) optimizer.step() optimizer.zero_grad() train_loss += loss.item() * inputs.size(0) _, preds = torch.max(outputs, 1) train_corrects += torch.sum(preds == labels.data) train_loss /= len(dataset_train) train_acc = train_corrects.double() / len(dataset_train) model.eval() test_loss = 0.0 test_corrects = 0 with torch.no_grad(): for inputs, labels in test_loader: inputs, labels = inputs.to(DEVICE), labels.to(DEVICE) outputs = model(inputs) loss = criterion(outputs, labels) test_loss += loss.item() * inputs.size(0) _, preds = torch.max(outputs, 1) test_corrects += torch.sum(preds == labels.data) test_loss /= len(dataset_test) test_acc = test_corrects.double() / len(dataset_test) # 更新最佳模型 if test_acc > best_acc: if use_ema: ema_model.load_state_dict(model.state_dict()) best_acc = test_acc # 更新学习率 scheduler.step() # 打印训练结果 print('Epoch [{}/{}], Train Loss: {:.4f}, Train Acc: {:.4f}, Test Loss: {:.4f}, Test Acc: {:.4f}'.format( epoch, EPOCHS, train_loss, train_acc, test_loss, test_acc)) ```

相关推荐

import cv2 import numpy as np import os # 提取图像的HOG特征 def get_hog_features(image): hog = cv2.HOGDescriptor() hog_features = hog.compute(image) return hog_features # 加载训练数据集 train_data = [r"I:\18Breakageratecalculation\SVM run\detection_cut\whole\train128"] train_labels = [r"I:\18Breakageratecalculation\SVM run\detection_cut\whole\train128\labels.txt"] num_samples = 681 for i in range(num_samples): img = cv2.imread(str(i).zfill(3)+'.jpg') hog_features = get_hog_features(image) hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) color_hist = cv2.calcHist([hsv_image], [0, 1], None, [180, 256], [0, 180, 0, 256]) color_features = cv2.normalize(color_hist, color_hist).flatten() train_data.append(hog_features) train_labels.append(labels[i]) # 训练SVM模型 svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_LINEAR) svm.train(np.array(train_data), cv2.ml.ROW_SAMPLE, np.array(train_labels)) # 对测试图像进行分类 test_image = cv2.imread('I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\split\result2\maskslic2_roi.png', 0) test_features = get_hog_features(test_image) result = svm.predict(test_features.reshape(1,-1)) # 显示分割结果 result_image = np.zeros(test_image.shape, np.uint8) for i in range(test_image.shape[0]): for j in range(test_image.shape[1]): if result[i,j] == 1: result_image[i,j] = 255 cv2.imshow('I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\split\result2\Result.png', result_image) cv2.waitKey(0) cv2.destroyAllWindows()

将这个代码修改为自适应序列采样的插值方法:import numpy as np import matplotlib.pyplot as plt def gen_data(x1, x2): y_sample = np.sin(np.pi * x1 / 2) + np.cos(np.pi * x1 / 3) y_all = np.sin(np.pi * x2 / 2) + np.cos(np.pi * x2 / 3) return y_sample, y_all def kernel_interpolation(y_sample, x1, sig): gaussian_kernel = lambda x, c, h: np.exp(-(x - x[c]) ** 2 / (2 * (h ** 2))) num = len(y_sample) w = np.zeros(num) int_matrix = np.asmatrix(np.zeros((num, num))) for i in range(num): int_matrix[i, :] = gaussian_kernel(x1, i, sig) w = int_matrix.I * np.asmatrix(y_sample).T return w def kernel_interpolation_rec(w, x1, x2, sig): gkernel = lambda x, xc, h: np.exp(-(x - xc) ** 2 / (2 * (h ** 2))) num = len(x2) y_rec = np.zeros(num) for i in range(num): for k in range(len(w)): y_rec[i] = y_rec[i] + w[k] * gkernel(x2[i], x1[k], sig) return y_rec if __name__ == '__main__': snum = 12 # control point数量 ratio =50 # 总数据点数量:snum*ratio sig = 2 # 核函数宽度 xs = -4 xe = 4 x1 = np.linspace(xs, xe, snum) x2 = np.linspace(xs, xe, (snum - 1) * ratio + 1) y_sample, y_all = gen_data(x1, x2) plt.figure(1) w = kernel_interpolation(y_sample, x1, sig) y_rec = kernel_interpolation_rec(w, x1, x2, sig) plt.plot(x2, y_rec, 'k') plt.plot(x2, y_all, 'r:') plt.ylabel('y') plt.xlabel('x') for i in range(len(x1)): plt.plot(x1[i], y_sample[i], 'go', markerfacecolor='none') plt.legend(labels=['reconstruction', 'original', 'control point'], loc='lower left') plt.title('kernel interpolation:$y=sin(\pi x/2)+cos(\pi x/3)$') plt.show()

import random import numpy as np import matplotlib.pyplot as plt 生成随机坐标点 def generate_points(num_points): points = [] for i in range(num_points): x = random.uniform(-10, 10) y = random.uniform(-10, 10) points.append([x, y]) return points 计算欧几里得距离 def euclidean_distance(point1, point2): return np.sqrt(np.sum(np.square(np.array(point1) - np.array(point2)))) K-means算法实现 def kmeans(points, k, num_iterations=100): num_points = len(points) # 随机选择k个点作为初始聚类中心 centroids = random.sample(points, k) # 初始化聚类标签和距离 labels = np.zeros(num_points) distances = np.zeros((num_points, k)) for i in range(num_iterations): # 计算每个点到每个聚类中心的距离 for j in range(num_points): for l in range(k): distances[j][l] = euclidean_distance(points[j], centroids[l]) # 根据距离将点分配到最近的聚类中心 for j in range(num_points): labels[j] = np.argmin(distances[j]) # 更新聚类中心 for l in range(k): centroids[l] = np.mean([points[j] for j in range(num_points) if labels[j] == l], axis=0) return labels, centroids 生成坐标点 points = generate_points(100) 对点进行K-means聚类 k_values = [2, 3, 4] for k in k_values: labels, centroids = kmeans(points, k) # 绘制聚类结果 colors = [‘r’, ‘g’, ‘b’, ‘y’, ‘c’, ‘m’] for i in range(k): plt.scatter([points[j][0] for j in range(len(points)) if labels[j] == i], [points[j][1] for j in range(len(points)) if labels[j] == i], color=colors[i]) plt.scatter([centroid[0] for centroid in centroids], [centroid[1] for centroid in centroids], marker=‘x’, color=‘k’, s=100) plt.title(‘K-means clustering with k={}’.format(k)) plt.show()import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.datasets import load_iris 载入数据集 iris = load_iris() X = iris.data y = iris.target K-means聚类 kmeans = KMeans(n_clusters=3, random_state=0).fit(X) 可视化结果 plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_) plt.xlabel(‘Sepal length’) plt.ylabel(‘Sepal width’) plt.title(‘K-means clustering on iris dataset’) plt.show()对这个算法的结果用SSE,轮廓系数,方差比率准则,DBI几个指标分析

显示代码中y_rec的函数表达式:import numpy as np import matplotlib.pyplot as plt def gen_data(x1, x2): y_sample = np.sin(np.pi * x1 / 2) + np.cos(np.pi * x1 / 3) y_all = np.sin(np.pi * x2 / 2) + np.cos(np.pi * x2 / 3) return y_sample, y_all def kernel_interpolation(y_sample, x1, sig): gaussian_kernel = lambda x, c, h: np.exp(-(x - x[c]) ** 2 / (2 * (h ** 2))) num = len(y_sample) w = np.zeros(num) int_matrix = np.asmatrix(np.zeros((num, num))) for i in range(num): int_matrix[i, :] = gaussian_kernel(x1, i, sig) w = int_matrix.I * np.asmatrix(y_sample).T return w def kernel_interpolation_rec(w, x1, x2, sig): gkernel = lambda x, xc, h: np.exp(-(x - xc) ** 2 / (2 * (h ** 2))) num = len(x2) y_rec = np.zeros(num) for i in range(num): for k in range(len(w)): y_rec[i] = y_rec[i] + w[k] * gkernel(x2[i], x1[k], sig) return y_rec if name == 'main': snum =4 # control point数量 ratio =50 # 总数据点数量:snum*ratio sig = 2 # 核函数宽度 xs = -14 xe = 14 #x1 = np.linspace(xs, xe,snum) x1 = np.array([9, 9.1, 13 ]) x2 = np.linspace(xs, xe, (snum - 1) * ratio + 1) y_sample, y_all = gen_data(x1, x2) plt.figure(1) w = kernel_interpolation(y_sample, x1, sig) y_rec = kernel_interpolation_rec(w, x1, x2, sig) plt.plot(x2, y_rec, 'k') plt.plot(x2, y_all, 'r:') plt.ylabel('y') plt.xlabel('x') for i in range(len(x1)): plt.plot(x1[i], y_sample[i], 'go', markerfacecolor='none') # 计算均方根误差 rmse = np.sqrt(np.mean((y_rec - y_all) ** 2)) # 输出均方根误差值 print("均方根误差为:", rmse) plt.legend(labels=['reconstruction', 'original', 'control point'], loc='lower left') plt.title('kernel interpolation:$y=sin(\pi x/2)+cos(\pi x/3)$') plt.show()

import torch import os import torch.nn as nn import torch.optim as optim import numpy as np import random class Net(nn.Module): def init(self): super(Net, self).init() self.conv1 = nn.Conv2d(1, 16, kernel_size=3,stride=1) self.pool = nn.MaxPool2d(kernel_size=2,stride=2) self.conv2 = nn.Conv2d(16, 32, kernel_size=3,stride=1) self.fc1 = nn.Linear(32 * 9 * 9, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 2) def forward(self, x): x = self.pool(nn.functional.relu(self.conv1(x))) x = self.pool(nn.functional.relu(self.conv2(x))) x = x.view(-1, 32 * 9 * 9) x = nn.functional.relu(self.fc1(x)) x = nn.functional.relu(self.fc2(x)) x = self.fc3(x) return x net = Net() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) folder_path = 'random_matrices2' # 创建空的tensor x = torch.empty((40, 1, 42, 42)) # 遍历文件夹内的文件,将每个矩阵转化为tensor并存储 for j in range(40): for j in range(40): file_name = 'matrix_{}.npy'.format(j) file_path = os.path.join(folder_path, file_name) matrix = np.load(file_path) x[j] = torch.from_numpy(matrix).unsqueeze(0) #y = torch.cat((torch.zeros(20), torch.ones(20))) y = torch.cat((torch.zeros(20, dtype=torch.long), torch.ones(20, dtype=torch.long))) for epoch in range(10): running_loss = 0.0 for i in range(40): inputs = x[i] labels = y[i].unsqueeze(0) labels = nn.functional.one_hot(labels, num_classes=2) optimizer.zero_grad() outputs = net(inputs) #loss = criterion(outputs, labels) loss = criterion(outputs.unsqueeze(0), labels.float()) loss.backward() optimizer.step() running_loss += loss.item() print('[%d] loss: %.3f' % (epoch + 1, running_loss / 40)) print('Finished Training') 报错:RuntimeError: expected scalar type Long but found Float,怎么修改?

import sys,numpy as np from keras.datasets import mnist (x_train,y_train),(x_test,y_test)=mnist.load_data() images,labels=(x_train[0:1000].reshape(1000,28*28)/255,y_train[0:1000]) one_hot_labels=np.zeros((len(labels),10)) for i,l in enumerate(labels): one_hot_labels[i][l]=1 labels=one_hot_labels test_images=x_test.reshape(len(x_test),28*28)/255 test_labels=np.zeros((len(y_test),10)) for i,l in enumerate(y_test): test_labels[i][l]=1 np.random.seed(1) def relu(x): return (x>=0)*x #此函数将所有负数设为0 def relu2deriv(output): return output>=0 #当input>0时,返回1,否则返回0 alpha,iterations,hidden_size=(0.005,300,100) pixels_per_image,num_labels=(784,10) weights_0_1=0.2*np.random.random((pixels_per_image,hidden_size))-0.1 weights_1_2=0.2*np.random.random((hidden_size,num_labels))-0.1 for j in range(iterations): error,correct_cnt=(0.0,0) for i in range(len(images)): layer_0=images[i:i+1] layer_1=relu(np.dot(layer_0,weights_0_1)) dropout_mask=np.random.randint(2,size=layer_1.shape) layer_1*=dropout_mask*2 layer_2=np.dot(layer_1,weights_1_2) error+=np.sum((labels[i:i+1]-layer_2)**2) correct_cnt+=int(np.argmax(layer_2)==\ np.argmax(labels[i:i+1])) layer_2_delta=(labels[i:i+1]-layer_2) layer_1_delta=layer_2_delta.dot(weights_1_2.T)*relu2deriv(layer_1) layer_1_delta+=dropout_mask weights_1_2+=alpha*layer_1.T.dot(layer_2_delta) weights_0_1+=alpha*layer_0.T.dot(layer_1_delta) if (j%10==0): test_error=0.0 test_correct_cnt=0 for i in range(len(test_images)): layer_0=test_images[i:i+1] layer_1=relu(np.dot(layer_0,weights_0_1)) layer_2=np.dot(layer_1,weights_1_2) test_error+=np.sum((test_labels[i:i+1]-layer_2)**2) test_correct_cnt+=int(np.argmax(layer_2)==np.argmax(test_labels[i:i+1])) sys.stdout.write("\n"+\ "I:"+str(j)+\ " Test-Error:"+str(test_error/float(len(test_images)))[0:5] +\ " Test-Acc:"+str(test_correct_cnt/float(len(test_images)))+\ " Train-Err:"+str(error/float(len(images)))[0:5]+\ " Train-Acc:"+str(correct_cnt/float(len(images))))帮我看看哪里有问题

def nnCostFunction(nn_params,input_layer_size, hidden_layer_size, num_labels,X, y,Lambda): # Reshape nn_params back into the parameters Theta1 and Theta2 Theta1 = nn_params[:((input_layer_size+1) * hidden_layer_size)].reshape(hidden_layer_size,input_layer_size+1) Theta2 = nn_params[((input_layer_size +1)* hidden_layer_size ):].reshape(num_labels,hidden_layer_size+1) m = X.shape[0] J=0 X = np.hstack((np.ones((m,1)),X)) y10 = np.zeros((m,num_labels)) a1 = sigmoid(X @ Theta1.T) a1 = np.hstack((np.ones((m,1)), a1)) # hidden layer a2 = sigmoid(a1 @ Theta2.T) # output layer for i in range(1,num_labels+1): y10[:,i-1][:,np.newaxis] = np.where(y==i,1,0) for j in range(num_labels): J = J + sum(-y10[:,j] * np.log(a2[:,j]) - (1-y10[:,j])*np.log(1-a2[:,j])) cost = 1/m* J reg_J = cost + Lambda/(2*m) * (np.sum(Theta1[:,1:]**2) + np.sum(Theta2[:,1:]**2)) # Implement the backpropagation algorithm to compute the gradients grad1 = np.zeros((Theta1.shape)) grad2 = np.zeros((Theta2.shape)) for i in range(m): xi= X[i,:] # 1 X 401 a1i = a1[i,:] # 1 X 26 a2i =a2[i,:] # 1 X 10 d2 = a2i - y10[i,:] d1 = Theta2.T @ d2.T * sigmoidGradient(np.hstack((1,xi @ Theta1.T))) grad1= grad1 + d1[1:][:,np.newaxis] @ xi[:,np.newaxis].T grad2 = grad2 + d2.T[:,np.newaxis] @ a1i[:,np.newaxis].T grad1 = 1/m * grad1 grad2 = 1/m*grad2 grad1_reg = grad1 + (Lambda/m) * np.hstack((np.zeros((Theta1.shape[0],1)),Theta1[:,1:])) grad2_reg = grad2 + (Lambda/m) * np.hstack((np.zeros((Theta2.shape[0],1)),Theta2[:,1:])) return cost, grad1, grad2,reg_J, grad1_reg,grad2_reg

def cell_counter(image, min_area=20): """细胞计数""" # for s in image: df = pd.DataFrame() image =cv2.imread(image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) distance = ndi.distance_transform_edt(opening) coords = peak_local_max(distance, min_distance=9, footprint=np.ones((7, 7)), labels=opening) mask = np.zeros(distance.shape, dtype=bool) mask[tuple(coords.T)] = True markers, _ = ndi.label(mask) labels = watershed(-distance, markers, mask=opening, watershed_line=True) labels_area = [region.area for region in regionprops(labels) if region.area > min_area] cell_num = len(labels_area) print(cell_num) df = df.append(pd.DataFrame({(file_path,cell_num)}, index=[0]), ignore_index=True) print(df) # return cell_num # df.to_excel('1.xlsx', index=False) if __name__ == '__main__': path = r'D:\0531test' slide_path = os.listdir(path) # df =pd.DataFrame(slide_path) # df.to_excel('1.xlsx',index=False) for i in slide_path: slide_name = os.path.basename(i) #slide_name 样本名称 file_path = os.path.join(path,slide_name) images = os.listdir(file_path) f = glob.glob(os.path.join(file_path, '*.jpg')) for image in f: # print(s) # for s in images: # image_name = os.path.basename(s) # name = image_name.replace('.jpg','') # df = df.append(pd.DataFrame({(file_path,name[:-8])}, index=[0]), ignore_index=True) cell_counter(image) # df.to_excel('1.xlsx',index=False)

最新推荐

recommend-type

基于springboot+vue+MySQL实现的在线考试系统+源代码+文档

web期末作业设计网页 基于springboot+vue+MySQL实现的在线考试系统+源代码+文档
recommend-type

318_面向物联网机器视觉的目标跟踪方法设计与实现的详细信息-源码.zip

提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB柱状图在数据分析中的作用:从可视化到洞察

![MATLAB柱状图在数据分析中的作用:从可视化到洞察](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB柱状图概述** 柱状图是一种广泛用于数据可视化的图表类型,它使用垂直条形来表示数据中不同类别或组别的值。在MATLAB中,柱状图通过`bar`函数创建,该函数接受数据向量或矩阵作为输入,并生成相应的高度条形。 柱状图的优点在于其简单性和易于理解性。它们可以快速有效地传达数据分布和组别之间的比较。此外,MATLAB提供了广泛的定制选项,允许用户调整条形颜色、
recommend-type

软件工程每个学期的生活及学习目标

软件工程每个学期的生活及学习目标可能包括以下内容: 1. 学习软件开发的基本理论和实践知识,掌握常用的编程语言和开发工具。 2. 熟悉软件开发的流程和方法,了解软件工程的标准和规范。 3. 掌握软件需求分析、设计、开发、测试、部署和维护的技能,能够独立完成简单的软件开发任务。 4. 培养团队合作的能力,学会与他人进行有效的沟通和协作,共同完成软件开发项目。 5. 提高自己的计算机技术水平,了解最新的软件开发技术和趋势,积极参与开源社区和技术交流活动。 6. 注重学习方法和习惯的培养,养成良好的学习和生活习惯,保持健康的身心状态。 7. 积极参加校内外的实践活动和比赛,拓展自己的视