透過細胞影像的標籤對模型表現的影響,為資料設定優先權和權重。
許多機器學習任務的主要障礙之一是缺乏標記資料。而標記資料可能會耗費很長的時間,而且很昂貴,因此很多時候嘗試使用機器學習方法來解決問題是不合理的。
為了解決這個問題,機器學習領域出現了一個叫做主動學習的領域。主動學習是機器學習中的一種方法,它提供了一個框架,根據模型已經看到的標記資料對未標記的資料樣本進行優先排序。如果想
細胞成像的分割和分類等技術是一個快速發展的領域研究。就像在其他機器學習領域一樣,數據的標註是非常昂貴的,而且對於數據標註的品質要求也非常的高。針對這個問題,本篇文章介紹一種對紅血球和白血球影像分類任務的主動學習端到端工作流程。
我們的目標是將生物學和主動學習的結合,並幫助其他人使用主動學習方法解決生物學領域中類似的和更複雜的任務。
本篇文章主要由三個部分組成:
- 細胞影像預處理-這裡將介紹如何預處理未分割的血球影像。
- 使用CellProfiler提取細胞特徵-展示如何從生物細胞照片影像中提取形態學特徵,以用作機器學習模型的特徵。
- 使用主動學習-展示一個模擬使用主動學習和不使用主動學習的對比實驗。
細胞影像預處理
我們將使用在MIT許可的血球影像資料集(GitHub和Kaggle)。每張圖片都根據紅血球(RBC)和白血球(WBC)分類進行標記。對於這4種白血球(嗜酸性球、淋巴球、單核球和嗜中性球)還有附加的標籤,但在本文的研究中並沒有使用這些標籤。
下面是一個來自資料集的全尺寸原始影像的範例:
#建立樣本DF
原始資料集包含一個export .py腳本,它將XML註解解析為一個CSV表,其中包含每個細胞的檔案名稱、細胞類型標籤和邊界框。
原始腳本沒有包含cell_id列,但我們要對單一細胞進行分類,所以我們稍微修改了程式碼,新增了該列並新增了一列包括image_id和cell_id的filename列:
import os, sys, randomimport xml.etree.ElementTree as ETfrom glob import globimport pandas as pdfrom shutil import copyfileannotations = glob('BCCD_Dataset/BCCD/Annotations/*.xml')df = []for file in annotations:#filename = file.split('/')[-1].split('.')[0] + '.jpg'#filename = str(cnt) + '.jpg'filename = file.split('\')[-1]filename =filename.split('.')[0] + '.jpg'row = []parsedXML = ET.parse(file)cell_id = 0for node in parsedXML.getroot().iter('object'):blood_cells = node.find('name').textxmin = int(node.find('bndbox/xmin').text)xmax = int(node.find('bndbox/xmax').text)ymin = int(node.find('bndbox/ymin').text)ymax = int(node.find('bndbox/ymax').text)row = [filename, cell_id, blood_cells, xmin, xmax, ymin, ymax]df.append(row)cell_id += 1data = pd.DataFrame(df, columns=['filename', 'cell_id', 'cell_type', 'xmin', 'xmax', 'ymin', 'ymax'])data['image_id'] = data['filename'].apply(lambda x: int(x[-7:-4]))data[['filename', 'image_id', 'cell_id', 'cell_type', 'xmin', 'xmax', 'ymin', 'ymax']].to_csv('bccd.csv', index=False)
裁剪
為了能夠處理數據,第一步是根據邊界框座標裁剪全尺寸圖像。這就產生了很多大小不一的細胞圖像:
import osimport pandas as pdfrom PIL import Imagedef crop_cell(row):"""crop_cell(row)given a pd.Series row of the dataframe, load row['filename'] with PIL,crop it to the box row['xmin'], row['xmax'], row['ymin'], row['ymax']save the cropped image,return cropped filename"""input_dir = 'BCCDJPEGImages'output_dir = 'BCCDcropped'# open imageim = Image.open(f"{input_dir}{row['filename']}")# size of the image in pixelswidth, height = im.size# setting the points for cropped imageleft = row['xmin']bottom = row['ymax']right = row['xmax']top = row['ymin']# cropped imageim1 = im.crop((left, top, right, bottom))cropped_fname = f"BloodImage_{row['image_id']:03d}_{row['cell_id']:02d}.jpg"# shows the image in image viewer# im1.show()# save imagetry:im1.save(f"{output_dir}{cropped_fname}")except:return 'error while saving image'return cropped_fnameif __name__ == "__main__":# load labels csv into Pandas DataFramefilepath = "BCCDdataset2-masterlabels.csv"df = pd.read_csv(filepath)# iterate through cells, crop each cell, and save cropped cell to filedataset_df['cell_filename'] = dataset_df.apply(crop_cell, axis=1)
以上就是我們所做的所有預處理操作。現在,我們繼續使用CellProfiler來擷取特徵。 使用CellProfiler提取細胞特徵CellProfiler是一個免費的開源影像分析軟體,可以從大規模細胞影像中自動定量測量。 CellProfiler還包含一個GUI介面,允許我們可視化的操作先下載CellProfiler,如果CellProfiler無法打開,則可能需要安裝Visual C 發布包,具體安裝方式參考官網。 開啟軟體就可以載入映像了, 如果想要建立管道可以在CellProfiler官網找到其提供的可用的功能清單。大多數功能分為三個主要群組:影像處理,目標的處理和測量。 常用的功能如下:影像處理- 轉為灰階圖:
测量 - 测量对象强度
CellProfiler可以将输出为CSV文件或者保存指定数据库中。这里我们将输出保存为CSV文件,然后将其加载到Python进行进一步处理。
说明:CellProfiler还可以将你处理图像的流程保存并进行分享。
主动学习
我们现在已经有了训练需要的搜有数据,现在可以开始试验使用主动学习策略是否可以通过更少的数据标记获得更高的准确性。 我们的假设是:使用主动学习可以通过大量减少在细胞分类任务上训练机器学习模型所需的标记数据量来节省宝贵的时间和精力。
主动学习框架
在深入研究实验之前,我们希望对modAL进行快速介绍: modAL是Python的活跃学习框架。 它提供了Sklearn API,因此可以非常容易的将其集成到代码中。 该框架可以轻松地使用不同的主动学习策略。 他们的文档也很清晰,所以建议从它开始你的一个主动学习项目。
主动学习与随机学习
为了验证假设,我们将进行一项实验,将添加新标签数据的随机子抽样策略与主动学习策略进行比较。开始用一些相同的标记样本训练2个Logistic回归估计器。然后将在一个模型中使用随机策略,在第二个模型中使用主动学习策略。
我们首先为实验准备数据,加载由Cell Profiler言创建的特征。 这里过滤了无色血细胞的血小板,只保留红和白细胞(将问题简化,并减少数据量) 。所以现在我们正在尝试解决二进制分类问题 - RBC与WBC。使用Sklearn Label的label encoder进行编码,并拆分数据集进行训练和测试。
# imports for the whole experimentimport numpy as npfrom matplotlib import pyplot as pltfrom modAL import ActiveLearnerimport pandas as pdfrom modAL.uncertainty import uncertainty_samplingfrom sklearn import preprocessingfrom sklearn.metrics import , average_precision_scorefrom sklearn.linear_model import LogisticRegression# upload the cell profiler features for each celldata = pd.read_csv('Zaretski_Image_All.csv')# filter plateletsdata = data[data['cell_type'] != 'Platelets']# define the labeltarget = 'cell_type'label_encoder = preprocessing.LabelEncoder()y = label_encoder.fit_transform(data[target])# take the learning features onlyX = data.iloc[:, 5:]# create training and testing setsX_train, X_test, y_train, y_test = train_test_split(X.to_numpy(), y, test_size=0.33, random_state=42)
下一步就是创建模型
<span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">dummy_learner</span> <span style="color: rgb(215, 58, 73); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">=</span> <span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">LogisticRegression</span>()<br><span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">active_learner</span> <span style="color: rgb(215, 58, 73); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">=</span> <span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">ActiveLearner</span>(<br><span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">estimator</span><span style="color: rgb(215, 58, 73); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">=</span><span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">LogisticRegression</span>(),<br><span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">query_strategy</span><span style="color: rgb(215, 58, 73); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">=</span><span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">uncertainty_sampling</span>()<br>)
dummy_learner是使用随机策略的模型,而active_learner是使用主动学习策略的模型。为了实例化一个主动学习模型,我们使用modAL包中的ActiveLearner对象。在“estimator”字段中,可以插入任何sklearnAPI兼容的模型。在query_strategy '字段中可以选择特定的主动学习策略。这里使用“uncertainty_sampling()”。这方面更多的信息请查看modAL文档。
将训练数据分成两组。第一个是训练数据,我们知道它的标签,会用它来训练模型。第二个是验证数据,虽然标签也是已知的,但是我们假装不知道它的标签,并通过模型预测的标签和实际标签进行比较来评估模型的性能。然后我们将训练的数据样本数设置成5。
# the training size that we will start withbase_size = 5# the 'base' data that will be the training set for our modelX_train_base_dummy = X_train[:base_size]X_train_base_active = X_train[:base_size]y_train_base_dummy = y_train[:base_size]y_train_base_active = y_train[:base_size]# the 'new' data that will simulate unlabeled data that we pick a sample from and label itX_train_new_dummy = X_train[base_size:]X_train_new_active = X_train[base_size:]y_train_new_dummy = y_train[base_size:]y_train_new_active = y_train[base_size:]
我们训练298个epoch,在每个epoch中,将训练这俩个模型和选择下一个样本,并根据每个模型的策略选择是否将样本加入到我们的“基础”数据中,并在每个epoch中测试其准确性。因为分类是不平衡的,所以使用平均精度评分来衡量模型的性能。
在随机策略中选择下一个样本,只需将下一个样本添加到虚拟数据集的“新”组中,这是因为数据集已经是打乱的的,因此不需要在进行这个操作。对于主动学习,将使用名为“query”的ActiveLearner方法,该方法获取“新”组的未标记数据,并返回他建议添加到训练“基础”组的样本索引。被选择的样本都将从组中删除,因此样本只能被选择一次。
# arrays to accumulate the scores of each simulation along the epochsdummy_scores = []active_scores = []# number of desired epochsrange_epoch = 298# running the experimentfor i in range(range_epoch):# train the models on the 'base' datasetactive_learner.fit(X_train_base_active, y_train_base_active)dummy_learner.fit(X_train_base_dummy, y_train_base_dummy)# evaluate the modelsdummy_pred = dummy_learner.predict(X_test)active_pred = active_learner.predict(X_test)# accumulate the scoresdummy_scores.append(average_precision_score(dummy_pred, y_test))active_scores.append(average_precision_score(active_pred, y_test))# pick the next sample in the random strategy and randomly# add it to the 'base' dataset of the dummy learner and remove it from the 'new' datasetX_train_base_dummy = np.append(X_train_base_dummy, [X_train_new_dummy[0, :]], axis=0)y_train_base_dummy = np.concatenate([y_train_base_dummy, np.array([y_train_new_dummy[0]])], axis=0)X_train_new_dummy = X_train_new_dummy[1:]y_train_new_dummy = y_train_new_dummy[1:]# pick next sample in the active strategyquery_idx, query_sample = active_learner.query(X_train_new_active)# add the index to the 'base' dataset of the active learner and remove it from the 'new' datasetX_train_base_active = np.append(X_train_base_active, X_train_new_active[query_idx], axis=0)y_train_base_active = np.concatenate([y_train_base_active, y_train_new_active[query_idx]], axis=0)X_train_new_active = np.concatenate([X_train_new_active[:query_idx[0]], X_train_new_active[query_idx[0] + 1:]], axis=0)y_train_new_active = np.concatenate([y_train_new_active[:query_idx[0]], y_train_new_active[query_idx[0] + 1:]], axis=0)
结果如下:
plt.plot(list(range(range_epoch)), active_scores, label='Active Learning')plt.plot(list(range(range_epoch)), dummy_scores, label='Dummy')plt.xlabel('number of added samples')plt.ylabel('average precision score')plt.legend(loc='lower right')plt.savefig("models robustness vs dummy.png", bbox_inches='tight')plt.show()
策略之间的差异还是很大的,可以看到主动学习只使用25个样本就可以达到平均精度0.9得分! 而使用随机的策略则需要175个样本才能达到相同的精度!
此外主動學習策略的模型的分數接近0.99,而隨機模型的分數在0.95左右停止了!如果我們使用所有數據,那麼它們最終分數是相同的,但是我們的研究目的是在少量標註數據的前提下訓練,所以只使用了數據集中的300個隨機樣本。
總結
本文展示了將主動學習用於細胞成像任務的好處。主動學習是機器學習中的一組方法,可根據其標籤對模型效能的影響來優先考慮未標記的資料範例的解決方案。由於標記資料是一項涉及許多資源(金錢和時間)的任務,因此判斷那些標記那些樣本可以最大程度地提高模型的表現是非常必要的。
細胞成像為生物學,醫學和藥理學領域做出了巨大貢獻。先前分析細胞影像需要有價值的專業人力資本,但是像主動學習這種技術的出現為醫學領域這種需要大量人力標註資料集的領域提供了一個非常好的解決方案。
以上是淺析細胞影像資料的主動學習的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Google正在領導這一轉變。它的“ AI概述”功能已經為10億用戶提供服務,在任何人單擊鏈接之前提供完整的答案。 [^2] 其他球員也正在迅速獲得地面。 Chatgpt,Microsoft Copilot和PE

