机器学习回归模型为每个图像预测相同的值

时间:2020-05-09 14:32:18

标签: python machine-learning keras regression

我目前正在从事一个项目,其中涉及训练回归模型,保存该模型,然后将其加载以使用该模型进行进一步的预测。但是我有一个问题。每当我对图像进行model.predict时,都会给出相同的预测。我不确定是什么问题,也许是在培训阶段,或者我做错了什么。 我正在关注this tutorial

所有文件都在此github repo

以下是代码中的一些内容: (这部分是训练模型并保存它)

model = create_cnn(400, 400, 3, regress=True)
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)

model.fit(X, Y, epochs=70, batch_size=8)
model.save("D:/statispic2/final-statispic_model.hdf5")

下一个代码部分是从加载模型和进行预测开始。

model = load_model("D:/statispic2/statispic_model.hdf5")  # Loading the model
prediction = model.predict(images_ready_for_prediction) #images ready for prediction include a numpy array 
#that is loaded with the images just like I loaded them for the training stage.
print(prediction_list)

试用后,这是模型的输出预测:

[[0.05169942]  # I gave it 5 images as parameters 
[0.05169942]
[0.05169942]
[0.05169942]
[0.05169942]]

如果不清楚,或者您想查看更多代码,请告诉我。

4 个答案:

答案 0 :(得分:4)

人们说回归和 CNN 是两种完全不同的东西,显然他们错过了他们机器学习课程中的一些基本知识。是的,它们完全不同!但不应该比较;)

CNN 是一种深度神经网络,通常因其在图像上的使用而闻名。因此它是一个解决问题的框架,可以同时解决回归和分类问题。

回归是指您预测的输出类型。所以说实话,直接比较两者是很愚蠢的。

我无法评论本节中误导您的具体人,因为我需要具体的分数才能这样做。

不过,回到问题上来。您是在保存之前还是之后遇到这个问题?如果您之前遇到过,我会尝试将您的输出值缩放到更简单的分布。如果它在您保存后发生,我会查看您的框架版本以及它们如何保存它的文档。

也可能只是图片中没有信息。

答案 1 :(得分:2)

不,不,不!回归与CNN完全不同。做一点研究,差异将很快变得明显。同时,我将在这里与您共享两个代码示例。

回归:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline
import sklearn

from sklearn.datasets import load_boston
boston = load_boston()

# Now we will load the data into a pandas dataframe and then will print the first few rows of the data using the head() function.
bos = pd.DataFrame(boston.data)
bos.head()

bos.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT']
bos.head()

bos['MEDV'] = boston.target

bos.describe()

bos.isnull().sum()

sns.distplot(bos['MEDV'])
plt.show()

enter image description here

sns.pairplot(bos)

enter image description here

corr_mat = bos.corr().round(2)
sns.heatmap(data=corr_mat, annot=True)

enter image description here

sns.lmplot(x = 'RM', y = 'MEDV', data = bos)

X = bos[['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX','PTRATIO', 'B', 'LSTAT']]
y = bos['MEDV']

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 10)

# Training the Model
# We will now train our model using the LinearRegression function from the sklearn library.

from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(X_train, y_train)

# Prediction
# We will now make prediction on the test data using the LinearRegression function and plot a scatterplot between the test data and the predicted value.

prediction = lm.predict(X_test)
plt.scatter(y_test, prediction)

df1 = pd.DataFrame({'Actual': y_test, 'Predicted':prediction})
df2 = df1.head(10)
df2
df2.plot(kind = 'bar')

enter image description here

from sklearn import metrics
from sklearn.metrics import r2_score
print('MAE', metrics.mean_absolute_error(y_test, prediction))
print('MSE', metrics.mean_squared_error(y_test, prediction))
print('RMSE', np.sqrt(metrics.mean_squared_error(y_test, prediction)))
print('R squared error', r2_score(y_test, prediction))

结果:

MAE 4.061419182954711
MSE 34.413968453138565
RMSE 5.866341999333023
R squared error 0.6709339839115628

CNN:

# keras imports for the dataset and building our neural network
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten
from keras.utils import np_utils

# to calculate accuracy
from sklearn.metrics import accuracy_score

# loading the dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# building the input vector from the 28x28 pixels
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

# normalizing the data to help with the training
X_train /= 255
X_test /= 255

# one-hot encoding using keras' numpy-related utilities
n_classes = 10
print("Shape before one-hot encoding: ", y_train.shape)
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)
print("Shape after one-hot encoding: ", Y_train.shape)

# building a linear stack of layers with the sequential model
model = Sequential()
# convolutional layer
model.add(Conv2D(25, kernel_size=(3,3), strides=(1,1), padding='valid', activation='relu', input_shape=(28,28,1)))
model.add(MaxPool2D(pool_size=(1,1)))
# flatten output of conv
model.add(Flatten())
# hidden layer
model.add(Dense(100, activation='relu'))
# output layer
model.add(Dense(10, activation='softmax'))

# compiling the sequential model
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

# training the model for 10 epochs
model.fit(X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_test, Y_test))

结果:

Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 27s 451us/step - loss: 0.2037 - accuracy: 0.9400 - val_loss: 0.0866 - val_accuracy: 0.9745
Epoch 2/10
60000/60000 [==============================] - 27s 451us/step - loss: 0.0606 - accuracy: 0.9819 - val_loss: 0.0553 - val_accuracy: 0.9812
Epoch 3/10
60000/60000 [==============================] - 27s 445us/step - loss: 0.0352 - accuracy: 0.9892 - val_loss: 0.0533 - val_accuracy: 0.9824
Epoch 4/10
60000/60000 [==============================] - 27s 446us/step - loss: 0.0226 - accuracy: 0.9930 - val_loss: 0.0572 - val_accuracy: 0.9825
Epoch 5/10
60000/60000 [==============================] - 27s 448us/step - loss: 0.0148 - accuracy: 0.9959 - val_loss: 0.0516 - val_accuracy: 0.9834
Epoch 6/10
60000/60000 [==============================] - 27s 443us/step - loss: 0.0088 - accuracy: 0.9976 - val_loss: 0.0574 - val_accuracy: 0.9824
Epoch 7/10
60000/60000 [==============================] - 26s 442us/step - loss: 0.0089 - accuracy: 0.9973 - val_loss: 0.0526 - val_accuracy: 0.9847
Epoch 8/10
60000/60000 [==============================] - 26s 440us/step - loss: 0.0047 - accuracy: 0.9988 - val_loss: 0.0593 - val_accuracy: 0.9838
Epoch 9/10
60000/60000 [==============================] - 28s 469us/step - loss: 0.0056 - accuracy: 0.9986 - val_loss: 0.0559 - val_accuracy: 0.9836
Epoch 10/10
60000/60000 [==============================] - 27s 449us/step - loss: 0.0059 - accuracy: 0.9981 - val_loss: 0.0663 - val_accuracy: 0.9820

答案 2 :(得分:1)

Cnn是深度学习。您可以使用回归来计算数字,例如汽车价格。

答案 3 :(得分:0)

我的模型在 pickle.dumppickle.load 后遇到了完全相同的问题。我遗漏的问题是,在使用模型进行预测之前,我没有对特征(向量 X)进行归一化。希望能帮到你。