在theano上的keras中处理灰度图像时出错

时间:2017-05-30 16:27:01

标签: computer-vision deep-learning keras theano

我正在进行物体检测,我正在使用keras而不是theano来构建模型。这是我的代码。

from keras.preprocessing import image
from scipy.misc import toimage
from keras.optimizers import Adadelta,SGD
from matplotlib import pyplot as plt
from keras.models import Sequential,load_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
import pickle
import numpy as np

X=pickle.load(open('Xvalues.p','rb'))
y=pickle.load(open('yvalues.p','rb'))


X_train=X[:1100,:,:]
y_train=y[:1100]
X_test=X[1100:,:,:]
y_test=y[1100:]

X_train = X_train.reshape(X_train.shape[0], 50, 50,1).astype('float32')
X_test = X_test.reshape(X_test.shape[0],50, 50,1).astype('float32')
#X_train=X_train[:,:,:,np.newaxis]
#X_test=X_test[:,:,:,np.newaxis]
X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
print type(num_classes)
print y_train.shape
print y_test.shape

opt=Adadelta()
model=Sequential()
model.add(Conv2D(48,(5,5),input_shape=(50,50,1),activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(64,(5,5),activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(X_train,y_train,epochs=15, batch_size=32, verbose=1)
print model.summary()
model.save('train2.h5',overwrite=True)
scores = model.evaluate(X_test, y_test, verbose=1)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))

当我使用彩色图像时效果很好,但是当我使用灰度图像时,会出现以下错误

File "D:/ML/classify.py", line 39, in <module>
    model.add(Conv2D(48,(5,5),input_shape=(50,50,1),activation='relu'))

Exception: ('The following error happened while compiling the node', Elemwise{Composite{(i0 + (i1 * i2))}}[(0, 2)](TensorConstant{(1L, 1L, 1..-0.0699854}, TensorConstant{(1L, 1L, 1..f 0.139971}, mrg_uniform{TensorType(float32, 4D),inplace}.1), '\n', 'Compilation failed (return status=1): C:\\Users\\DELL\\Anaconda2\\libs/python27.lib: error adding symbols: File in wrong format\r. collect2.exe: error: ld returned 1 exit status\r. ', '[Elemwise{Composite{(i0 + (i1 * i2))}}[(0, 2)](TensorConstant{(1L, 1L, 1..-0.0699854}, TensorConstant{(1L, 1L, 1..f 0.139971}, <TensorType(float32, (False, False, True, False))>)]')

我无法理解什么是错的。

0 个答案:

没有答案