2

I have a tfrecord with 23 classes with 35 images in each class (805 in total). My current tfrecord read function is:

def read_tfrecord(serialized_example):
 feature_description = {
    'image': tf.io.FixedLenFeature((), tf.string),
    'label': tf.io.FixedLenFeature((), tf.int64),
    'height': tf.io.FixedLenFeature((), tf.int64),
    'width': tf.io.FixedLenFeature((), tf.int64),
    'depth': tf.io.FixedLenFeature((), tf.int64)
 }

 example = tf.io.parse_single_example(serialized_example, feature_description)
 image = tf.io.parse_tensor(example['image'], out_type=float)
 image_shape = [example['height'], example['width'], example['depth']]
 image = tf.reshape(image, image_shape)
 label = tf.cast(example["label"], tf.int32)
 image = image/255

 return image, label

I then have a make_dataset function that looks like this:

def make_dataset(tfrecord, BATCH_SIZE, EPOCHS, cache=True):
 files = tf.data.Dataset.list_files(os.path.join(os.getcwd(), tfrecord))
 dataset = tf.data.TFRecordDataset(files)

 if cache:
    if isinstance(cache, str):
      dataset = dataset.cache(cache)
    else:
      dataset = dataset.cache()

 dataset = dataset.shuffle(buffer_size=FLAGS.shuffle_buffer_size)
 dataset = dataset.map(map_func=read_tfrecord, num_parallel_calls=AUTOTUNE)
 dataset = dataset.repeat(EPOCHS)
 dataset = dataset.batch(batch_size=BATCH_SIZE)
 dataset = dataset.prefetch(buffer_size=AUTOTUNE)

 return dataset

This make_dataset function gets passed into

train_ds = make_dataset(tfrecord=FLAGS.tf_record, BATCH_SIZE=BATCH_SIZE, EPOCHS=EPOCH)
image_batch, label_batch = next(iter(train_ds))
feature_extractor_layer = hub.KerasLayer(url, input_shape=IMAGE_SHAPE + (3,)) 
feature_batch = feature_extractor_layer(image_batch)
feature_extractor_layer.trainable = False
model = tf.keras.Sequential([feature_extractor_layer, layers.Dense(2048, input_shape=(2048,)), layers.Dense(len(CLASS_NAMES), activation='softmax')])

model.summary()
predictions = model(image_batch)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=rate),
                              loss='categorical_crossentropy',
                              metrics=['acc'])

batch_stats_callback = CollectBatchStats()
STEPS_PER_EPOCH = np.ceil(image_count / BATCH_SIZE)
history = model.fit(image_batch, label_batch, epochs=EPOCH, batch_size=BATCH_SIZE, steps_per_epoch=STEPS_PER_EPOCH, callbacks=[batch_stats_callback])

This code runs in the sense that it outputs the usual information about how many epochs I have and some training accuracy data (which is 0 with a loss around 100k). The error I get doesn't have any meaning to me, as it says: Function instantiation has undefined input shape at index: 100 in the outer inference context. You can substitute the number to anything below 1000 (not sure if it ever surpasses the number of images I have in my tfrecord).

I'm at a complete loss with this one.

EDIT:

It seems this "error" I was getting was nothing but a warning message. I suspect it is related to the use of TensorFlow Hub and potentially eager execution. I added

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

at the beginning of the file and the warning has vanished.

kelkka
  • 874
  • 7
  • 18

2 Answers2

2

As specified by kelkka, it is not an Error, but just a Warning.

Adding the below line of code could, at the start of the Program, will Restrict the Warning Messages to be printed.

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

Other values of that Environment Variable, along with their Behavior is mentioned below:

  • 0 = all messages are logged (default behavior)
  • 1 = INFO messages are not printed
  • 2 = INFO and WARNING messages are not printed
  • 3 = INFO, WARNING, and ERROR messages are not printed

For more information about controlling the Verbosity of Warning Messages, please refer this Stack Overflow Answer.

  • 1
    I think this was merely a warning, not an error. Could you elaborate why changing logging level is a viable solution to the problem at hand? – Szymon Maszke Feb 26 '20 at 11:07
  • 1
    Modified the Answer in order to elaborate why changing logging level is a viable solution. Thanks! –  Mar 04 '20 at 09:35
1

I found the warning message disappears if the output shape of the feature extractor layer is specified, like this:

 feature_extractor_layer = hub.KerasLayer(feature_extractor_url, input_shape=(224, 224, 3), output_shape=[1280])

For the actual input and output shapes of the feature extractor you are using. The training process will run as it was running with the warning.

CarlosPau
  • 21
  • 1