tensorflow训练常见问题1——accuracy=0

1问题

16/739 […] - ETA: 5:53 - loss: 0.4488 - acc: 0.0000e+00

2解决方法

2.1激活函数用错了

多分类要用softmax,不用sigmoid

2.2数据没有预处理

比如分类的时候,需要把结果数据
转换为0,1,2形式
比如下面在语义分割的时候,需要先检查数据,
把像素值先做归一化

检查数据:大小,通道,值的范围,值都有哪些

img = tf.io.read_file(r'G:\XiaoMa\08OwnWork\zhongxian\语义分割数据\images\z1.jpg')
img = tf.image.decode_png(img)  # 解码图像
print("img的格式:")
print(img.shape)
img = tf.squeeze(img)   # 把3维的图像压缩成2维,就是把维度为1的都压缩了
print(img.shape)        # 图像的形状大小
print(img.numpy().max())  # 图像像元值的最大值
np.unique(img.numpy())  # 查看图像像元值的具体值有哪些
print(np.unique(img.numpy()))

img2 = tf.io.read_file(r'G:\XiaoMa\Bursxylophilus\dataset\SemSegdataset\ceshi\onepng\1_1zx53.png')
img2 = tf.image.decode_png(img2)  # 解码图像
print("envi里单波段提取后:")
print('原始图像的形状:',img2.shape)
img2 = tf.squeeze(img2)   # 把3维的图像压缩成2维,就是把维度为1的都压缩了
print('压缩后图像的形状:',img2.shape)        # 图像的形状大小
print('图像的最大值:',img2.numpy().max())  # 图像像元值的最大值
print("像元值具体有哪些:",np.unique(img2.numpy()))

解决方法:解码,归一化等

images = glob.glob(r'G:\XiaoMa\08OwnWork\zhongxian\语义分割数据\images\*.jpg')
len(images)

anno = glob.glob(r'G:\XiaoMa\08OwnWork\zhongxian\语义分割数据\单波段处理后的png\*.png')

print(anno[-5:])
print(images[-5:])


# 进行乱序,inages和anno必须保持一致
np.random.seed(2019)   # 随机数种子
index = np.random.permutation(len(images))  # 随机数的索引,随机排列序列,https://blog.csdn.net/weixin_44188264/article/details/93752505
images = np.array(images)[index]
anno = np.array(anno)[index]
print(anno[-5:])
print(images[-5:])       # 查看标签和影像是否是一一对应

# 将读取的图片转换为数据集
dataset = tf.data.Dataset.from_tensor_slices((images,anno))
test_count = int(len(images)*0.2)             # 一部分为测试集
train_count = len(images)-test_count          # 一部分为训练集
print("测试和训练数据集数量:")
print(test_count,train_count)

data_train = dataset.skip(test_count)       # 跳过多少个进行选取
data_test = dataset.take(test_count)        # 选取多少个


def read_jpg(path):
    """读取并解码jpg图像"""
    img_de = tf.io.read_file(path)
    img_de = tf.image.decode_jpeg(img_de,channels=3)
    return img_de

def read_png(path):
    """读取并解码png图像"""
    img_de_png = tf.io.read_file(path)
    img_de_png = tf.image.decode_png(img_de_png,channels=1)
    return img_de_png


def normal_img(input_images,input_anno):
    """数据归一化"""
    input_images = tf.cast(input_images,tf.float32)
    input_images = input_images/127.5-1
    input_anno = tf.cast(input_anno, tf.float32)
    input_anno = input_anno/255.0
    # 如果是1,2,3,就减1
    # input_anno -= 1
    return input_images,input_anno


def load_images(input_images_path,input_anno_path):
    """加载图片并改变图像大小"""
    input_image = read_jpg(input_images_path)
    input_anno = read_png(input_anno_path)
    input_image = tf.image.resize(input_image,(608,608))    # 这个resize()的原理
    input_anno = tf.image.resize(input_anno,(608,608))
    return normal_img(input_image,input_anno)


# 对图像进行预处理
data_train = data_train.map(load_images)     # map()函数是,对所有数据用某个函数进行处理
data_test = data_test.map(load_images)

BATCH_SIZE = 2
#  repeat()函数就是对数据集进行重复,防止将数据读取完 https://blog.csdn.net/seuzhouchenglong/article/details/104047784
#  shuffle()函数就是将数据打乱
data_train = data_train.repeat().shuffle(30).batch(BATCH_SIZE)
data_test = data_test.batch(BATCH_SIZE)

2.3损失函数用错了

在这里插入图片描述

2.4学习率太大了

学习率太大,拟合不了细节,
在语义分割通常表现为验证集准确率不变

2.5批次量太小了

批次量太小也会导致学习不到整体细节

2.6epoch太小

数据还没学习就结束了

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓码bigdata

如果文章给您带来帮助,感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值