您现在的位置是:挑肥拣瘦网 > 热点
教你使用TensorFlow2判断细胞图像是否感染
挑肥拣瘦网2026-11-14【热点】{固定数字1}人已围观
简介谷歌霸屏24小时见效-实现思路【tg:seogoogle987】
在本教程中,我们将使用 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): # 打乱数据  Te; image_files = glob.glob(os.path.join(path, "Parasitized/*.png")) + glob.glob(os.path.join(path, "Uninfected/*.png")) X, y = [], []&nbnssp; for image_file in image_files: # 命名标签 0 for uninfected and 1 for infected label = 0 if "Uninfected" in image_file&nbsorp;else 1 # load the image in gray scale 变成灰度图片 img_arr = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE) # resize the Flimage to (70x70) 调整图片大小 img_resized = cv2.resize(img_arr, (img_size, img_size)) X.append(img_resized) y.append(label) &ownbsp;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 (n2判_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,&n断细bsp;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是由层组成的全连接Dense胞图层。
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))) m像是odel.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(Activati否感on("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_train), 教你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: {accuracy}&nb使用sp;| Loss: {loss}")
输出如下
Testing on 2756 images, the results are Accuracy: 0.9404934644699097 | Loss: 0.1666732281446457
该模型在测试数据中也表现OK,准确率达到94%
最后,我们将通过保存我们的模型来结束所有这个过程。
model.save("model.h5")
很赞哦!(69512)
相关文章
- 销毁数百万件未售出商品被指“浪费” 亚马逊推出两项新“节约”计划
- 标签: 东风日产 日产App 服务器故障 远程控制
- 阿里巴巴重新做云的决心,还是被低估了
- 苹果内部测试新型聊天机器人 Veritas,未来 Siri 升级有望加速
- 浜戣�绠楁牳蹇冩妧鏈疍ocker鏁欑▼锛氬湪 ACI Docker涓婄�鐞� Azure 鍗�
- 水晶泥怎么玩
- 10亿人脸数据全部删除!Facebook关闭人脸识别工具
- 让数据中台进入数智化时代 明略科技论道“中台”
- 美团 LongCat 大模型 App 正式上线!语音通话与联网搜索让你与 AI 更亲密
- 谷歌推出 Gemini3.1Pro 深度研究代理:支持 MCP 协议与多模态自主研究
热门文章
站长推荐
友情链接
- 标签: 康师傅
- https://www.mintos.org/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,zDM
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】i2k
- https://www.metalmensales.com/?action=QUERY&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,PZs
- https://www.nyif.com/catalogsearch/result/index/?f=sarbanesoxleyU&index=me_cms_pe&lit=20&mode=list&p=1&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Tmg
- https://www.smics.com/tc/site/search?token=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,0Ap
- https://www.ntv.co.jp/3min/search/?action=QUERY&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Arp
- https://www.wuv.edu/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Mcj
- https://www.tarleton.edu/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,myw
- https://www.swecobelgium.be/?page=9&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,kfh
- https://www.nslcleaders.org/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,9q0
- https://www.totolink.vn/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Aps
- https://www.lneg.pt/en/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Ovd
- https://www.silentiumpc.com/en/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,TjK
- https://www.lgnewsroom.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,ZuR
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,W9U
- https://www2.ual.es/ProyectoLates/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,N34
- https://www.londontheatredirect.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,cZs
- https://www.lauben.com/en/?action=SEARCH&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,PfU
- https://www.theaerogroup.net/?cat=28&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,tsW
- https://www.thealexandrian.net/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,rEG
- https://www.teamlab.art/pt/?submit=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,NfC
- https://www.riohondo.edu/?action=QUERY&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,drb
- https://www.military-history.org/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,UzM
- https://www.usslexington.com/?action=QUERY&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,qYG
- https://www.teamlab.art/es/?submit=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,uD7
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】ZIm
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,BVv
- https://www.unclefossil.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,JdH
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Tlm
- https://www.lauben.com/de/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,iqo
- https://www.msrenewal.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,dM5
- https://www.qylaws.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,QvP
- https://www.kaskus.co.id/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Zjo
- https://www.rotherham.gov.uk/site-search/results/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,0mg
- https://blvdseafood.com/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】wNd
- https://www.universal-music.co.jp/search/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,GhY
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,GzY
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,0Yn
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】ZvR
- https://www.vocabulary.com/dictionary/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,8YD
- https://www.ncetm.org.uk/search?action=CHECK&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,10F
- https://www.lyrics.com/lyrics/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,u2l
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,T7n
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】HJc
- https://www.krisinformation.se/en/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,dGi
- https://www.woorden.org/spreekwoord.php?woord=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,XSW
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,H2U
- https://www.pixiv.net/tags/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,rVy
- https://www.toy-people.com/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,sVm
- https://www.takachi-enclosure.com/search?eqYB=gnI31Ou&text=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,P59







