Загрузка записей TF в Keras - PullRequest
1 голос
/ 19 июня 2020

Я пытаюсь загрузить пользовательский файл TFRecord в свою модель keras. Я попытался следовать этому руководству: https://medium.com/@moritzkrger / speeding-up-keras-with-tfrecord-datasets-5464f9836c36 , но приспосабливаясь к моему использованию.

Моя цель - иметь функции работают аналогично ImageDataGenerator от Keras. Я не могу использовать эту функцию, потому что я указываю метаданные c из изображений, которые генератор не захватывает. Я не включаю эти метаданные сюда, потому что мне просто нужна сеть basi c для начала работы.

Я также хочу иметь возможность применить это к приложению обучения передачи.

I продолжайте получать эту ошибку: TypeError: Could not build a TypeSpec for None with type NoneType Я использую Tensorflow 2.2

def _parse_function(serialized):
    features = \
    {
        'image': tf.io.FixedLenFeature([], tf.string),
        'label': tf.io.FixedLenFeature([], tf.int64),
        'shapex': tf.io.FixedLenFeature([], tf.int64),
        'shapey': tf.io.FixedLenFeature([], tf.int64),
    }
    parsed_example = tf.io.parse_single_example(serialized=serialized,
                                                features=features)
    shapex = tf.cast(parsed_example['shapex'], tf.int32)
    shapey = tf.cast(parsed_example['shapey'], tf.int32)
    image_shape = tf.stack([shapex, shapey, 3])
    image_raw = parsed_example['image']
    # Decode the raw bytes so it becomes a tensor with type.
    image = tf.io.decode_raw(image_raw, tf.uint8)
    image = tf.reshape(image, image_shape)
    # Get labels
    label = tf.cast(parsed_example['label'], tf.float32)
    return image, label

def imgs_inputs(type, perform_shuffle=False):
    records_dir = '/path/to/tfrecord/'
    record_paths = [os.path.join(records_dir,record_name) for record_name in os.listdir(records_dir)]
    full_dataset = tf.data.TFRecordDataset(filenames=record_paths)
    full_dataset = full_dataset.map(_parse_function, num_parallel_calls=16)

    dataset_length = (len(list(full_dataset))) #Gets length of datase

    iterator = tf.compat.v1.data.make_one_shot_iterator(databatch)
    image, label = iterator.get_next()
    #labels saved as values ex: [1,2,3], and are now converted to one hot encoded
    label = to_categorical(label)
    return image, label

image, label = imgs_inputs(type ='Train',perform_shuffle=True)

#Combine it with keras
# base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(200,200,3), dropout=.3)
model_input = Input(shape=[200,200,3])

#Build your network
model_output = Flatten(input_shape=(200, 200, 3))(model_input)
model_output = Dense(19, activation='relu')(model_output)

#Create your model
train_model = Model(inputs=model_input, outputs=model_output)

#Compile your model
optimizer = Adam(learning_rate=.001)
train_model.compile(optimizer=optimizer,loss='mean_squared_error',metrics=['accuracy'],target_tensors=[label])

#Train the model
train_model.fit(epochs=10,steps_per_epoch=2)

image возвращает массив формы (100,200,200,3), который представляет собой пакет из 100 изображений label возвращает массив формы (100 , 19) что представляет собой партию из 100 этикеток (всего 19 этикеток)

1 Ответ

1 голос
/ 19 июня 2020

Проблема, связанная с shapex и shapey, но я не знаю точно, почему. Я установил shapex = 200 и shapey=200. Затем я переписал модель, чтобы включить трансферное обучение.

base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(200,200,3), dropout=.3)
x = base_model.output
types = Dense(19,activation='softmax')(x)

model = Model(inputs=base_model.input,outputs=types)

model.compile(
    optimizer='adam',
    loss = 'sparse_categorical_crossentropy',
    metrics=['accuracy']
history = model.fit(get_batches(), steps_per_epoch=1000, epochs=10)

I found everything I needed on this Google Colab:
[https://colab.research.google.com/github/GoogleCloudPlatform/training-data-analyst/blob/master/courses/fast-and-lean-data-science/04_Keras_Flowers_transfer_learning_solution.ipynb#scrollTo=XLJNVGwHUDy1][1]


  [1]: https://colab.research.google.com/github/GoogleCloudPlatform/training-data-analyst/blob/master/courses/fast-and-lean-data-science/04_Keras_Flowers_transfer_learning_solution.ipynb#scrollTo=XLJNVGwHUDy1
...