对图像进行分类时出现“用户警告:EXIF 数据可能损坏”

Posted

技术标签:

【中文标题】对图像进行分类时出现“用户警告:EXIF 数据可能损坏”【英文标题】:"UserWarning: Possibly corrupt EXIF data" while classifying images 【发布时间】:2018-02-24 06:10:11 【问题描述】:

以下是我的多图像分类代码。我收到一个错误;我认为这是因为加载和其他地方的尺寸不匹配。

错误消息从代码结束的地方开始。有人能看出问题吗?

#importing necessary packages
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from PIL import Image
import tflearn
import tensorflow as tf


import tflearn
#for writing text files

import glob
import os
import random
#reading images from a text file
from tflearn.data_utils import image_preloader
import math


IMAGE_FOLDER = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/Train'
TRAIN_DATA = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/training_data.txt'
TEST_DATA = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/test_data.txt'
VALIDATION_DATA = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/validation_data.txt'
train_proportion=0.7
test_proportion=0.2
validation_proportion=0.1


#read the image directories
filenames_image = os.listdir(IMAGE_FOLDER)
#shuffling the data is important otherwise the model will be fed with a single class data for a long time and
#network will not learn properly
random.shuffle(filenames_image)


#total number of images
total=len(filenames_image)
##  *****training data********
fr = open(TRAIN_DATA, 'w')
train_files=filenames_image[0: int(train_proportion*total)]
for filename in train_files:
    if filename[0:4] == 'Mark':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:5] == 'lucas':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
    elif filename[0:3] == 'Ann':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 2\n')
    elif filename[0:5] == 'Henry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 3\n')
    elif filename[0:5] == 'Hanna':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 4\n')
    elif filename[0:4] == 'Jack':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 5\n')
    elif filename[0:5] == 'Harry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 6\n')
    elif filename[0:3] == 'Lui':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 7\n')
    elif filename[0:6] == 'Karlos':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 8\n')
    elif filename[0:4] == 'Guan':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 9\n')

fr.close()
##  *****testing data********
fr = open(TEST_DATA, 'w')
test_files=filenames_image[int(math.ceil(train_proportion*total)):int(math.ceil((train_proportion+test_proportion)*total))]
for filename in test_files:
    if filename[0:4] == 'Mark':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:5] == 'lucas':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
    elif filename[0:3] == 'Ann':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 2\n')
    elif filename[0:5] == 'Henry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 3\n')
    elif filename[0:5] == 'Hanna':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 4\n')
    elif filename[0:4] == 'Jack':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 5\n')
    elif filename[0:5] == 'Harry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 6\n')
    elif filename[0:3] == 'Lui':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 7\n')
    elif filename[0:6] == 'Karlos':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 8\n')
    elif filename[0:4] == 'Guan':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 9\n')
fr.close()

##  *****validation data********
fr = open(VALIDATION_DATA, 'w')
valid_files=filenames_image[int(math.ceil((train_proportion+test_proportion)*total)):total]
for filename in valid_files:
    if filename[0:4] == 'Mark':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:5] == 'lucas':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
    elif filename[0:3] == 'Ann':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 2\n')
    elif filename[0:5] == 'Henry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 3\n')
    elif filename[0:5] == 'Hanna':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 4\n')
    elif filename[0:4] == 'Jack':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 5\n')
    elif filename[0:5] == 'Harry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 6\n')
    elif filename[0:3] == 'Lui':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 7\n')
    elif filename[0:6] == 'Karlos':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 8\n')
    elif filename[0:4] == 'Guan':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 9\n')
fr.close()

#Importing data
X_train, Y_train = image_preloader(TRAIN_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)
X_test, Y_test = image_preloader(TEST_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)
X_val, Y_val = image_preloader(VALIDATION_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)