2022年,他創立了社會工程防禦初創公司Doppel,以此做到這一點。隨著網絡犯罪分子越來越高級的AI模型來渦輪增壓,Doppel的AI系統幫助企業對其進行了大規模的對抗 - 更快,更快,

瞧,通過與合適的世界模型進行交互,可以實質上提高生成的AI和LLM。 讓我們來談談。 對創新AI突破的這種分析是我正在進行的《福布斯》列的最新覆蓋範圍的一部分,包括

勞動節2050年。全國范圍內的公園充滿了享受傳統燒烤的家庭,而懷舊遊行則穿過城市街道。然而,慶祝活動現在具有像博物館般的品質 - 歷史重演而不是紀念C

為了幫助解決這一緊急且令人不安的趨勢,在2025年2月的TEM期刊上進行了同行評審的文章,提供了有關該技術深擊目前面對的最清晰,數據驅動的評估之一。 研究員

從大大減少制定新藥所需的時間到創造更綠色的能源,企業將有巨大的機會打破新的地面。 不過,有一個很大的問題:嚴重缺乏技能的人

幾年前,科學家發現某些類型的細菌似乎通過發電而不是吸收氧氣而呼吸,但是它們是如何做到的,這是一個謎。一項發表在“雜誌”雜誌上的新研究確定了這種情況的發生方式:Microb

在本週的RSAC 2025會議上,Snyk舉辦了一個及時的小組,標題為“前100天:AI,政策和網絡安全如何碰撞”,其中包括全明星陣容:前CISA董事Jen Easterly;妮可·珀洛斯(Nicole Perlroth),前記者和帕特納(Partne)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。