问题
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.
来源:https://stackoverflow.com/questions/59039788/how-to-combine-a-pre-trained-keraslayer-from-tensorflow-v-2-hub-and-tfrecords