使用的是cifar-10数据集,数据格式为.png。
训练集有50000个,但fit的时候只有782个,不知道出了什么问题,有大佬解答一下吗?
cifa-10训练集链接如下
链接:https://pan.baidu.com/s/1YkzxuVnOKH1JJYkYmtlGMw?pwd=wk3g
提取码:wk3g
--来自百度网盘超级会员V3的分享
完整代码如下
import os
import tensorflow as tf
from tensorflow.python.keras import layers, models
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def load_data(path):
image_folder = path
# 获取文件夹中所有图片的路径
image_paths = [os.path.join(image_folder, f)
for f in os.listdir(image_folder) if f.endswith('.png')]
# 将图片路径切片,再转为数字
labels = []
labels_all = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
for fn in os.listdir(image_folder):
label_name = fn.split("_")[1].split('.')[0]
index = labels_all.index(label_name)
labels.append(index)
dataset = tf.data.Dataset.from_tensor_slices((image_paths, labels))
# 定义函数,传入图片路径和标签名,返回张量和独热码
def train_data(image_path, label):
image = tf.io.read_file(image_path)
image = tf.image.decode_png(image, channels=3)
image = tf.image.resize(image, [32, 32]) # 原始图片大小重设为(32,32)
image = image / 255.0
label = tf.one_hot(label, depth=10)
return image, label
# 使用map方法加载数据
dataset = dataset.map(train_data)
return dataset
path1 = './cifar/train/'
dataset = load_data(path1)
print(dataset)
dataset = dataset.shuffle(1337, seed=10).batch(64)
def build_model():
model = models.Sequential()
# 卷积32,(3,3)——池化——卷积64,(3,3)——池化——卷积64,(3,3)——全连接——输出
# 卷积32,(3,3)
model.add(layers.Conv2D(input_shape=(32, 32, 3), filters=32,
kernel_size=(3, 3), strides=(1, 1),
padding='valid', activation='relu'))
# 池化,最大化抽样,窗口2*2
model.add(layers.MaxPool2D(pool_size=(2, 2)))
# 卷积64,(3,3)
model.add(layers.Conv2D(filters=64,
kernel_size=(3, 3), strides=(1, 1),
padding='valid', activation='relu'))
# 池化,最大化抽样,窗口2*2
model.add(layers.MaxPool2D(pool_size=(2, 2)))
# 卷积64,(3,3)
model.add(layers.Conv2D(filters=64,
kernel_size=(3, 3), strides=(1, 1),
padding='valid', activation='relu'))
# 全连接层、flattern()将卷积和池化后提取的特征摊平后输入全连接网络
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
# 分类
model.add(layers.Dense(10, activation='softmax'))
# 模型编译
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
model = build_model()
history = model.fit(dataset,
epochs=10,
verbose=2)
path2 = './cifar/test/'
dataset_test = load_data(path2)
dataset_test = dataset_test.shuffle(1337, seed=10).batch(64)
test_loss, test_acc = model.evaluate(dataset_test,
verbose=2)
print(test_acc)
输出结果为
如上图所示,只fit了782个训练集,求大佬帮忙解决。
训练集有50000个,但fit的时候只有782个,不知道出了什么问题,有大佬解答一下吗?
cifa-10训练集链接如下
链接:https://pan.baidu.com/s/1YkzxuVnOKH1JJYkYmtlGMw?pwd=wk3g
提取码:wk3g
--来自百度网盘超级会员V3的分享
完整代码如下
import os
import tensorflow as tf
from tensorflow.python.keras import layers, models
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def load_data(path):
image_folder = path
# 获取文件夹中所有图片的路径
image_paths = [os.path.join(image_folder, f)
for f in os.listdir(image_folder) if f.endswith('.png')]
# 将图片路径切片,再转为数字
labels = []
labels_all = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
for fn in os.listdir(image_folder):
label_name = fn.split("_")[1].split('.')[0]
index = labels_all.index(label_name)
labels.append(index)
dataset = tf.data.Dataset.from_tensor_slices((image_paths, labels))
# 定义函数,传入图片路径和标签名,返回张量和独热码
def train_data(image_path, label):
image = tf.io.read_file(image_path)
image = tf.image.decode_png(image, channels=3)
image = tf.image.resize(image, [32, 32]) # 原始图片大小重设为(32,32)
image = image / 255.0
label = tf.one_hot(label, depth=10)
return image, label
# 使用map方法加载数据
dataset = dataset.map(train_data)
return dataset
path1 = './cifar/train/'
dataset = load_data(path1)
print(dataset)
dataset = dataset.shuffle(1337, seed=10).batch(64)
def build_model():
model = models.Sequential()
# 卷积32,(3,3)——池化——卷积64,(3,3)——池化——卷积64,(3,3)——全连接——输出
# 卷积32,(3,3)
model.add(layers.Conv2D(input_shape=(32, 32, 3), filters=32,
kernel_size=(3, 3), strides=(1, 1),
padding='valid', activation='relu'))
# 池化,最大化抽样,窗口2*2
model.add(layers.MaxPool2D(pool_size=(2, 2)))
# 卷积64,(3,3)
model.add(layers.Conv2D(filters=64,
kernel_size=(3, 3), strides=(1, 1),
padding='valid', activation='relu'))
# 池化,最大化抽样,窗口2*2
model.add(layers.MaxPool2D(pool_size=(2, 2)))
# 卷积64,(3,3)
model.add(layers.Conv2D(filters=64,
kernel_size=(3, 3), strides=(1, 1),
padding='valid', activation='relu'))
# 全连接层、flattern()将卷积和池化后提取的特征摊平后输入全连接网络
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
# 分类
model.add(layers.Dense(10, activation='softmax'))
# 模型编译
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
model = build_model()
history = model.fit(dataset,
epochs=10,
verbose=2)
path2 = './cifar/test/'
dataset_test = load_data(path2)
dataset_test = dataset_test.shuffle(1337, seed=10).batch(64)
test_loss, test_acc = model.evaluate(dataset_test,
verbose=2)
print(test_acc)
输出结果为
如上图所示,只fit了782个训练集,求大佬帮忙解决。