问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
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'
我的解答思路和尝试过的方法
我想要达到的结果