模型训练过程中如何忽略“corrupt jpeg data:”的日志打印 · The Chat Archive
Explore 模型训练过程中如何忽略“corrupt jpeg data:”的日志打印 Save Share Download ▾ Report模型训练过程中如何忽略“corrupt jpeg data:”的日志打印
Assistant2024-07-17 06:48 UTC Copy response
当你在训练模型时,可能会遇到一些损坏的 JPEG 图像,并且在日志中看到类似“corrupt jpeg data”的错误信息。这些错误可能会打乱日志的整洁性,但你可以采取一些措施来忽略或处理这些损坏的数据。
以下是一些可能的方法:
1. 过滤损坏的图像文件
在加载数据之前,先检查图像文件的完整性,过滤掉损坏的文件。
Copy first prompt Copy first response
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language zh
Conversation date Jul 17, 2024
Added to archive Nov 16, 2026
ID 01m7h41rs92288wxyyfff98a49Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: dd478cdcc8f0d6968555cac2f2d94233
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
from PIL import Image
import os
def is_valid_image(file_path):
try:
img = Image.open(file_path)
img.verify()
return True
except (IOError, SyntaxError) as e:
return False
valid_images = [filename for filename in os.listdir(image_dir) if is_valid_image(os.path.join(image_dir, filename))]
2. 捕获和忽略特定错误 在数据加载过程中捕获并忽略损坏的图像文件,可以使用 try-except 语句。
import tensorflow as tf
def load_and_preprocess_image(file_path):
try:
image = tf.io.read_file(file_path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [height, width])
image = image / 255.0 # 归一化
return image
except tf.errors.InvalidArgumentError as e:
print(f"Skipping corrupt image: {file_path}")
return None
dataset = tf.data.Dataset.list_files(image_dir + '/*.jpg')
dataset = dataset.map(lambda x: tf.py_function(load_and_preprocess_image, [x], [tf.float32]))
dataset = dataset.filter(lambda x: x is not None)
3. 修改日志级别 如果只想忽略这些日志信息而不想修改数据集,可以调整日志级别。例如,使用 TensorFlow 中的 tf.compat.v1.logging 来设置日志级别。
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
4. 自定义日志函数 你也可以自定义函数来记录日志,将不重要的日志过滤掉。
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def log_message(message):
if "corrupt jpeg data" in message:
return
else:
logger.info(message)
log_message("This is an info message")
log_message("corrupt jpeg data: something went wrong")
5. 使用 TensorFlow 的 tf.data API 处理错误 如果你使用的是 TensorFlow 的 tf.data API,可以使用 apply 方法来忽略错误。
import tensorflow as tf
def process_path(file_path):
try:
img = tf.io.read_file(file_path)
img = tf.image.decode_image(img, channels=3)
return img
except tf.errors.InvalidArgumentError:
return tf.zeros([height, width, 3])
dataset = tf.data.Dataset.list_files(image_dir + '/*.jpg')
dataset = dataset.map(lambda x: tf.py_function(process_path, [x], [tf.float32]))
根据需要选择一种方法或者结合使用。这将帮助你在模型训练过程中更好地处理损坏的 JPEG 数据,并保持日志的整洁。
Not yet included in a published release.