Я изо всех сил пытаюсь понять, почему эта функция работает:
def tf_load_data(training, batch_size, img_path_list, label_list):
# Arguments:
# path_to_image: a Tensor of type string
# returns 3-D float Tensor of shape [new_height, new_width, channels]
def get_img(path_to_img):
# load the raw data, encoded as string.
raw_img = tf.io.read_file(path_to_img)
# Creates a 3D uint8 tensor.
img = ts.io.decode_png(raw_img, channels=3) # pictures are not saved as Grayscale
# Changes the values in the tensor to be floats in [0,1). -- Normalization
img = ts.image.convert_image_dtype(img, tf.float32)
# Resize all pictures to the same format.
return ts.image.resize(img, [constant.IMG_WIDTH, constant.IMG_HEIGHT])
# Arguments:
# label_string: as byte:32 which represents a string
# path_to_image: as byte:32 which represents a string
# returns a pair of two Tensors
def get_pair(path_to_img, label_string):
return get_img(path_to_img), lable_string
# Arguments: -- function is use together with tf.data.Dataset.map or tf.data.Dataset.apply
# img: is a Tensor of type String
# label: is a Tensor of type String
# return: the type is the same as input
def pre_process(img, label):
# Do all the pre-processing:
return img, label
dataset = tf.data.Dataset.from_tensor_slices((img_path_list, label_list))
dataset_tensor = dataset.map(map_func=get_pair, num_parallel_calls=None)
img_path_list и label_list являются списками типа string.
То, что я не получаю, это то, что, очевидно, dataset = tf.data.Dataset.from_tensor_slices((img_path_list, label_list))
- Тензор, содержащий кортежи (). Поэтому, когда я запускаю .map(map_func=get_pair, num_parallel_calls=None)
, две строки передаются как кортеж в функцию get_pair(path_to_img, label_string):
. Одна из этих строк затем передается в функцию get_img(path_to_img)
и, наконец, tf.io.read_file(path_to_img)
. Проблема в том, что io.read_file()
требуется ввод: "Тензор типа string" (см. Документы: https://www.tensorflow.org/api_docs/python/tf/io/read_file). Но строка! = Тензор типа строка:
isinstance(tf.constant["hello"], tf.Tensor) == True
isinstance("hello", tf.Tensor) == False
, а также: isinstance(["hello"], tf.Tensor) == False
Спасибо за помощь!