教你使用TensorFlow2判断细胞图像是否感染
在本教程中,我们将使用 TensorFlow (Keras API) 实现一个用于二进制分类任务的深度学习模型,该任务包括将细胞的图像标记为感染或未感染疟疾。
数据集来源:https://www.kaggle.com/iarunava/cell-images-for-detecting-malaria
数据集包含2个文件夹
感染::13780张图片 未感染:13780张图片
总共27558张图片。
此数据集取自NIH官方网站:https://ceb.nlm.nih.gov/repositories/malaria-datasets/
环境:kaggle,天池实验室或者gogole colab都可以。
导入相关模块
import cv2 import tensorflow教你 as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Activation from sklearn.model_selection import train_test_split import numpy as np import 使用;matplotlib.pyplot as plt import glob import os
对于图片数据存在形状不一样的情况,因此需要使用 OpenCV 进行图像预处理。
将图片变成 numpy 数组(数字格式)的形式转换为灰度,并将其调整为一个(70x70)形状。
img_dir="../input/cell-images-for-detecting-malaria/cell_images" img_size=70 def load_img_data(path): # 打乱数据 &nbsTep; image_files = glob.glob(os.path.join(path, "Parasitized/*.png")) + glob.glob(os.path.join(path, "Uninfected/*.png")) X, y = [], ns;[] for image_file in image_files: # 命名标签 0 for uninfected and 1 for infected label = 0 if "Uninfected" in image_file&ornbsp;else 1 # load the image in gray scale 变成灰度图片 img_arr = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE) # resize theFl image to (70x70) 调整图片大小 img_resized = cv2.resize(img_arr, (img_size, img_size)) X.append(img_resized) y.append(label)  ow; return X, y X, y = load_img_data(img_dir)
查看X的shape。
print(X.shape)
X的shape为(27558, 70, 70, 1),27558表示图片的数据,70*70表示图片的长和宽像素。
另外,为了帮助网络更快收敛,我们应该进行数据归一化。在sklearn 中有一些缩放方法,例如:
在这里我们将除以255,因为像素可以达到的最大值是255,这将导致应用缩放后像素范围在 0 和 1 之间。
X, y = load_img_data(img_dir) # reshape to&2判nbsp;(n_samples, 70, 70, 1) (to fit the NN) X = np.array(X).reshape(-1, img_size, img_size, 1) #从[0,255]到[0,1]缩放像素 帮助神经网络更快地训练 X = X / 255 # shuffle & split the dataset X_train, X_test, y断细_train, y_test = train_test_split(X, y, test_size=0.1, stratify=y) print("Total training samples:", X_train.shape) print("Total validation samples:", X_test.shape[0])
使用sklearn的train_test_split()方法将数据集划分为训练集和测试集,我们使用总数据的 10% 稍后对其进行验证。
在建立的模型中,我们将添加 3 个卷积层,然后Flatten是由层组成的全连接D胞图ense层。
model = Sequential() model.add(Conv2D(64, (3, 3), input_shape=X_train.shape[1:])) model.add(Activation("relu")) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation("relu")) model.add(MaxPool2D(pool_size=(2, 2))) 像是; model.add(Conv2D(64, (3, 3))) model.add(Activation("relu")) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation("relu")) model.add(Dense(64)) model.add(Activation("relu")) model.add(Dense(1)) model.add(A否感ctivation("sigmoid")) model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]) print(model.summary())
由于输出是二进制的(感染或未感染),我们使用Sigmoid 函数作为输出层的激活函数。
# train the model with 10 epochs, 64 batch size model.fit(X_train, np.array(y_tr教你ain), batch_size=64, epochs=10, validation_split=0.2)
在训练数据集及其验证拆分上实现了94%的准确率。
现在使用evaluate() 来评估测试数据集上的模型
loss, accuracy = model.evaluate(X_test, np.array(y_test), verbose=0) print(f"Testing on {len(X_test)} images, the results are Accuracy: {ac使用curacy} | Loss: {loss}")
输出如下
Testing on 2756 images, the results are Accuracy: 0.9404934644699097 | Loss: 0.1666732281446457
该模型在测试数据中也表现OK,准确率达到94%
最后,我们将通过保存我们的模型来结束所有这个过程。
model.save("model.h5")
-
英伟达发布全新小型模型 Nemotron-Nano-9B-V2:免费商用且性能卓越谷歌推出 Gemini3.1Pro 深度研究代理:支持 MCP 协议与多模态自主研究Facebook承诺未来3年将向新闻行业投资至少10亿美元标签: 豆包 Seed2.1 字节跳动 火山引擎昆仑万维AI 音乐创作平台Mureka上线Agent Studio功能,让音乐创作变得触手可及!标签: 滴滴 DiDi Bike 共享单车 海外业务马斯克与阿尔特曼的法庭对决:关于 OpenAI 创始使命的争论金山云Q1营收18.1亿元 其中公有云服务营收13.92亿元Meta 成立超级政治行动委员会 抗击 AI 监管政策标签: Last War Whiteout Survival MONOPOLY GO! Royal Match
- ·云安全日报210713:红帽XML序列化库发现远程命令执行攻击漏洞,需要尽快升级
- ·标签: 赵明 荣耀 复旦管院 AI手机
- ·标签: 董明珠 格力电器 AI替代焦虑 个税起征点
- ·Kubernetes组件问题排查的一些方法
- ·5分钟了解Docker原理之二,最简单的Cgroups介绍!
- ·标签: 智汇矿业 港交所上市 西藏矿业 矿业公司
- ·云计算开发:Python练习实例-综合画图
- ·标签: 天海电子 汽车线束 招商证券 国产品牌
- ·Facebook正研发一款适合13岁以下用户使用的Instagram
- ·标签: 东软集团 陈宏印 高级副总裁
- ·法国总统马克龙提出欧洲“数字主权”构想,建设欧洲云成为重点
- ·标签: 瑞银 瑞信 裁员 IT整合
- ·标签: 阿维塔 港交所上市 电动汽车 长安汽车
- ·标签: 海大国际 港交所上市 饲料业务 水产料
- ·韩国起亚和现代汽车称没有与苹果就电动汽车合作展开讨论
- ·标签: 苹果 iPhone 服务业务 蒂姆・库克
- ·https://www.southmeadows.co.uk/2019/07/?s=歌外推【tg:seogoogle987】,9hQ
- ·https://www.writingforward.com/?action=SEARCH&s=歌外推【tg:seogoogle987】,urE
- ·https://www.uctronics.com/catalogsearch/result/?q=歌外推【tg:seogoogle987】,hIB
- ·https://www.wuv.edu/?s=歌外推【tg:seogoogle987】,QVG
- ·https://www.teamlab.art/ko/?submit=歌外推【tg:seogoogle987】,b4O
- ·https://www.impactpool.org/search/?type=79&q=歌外推【tg:seogoogle987】A5V
- ·https://www.temu.com/search_result.html?search_key=歌外推【tg:seogoogle987】7Y3
- ·https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=歌外推【tg:seogoogle987】,3fx
- ·https://www.reginatangoshoes.com/en/catalogsearch/result/index/?T=1686132113407&q=歌外推【tg:seogoogle987】,1Tg
- ·https://www.roberthalf.com/us/en/jobs/all/歌外推【tg:seogoogle987】bHu
- ·火山引擎、阿里云、腾讯云联合发布“超低延时”直播技术标准
- ·一文搞懂HBase的基本原理
- ·飞猪AI“问一问”推出拍照讲解服务,随手一拍即享专业导览
- ·高通杀入数据中心!推AI200/AI250芯片剑指英伟达,股价单日暴涨20%
- ·苹果 FastVLM 上线:5分钟体验85倍速视觉 AI,数据永不出设备
- ·标签: 绿捷公司 光明食品 校园餐 张某华
- ·腾讯混元3D 3.0亮相:攻克人脸雕刻难题,实现真人手办自由
- ·谷歌推出 Gemini CLI GitHub Actions:免费、安全的 AI 集成助力开发者
- ·腾讯混元游戏视觉生成平台正式发布2.0版本
- ·标签: 美团 王兴 食杂零售 AI
