机器学习回归模型为每张图像预测相同的值
Posted
技术标签:
【中文标题】机器学习回归模型为每张图像预测相同的值【英文标题】:Machine learning regression model predicts same value for every image 【发布时间】:2020-08-25 03:28:30 【问题描述】:我目前正在开展一个项目,该项目涉及训练回归模型、保存它然后加载它以使用该模型进行进一步的预测。但是我遇到了问题。每次我对图像建模时,它都会给出相同的预测。我不完全确定问题出在哪里,也许是在训练阶段,或者我只是做错了什么。 我在关注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]]
如果有任何不清楚的地方,或者您想查看更多代码,请告诉我。
【问题讨论】:
我已经通过将所有像素除以 255.0 预先对图像进行了归一化 @Mike 不,这是一个回归模型。 标题中提到模型是回归模型。 您希望预测什么? (不要介意房价与图片) 嗨@NicolasGervais!我想过给你们代码文件,这样你们就可以更好地理解我想要做什么,但是问题会变得混乱,可能会得到很多反对意见。你会如何建议我这样做?所以你们可以帮我查明真相。 【参考方案1】:说回归和 CNN 是两个完全不同的东西的人显然错过了他们在机器学习课程中的一些基本知识。是的,它们完全不同!但不应该比较;)
CNN 是一种深度神经网络,通常因其在图像上的使用而闻名。因此它是一个解决问题的框架,可以解决回归和分类问题。
回归是指您预测的输出类型。所以说实话直接比较两者是很愚蠢的。
我无法评论本节中误导您的特定人员,因为我需要特定数量的积分才能这样做。
但是,回到问题上来。您是在保存之前还是之后遇到这个问题?如果您以前遇到过,我会尝试将您的输出值缩放到更容易的分布。如果它发生在您保存后,我会查看您的框架版本以及他们如何保存它的文档。
也有可能只是图片中没有信息。
【讨论】:
【参考方案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()
sns.pairplot(bos)
corr_mat = bos.corr().round(2)
sns.heatmap(data=corr_mat, annot=True)
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')
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
【讨论】:
【参考方案3】:CNN 是深度学习。您使用回归模型来计算数字,例如汽车的价格。
【讨论】:
【参考方案4】:在pickle.dump
和pickle.load
我的模型之后,我遇到了完全相同的问题。我缺少的问题是在使用模型进行预测之前我没有对特征(向量 X)进行归一化。希望对你有帮助。
【讨论】:
【参考方案5】:将优化器从 Adam() 更改为 RMSprop(),学习率 >0.001 对我有用。
【讨论】:
以上是关于机器学习回归模型为每张图像预测相同的值的主要内容,如果未能解决你的问题,请参考以下文章