Model.fit()的准确性很高,但model.predict()的准确性确实很差

时间:2020-03-25 17:45:20

标签: python tensorflow keras

我正在尝试建立一个神经网络,以检测不同种类的乐高积木。我安装了模型,并且精度超过90%。但是,当我预测其他照片时,几乎所有照片都是错误的。这是我的模型代码:

        model = Sequential()

        model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        for l in range(conv_layer-1):
            model.add(Conv2D(layer_size, (3, 3)))
            model.add(Activation('relu'))
            model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors

        for d in range(dense_layer):
            model.add(Dense(layer_size))
            model.add(Activation('relu'))

        model.add(Dense(1))
        model.add(Activation('sigmoid'))

        tb = TensorBoard(log_dir="logs\\{}".format(NAME))

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

        model.fit(X, y, batch_size=32, epochs=10, validation_split=0.2, callbacks=[tb])

        path = "C:/Users/antho/Bachelorproef/LegoFotos/{}".format(TABLE)             # PATH aanpassen naar waar alle fotos van de legoblokjes staan

        for img in os.listdir(path)[:10]:
            path2 = os.path.join(path,img)
            IMG_SIZE = 100
            img_array = cv2.imread(path2, cv2.IMREAD_GRAYSCALE)  
            new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
            new_array = new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
            pred = model.predict(new_array)
            print(pred)

        model.save("2DENS_4CONV_128SIZE")

这是我的输出:

'''1600/1600 [==============================] - 5s 3ms/sample - loss: 0.2123 - accuracy: 0.9112 - val_loss: 0.2395 - val_accuracy: 0.8925
[[0.]]
[[0.]]
[[0.]]
[[0.]]
[[0.]]
[[1.]]
[[0.]]
[[1.]]
[[0.]]
[[0.]]
2020-03-25 18:40:59.061979: W tensorflow/python/util/util.cc:319] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:tensorflow:From C:\Users\antho\anaconda3\envs\tf20\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1786: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.'''

有人可以帮助我吗?

0 个答案:

没有答案