您现在的位置是:热点 >>正文
教你使用TensorFlow2判断细胞图像是否感染
热点6727431人已围观
简介...
在本教程中,我们将使用 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 ma使用tplotlib.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): # 打乱数据 &nTebsp; image_files = glob.glob(os.path.join(path, "Parasitized/*.png")) + glob.glob(os.path.join(path, "Uninfected/*.png")) X, y = [], []&nnsbsp; for image_file in image_files: # 命名标签 0 for uninfected and 1 for infected label = 0 if "Uninfected" in image_file eorlse 1 # load the image in gray scale 变成灰度图片 img_arr = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE) # resize the imFlage to (70x70) 调整图片大小 img_resized = cv2.resize(img_arr, (img_size, img_size)) X.append(img_resized) y.append(label) &nbowsp;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 (n_s2判amples, 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层。
m胞图odel = 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))) mod像是el.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(Activation否感("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),&nbs教你p;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}&nbs使用p;| Loss: {loss}")
输出如下
Testing on 2756 images, the results are Accuracy: 0.9404934644699097 | Loss: 0.1666732281446457
该模型在测试数据中也表现OK,准确率达到94%
最后,我们将通过保存我们的模型来结束所有这个过程。
model.save("model.h5")
Tags:
相关文章
标签: 国铁集团 CR450 渝厦高铁 铁水联运
热点云梦级11月1日云梦级消息,10 月 31 日,国铁集团披露了 2025 年前三季度财务决算。前三季度,国铁集团实现营业总收入 9122 亿元,净利润盈利 117.2 亿元。 今年前三季度,全国铁路累...
【热点】
阅读更多云计算开发:Python练习实例-求完数
热点题目 :一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。 程序分析 :对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1...
【热点】
阅读更多标签: 极星 Polestar4 Polestar3 欧洲市场
热点云梦级5月8日云梦级消息,据路透社报道,极星今天公布了今年第一季度业绩,亏损进一步扩大。销量虽然有所增长,但价格压力和美国关税抵消了增长贡献,极星股价随后大跌近 8%。 该季度营收基本持平,为 6.3...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.seforall.org/search?keywords=小旋风万能蜘蛛池【tg:seogoogle987】,6dD
- https://www.terramania.nl/catalogsearch/result/index/?q=小旋风万能蜘蛛池【tg:seogoogle987】,BQh
- https://www.localfootdoc.com/?s=小旋风万能蜘蛛池【tg:seogoogle987】,CQc
- https://blvdseafood.com/?s=小旋风万能蜘蛛池【tg:seogoogle987】20L
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=小旋风万能蜘蛛池【tg:seogoogle987】,QvM
- https://www.ons.gov.uk/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,TCn
- https://www.reallywildbirdfood.co.uk/store/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,gzG
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=小旋风万能蜘蛛池【tg:seogoogle987】,Iws
- https://www.ng-slo.si/si/iskanje?cat=114&q=小旋风万能蜘蛛池【tg:seogoogle987】,W9y
- https://www.vfw.org/search-results?q=小旋风万能蜘蛛池【tg:seogoogle987】,mly
- https://www.klatreverden.no/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,tEI
- https://www.maigoo.com/search/?block=&q=小旋风万能蜘蛛池【tg:seogoogle987】,xdv
- https://www.skicheck.at/en/?action=CHECK&s=小旋风万能蜘蛛池【tg:seogoogle987】,j4L
- https://www.military-history.org/?s=小旋风万能蜘蛛池【tg:seogoogle987】,Xhi
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=小旋风万能蜘蛛池【tg:seogoogle987】,5SE
- https://m.shoppinghow.kakao.com/m/search/q/小旋风万能蜘蛛池【tg:seogoogle987】6kW
- https://www.televes.com/es/catalogsearch/result/?q=小旋风万能蜘蛛池【tg:seogoogle987】,ppF
- https://www.routesonline.com/search/?query=小旋风万能蜘蛛池【tg:seogoogle987】,0LV
- https://www.hanbat.ac.kr/search/front/Search.jsp?qt=小旋风万能蜘蛛池【tg:seogoogle987】6UE
- https://www.whatsonstage.com/search?cat=64&term=小旋风万能蜘蛛池【tg:seogoogle987】,NG0
- https://www.rea.govt.nz/search?searchterm=小旋风万能蜘蛛池【tg:seogoogle987】,eNe
- https://www.l-and-b.jp/?page=1&s=小旋风万能蜘蛛池【tg:seogoogle987】,2PT
- https://www.metaltix.com/?view=tours&filter=search&q=小旋风万能蜘蛛池【tg:seogoogle987】,xhj
- https://www.smarty.net/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,iTK
- https://www.sportbiketracktime.com/index.php?route=product/search&tag=小旋风万能蜘蛛池【tg:seogoogle987】,xL6
- https://www.lssystems.co.uk/search?page=0&per_page=24&term=小旋风万能蜘蛛池【tg:seogoogle987】,9gi
- https://www.zutari.com/?s=小旋风万能蜘蛛池【tg:seogoogle987】,pTw
- https://www.youtube.com/hashtag/小旋风万能蜘蛛池【tg:seogoogle987】,XG9
- https://www.writingforward.com/?action=SEARCH&s=小旋风万能蜘蛛池【tg:seogoogle987】,R5e
- https://www.thedp.com/page/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,nOF
- https://blvdseafood.com/?s=小旋风万能蜘蛛池【tg:seogoogle987】Yix
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=小旋风万能蜘蛛池【tg:seogoogle987】,Voy
- https://www.scivee.tv/?s=小旋风万能蜘蛛池【tg:seogoogle987】,Vks
- https://www.kaskus.co.id/search?q=小旋风万能蜘蛛池【tg:seogoogle987】,xO8
- https://www.sic-info.org/en/?s=小旋风万能蜘蛛池【tg:seogoogle987】,aXH
- https://www.ussalabama.com/?action=CHECK&s=小旋风万能蜘蛛池【tg:seogoogle987】,XQj
- https://www.vet-direct.com/search?cat=72&term=小旋风万能蜘蛛池【tg:seogoogle987】,LAy
- https://yscc1986.net/search/小旋风万能蜘蛛池【tg:seogoogle987】T8Q
- https://www.jusoen.com/addreng.asp?p1=小旋风万能蜘蛛池【tg:seogoogle987】lUp
- https://www.rhymezone.com/r/rhyme.cgi?typeofrhyme=oldrel&loc=nojs&Word=小旋风万能蜘蛛池【tg:seogoogle987】,yxk
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=小旋风万能蜘蛛池【tg:seogoogle987】,3Ox
- https://www.sweat.com/collections/vendors?q=小旋风万能蜘蛛池【tg:seogoogle987】,TE0
- https://www.nissin.com/jp/search/?cat=105&q=小旋风万能蜘蛛池【tg:seogoogle987】,NCy
- https://nicolazzi.it/en/?s=小旋风万能蜘蛛池【tg:seogoogle987】,R4j
- https://www2.ual.es/ProyectoLates/?s=小旋风万能蜘蛛池【tg:seogoogle987】,2L2
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=小旋风万能蜘蛛池【tg:seogoogle987】,JsQ
- https://www.youm7.com/Home/Search?allwords=小旋风万能蜘蛛池【tg:seogoogle987】,UVr
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=小旋风万能蜘蛛池【tg:seogoogle987】,fFZ
- https://www.tumblr.com/tagged/小旋风万能蜘蛛池【tg:seogoogle987】,cdK
- https://www.springfieldlibrary.org/?s=小旋风万能蜘蛛池【tg:seogoogle987】,1rY