«Та же» сеть на MATLAB и Keras имеет очень разные результаты - PullRequest
0 голосов
/ 06 ноября 2018

Я пытался повторить ту же простую структуру сети в MATLAB и Keras. Проблема в том, что точность, которую я получаю, сильно отличается. Код MATLAB получает точность около 0,84, а потери - около 17, а код Keras - точность около 0,63, а потери - около 130, при этом Keras использует двойные эпохи для обучения и одинаковые данные. Я думаю, что разница слишком велика, чтобы быть предметом реализации, поэтому я думаю, что что-то упустил.

Исходный код взят из примера 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)

Кто-нибудь знает, где может быть разрыв?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...