Невозможно использовать набор данных Tensorflow с моделью Keras для сегментации изображения - PullRequest
0 голосов
/ 21 октября 2018

Я пытаюсь выполнить сегментацию изображения в Keras, используя набор данных tenorflow, но я получаю ошибку AttributeError: 'MapDataSet' has no attribute 'ndim', когда пытаюсь вызвать model.fit().Я использую Блокнот Jupyter в Google Colab.Последняя строка кода выдает ошибку.

import numpy as np
import tensorflow as tf
from tensorflow import keras 
import sys
from json import load
import math
import tensorflow as tf
from tensorflow.contrib import layers
from tensorflow.contrib.data import map_and_batch

export_file = 'export.json'
IMG_WIDTH = 256
IMG_HEIGHT = 256
with open(export_file) as f:
  export_json = load(f)
legend = export_json['legend']
tfrecord_paths = export_json['tfrecord_paths']

# Fairly certain nothing wrong here
# -----------------------------------------------
def _parse_tfrecord(serialized_example):
    example = tf.parse_single_example(
        serialized_example,
        features={
            'image/encoded': tf.FixedLenFeature([], tf.string),
            'image/filename': tf.FixedLenFeature([], tf.string),
            'image/ID': tf.FixedLenFeature([], tf.string),
            'image/format': tf.FixedLenFeature([], tf.string),
            'image/height': tf.FixedLenFeature([], tf.int64),
            'image/width': tf.FixedLenFeature([], tf.int64),
            'image/channels': tf.FixedLenFeature([], tf.int64),
            'image/colorspace': tf.FixedLenFeature([], tf.string),
            'image/segmentation/class/encoded': tf.FixedLenFeature([], tf.string),
            'image/segmentation/class/format': tf.FixedLenFeature([], tf.string),
            })
    image = tf.image.decode_image(example['image/encoded'])
    image.set_shape([IMG_WIDTH, IMG_HEIGHT, 3])
    label = tf.image.decode_image(example['image/segmentation/class/encoded'])
    label.set_shape([IMG_WIDTH, IMG_HEIGHT, 1])
    image_float = tf.to_float(image)
    label_float = tf.to_float(label)
    return (image_float, label_float)
# -----------------------------------------------

# Create training and testing datasets
test_set_size = math.floor(0.20 * len(tfrecord_paths))
training_dataset = tf.data.TFRecordDataset(tfrecord_paths)
training_dataset = training_dataset.skip(test_set_size)
training_dataset = training_dataset.map(_parse_tfrecord)

test_dataset = tf.data.TFRecordDataset(tfrecord_paths)
test_dataset = test_dataset.take(test_set_size)
test_dataset = test_dataset.map(_parse_tfrecord)

# Printing training_dataset yields: '<MapDataset shapes: ((256, 256, 3), (256, 256, 1)), types: (tf.float32, tf.float32)>'

# Load Inception v3
from keras.applications.inception_v3 import InceptionV3
model = InceptionV3(input_shape = (IMG_HEIGHT, IMG_WIDTH, 3), include_top=False, weights='imagenet')
model.compile(optimizer='RMSProp', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(training_dataset, epochs=10, steps_per_epoch=30) # -=CAUSES ERROR=-

От Как правильно объединить API набора данных TensorFlow и Keras? , я пытался использовать второе решение, но я получил ту же ошибку,даже при изменении model.fit() на one_shot_iterator вместо tf.data.Dataset.

Кажется, что это может быть более распространенной проблемой, как я обнаружил в https://github.com/tensorflow/tensorflow/issues/20698. Однако каждое опубликованное здесь решение приводило к той же ошибке, что и я.

...