I'm trying to do mutli-label image classification and I'm trying to convert the image data set into TFRecords format.
Here is my Code:
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, image_name, df)
write.write(tf_example.SerializeToString())
But it throws the error:
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,image_name,df)
8 write.write(tf_example.SerializeToString())
<ipython-input-64-de0476ade753> in image_example(image_string, image_name, df)
17 'width': _int64_feature(image_shape[1]),
18 'depth': _int64_feature(image_shape[2]),
---> 19 'label_1': _int64_feature(df.loc(image_name,'Atelectasis')),
20 'label_2': _int64_feature(df.loc(image_name,'Cardiomegaly')),
21 'label_3': _int64_feature(df.loc(image_name,'Consolidation')),
TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given
Q1) Is it okay if I store the features like this? Cant the models from tensorflowHub understand it. Can I "tell" the models what to expect as the input?
Q2) Why am I getting that error? Image_example clearly takes 3 arguments.
df.loc(image_name,'X')
需要是:
df.loc[image_name,'X']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.