简体   繁体   English

训练具有多个文件夹和子文件夹的 CNN 模型

[英]Train CNN model with multiple folders and sub-folders

I am developing a convolution neural network (CNN) model to predict whether a patient in category 1,2,3 or 4. I use Keras on top of TensorFlow.我正在开发一个卷积神经网络 (CNN) 模型来预测患者是否属于 1、2、3 或 4 类。我在 TensorFlow 之上使用 Keras。

I have 64 breast cancer patient data, classified into four category (1=no disease, 2= …., 3=….., 4=progressive disease).我有 64 个乳腺癌患者数据,分为四类(1=无疾病,2=……,3=…………,4=疾病进展)。 In each patient's data, I have 3 set of MRI scan images taken at different dates and inside each MRI folder, I have 7 to 8 sub folders containing MRI images in different plane (such as coronal plane/sagittal plane etc).在每个患者的数据中,我有 3 组在不同日期拍摄的 MRI 扫描图像,在每个 MRI 文件夹内,我有 7 到 8 个子文件夹,其中包含不同平面(如冠状面/矢状面等)的 MRI 图像。

I learned how to deal with basic “Cat-Dog-CNN-Classifier”, it was easy as I put all the cat & dog images into a single folder to train the network.我学会了如何处理基本的“Cat-Dog-CNN-Classifier”,这很容易,因为我将所有的猫和狗图像放在一个文件夹中来训练网络。 But how do I tackle the problem in my breast cancer patient data?但是我该如何解决我的乳腺癌患者数据中的问题呢? It has multiple folders and sub-solders.它有多个文件夹和子焊料。

Please suggest.请建议。

You need to build your dataset navigating through folders.您需要通过文件夹来构建数据集。 This can be done easily in python.这可以在 python 中轻松完成。

So, suppose you have the following folder tree:因此,假设您有以下文件夹树:

root/
\__ Patient_1/
\__ \_______ MRI_1/
\__ \_______ \___ folder_1/
\__ \_______ \___ folder_2/
\__ \_______ \___ folder_3/
\__ \_______ \___ ...     /
\__ \_______ \___ folder_8/
\__ \_______ MRI_2/
\__ \_______ ...  /...
\__ Patient_2/
...

First, you need to get your current working directory (cwd):首先,您需要获取当前工作目录 (cwd):

import os
cwd = os.getcwd()

Then you can start building your dataset:然后你可以开始构建你的数据集:

dataset = [];

for p in range(1, 65):
    for mri in range(1, 4):
        mri_folder = cwd + '/Patient_{}/MRI_{}/'.format(p, mri)

        # Since we do not know how many folders there will be, 
        # we need to list all the folders in the current MRI folder.

        folders_names = os.listdir(mri_folder)
        for f in folder_names:
            # list all images in current folder
            fnames = os.listdir('{}/{}/'.format(mri_folder, f))

            for fname in fnames:
                # Now you can open your image and append it to dataset

Hope it helps!希望能帮助到你!

use this loop:使用这个循环:

for _,_,files in os.walk("data_folder/"):
    for name in files:
        FP = os.path.join(root, name)

使用os.walk递归访问子目录中的所有文件os.walk加到数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM