Keras中的数据集预测错误

时间:2019-01-28 16:21:10

标签: keras prediction

我在6波段Landsat图像和要预测的图像上有一些训练数据点,所有这些都以TFRecords格式提供。我能够成功地构建训练和测试数据集,并创建Keras模型并进行拟合和测试。

def trainDataGenerator(fileName, numEpochs=None, batchSize=None):

  dataset = tf.data.TFRecordDataset(fileName, compression_type='GZIP')

  # Map the parsing function over the dataset
  dataset = dataset.map(parse_tfrecord) # it returnes a dictionary of bands and values, plus a label
  dataset = dataset.map(addFeatures) # It addes NDVI to the dictionary and convert features to a list (to be an np.array)
  dataset = dataset.batch(batchSize)
  dataset = dataset.repeat(numEpochs)
  return dataset

training_data = trainDataGenerator(fileName=<trainData>, numEpochs=8, batchSize=1)
testing_data = trainDataGenerator(fileName=<testData>, batchSize=1)

def keras_model():
    from tensorflow.keras.layers import Dense, Input

    inputs = Input(shape=(7,))
    x = Dense(5, activation='relu')(inputs)
    x = Dense(7, activation='relu')(x)
    x = Dense(5, activation='relu')(x)
    outputs = Dense(3, activation='softmax')(x)

    return tf.keras.Model(inputs, outputs)

model = keras_model()
model.compile('adam', 'categorical_crossentropy', metrics=['acc'])
model.fit(training_data.make_one_shot_iterator(), steps_per_epoch=57, epochs=8, verbose=0)
loss, test_acc = model.evaluate(testing_data.make_one_shot_iterator(), steps=41, verbose=0)

要预测的图像保存在256x256像素的色块中,因此我具有稍微修改的数据集生成器功能来处理它,并且在这种情况下也没有标签。

def predictDataGenerator(fileNames):

    dataset = tf.data.TFRecordDataset(fileNames, compression_type='GZIP')

    def parse_image(example_proto):
        parsed_features = tf.parse_single_example(example_proto, imageFeaturesDict)
        return parsed_features

    # This function adds NDVI to a feature that doesn't have a label.
    def addImageFeatures(features):
        return addFeatures(features, None)[0]

    dataset = dataset.map(parse_image, num_parallel_calls=5)

    # Break our long tensors into many littler ones
    dataset = dataset.flat_map(lambda features: tf.data.Dataset.from_tensor_slices(features))

    # Add additional features (NDVI).
    dataset = dataset.map(addImageFeatures)

    # Read in batches corresponding to patch size.
    dataset = dataset.batch(PATCH_WIDTH * PATCH_HEIGHT)

    return dataset

dataset = predictDataGenerator(fileNames)
predictions = model.predict(dataset.make_one_shot_iterator(), steps = 96)

我希望能够运行类似于model.evaluate和model.fit的model.predict函数,但是当我运行它时,出现以下错误:

Traceback (most recent call last):
  File "C:\Users\sshahhey.ESFADMIN\AppData\Roaming\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py", line 1664, in <module>
    main()
  File "C:\Users\sshahhey.ESFADMIN\AppData\Roaming\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Users\sshahhey.ESFADMIN\AppData\Roaming\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Users\sshahhey.ESFADMIN\AppData\Roaming\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/sshahhey.ESFADMIN/OneDrive/Projects/Tensorflow/First/SampleDNNClassifier-Keras.py", line 163, in <module>
    predictions = model.predict(dataset.make_one_shot_iterator(), steps = 96)
  File "c:\python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1864, in predict
    x, check_steps=True, steps_name='steps', steps=steps)
  File "c:\python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 986, in _standardize_user_data
    'Received %s' % next_element)
TypeError: not all arguments converted during string formatting

似乎它正在寻找要提供的目标(标签)以及数据,但这只是预测,目前我不应该有任何标签。 有帮助吗?

0 个答案:

没有答案