我正在尝试通过Keras库将用Matlab编写的深度学习网络转换为Python。网络的任务是识别给定图像中的特定音符,并返回与特定声音名称相对应的单个字符(从“ a”到“ h”)
Matlab中的代码:
digitDatasetPath = fullfile('zbior_uczacy');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
labelCount = countEachLabel(imds)
img = readimage(imds,1);
size(img)
numTrainFiles = 1200;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([50 30 1])
convolution2dLayer(6,12,'Padding',1)
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(6,24,'Padding',1)
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(6,48,'Padding',1)
reluLayer
fullyConnectedLayer(7)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.3, ...
'LearnRateDropPeriod', 5, ...
'MaxEpochs', 50, ...
'MiniBatchSize', 300);
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)
代码已经过测试,可以正常工作。 现在,我正在尝试将此代码转换为Keras代码。完整代码在这里:
import numpy as np
import cv2
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Input
from keras.layers import Convolution2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras import backend as K
from keras.preprocessing import image
print("\n")
print("\n")
trained_weights = []
trained_bias = []
trained_weights = np.loadtxt('Weights.csv', delimiter=';')
trained_bias = np.loadtxt('Bias.csv', delimiter=';')
network_weights = np.array([[trained_weights]])
network_outputs = np.array([['a','c','d','e','f','g','h']])
# Load test image
img = load_img('note_a.png')
note = image.img_to_array(img)
note = np.expand_dims(note, axis=0)
# Model architecture
model = Sequential()
# Layer 1
model.add(Convolution2D(12, (6, 6), input_shape=(50, 30, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Layer 2
model.add(Convolution2D(24, (6, 6), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Layer 3
model.add(Convolution2D(48, (6, 6), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(48))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
model.set_weights([network_weights])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics = ['accuracy'])
#model.fit((note,network_outputs), batch_size=32, nb_epoch=10, verbose=1)
score = model.evaluate(note, network_outputs)
为了避免所有学习,我只是将学习到的Matlab网络的权重导出到.csv文件,然后将其导入到我的网络中。因此,此代码的目标是仅识别给定的图像,然后返回字符。这里没有学习。
该代码返回以下错误:
Traceback (most recent call last):
File "Train_Keras.py", line 51, in <module>
score = model.evaluate(note, network_outputs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\models.py", line 989, in evaluate
sample_weight=sample_weight)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\engine\training.py", line 1723, in evaluate
batch_size=batch_size)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\engine\training.py", line 1414, in _standardize_user_data
exception_prefix='input')
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\engine\training.py", line 153, in _standardize_input_data
str(array.shape))
ValueError: Error when checking input: expected conv2d_1_input to have shape (None, 50, 30, 3) but got array with shape (1, 3, 50, 30)
我的问题是,我是否在正确执行图像到数组的转换?以及我应该如何将空行添加到数组?发现某个地方,如果我要增加第一个卷积层的批处理大小:
model.add(Convolution2D(12, (6, 6), batch_size=1, input_shape=(50, 30, 3), activation='relu'))
然后错误更改为:“检查输入时出错:预期conv2d_1_input具有形状(1、50、30、3)”,但是我不确定是否添加batch_size不会改变网络活动。
此外,我发现某个地方按照网络希望它重塑图像的确切方式可能会有所帮助:
note = np.reshape(note, [1, 50, 30, 3])
但是返回以下错误。老实说,我完全不明白那个:
Traceback (most recent call last):
File "Train_Keras.py", line 52, in <module>
score = model.evaluate(note, network_outputs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\models.py", line 989, in evaluate
sample_weight=sample_weight)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\engine\training.py", line 1729, in evaluate
self._make_test_function()
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\engine\training.py", line 1012, in _make_test_function
**self._function_kwargs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\backend\theano_backend.py", line 1233, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\backend\theano_backend.py", line 1219, in __init__
**kwargs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\compile\function.py", line 317, in function
output_keys=output_keys)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\compile\pfunc.py", line 486, in pfunc
output_keys=output_keys)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 1839, in orig_function
name=name)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 1519, in __init__
optimizer_profile = optimizer(fgraph)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\gof\opt.py", line 108, in __call__
return self.optimize(fgraph)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\gof\opt.py", line 97, in optimize
ret = self.apply(fgraph, *args, **kwargs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\gof\opt.py", line 251, in apply
sub_prof = optimizer.optimize(fgraph)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\gof\opt.py", line 93, in optimize
self.add_requirements(fgraph)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\tensor\opt.py", line 1488, in add_requirements
fgraph.attach_feature(ShapeFeature())
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\gof\fg.py", line 546, in attach_feature
attach(self)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\tensor\opt.py", line 1305, in on_attach
self.on_import(fgraph, node, reason='on_attach')
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\tensor\opt.py", line 1368, in on_import
self.set_shape(r, s)
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\tensor\opt.py", line 1157, in set_shape
shape_vars.append(self.unpack(s[i], r))
File "C:\Users\Karol\Anaconda3\lib\site-packages\theano\tensor\opt.py", line 1079, in unpack
raise AssertionError(msg)
AssertionError: There is a negative shape in the graph!
Backtrace when that variable is created:
File "Train_Keras.py", line 30, in <module>
model.add(Convolution2D(12, (6, 6), input_shape=(50, 30, 3), activation='relu'))
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\models.py", line 464, in add
layer(x)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\engine\topology.py", line 603, in __call__
output = self.call(inputs, **kwargs)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\layers\convolutional.py", line 164, in call
dilation_rate=self.dilation_rate)
File "C:\Users\Karol\Anaconda3\lib\site-packages\keras\backend\theano_backend.py", line 1913, in conv2d
filter_dilation=dilation_rate)
对于解决该问题的任何技巧将不胜感激。 谢谢!