【问题标题】:Hidden file in my training dataset which makes tensorflow return "Unknown image file format. One of JPEG, PNG, GIF, BMP required."我的训练数据集中的隐藏文件使 tensorflow 返回“未知的图像文件格式。需要 JPEG、PNG、GIF、BMP 之一。”
【发布时间】:2021-09-17 04:36:07
【问题描述】:

我有 tensorflow 模型,在第一个 epoch 训练的第一部分,它一直工作到大约中点(735/2201 [=========>.......... ..........]) 然后它返回标题中的错误。

首先我编写了一个脚本来删除该目录中所有不以 .jpg 结尾但没有任何改变的文件。

import os
for file in os.listdir(path):
    if not file.endswith('.jpg'):
        os.remove(os.path.join(path,file))

然后我打开了我的 macs bash 并列出了目录中的所有文件以查看任何隐藏文件,但都只是 jpgs。

编辑:

Nessuno 的回答是正确的,但你必须遍历绝对路径而不仅仅是文件名,这样的东西应该可以工作

import os
import imghdr
#define your path
path = '' 

files = os.listdir(path)


for file in files:
    format = imghdr.what(os.path.join(path, file))
    if format != 'jpeg':
        os.remove(os.path.join(path, file))

我最终删除了 5 个不是 jpeg 的文件

【问题讨论】:

    标签: python file operating-system tensorflow2.0 hidden-files


    【解决方案1】:

    您的path 中有一些文件具有.jpg 扩展名,但它包含不同的文件格式。

    您可以使用imghdr 库(Python 本身附带的:https://docs.python.org/3/library/imghdr.html)并检查标头是否等于jpeg,并在这种情况下删除图像。

    简而言之,您可以将脚本更改为:

    import os
    import imghdr
    
    for file in os.listdir(path):
        if imghdr.what(file) != 'jpeg':
            os.remove(os.path.join(path, file))
    

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2021-04-02
      • 2021-08-01
      • 2023-01-16
      • 2019-10-10
      • 2018-05-04
      • 2016-07-06
      • 2020-01-03
      • 1970-01-01
      相关资源
      最近更新 更多