教你使用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): # 打乱数据 image_files = glob.glob(os.path.join(path, "Parasitized/*.png")) + glob.glob(os.path.join(path, "Uninfected/*.png")) X, y = [], [] for image_file in image_files: # 命名标签 0 for uninfected and 1 for infected label = 0 if "Uninfected" in image_file else 1 # load the image in gray scale 变成灰度图片 img_arr = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE) # resize the image to (70x70) 调整图片大小 img_resized = cv2.resize(img_arr, (img_size, img_size)) X.append(img_resized) y.append(label) 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_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是由层组成的全连接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))) 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(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), 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} | Loss: {loss}")
输出如下
Testing on 2756 images, the results are Accuracy: 0.9404934644699097 | Loss: 0.1666732281446457
该模型在测试数据中也表现OK,准确率达到94%
最后,我们将通过保存我们的模型来结束所有这个过程。
model.save("model.h5")
您可能也感兴趣:
官方微博/微信

每日头条、业界资讯、热点资讯、八卦爆料,全天跟踪微博播报。各种爆料、内幕、花边、资讯一网打尽。百万互联网粉丝互动参与,TechWeb官方微博期待您的关注。

想在手机上看科技资讯和科技八卦吗?
想第一时间看独家爆料和深度报道吗?
请关注TechWeb官方微信公众帐号:
1.用手机扫左侧二维码;
2.在添加朋友里,搜索关注TechWeb。
为您推荐
新款iPhone SE在日本市场需求强劲 但美国市场表现欠佳
外媒:三星SDI正为特斯拉4680电池准备试验线 未来或在马来西亚量产
艾迈斯欧司朗推出其首个纯集成电路生命体征传感器系列AS705x
领先台积电 三星电子已开始量产3纳米芯片
LG显示OLED面板扩张计划放缓 因受供应链限制
Snapchat应用程序付费版正式发布
快手与乐视视频展开深度合作 共享二次创作内容红利
吉利退出集度汽车?回应:百度和吉利所持股份不变
西门子和英伟达合作扩大数字服务 以Omniverse平台创造工业元宇宙
更多
- 全系自带“徕卡水印”展现百年美学风格 小米12S系列支持徕卡原生双画质
- 国内数字藏品行业再推自律发展倡议 蚂蚁、腾讯、百度、京东等联合发起
- 饿了么“免单1分钟”十天免95.6万单 商家增长消费激发“不止1分钟”
- 加速拓展第二曲线 中兴通讯力争2-3年进入世界500强
- 艾迈斯欧司朗推出其首个纯集成电路生命体征传感器系列AS705x
- 天猫App上线“数字藏品”专区 买盲盒送同款数字藏品
- 便利蜂启动第二届“717蜂享节” 超50款畅销品进入折扣季
- 快手与乐视视频展开深度合作 共享二次创作内容红利
- 辛巴辛有志开启“直播带岗” 助力多家企业共收超17万份简历
- 联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升