0

Hi I want to create a tfrecord for images and their one hot array labels.Im able to acheive it for the images,but not for the labels.I referred to this SOF link,but getting the same error.Below is my code.

def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


for i in range(len(train_addrs)):

  print('reading image no {0} : and image address {1}'.format(i,train_addrs[i]))

  img = load_image(train_addrs[i])#loading the preprocessed image

  label = train_labels[i]#loading associated one-hot array

  print('label is ',label) #array([0, 1]) of type uint8 ,I tried with int64,int32 also;but no use

  feature = {'train/label':_int64_feature(label),
             'train/image':_bytes_feature(tf.compat.as_bytes(img.tostring())) #this part works 
           }    


  example = tf.train.Example(features=tf.train.Features(feature=feature))

  serToString = example.SerializeToString()

  writer.write(serToString)

When I execute this code,Im getting the following error.

 TypeError: array([0, 1]) has type <type 'numpy.ndarray'>, but expected one of: (<type 'int'>, <type 'long'>)

Im not sure where am I going wrong?Any help would be really helpful.

george
  • 339
  • 3
  • 12

1 Answers1

1

Since you defined label as _int64_feature, you have to use a int for label not a numpy array

  label = train_labels[i]#loading associated one-hot array
  label = np.argmax(label)

you can convert them to one_hot format while reading the data.

If you want to pass it as a list; modify your function definition

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
Ishant Mrinal
  • 4,898
  • 3
  • 29
  • 47