我有输入函数,它从filename参数中读取所有行。然后它将解析器函数应用于每一行。我的.csv文件中的每一行如下:" path_to_img,label_num"。这是代码:
def input_fn(filename):
def parser(column):
d = tf.decode_csv(column, record_defaults=[[''], [-1]])
img, label = d
img = tf.read_file(img)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.resize_images(img, (300, 200))
img = tf.cast(img, tf.float32)
return {'image': img}, label
dataset = tf.data.TextLineDataset(filename).map(parser)
dataset = dataset.repeat(None).shuffle(5000).batch(500)
return dataset.make_one_shot_iterator().get_next()
然后我创建分类器并训练:
feat_cols = [tf.feature_column.numeric_column('image', shape=(300, 200))]
model = tf.estimator.DNNClassifier([4028, 1024, 512], feature_columns=feat_cols, model_dir=OUTDIR, n_classes=5)
model.train(input_fn=lambda: input_fn('data.csv'), steps=1000)
但最终我得到的错误与解码操作有关,但仍无法理解错误
tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got unknown format starting with '\"\"\"\n flickr.p'
[[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false](ReadFile)]]
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,300,200,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]