tensorflow - 分支 CNN 分类器
问题描述
我在 TensorFlow 中实现分支 CNN 分类器时遇到问题。
首先,我有具有3 个类(A、B 和 C)的模型 1, 然后是在 C类上工作的模型 2,它将被分类为(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)
解决方案
好的,我可以帮助你,但我仍然很困惑。您的 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.
推荐阅读
- svg - 如何将地理参考 SVG 放置在 openlayers 地图上(不是图像)
- c++ - 坚持在 C++ 中调用一些方法来创建菜单
- python - 如何在python中使用乌龟画一个圆?
- javascript - 无法使用 jquery ajax 和 codeigniter 分页在页面上显示搜索结果
- javascript - 计算 Javascript 数组的多级小计
- python - SQLalchemy .filter 不能按预期工作
- python - 将 15 小时长的音频文件拆分为 1 小时长的文件
- php - 如何在 PHP 中使用 Android Jetpack Paging 3
- python - 带有标准分隔符的 MathJax 内联公式未使用 wkhtmltopdf 正确呈现
- javascript - 用于表单验证的 Javascript 循环数组