问题描述
我正在尝试进行多标签图像分类,并且正在尝试将图像数据集转换为TFRecords格式。
这是我的代码:
def _bytes_feature(value):
if isinstance(value,type(tf.cosntant(0))):
value = value.numpy()
return tf.train.Feature(byte_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list = tf.train.Int64List(value=[value]))
def image_example(image_string,image_name,df):
image_shape = tf.image.decode_png(image_string).shape
feature = {
'height': _int64_feature(image_shape[0]),'width': _int64_feature(image_shape[1]),'depth': _int64_feature(image_shape[2]),'label_1': _int64_feature(df.loc(image_name,'Atelectasis')),'label_2': _int64_feature(df.loc(image_name,'Cardiomegaly')),'label_3': _int64_feature(df.loc(image_name,'Consolidation')),'label_4': _int64_feature(df.loc(image_name,'Edema')),'label_5': _int64_feature(df.loc(image_name,'Effusion')),'label_6': _int64_feature(df.loc(image_name,'Emphysema')),'label_7': _int64_feature(df.loc(image_name,'Fibrosis')),'label_8': _int64_feature(df.loc(image_name,'Hernia')),'label_9': _int64_feature(df.loc(image_name,'Infiltration')),'label_10': _int64_feature(df.loc(image_name,'Mass')),'label_11': _int64_feature(df.loc(image_name,'Nodule')),'label_12': _int64_feature(df.loc(image_name,'Pleural_Thickening')),'label_13': _int64_feature(df.loc(image_name,'Pneumonia')),'label_14': _int64_feature(df.loc(image_name,'Pneumothorax')),'label_15': _int64_feature(df.loc(image_name,'No Finding')),'image_raw': _bytes_feature(image_string),}
return tf.train.example(features = tf.train.Features(feature=feature))
record_image = 'image.tfrecords'
with tf.io.TFRecordWriter(record_image) as write:
for row in df.index:
full_path = '/content/images/'+df['Image Index'][row]
image_string = tf.io.read_file(full_path)
image_name = pd.Series(df['Image Index'])[row]
tf_example = image_example(image_string,df)
write.write(tf_example.SerializetoString())
但是它抛出错误:
TypeError Traceback (most recent call last)
<ipython-input-66-f42cc357d799> in <module>()
5 image_string = tf.io.read_file(full_path)
6 image_name = pd.Series(df['Image Index'])[row]
----> 7 tf_example = image_example(image_string,df)
8 write.write(tf_example.SerializetoString())
<ipython-input-64-de0476ade753> in image_example(image_string,df)
17 'width': _int64_feature(image_shape[1]),18 'depth': _int64_feature(image_shape[2]),---> 19 'label_1': _int64_feature(df.loc(image_name,20 'label_2': _int64_feature(df.loc(image_name,21 'label_3': _int64_feature(df.loc(image_name,TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given
Q1)可以存储这样的功能吗? tensorflowHub的模型无法理解它。我可以“告诉”模型期望什么作为输入吗?
Q2)为什么会出现该错误? Image_example显然带有3个参数。
解决方法
df.loc(image_name,'X')
需要是:
df.loc[image_name,'X']