[英]Issue with TF2.0 Custom Generator
I am trying to learn how to use the tf.data.Dataset methods and working on a custom generator from a CSV file.我正在尝试学习如何使用 tf.data.Dataset 方法并使用 CSV 文件中的自定义生成器。 The CSV holds three data points. CSV 包含三个数据点。 1 the path to the image, 2 the label, 3 the image name (Not used). 1 图像路径,2 标签,3 图像名称(未使用)。 What I am trying to do is read in the CSV in pandas and then iterate over each roll yeilding the read in an image (Keras load_image) and the lable which was encoded using Pandas.我想要做的是在 Pandas 中读取 CSV 文件,然后迭代每个卷,以读取图像(Keras load_image)和使用 Pandas 编码的标签。 It gives me this error which makes me think it is the generator issue as it is not doing what it should.它给了我这个错误,这让我认为这是生成器问题,因为它没有做它应该做的。
ValueError: as_list() is not defined on an unknown TensorShape. ValueError: as_list() 未在未知的 TensorShape 上定义。
This is my code that I am working with.这是我正在使用的代码。
csv_path = '../Dataset/ShowData.csv'
df = pd.read_csv(csv_path)
base_path = "../Dataset/"
# Make one hot encoding for lables
le = LabelEncoder()
df['Label'] = le.fit_transform(df['Label'])
print(df.head(1)['Label'])
print(df.tail(1)['Label'])
'''
# View the labels (if you want)
list(encoder.classes_)
# Convert some integers into their category names
list(encoder.inverse_transform([2, 2, 1]))
'''
def process_dataframe(dataframe):
for index, row in dataframe.iterrows():
# print(row['Path'], row['Label'])
# Load image and get lable
img_path = os.path.join(base_path, row['Path'])
img = load_img(img_path, target_size=(200, 200))
img = img_to_array(img)
img = img/255 # normalize the image
label = row['Label']
#label = to_categorical(label, num_classes, dtype=tf.float32)
yield img, label
def generate_dataset(dataframe):
generator = lambda: process_dataframe(dataframe)
return tf.data.Dataset.from_generator(generator=generator,
output_types= (tf.float32, tf.int32))
dataset = generate_dataset(df)
data_batch = dataset.shuffle(10000).batch(32)
print(data_batch)
model = Sequential([
Flatten(input_shape=(200, 200, 3)),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.Accuracy()])
model.fit(data_batch, epochs=10, verbose=1)
Oh this is what the data_batch prints out哦,这是 data_batch 打印出来的
<DatasetV1Adapter shapes: (<unknown>, <unknown>), types: (tf.float32, tf.int32)>
And this is the label type这是标签类型
Name: Label, dtype: int32
You should define the output_shapes
for the tf.data.Dataset.from_generator
call.你应该定义output_shapes
为tf.data.Dataset.from_generator
电话。 You can find the documentation at the Tensorflow page .您可以在Tensorflow 页面找到文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.