当前位置: 首页 > news >正文

好用的做网站的app中国建设教育培训中心官网

好用的做网站的app,中国建设教育培训中心官网,seo外包方案,网络直播营销方式基于WIN10的64位系统演示 一、写在前面 本期内容对等于机器学习二分类系列的误判病例分析#xff08;传送门#xff09;。既然前面的数据可以这么分析#xff0c;那么图形识别自然也可以。 本期以mobilenet_v2模型为例#xff0c;因为它建模速度快。 同样#xff0c;基…基于WIN10的64位系统演示 一、写在前面 本期内容对等于机器学习二分类系列的误判病例分析传送门。既然前面的数据可以这么分析那么图形识别自然也可以。 本期以mobilenet_v2模型为例因为它建模速度快。 同样基于GPT-4辅助编程后续会分享改写过程。 二、误判病例分析实战 继续使用胸片的数据集肺结核病人和健康人的胸片的识别。其中肺结核病人700张健康人900张分别存入单独的文件夹中。 a直接分享代码 ######################################导入包################################### from tensorflow import keras import tensorflow as tf from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout, Activation, Reshape, Softmax, GlobalAveragePooling2D, BatchNormalization from tensorflow.python.keras.layers.convolutional import Convolution2D, MaxPooling2D from tensorflow.python.keras import Sequential from tensorflow.python.keras import Model from tensorflow.python.keras.optimizers import adam_v2 import numpy as np import matplotlib.pyplot as plt from tensorflow.python.keras.preprocessing.image import ImageDataGenerator, image_dataset_from_directory from tensorflow.python.keras.layers.preprocessing.image_preprocessing import RandomFlip, RandomRotation, RandomContrast, RandomZoom, RandomTranslation import os,PIL,pathlib import warnings #设置GPU gpus tf.config.list_physical_devices(GPU)warnings.filterwarnings(ignore) #忽略警告信息 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号################################导入数据集##################################### data_dir ./MTB data_dir pathlib.Path(data_dir) image_count len(list(data_dir.glob(*/*))) print(图片总数为, image_count)batch_size 32 img_height 100 img_width 100# 创建一个数据集其中包含所有图像的路径。 list_ds tf.data.Dataset.list_files(str(data_dir/*/*), shuffleTrue) # 切分为训练集和验证集 val_size int(image_count * 0.2) train_ds list_ds.skip(val_size) val_ds list_ds.take(val_size)class_names np.array(sorted([item.name for item in data_dir.glob(*) if item.name ! LICENSE.txt])) print(class_names)def get_label(file_path):parts tf.strings.split(file_path, os.path.sep)one_hot parts[-2] class_namesreturn tf.argmax(one_hot)def decode_img(img):img tf.image.decode_jpeg(img, channels3)img tf.image.resize(img, [img_height, img_width])img img / 255.0 # normalize to [0,1] rangereturn imgdef process_path_with_filename(file_path):label get_label(file_path)img tf.io.read_file(file_path)img decode_img(img)return img, label, file_pathAUTOTUNE tf.data.AUTOTUNE# 在此处对train_ds和val_ds进行图像处理包括添加文件名信息 train_ds_with_filenames train_ds.map(process_path_with_filename, num_parallel_callsAUTOTUNE) val_ds_with_filenames val_ds.map(process_path_with_filename, num_parallel_callsAUTOTUNE)# 对训练数据集进行批处理和预加载 train_ds_with_filenames train_ds_with_filenames.batch(batch_size) train_ds_with_filenames train_ds_with_filenames.prefetch(buffer_sizeAUTOTUNE)# 对验证数据集进行批处理和预加载 val_ds_with_filenames val_ds_with_filenames.batch(batch_size) val_ds_with_filenames val_ds_with_filenames.prefetch(buffer_sizeAUTOTUNE)# 在进行模型训练时不需要文件名信息所以在此处移除 train_ds train_ds_with_filenames.map(lambda x, y, z: (x, y)) val_ds val_ds_with_filenames.map(lambda x, y, z: (x, y))for image, label, path in train_ds_with_filenames.take(1):print(Image shape: , image.numpy().shape)print(Label: , label.numpy())print(Path: , path.numpy())train_ds train_ds.prefetch(buffer_sizeAUTOTUNE) val_ds val_ds.prefetch(buffer_sizeAUTOTUNE)plt.figure(figsize(10, 8)) # 图形的宽为10高为5 plt.suptitle(数据展示)for images, labels, paths in train_ds_with_filenames.take(1):for i in range(15):plt.subplot(4, 5, i 1)plt.xticks([])plt.yticks([])plt.grid(False)# 显示图片plt.imshow(images[i])# 显示标签plt.xlabel(class_names[labels[i]])plt.show()######################################数据增强函数################################data_augmentation Sequential([RandomFlip(horizontal_and_vertical),RandomRotation(0.2),RandomContrast(1.0),RandomZoom(0.5, 0.2),RandomTranslation(0.3, 0.5), ])def prepare(ds, augmentFalse):ds ds.map(lambda x, y, z: (data_augmentation(x, trainingTrue), y, z) if augment else (x, y, z), num_parallel_callsAUTOTUNE)return dstrain_ds_with_filenames prepare(train_ds_with_filenames, augmentTrue)# 在进行模型训练时不需要文件名信息所以在此处移除 train_ds train_ds_with_filenames.map(lambda x, y, z: (x, y)) val_ds val_ds_with_filenames.map(lambda x, y, z: (x, y))train_ds train_ds.prefetch(buffer_sizeAUTOTUNE) val_ds val_ds.prefetch(buffer_sizeAUTOTUNE)###############################导入mobilenet_v2################################ #获取预训练模型对输入的预处理方法 from tensorflow.python.keras.applications import mobilenet_v2 from tensorflow.python.keras import Input, regularizers IMG_SIZE (img_height, img_width, 3)base_model mobilenet_v2.MobileNetV2(input_shapeIMG_SIZE, include_topFalse, #是否包含顶层的全连接层weightsimagenet)inputs Input(shapeIMG_SIZE) #模型 x base_model(inputs, trainingFalse) #参数不变化 #全局池化 x GlobalAveragePooling2D()(x) #BatchNormalization x BatchNormalization()(x) #Dropout x Dropout(0.8)(x) #Dense x Dense(128, kernel_regularizerregularizers.l2(0.1))(x) # 全连接层减少到128添加 L2 正则化 #BatchNormalization x BatchNormalization()(x) #激活函数 x Activation(relu)(x) #输出层 outputs Dense(2, kernel_regularizerregularizers.l2(0.1))(x) # 添加 L2 正则化 #BatchNormalization outputs BatchNormalization()(outputs) #激活函数 outputs Activation(sigmoid)(outputs) #整体封装 model Model(inputs, outputs) #打印模型结构 print(model.summary()) #############################编译模型######################################### #定义优化器 from tensorflow.python.keras.optimizers import adam_v2, rmsprop_v2optimizer adam_v2.Adam()#编译模型 model.compile(optimizeroptimizer,losssparse_categorical_crossentropy,metrics[accuracy])#训练模型 from tensorflow.python.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping, ReduceLROnPlateau, LearningRateSchedulerNO_EPOCHS 5 PATIENCE 10 VERBOSE 1# 设置动态学习率 annealer LearningRateScheduler(lambda x: 1e-5 * 0.99 ** (xNO_EPOCHS))# 设置早停 earlystopper EarlyStopping(monitorloss, patiencePATIENCE, verboseVERBOSE)# checkpointer ModelCheckpoint(mtb_jet_best_model_mobilenetv3samll-1.h5,monitorval_accuracy,verboseVERBOSE,save_best_onlyTrue,save_weights_onlyTrue)train_model model.fit(train_ds,epochsNO_EPOCHS,verbose1,validation_dataval_ds,callbacks[earlystopper, checkpointer, annealer])#保存模型 model.save(mtb_jet_best_model_mobilenet-1.h5) print(The trained model has been saved.)###########################误判病例分析################################## 训练模型后现在使用模型对所有图片进行预测并保存预测结果到csv文件中 import pandas as pd# 保存预测结果的dataframe result_df pd.DataFrame(columns[原始图片的名称, 属于训练集还是验证集, 预测为Tuberculosis的概率值, 判定的组别])# 对训练集和验证集中的每一批图片进行预测 for dataset, dataset_name in zip([train_ds_with_filenames, val_ds_with_filenames], [训练集, 验证集]):for images, labels, paths in dataset:# 使用模型对这一批图片进行预测probabilities model.predict(images)predictions tf.math.argmax(probabilities, axis-1)# 遍历这一批图片for path, label, prediction, probability in zip(paths, labels, predictions, probabilities):# 获取图片名称和真实标签image_name path.numpy().decode(utf-8).split(/)[-1]original_label class_names[label]# 根据预测结果和真实标签判定图片所属的组别group Noneif original_label Tuberculosis and probability[1] 0.5:group Aelif original_label Normal and probability[1] 0.5:group Belif original_label Normal and probability[1] 0.5:group Celif original_label Tuberculosis and probability[1] 0.5:group D# 将结果添加到dataframe中result_df result_df.append({原始图片的名称: image_name,属于训练集还是验证集: dataset_name,预测为Tuberculosis的概率值: probability[1],判定的组别: group}, ignore_indexTrue)# 保存结果到csv文件 result_df.to_csv(result.csv, indexFalse) 相比于之前的代码主要有两处变化 导入数据集部分由之前的“image_dataset_from_directory”改成“tf.data.Dataset”因为前者不能保存图片的原始路径和名称。而在误判病例分析中我们需要知道每一张图片的名称、被预测的结果等详细信息。误判病例分析部分也就是需要知道哪些预测正确哪些预测错误。 b调教GPT-4的过程 b1导入数据集部分“image_dataset_from_directory”改成“tf.data.Dataset” 让GPT-4帮你改写即可自行尝试 ################################导入数据集##################################### data_dir ./MTB data_dir pathlib.Path(data_dir) image_count len(list(data_dir.glob(*/*))) print(图片总数为, image_count)batch_size 32 img_height 100 img_width 100# 创建一个数据集其中包含所有图像的路径。 list_ds tf.data.Dataset.list_files(str(data_dir/*/*), shuffleTrue) # 切分为训练集和验证集 val_size int(image_count * 0.2) train_ds list_ds.skip(val_size) val_ds list_ds.take(val_size)class_names np.array(sorted([item.name for item in data_dir.glob(*) if item.name ! LICENSE.txt])) print(class_names)def get_label(file_path):parts tf.strings.split(file_path, os.path.sep)one_hot parts[-2] class_namesreturn tf.argmax(one_hot)def decode_img(img):img tf.image.decode_jpeg(img, channels3)img tf.image.resize(img, [img_height, img_width])img img / 255.0 # normalize to [0,1] rangereturn imgdef process_path_with_filename(file_path):label get_label(file_path)img tf.io.read_file(file_path)img decode_img(img)return img, label, file_pathAUTOTUNE tf.data.AUTOTUNE# 在此处对train_ds和val_ds进行图像处理包括添加文件名信息 train_ds_with_filenames train_ds.map(process_path_with_filename, num_parallel_callsAUTOTUNE) val_ds_with_filenames val_ds.map(process_path_with_filename, num_parallel_callsAUTOTUNE)# 对训练数据集进行批处理和预加载 train_ds_with_filenames train_ds_with_filenames.batch(batch_size) train_ds_with_filenames train_ds_with_filenames.prefetch(buffer_sizeAUTOTUNE)# 对验证数据集进行批处理和预加载 val_ds_with_filenames val_ds_with_filenames.batch(batch_size) val_ds_with_filenames val_ds_with_filenames.prefetch(buffer_sizeAUTOTUNE)# 在进行模型训练时不需要文件名信息所以在此处移除 train_ds train_ds_with_filenames.map(lambda x, y, z: (x, y)) val_ds val_ds_with_filenames.map(lambda x, y, z: (x, y))for image, label, path in train_ds_with_filenames.take(1):print(Image shape: , image.numpy().shape)print(Label: , label.numpy())print(Path: , path.numpy())train_ds train_ds.prefetch(buffer_sizeAUTOTUNE) val_ds val_ds.prefetch(buffer_sizeAUTOTUNE)plt.figure(figsize(10, 8)) # 图形的宽为10高为5 plt.suptitle(数据展示)for images, labels, paths in train_ds_with_filenames.take(1):for i in range(15):plt.subplot(4, 5, i 1)plt.xticks([])plt.yticks([])plt.grid(False)# 显示图片plt.imshow(images[i])# 显示标签plt.xlabel(class_names[labels[i]])plt.show()######################################数据增强函数################################data_augmentation Sequential([RandomFlip(horizontal_and_vertical),RandomRotation(0.2),RandomContrast(1.0),RandomZoom(0.5, 0.2),RandomTranslation(0.3, 0.5), ])def prepare(ds, augmentFalse):ds ds.map(lambda x, y, z: (data_augmentation(x, trainingTrue), y, z) if augment else (x, y, z), num_parallel_callsAUTOTUNE)return dstrain_ds_with_filenames prepare(train_ds_with_filenames, augmentTrue)# 在进行模型训练时不需要文件名信息所以在此处移除 train_ds train_ds_with_filenames.map(lambda x, y, z: (x, y)) val_ds val_ds_with_filenames.map(lambda x, y, z: (x, y))train_ds train_ds.prefetch(buffer_sizeAUTOTUNE) val_ds val_ds.prefetch(buffer_sizeAUTOTUNE) 代码解读 数据导入代码首先指定图像数据的存放路径data_dir并统计路径下的图像总数image_count。之后它将图片的高度和宽度分别设定为100并且指定批量处理的大小为32。 数据集创建使用tf.data.Dataset.list_files创建一个包含所有图像路径的数据集并通过洗牌(shuffle)打乱数据。然后将20%的数据作为验证集其余的作为训练集。 类别标签获取根据文件夹名称作为类别标签其中去除了名为LICENSE.txt的文件。 图像和标签处理定义了几个函数来处理图像和标签。get_label函数通过切割文件路径获取类别标签并转为one-hot编码decode_img函数将图像解码并调整大小且将像素值归一化到[0,1]范围内process_path_with_filename函数则通过调用前两个函数处理图像和标签。 并行处理和预加载使用tf.data.AUTOTUNE对训练集和验证集进行并行处理和预加载提高数据读取效率。 数据展示展示了部分训练数据的图片、类别和文件路径帮助我们对数据有个初步了解。 数据增强定义了一个数据增强管道其中包含了随机翻转、旋转、对比度调整、缩放和平移等操作。然后在训练集上应用这个数据增强管道。 b2误判病例分析部分 咒语在{代码1}的基础上续写代码达到下面要求 1首先提取出所有图片的“原始图片的名称”、“属于训练集还是验证集”、“预测为Tuberculosis的概率值”文件的路劲格式为例如“MTB\Normal\Normal-690.png”属于Normal也就是0标签“MTB\Tuberculosis\Tuberculosis-680.png”属于Tuberculosis也就是1标签 2其次由于模型以0.5为阈值因此可以样本分为三份a本来就是Tuberculosis的图片预测为Tuberculosis的概率值大于等于0.5则说明预测正确判定为A组b本来就是Normal的图片预测为Tuberculosis的概率值小于0.5则说明预测正确判定为B组c本来就是Normal的图片预测为Tuberculosis的概率值大于等于0.5则说明预测错误判定为C组d本来就是Tuberculosis的图片预测为Tuberculosis的概率值小于0.5则说明预测正确判定为D组 3居于以上计算的结果生成一个名为result.csv表格文件。列名分别为“原始图片的名称”、“属于训练集还是验证集”、“预测为Tuberculosis的概率值”、“判定的组别”。其中“原始图片的名称”为所有1600张图片的图片名称“属于训练集还是验证集”为这个图片属于训练集还是验证集“预测为Tuberculosis的概率值”为模型预测该样本是Tuberculosis的概率值“判定的组别”为根据步骤3判定的组别A、B、C和D四组。 4需要把所有的图片都进行上面操作注意是所有图片而不只是一个批次的图片。 代码1为{XXXX} 主要是把需求写清楚即可代码及其解读 # 训练模型后现在使用模型对所有图片进行预测并保存预测结果到csv文件中 import pandas as pd# 保存预测结果的dataframe result_df pd.DataFrame(columns[原始图片的名称, 属于训练集还是验证集, 预测为Tuberculosis的概率值, 判定的组别])# 对训练集和验证集中的每一批图片进行预测 for dataset, dataset_name in zip([train_ds_with_filenames, val_ds_with_filenames], [训练集, 验证集]):for images, labels, paths in dataset:# 使用模型对这一批图片进行预测probabilities model.predict(images)predictions tf.math.argmax(probabilities, axis-1)# 遍历这一批图片for path, label, prediction, probability in zip(paths, labels, predictions, probabilities):# 获取图片名称和真实标签image_name path.numpy().decode(utf-8).split(/)[-1]original_label class_names[label]# 根据预测结果和真实标签判定图片所属的组别group Noneif original_label Tuberculosis and probability[1] 0.5:group Aelif original_label Normal and probability[1] 0.5:group Belif original_label Normal and probability[1] 0.5:group Celif original_label Tuberculosis and probability[1] 0.5:group D# 将结果添加到dataframe中result_df result_df.append({原始图片的名称: image_name,属于训练集还是验证集: dataset_name,预测为Tuberculosis的概率值: probability[1],判定的组别: group}, ignore_indexTrue)# 保存结果到csv文件 result_df.to_csv(result.csv, indexFalse) 代码解读 首先代码创建了一个名为result_df的pandas DataFrame用于存储预测结果。这个DataFrame有四个列分别为原始图片的名称、属于训练集还是验证集、预测为Tuberculosis的概率值、判定的组别。 然后代码开始遍历训练集和验证集中的每一批图片。对于每一批图片首先使用训练好的模型进行预测得到每个样本属于每个类别的概率值probabilities。然后通过取概率值最大的类别作为预测结果predictions。 在对一批图片进行预测后代码遍历这批图片。对于每个图片代码首先从图片路径中获取图片名称image_name并从标签中获取图片的真实标签original_label。 然后代码根据预测结果和真实标签判断图片属于哪个组别A、B、C、D。这里假设Tuberculosis和Normal是所有类别中的两个类别而且概率值中的第二个元素表示的是预测为Tuberculosis的概率。具体的判断规则如下 --如果真实标签是Tuberculosis且预测为Tuberculosis的概率值大于等于0.5则图片属于组别A。 --如果真实标签是Normal且预测为Tuberculosis的概率值小于0.5则图片属于组别B。 --如果真实标签是Normal且预测为Tuberculosis的概率值大于等于0.5则图片属于组别C。 --如果真实标签是Tuberculosis且预测为Tuberculosis的概率值小于0.5则图片属于组别D。 最后代码将预测结果添加到result_df中其中包含图片名称、数据集名称训练集或验证集、预测为Tuberculosis的概率值以及判定的组别。当所有图片都进行完预测后将result_df保存为一个CSV文件。 三、输出结果 有了这个表又可以水不少图了。 四、数据 链接https://pan.baidu.com/s/15vSVhz1rQBtqNkNp2GQyVw?pwdx3jf 提取码x3jf
http://www.lakalapos1.cn/news/61004/

