当前位置: 首页 > 知识库问答 >
问题:

为什么keras模型不那么准确,不被认可?

湛联
2023-03-14

我下载了mnist数据集(jpg),并用. hdf(. h5)创建了一个模型文件。
图像是使用. h5模型识别的,但是识别率很低...

实际编译时精度很低...
我做了吗有问题吗?。

图像使用了灰度的28x28图像...

import os
import cv2
import numpy as np
import tensorflow as tf
from PIL import Image
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import random

FILENAME = 'model.h5'

WIDTH = 28
HEIGHT = 28
EPOCHES = 100
BATCH_SIZE = 32

def create_dataset(img_folder):
    img_data_array=[]
    class_name=[]
   
    for path in os.listdir(img_folder):
        if path == ".DS_Store":
            continue
        for file in os.listdir(os.path.join(img_folder, path)):
            if file == ".DS_Store":
                continue
            image_path = os.path.join(img_folder, path,  file)

            image = cv2.imread( image_path, cv2.IMREAD_UNCHANGED)


            image = cv2.resize(image, (HEIGHT, WIDTH),interpolation = cv2.INTER_AREA)
            image = np.array(image)
            
            image = image.astype('float32')
            image /= 255 
            img_data_array.append(image)
            class_name.append(path)
    return img_data_array, class_name

img_data, class_name = create_dataset(r'/Users/animalman/Documents/test/grayscale/train')
test, test_class_name = create_dataset(r'/Users/animalman/Documents/test/grayscale/test')

target_dict = {k: v for v, k in enumerate(np.unique(class_name))}
target_val = [target_dict[class_name[i]] for i in range(len(class_name))]

test_dict = {k: v for v, k in enumerate(np.unique(test_class_name))}
test_val = [test_dict[test_class_name[i]] for i in range(len(test_class_name))]

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# tensor
history = model.fit(x=tf.cast(np.array(img_data), tf.float64), y=tf.cast(list(map(int,target_val)),tf.int32), epochs=EPOCHES, batch_size=BATCH_SIZE, validation_split=0.33)

evaluate = model.evaluate(x=tf.cast(np.array(img_data), tf.float64), y=tf.cast(list(map(int,target_val)),tf.int32), batch_size=BATCH_SIZE)
print('Train:', evaluate)

test_evaluate = model.evaluate(x=tf.cast(np.array(test), tf.float64), y=tf.cast(list(map(int,test_val)),tf.int32), batch_size=BATCH_SIZE)
print('Test:', test_evaluate)

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

test_loss, test_acc = model.evaluate(x_test, y_test)
print('mnist', test_acc)


model.save(FILENAME)

...

历元98/100 1257/1257[==========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================1.3.3.3 E-08-准确:1.3.3.3.3-3.3 E-08-准确:1.3.3.3 E-08-精确:1.3.3.3.3.3.3.3.3.3.3.3.3.4.4.4.4.4.28-val-val/u损失:val-val(val)损失:val(val)损失:损失:1.4.4.4.4.4628-val(val(val)val(val(val)损失:val(val)损失:val(val(val)损失:损失:损失:损失:损失:3.4.4.4.4.4628)val(val(val(val)准确:3.4.4.28)val(val(val)准确:val(val(val(val)准确:val)损失:val)0.699000009536743]
313/313[================================================-0s 850us/步-损耗:3887.2236-精度:0.6991
mnist:0.6991000175476074

共有1个答案

祖波光
2023-03-14

从这里

Epoch 100/100 1257/1257 [==============================] - 3s 2ms/step - loss: 2.3033e-08 - accuracy: 1.0000 - val_loss: 43.4628 - val_accuracy: 0.1136

您可以看到训练acc是1.0,验证acc是0.1136

-

关于过度装配:https://en.wikipedia.org/wiki/Overfitting

 类似资料:
  • 这样一个看似简单的数字怎么会“太大”而无法在64位内存中表达呢?

  • 我的代码是: 我的数据如下: 我的结果是: 两个时代后它就卡在那里了。我能做些什么来防止它这么快卡住?

  • 我有一个问题,关于什么是正确的做法,使用SwingU实用程序的调用稍后方法。 所以首先,我想确认我理解正确。 据我所知,对GUI的更改必须在EDT上完成,因为Swing组件不是线程安全的。invokeLater方法将Runnable作为参数,该Runnable中包含的任何内容都将在EDT上运行。因此,对Swing组件的任何调用都被放入一种队列中,在EDT上一次执行一个。 有了这些,我的问题是:使用

  • 问题内容: 我问了一个一般性的Spring问题:自动播发Spring Bean,并让多个人回答说应尽可能避免调用Spring 。这是为什么? 我还应该如何访问配置了Spring创建的Bean? 我在非Web应用程序中使用Spring,并计划按照LiorH的描述访问共享对象。 修正案 我接受下面的答案,但这是Martin Fowler的另一种选择,他讨论了依赖注入与使用(本质上与调用相同)的优点。

  • 以上是目前我的CNN的架构。然而,它说它有1.8m可训练的参数。为什么会这样?我以为第一层给出了(32*4=128个参数),但是我如何找到模型的其余部分有多少个参数? 我的理解是,CNN架构应该只依赖于过滤和最大池,因为它们是共享权重。为什么我有这么多参数?我应该如何着手减少这个数字? 我不是问如何使用“汇总”找到参数的数量。我是问为什么我的模型有这么多参数,以及我如何减少这个数字。我不直观地理解

  • 问题内容: 为什么是选择?如果添加了想要的新列,这是否意味着更少的代码更改? 我知道这在某些数据库上是性能问题,但是如果您真的想要每列,该怎么办? 问题答案: 确实有三个主要原因: 将数据转移到消费者方面效率低下。 当您选择SELECT *时,通常从数据库中检索到的列数超出了应用程序实际需要运行的列数。这将导致更多数据从数据库服务器移至客户端,从而减慢访问速度并增加计算机上的负载,并花费更多时间在