print ("Dataset")
print ("Number of training images ".format(len(X_train)))
print ("Number of testing images ".format(len(X_test)))
print ("Number of validation images ".format(len(X_val)))
print ("Shape of an image " .format(X_train[1].shape))
print ("Shape of label: ,number of classes: ".format(Y_train[1].shape,len(Y_train[1])))


#Sample Image
plt.imshow(X_train[1])
plt.axis('off')
plt.title('Sample image with label '.format(Y_train[1]))
plt.show()
print(type(X_test))


#input image
x=tf.placeholder(tf.float32,shape=[None,56,56,3] , name='input_image')
#input class


y_=tf.placeholder(tf.float32,shape=[None, 10] , name='input_class')



input_layer=x
print("Hiiiiiiii No error till this point")


#convolutional layer 1 --convolution+RELU activation
conv_layer1=tflearn.layers.conv.conv_2d(input_layer, nb_filter=64, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu', regularizer="L2", name='conv_layer_1')

#2x2 max pooling layer
out_layer1=tflearn.layers.conv.max_pool_2d(conv_layer1, 10)


#second convolutional layer
conv_layer2=tflearn.layers.conv.conv_2d(out_layer1, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')
out_layer2=tflearn.layers.conv.max_pool_2d(conv_layer2, 10)
# third convolutional layer
conv_layer3=tflearn.layers.conv.conv_2d(out_layer2, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')
out_layer3=tflearn.layers.conv.max_pool_2d(conv_layer3, 10)

#fully connected layer1
fcl= tflearn.layers.core.fully_connected(out_layer3, 4096, activation='relu' , name='FCL-1')
fcl_dropout_1 = tflearn.layers.core.dropout(fcl, 0.8)
#fully connected layer2
fc2= tflearn.layers.core.fully_connected(fcl_dropout_1, 4096, activation='relu' , name='FCL-2')
fcl_dropout_2 = tflearn.layers.core.dropout(fc2, 0.8)
#softmax layer output
y_predicted = tflearn.layers.core.fully_connected(fcl_dropout_2, 10, activation='softmax', name='output')
#loss function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_predicted+np.exp(-10)), reduction_indices=[1]))
#optimiser -
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#calculating accuracy of our model
correct_prediction = tf.equal(tf.argmax(y_predicted,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# session parameters
sess = tf.InteractiveSession()
#initialising variables
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver()
save_path="C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/mark2.ckpt"

g = tf.get_default_graph()



# every operations in our graph
[op.name for op in g.get_operations()]
epoch=5000

batch_size=20
previous_batch=0
for i in range(epoch):
    #batch wise training

    if previous_batch >= len(X_train) :
            previous_batch=0
    current_batch=previous_batch+batch_size
    x_input=X_train[previous_batch:current_batch]
    x_images=np.array(x_input)
    x_images=np.reshape(x_images,[batch_size,56,56,3])
    y_input=Y_train[previous_batch:current_batch]
    y_label=np.reshape(y_input,[batch_size,10])
    previous_batch=previous_batch+batch_size
    _,loss=sess.run([train_step, cross_entropy], feed_dict=x: x_images,y_: y_label)
    if i%500==0:
        n=50 #number of test samples
        X_test=np.array(X_test)
        x_test_images=np.reshape(X_test[0:n],[n,56,56,3])
        y_test_labels=np.reshape(Y_test[0:n],[n,10])
        Accuracy=sess.run(accuracy,feed_dict=x: x_test_images ,y_: y_test_labels)
        print("Iteration no : , Accuracy: , Loss : " .format(i,Accuracy,loss))
        saver.save(sess, save_path, global_step = i)
    elif i % 100 ==0:
        print("Iteration no : Loss : " .format(i,loss))
x_input=X_val
x_images=np.reshape(x_input,[len(X_val),56,56,3])
y_input=Y_val
y_label=np.reshape(y_input,[len(Y_val),10])

Accuracy_validation=sess.run(accuracy,feed_dict=x: x_images ,y_: y_label)
Accuracy_validation=round(Accuracy_validation*100,2)
print("Accuracy in the validation dataset:  %".format(Accuracy_validation))


Test_FOLDER = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/Test'


filenames_image = os.listdir(Test_FOLDER)
total=len(filenames_image)
print(total)

test_files=filenames_image[0: int(total)]
for filename in test_files:

    marty=Image.open(Test_FOLDER+'/'+filename)
    marty_resize=marty.resize((56,56),Image.ANTIALIAS)
    marty_resize=np.array(marty_resize)
    marty_test=marty_resize/np.max(marty_resize).astype(float)
    marty_test=np.reshape(marty_test,[1,56,56,3])
    c=sess.run(y_predicted, feed_dict=x: marty_test)
    d= np.argmax(c)
#test your own images
#test_image=Image.open('/path to file')
#test_image= process_img(test_image)
#predicted_array= sess.run(y_predicted, feed_dict=x: test_image)
    #predicted_class= np.argmax(predicted_array)
    if d==0:

        print("This is Mark\n")
    elif d==1:

        print("This is lucas\n")
    elif d==2:
        print("This is Ann")
    elif d==3:

        print("This is Henrry\n")
    elif d==4:

        print("This is Hanna\n")
    elif d==5:
        print("This is Jack")

    elif d==6:

        print("This is Harry\n")
    elif d==7:
        print("This is Lui")
    elif d==8:

        print("This is Karlos\n")
    elif d==9:

        print("This is guan\n")

这是错误:

C:\Users\kdeepshi\Desktop\PyforE\Face-Detection>multi.py

此机器不支持curses(请安装/重新安装 诅咒以获得最佳体验)

数据集

训练图像数量 22

测试图像数量 6

验证图像数量 3

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 12 个字节 但只有 0。跳过标签 270

"跳过标签 %s" % (size, len(data), tag))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 6 个字节 但只有 0。跳过标签 271

"跳过标签 %s" % (size, len(data), tag))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 8 个字节 但只有 0. 跳过标签 272 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 64 字节 但只有 0. 跳过标签 282 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 64 字节 但只有 0. 跳过标签 283 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 20 个字节 但只有 0. 跳过标签 306 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 24 个字节 但只得到 0. 跳过标签 529 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 24 个字节 但只有 0. 跳过标签 532 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692: 用户警告:可能损坏的 EXIF 数据。期望读取 40 个字节 但只得到 0. 跳过标签 33432 "跳过标签 %s" % (size, len(数据),标签))

C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:709: 用户警告:损坏的 EXIF 数据。期望读取 2 个字节,但仅 得到 0.warnings.warn(str(msg))

图像的形状(56、56、3)

标签形状:(10,),类数:10

【问题讨论】:

【参考方案1】:

从 PIL 模块中的图像读取 EXIF 数据时出现问题。我倾向于认为这是 PIL 中的错误,而不是图像损坏。由于深度学习不需要这些数据,您只需清理文件即可。为此,请下载 ExifTool 并运行以下命令:

exiftool -r -all= -ext JPEG D:\datasets\ImageNet\train

-r: 递归 -all: 将所有 EXIF 数据设置为空 -ext: 文件扩展名

这适用于 Windows 和 Linux。

【讨论】:

以上是关于对图像进行分类时出现“用户警告:EXIF 数据可能损坏”的主要内容,如果未能解决你的问题,请参考以下文章

对 JPG 图像进行操作时出现“无法将模式 P 写入 JPEG”

从二进制文件加载图像像素时出现 C++ 分段错误

在opencv c ++中查找图像卷积时出现分段错误(核心转储)错误

上传图像以使用 keras 进行预测时出现错误消息

imagettfbbox():使用 Laravel 干预/图像进行文本布局时出现问题

通过选定的星图对视觉和后端进行评级时出现问题 选择输入类型收音机的隐藏值