【发布时间】:2021-04-15 08:24:48
【问题描述】:
我正在尝试运行一个 CNN,其中输入图像具有三个通道 (rgb),而标签(目标)图像是灰度图像(1 个通道)。输入和标签图像为float32和tif格式。
我得到的图像和标签瓦片对列表如下:
def get_train_test_lists(imdir, lbldir):
imgs = glob.glob(imdir+"/*.tif")
dset_list = []
for img in imgs:
filename_split = os.path.splitext(img)
filename_zero, fileext = filename_split
basename = os.path.basename(filename_zero)
dset_list.append(basename)
x_filenames = []
y_filenames = []
for img_id in dset_list:
x_filenames.append(os.path.join(imdir, "{}.tif".format(img_id)))
y_filenames.append(os.path.join(lbldir, "{}.tif".format(img_id)))
print("number of images: ", len(dset_list))
return dset_list, x_filenames, y_filenames
train_list, x_train_filenames, y_train_filenames = get_train_test_lists(img_dir, label_dir)
test_list, x_test_filenames, y_test_filenames = get_train_test_lists(test_img_dir, test_label_dir)
from sklearn.model_selection import train_test_split
x_train_filenames, x_val_filenames, y_train_filenames, y_val_filenames =
train_test_split(x_train_filenames, y_train_filenames, test_size=0.1, random_state=42)
num_train_examples = len(x_train_filenames)
num_val_examples = len(x_val_filenames)
num_test_examples = len(x_test_filenames)
为了将瓦片读入张量,首先我定义了图像尺寸和批量大小:
img_shape = (128, 128, 3)
batch_size = 2
我注意到基于this link 的 tif 图像在 tensorflow 中没有解码器。 tfio.experimental.image.decode_tiff 可以使用,但it decodes to unit8 tensor.
这是png images的示例代码:
def _process_pathnames(fname, label_path):
# We map this function onto each pathname pair
img_str = tf.io.read_file(fname)
img = tf.image.decode_png(img_str, channels=3)
label_img_str = tf.io.read_file(label_path)
# These are png images so they return as (num_frames, h, w, c)
label_img = tf.image.decode_png(label_img_str, channels=1)
# The label image should have any values between 0 and 9, indicating pixel wise
# cropt type class or background (0). We take the first channel only.
label_img = label_img[:, :, 0]
label_img = tf.expand_dims(label_img, axis=-1)
return img, label_img
是否可以通过tf.convert_to_tensor 或任何其他选项修改此代码以从 tif 图像中获取 float32 张量? (我之前问过this question,但我不知道如何将tf.convert_to_tensor与提到的代码集成)
【问题讨论】:
标签: python tensorflow conv-neural-network