【问题标题】:Branched CNN classifier分支 CNN 分类器
【发布时间】:2021-01-14 01:03:20
【问题描述】:

我在 TensorFlow 中实现分支 CNN 分类器时遇到问题。

首先我有 model1 有 3 个类 (A, B & C) 然后 model2 作用于 C 类,将被分类为 (C1&C2)

任何人都可以帮助我吗,如果这可能的话?

代码示例

导入数据

datagen = ImageDataGenerator(
    preprocessing_function= \
    tensorflow.keras.applications.mobilenet.preprocess_input)

train_batches = datagen.flow_from_directory(train_path,
                                            target_size=(image_size,image_size),
                                            batch_size=train_batch_size)

valid_batches = datagen.flow_from_directory(valid_path,
                                            target_size=(image_size,image_size),
                                            batch_size=val_batch_size)

# Note: shuffle=False causes the test dataset to not be shuffled
test_batches = datagen.flow_from_directory(valid_path,
                                            target_size=(image_size,image_size),
                                            batch_size=1,
                                            shuffle=False)

【问题讨论】:

  • 不清楚你需要什么。如果我理解您希望模型 1 将数据集分类为 A、B 或 C 类。那么您希望将那些分类为 C 类的样本用作模型 2 的输入。如果正确?另外,您的数据集如何输入到 model1 中?数据集是否组织为子目录,每个类一个?
  • 是的..正是!
  • 请查看我的评论更新。我可以帮助您,但需要有关数据集结构等的更多信息
  • 您是否也使用生成器向模型提供数据?例如 Image_Data_Generator?
  • 数据集组织如下:2个文件夹,一个用于训练数据,另一个用于验证然后每个文件夹都有一个用于每个类的子目录

标签: tensorflow deep-learning conv-neural-network


【解决方案1】:

好的,我可以帮助你,但我仍然很困惑。您的 test_batches 与已划分为 A、B 和 C 类的有效批次相同。那么为什么要使用它们进行测试呢?我想你会有一个单独的目录来存放测试文件。如果您的模型是准确的分类器,则测试集的预测将与您在创建验证集时所做的分类密切匹配。是的,由于分类错误可能会存在一些差异,也许这就是您要检测的内容。但是好的,请参见下面的代码。我没有测试它,但它应该做你想要的。它将确定哪些测试文件被归类为 C 类。然后它将这些文件存储到您定义的目录中。然后您可以使用 ImageDataGenerator.flow_from_directory 将文件作为输入提供给 model2.predict。

c_file_list=[]
class_dict=test_batches.class_indices # dictionary of the form {string of class name: integer indicating class index}
c_index=class_dict['C']  # assumes in the valid directory class subdirectories are named as A,B,C get index for class C
# somewhere in your code you do preds= model1.predict on the test batches
test_file_names=test_batches.filenames # list of file names in the order in which they were processed
for i, p in enumerate (preds): # iterate through the predictions
    pred_index=np.argmax(p) #find the index with the highest probability
    if pred_index==c_index:
        c_file_list.append(test_file_names[i])  # store the file names of all files classified as class C
# now we have some choices in this case I will assume you want to save the files classified as C to
# a directory. Lets call it 'c:\temp\c_classified'    name it as you wish
save_path=r'c:\temp\c_classified'
if os.path.isdir (save_path)==False:
    os.mkdir(save_path) #if the directory does not exist create it
test_list=os.listdir(valid_path) #get a list of files in the valid directory
for klass in test_list:
    klass_path=os.path.join(valid_path, klass)
    klass_list=os.listdir(klass_path)
    for f in klass_list:
        f_path=os.path.join(klass_path,f)        
        test_file_name=os.path.join(klass, f)        
        if test_file_name in c_file_list: #if the filenames matches one in the c_file_list the file was classified as class C
            tfn=os.path.basename(test_file_name)
            dest_path=os.path.join(save_path, tfn) #define the path to save the file to            
            shutil.copy (f_path, dest_path) #copy the file to the save_dir
# Now you can use the files in this directory as input to model2.predict.

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2018-02-02
    • 2020-09-03
    • 2021-04-26
    相关资源
    最近更新 更多