【问题标题】:How can I copy ML model's training results to specific folders如何将 ML 模型的训练结果复制到特定文件夹
【发布时间】:2022-01-22 12:29:48
【问题描述】:

我创建了一个机器学习模型,可以从狗对猫数据库中对猫和狗进行分类。这是一个巨大的数据库。我应用此代码:

predictions = model.predict(test, verbose=0)
for i in range(0,10):
    if predictions[i, 0] >= 0.5: 
        print('I am {:.2%} sure this is a Dog'.format(predictions[i][0]))
    else: 
        print('I am {:.2%} sure this is a Cat'.format(1-predictions[i][0]))

    plt.imshow(test[i])
    plt.show()

它给了我这样的照片和文字输出:

(Cat photo from test dataset)
I am 100.00% sure this is a Cat

到目前为止没有任何问题。作为另一个不同的示例,它可以显示如下:

(Dog photo from test dataset)
I am 62.34% sure this is a Dog

所以,我的问题是,如果这些照片大于 %50%,我想将这些照片复制到文件中。

例如,如果它是这样说的:

I am 100.00% sure this is a Cat

我想把它复制到 cat 文件夹。该文件夹以前不存在。如果是这样说的:

I am 62.34% sure this is a Dog

我想把它复制到狗文件夹。该文件夹以前也不存在。

我真的被困在这一点上。有任何想法吗?感谢您的所有 cmets

【问题讨论】:

    标签: python machine-learning


    【解决方案1】:

    你可以用来完成这个任务的两个模块是 subprocess 和 os + shutil。

    1. 使用子流程模块:

    假设您可以访问原始路径

    import subprocess
    make_dir = subprocess.Popen("mkdir dogs & mkdir cats", shell=True)
    make_dir.wait()
    if predictions[i, 0] >= 0.5:
        p = subprocess.Popen('cp {original_file_name} {new_path}'.format(original_file_name=original_file_name, new_path=new_path), shell=True)
        p.wait()
    
    1. 使用 Shuttle 和 OS 模块
    import os, shutil
    os.mkdir('dogs')
    os.mkdir('cats')
    if predictions[i, 0] >= 0.5:
        shutil.copyfile(original_file_name, new_path)
    

    【讨论】:

    • 可以访问原路径 但是问题是,原来的test文件夹里面的猫狗图片太多了,太多了,不能手动复制,因为有没有标记。但是,我写了一个代码,它可以将它分类为狗或猫,代码中有一定的百分比。所以,根据那个结果。我想将分类图像复制到单独的文件中。像 123.jpg 我们对此一无所知 如果代码结果显示,它是 %56% 狗。我想将它们复制到 Dog 文件夹中。另一个例子,如果它说 %70 cat,我想把它复制到 Cat 文件夹中。
    猜你喜欢
    • 2017-12-31
    • 2020-06-24
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2019-07-19
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多