MATLAB和Keras上的“相同”网络有非常不同的结果

时间:2018-11-06 14:47:13

标签: python matlab tensorflow keras conv-neural-network

我一直在尝试在MATLAB和Keras中复制相同的简单网络结构。问题是我得到的精度有很大不同。使用Keras使用双历元训练和相同数据时,MATLAB代码的精度接近0.84,损耗接近17,Keras代码的精度接近0.63,损耗接近130。我认为差异太大,无法执行,因此我想缺少一些东西。

原始代码来自MATLAB示例,在其中进行了一些更改以避免在第一层中进行标准化。

这是MATLAB代码:

% Load the digit training set as 4-D array data using
% |digitTrain4DArrayData|.
[trainImages,~,trainAngles] = digitTrain4DArrayData;

disp("Train Images:")
disp(trainImages(:,:,:,1))
% Display 20 random sample training digits using |imshow|.
numTrainImages = size(trainImages,4);

figure
idx = randperm(numTrainImages,20);
for i = 1:numel(idx)
    subplot(4,5,i)

    imshow(trainImages(:,:,:,idx(i)))
    drawnow
end

%%
% Combine all the layers together in a |Layer| array.
layers = [ ...
    imageInputLayer([28 28 1], 'Normalization', 'none')
    convolution2dLayer(12,25)
    reluLayer
    fullyConnectedLayer(1)
    regressionLayer];

%% Train Network'

options = trainingOptions('sgdm','InitialLearnRate',0.001, ...
    'MaxEpochs',15)

net = trainNetwork(trainImages,trainAngles,layers,options)

net.Layers

%% Test Network
[testImages,~,testAngles] = digitTest4DArrayData;

predictedTestAngles = predict(net,testImages);

% *Evaluate Performance*
predictionError = testAngles - predictedTestAngles;

thr = 10;
numCorrect = sum(abs(predictionError) < thr);
numTestImages = size(testImages,4);

accuracy = numCorrect/numTestImages

%%
% Use the root-mean-square error (RMSE) to measure the differences between
% the predicted and actual angles of rotation.
squares = predictionError.^2;
rmse = sqrt(mean(squares))

%%
% *Display Box Plot of Residuals for Each Digit Class*
residuals = testAngles - predictedTestAngles;

residualMatrix = reshape(residuals,500,10);

figure
boxplot(residualMatrix, ...
    'Labels',{'0','1','2','3','4','5','6','7','8','9'})

xlabel('Digit Class')
ylabel('Degrees Error')
title('Residuals')

idx = randperm(numTestImages,49);
for i = 1:numel(idx)
    image = testImages(:,:,:,idx(i));
    predictedAngle = predictedTestAngles(idx(i));

    imagesRotated(:,:,:,i) = imrotate(image,predictedAngle,'bicubic','crop');
end

figure
subplot(1,2,1)
montage(testImages(:,:,:,idx))
title('Original')

subplot(1,2,2)
montage(imagesRotated)
title('Corrected')

这是Keras代码:

import numpy as np
import scipy.io
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense, Activation, Conv2D, BatchNormalization, AveragePooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras import regularizers

np.random.seed(1671) # for reproducibility
# network and training
NB_EPOCH = 30
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 # number of outputs = number of digits
OPTIMIZER = SGD() # SGD optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION

data = scipy.io.loadmat('RegressionImageData.mat')

XTrain = np.rollaxis(data['XTrain'],3,0)
XTest = np.rollaxis(data['XTest'],3,0)
YTest = np.squeeze(data['YTest'])
YTrain = np.squeeze(data['YTrain'])

print("Train Images:")
print(XTrain.shape)
print(type(XTrain))
print(XTrain)

XTrain_test = np.reshape(XTrain, (5000,28,28))

with open("./test.txt", "a+") as file:
    np.set_printoptions(threshold=np.nan)
    file.write(np.array2string(XTrain_test[0], max_line_width=np.inf))

model = Sequential()
model.add(Conv2D(25,(12,12), input_shape=(28,28,1), strides=(1,1), activation = "relu"))
model.add(Flatten())
model.add(Dense(1))
model.summary()

sgd = SGD(lr=0.001, decay=0.1, momentum=0.9, nesterov=False)
model.compile(loss='mean_squared_error', optimizer=sgd)

history = model.fit(XTrain, YTrain,
batch_size=BATCH_SIZE, epochs=NB_EPOCH,
verbose=VERBOSE, validation_split=VALIDATION_SPLIT,
shuffle=False)

predictions= model.predict(XTrain)
[np.transpose(predictions[1:50]), np.transpose(YTrain[1:50]), np.abs(np.transpose(predictions[1:50])- np.transpose(YTrain[1:50]))]

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('rmse')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

Ypred_test= model.predict(XTest)
Ypred_test=np.reshape(Ypred_test, (5000,))
predictionError = Ypred_test - YTest
thr = 10;
numCorrect = np.sum((np.abs(predictionError) < thr)*1)
numValidationImages = len(YTest)

accuracy = numCorrect/numValidationImages
print(accuracy)
squares = np.power(predictionError,2)
rmse = np.sqrt(np.mean(squares))
print(rmse)

有人知道差距在哪里吗?

0 个答案:

没有答案