three_cranes 2022-04-28 14:35 采纳率: 100%
浏览 217
已结题

图像识别,猫狗图像识别系统提取数据报错,找不到文件夹

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

import os
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator

#数据所在文件夹
base_dir = './Users/Que.s/graduation_project/catdogdataset'
train_dir = os.path.join(base_dir,'train')
validation_dir = os.path.join(base_dir,'validation')

#训练集
train_cat_dir = os.path.join(train_dir,'cat')
train_dog_dir = os.path.join(train_dir,'dog')

#验证集
validation_cat_dir = os.path.join(train_dir,'cat')
validation_dog_dir = os.path.join(train_dir,'dog')

model = tf.keras.models.Sequential([
    #如果训练慢,可以把数据设置的更小一点
    tf.keras.layers.Conv2D(32,(3,3), activation='relu', input_shape=(64, 64, 3)),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(64,(3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(128,(3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    #为全连接层准备
    tf.keras.layers.Flatten(),
    
    tf.keras.layers.Dense(512, activation='relu'),
    
    #二分类sigmoid就够了
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(loss='binary_crossentropy',
              optimizer = Adam(lr = 1e-4),
              metrics=['acc'])

train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size = (64,64),
    batch_size = 20,
    class_mode = 'binary')


validation_generator = test_datagen.flow_from_directory(
    validation_dir,
    target_size = (64,64),
    batch_size = 20,
    class_mode = 'binary')
运行结果及报错内容 :训练集不会报错,但是显示0张图片,验证集直接报错,附上了我的文件夹格式

Found 0 images belonging to 0 classes.
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9136/1350380537.py in <module>
      6 
      7 
----> 8 validation_generator = test_datagen.flow_from_directory(
      9     validation_dir,
     10     target_size = (64,64),

~\AppData\Roaming\Python\Python39\site-packages\keras\preprocessing\image.py in flow_from_directory(self, directory, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)
    974             and `y` is a numpy array of corresponding labels.
    975     """
--> 976     return DirectoryIterator(
    977         directory,
    978         self,

~\AppData\Roaming\Python\Python39\site-packages\keras\preprocessing\image.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
    392         dtype = backend.floatx()
    393       kwargs['dtype'] = dtype
--> 394     super(DirectoryIterator, self).__init__(
    395         directory, image_data_generator,
    396         target_size=target_size,

~\AppData\Roaming\Python\Python39\site-packages\keras_preprocessing\image\directory_iterator.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
    113         if not classes:
    114             classes = []
--> 115             for subdir in sorted(os.listdir(directory)):
    116                 if os.path.isdir(os.path.join(directory, subdir)):
    117                     classes.append(subdir)

FileNotFoundError: [WinError 3] 系统找不到指定的路径。: './Users/Que.s/graduation_project/catdogdataset\\validation'

img

img

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 不会长胖的斜杠 后端领域新星创作者 2022-04-28 14:52
    关注

    base_dir = './Users/Que.s/graduation_project/catdogdataset'
    改成
    base_dir = 'C:/Users/Que.s/graduation_project/catdogdataset'
    有帮助望采纳,谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解