【问题标题】:TF version : 2.4.1, TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of stringTF 版本:2.4.1,TypeError:“ReadFile”Op 的输入“文件名”的类型为 float32,与预期的字符串类型不匹配
【发布时间】:2021-04-03 16:28:25
【问题描述】:

我在 Google Colab 上使用此数据 http://weegee.vision.ucmerced.edu/datasets/landuse.html

尝试使用以下方法将图像加载为数据框:

# Download and unzip images
!wget http://weegee.vision.ucmerced.edu/datasets/UCMerced_LandUse.zip
!unzip UCMerced_LandUse.zip
print("DONE!")
filename = '//content/UCMerced_LandUse/Images'
data = tf.keras.preprocessing.image_dataset_from_directory(filename)

我收到以下错误:

TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string.

Error output

它找到了带有标签的文件夹,但它错过了每个标签文件夹中的所有 100 张图像。图片格式为“.tif”。

在图片中可以观察到目录结构。

功能:

tf.keras.preprocessing.image.load_img('/content/UCMerced_LandUse/Images/agricultural/agricultural00.tif', grayscale=False, color_mode="rgb", target_size=None, interpolation="nearest")

Works well and show the image.

我已经尝试了以下帖子中的所有内容: TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string 比如重命名文件:

all_dir = glob.glob('UCMerced_LandUse/Images/*/')
for dir in sort(all_dir):
    name = dir.split('/')[2]
    for img, n in zip(sort(glob.glob(dir + '*.tif')), range(0,100)):  
       new_file = os.path.join(dir, '{}{}.tif'.format(name, n))
       os.rename(img, str(new_file))

以及互联网上提出的其他一些“解决方案”。但是没有人解决这个问题。

如果你能从我失败的地方找到一些线索,我们将不胜感激。

谢谢。

【问题讨论】:

    标签: python tensorflow keras image-preprocessing


    【解决方案1】:

    查看问题后我意识到:

    image_dataset_from_directory 函数不接受 .tif 格式,如文档所示:

    Supported image formats: jpeg, png, bmp, gif. Animated gifs are truncated to the first frame.

    我必须使用 flow_from_directory 方法和 ImageDataGenerator 类来代替 image_dataset_from_directory。

    首先我生成代码来分割训练和验证文件夹中的图像:

    import shutil
    import random
    
    
    validaton = 33
    
    for dir in all_dir:
       validation = []
       train = []
       validation = random.sample(list(sort(glob.glob(dir + '*.tif'))), validaton)
       for i_val in glob.glob(dir + '*.tif'):
           if i_val not in validation:
              train.append(i_val)
      
    label_name = dir.split('/')[-2]
    if not os.path.exists('/content/UCM_cust/validation'+'/'+label_name) & 
        os.path.exists('/content/UCM_cust/train'+'/'+label_name):
        os.mkdir('/content/UCM_cust/validation'+'/'+label_name)
        os.mkdir('/content/UCM_cust/train'+'/'+label_name)
    
    
    for z in validation:
       dest_name = None
       dest_name = z.split('/')[-1]
       shutil.copyfile(z,'/content/UCM_cust/validation/'+label_name+'/'+dest_name)
    
    for t in train:
       dest_name = None
       dest_name = t.split('/')[-1]
       shutil.copyfile(t, '/content/UCM_cust/train/'+label_name+'/'+dest_name)
    

    然后我按照blog.keras的教程,使用以下代码加载数据

    batch_size = 32
    
    # this is the augmentation configuration we will use for training
    train_datagen = ImageDataGenerator(rescale=1./255)
    
    
    test_datagen = ImageDataGenerator(rescale=1./255)
    
    # this is a generator that will read pictures found in
    # subfolers of 'data/train', and indefinitely generate
    
    train_generator = train_datagen.flow_from_directory(
        '/content/UCM_cust/train',  # this is the target directory
        target_size=(256, 256),  
        batch_size=batch_size,
        class_mode='categorical')  
    
    # this is a similar generator, for validation data
    validation_generator = test_datagen.flow_from_directory(
        '/content/UCM_cust/validation',
        target_size=(256, 256),
        batch_size=batch_size,
        class_mode='categorical')
    

    火车: 找到属于 21 个类别的 1407 张图片。

    验证: 找到属于 21 个类别的 693 张图片。

    我希望这对其他人有用

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2021-05-04
      • 2021-01-06
      相关资源
      最近更新 更多