我在本地有一個(gè)非常龐大的圖像數(shù)據(jù)庫(kù),數(shù)據(jù)分布就像每個(gè)文件夾都包含一個(gè)類的圖像。我想使用 tensorflow 數(shù)據(jù)集 API 來(lái)批量獲取數(shù)據(jù),而無(wú)需將所有圖像加載到內(nèi)存中。我試過(guò)這樣的事情:def _parse_function(filename, label): image_string = tf.read_file(filename, "file_reader") image_decoded = tf.image.decode_jpeg(image_string, channels=3) image = tf.cast(image_decoded, tf.float32) return image, labelimage_list, label_list, label_map_dict = read_data()dataset = tf.data.Dataset.from_tensor_slices((tf.constant(image_list), tf.constant(label_list)))dataset = dataset.shuffle(len(image_list))dataset = dataset.repeat(epochs).batch(batch_size)dataset = dataset.map(_parse_function)iterator = dataset.make_one_shot_iterator()image_list 是一個(gè)列表,其中附加了圖像的路徑(和名稱),label_list 是一個(gè)列表,其中每個(gè)圖像的類都以相同的順序附加。但是 _parse_function 不起作用,我的錯(cuò)誤是:ValueError: Shape must be rank 0 but is rank 1 for 'file_reader' (op: 'ReadFile') with input shape: [?]。我用谷歌搜索了錯(cuò)誤,但對(duì)我沒(méi)有任何作用。如果我不使用 map 函數(shù),我只是記錄圖像的路徑(它們存儲(chǔ)在 image_list 中),所以我認(rèn)為我需要 map 函數(shù)來(lái)讀取圖像,但我無(wú)法使其工作。先感謝您。編輯: def read_data(): image_list = [] label_list = [] label_map_dict = {} count_label = 0 for class_name in os.listdir(base_path): class_path = os.path.join(base_path, class_name) label_map_dict[class_name]=count_label for image_name in os.listdir(class_path): image_path = os.path.join(class_path, image_name) label_list.append(count_label) image_list.append(image_path) count_label += 1
1 回答

泛舟湖上清波郎朗
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
錯(cuò)誤在這一行dataset = dataset.repeat(epochs).batch(batch_size)您的管道將批量大小添加為輸入的維度。
您需要像這樣在 map 函數(shù)之后批處理您的數(shù)據(jù)集
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(image_list), tf.constant(label_list)))
dataset = dataset.shuffle(len(image_list))
dataset = dataset.repeat(epochs)
dataset = dataset.map(_parse_function).batch(batch_size)
添加回答
舉報(bào)
0/150
提交
取消