【问题标题】:Accuracy of categorical classification on my own photos get better?对我自己的照片进行分类的准确性会提高吗?
【发布时间】:2020-11-15 07:32:14
【问题描述】:

我尝试使用自己的照片“0、1、2、3、4、5、6、7、8、9”进行分类,例如 MNIST。 但是精度不太好。。下面的程序有什么问题吗? 如果你发现了什么,请给我一些建议。

精度:0.1000

print(x_train.shape)
print(x_test.shape)
#(80, 28, 28, 1)
#(20, 28, 28, 1)

print(y_train)
print(y_test)
#[7 5 4 2 8 8 0 1 8 7 4 7 3 4 2 5 2 2 5 3 0 4 9 1 9 0 5 7 6 6 8 0 1 0 8 0 2
# 8 9 9 4 7 4 0 6 1 1 2 1 9 6 7 4 3 7 4 9 4 0 8 5 2 3 8 1 9 3 7 3 5 4 2 3 1
# 0 9 6 0 6 8]
#[9 6 6 5 2 1 7 1 8 5 6 6 5 2 3 3 5 3 7 9]

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()

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

datagen = ImageDataGenerator(rescale=1./255)

history=model.fit_generator(
    datagen.flow(x_train,y_train,batch_size=32),
    steps_per_epoch=x_train.shape[0] // 32,
    epochs=100,
    validation_data=(x_test, y_test))

loss, acc = model.evaluate(x_train,y_train,verbose=2)
#80/1 - 0s - loss: 107.2582 - accuracy: 0.1000

【问题讨论】:

    标签: machine-learning keras neural-network


    【解决方案1】:

    您好,我认为您没有为 y_train 和 y_test 应用一个热编码标签

    from tensorflow.keras.utils import to_categorical
    
    # convert to one-hot vector
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)
    

    【讨论】:

    • 不是loss='sparse_categorical_crossentropy',像这里。
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2018-04-09
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2021-11-24
    相关资源
    最近更新 更多