相关文章:

  • 建一个网站流程安徽科技学院官网
  • 做网站的得多少钱ssl正式申请后wordpress
  • 建站系统平台怎么做课题组网站
  • 张家港网站制作服务制作网页最简单的方法
  • 网站建设 视频教程佛山营销网站建设服务公司
  • 谷歌网站提交阿里wordpress怎么安装教程
  • 郑州做优惠券网站的公司谷歌浏览器下载手机版安卓
  • 网站开发的方法和步骤莱芜金点子广告电子版2024
  • dede可以做购物网站吗东莞网站建设实例分析
  • 株洲营销网站建设上海官网seo
  • 网站首页建设网站怎样给建设的网站提意见
  • 重庆专业网站建设成都市建设工程施工安监站网站
  • 课程设计做淘宝网站的目的有赞微商城登录
  • 网站建设与小程序开发熊掌号建设网站是什么意思
  • 延庆网站建设优化seo网站建设背景怎么写
  • 哈尔滨网站建设制作哪家好网站开发平台论文
  • 网站策划主要做什么工作舆情网站
  • 医疗器械网站建设策划书将自己做的网站发布到
  • 上海的招聘网站有哪些店面设计有哪些
  • 有个做图片mv的网站56泉州网站seo公司
  • 十佳网站设计怎么可以建网站
  • 搜索引擎网站录入网站建设排行公司
  • 德吉机械东莞网站建设网页建设方案
  • 做网站和做推广的区别怎么做网站流量
  • 郑州网站改版升级旅游网站开发的结论
  • 申请网站建设费用的请示工作室网站设计
  • 营销型品牌网站建设价格手机端怎么变成电脑端
  • 网站的建设和推广自己制作logo免费 生成器
  • 呼和浩特网站运营公司seo入门教程seo入门
  • 东营网站推广公司网页设计公司概念