我正在使用NodaTime。 当我测试整个网络时,我会收到
“ ValueError:新数组的总大小必须保持不变。
可能是什么原因?任何帮助将不胜感激。
我已经测试了作者提供的数据集样本。我拍摄了约700张图像,然后尝试了。作者提供了7890个图像数据集。
'''
def get_optimizer(name = 'adagrad', l_rate = 0.0001, dec = 0.0, b_1 = 0.9, b_2 = 0.999, mom = 0.5, rh = 0.9):
eps = 1e-8
adam = Adam(lr = l_rate, beta_1 = b_1, beta_2 = b_2, epsilon = eps, decay = dec)
sgd = SGD(lr = l_rate, momentum = mom, decay = dec, nesterov = True)
rmsp = RMSprop(lr = l_rate, rho = rh, epsilon = eps, decay = dec)
adagrad = Adagrad(lr = l_rate, epsilon = eps, decay = dec)
optimizers = {'adam': adam, 'sgd':sgd, 'rmsp': rmsp, 'adagrad': adagrad}
return optimizers[name]
def build_autoencoder():
#define network architecture for encoder
model = Sequential()
#6 convoltuion layers
model.add(Convolution2D(16, 3, 3, border_mode='same', subsample = (2,2), activation = 'relu',
input_shape=(224, 224, 3)))
model.add(Convolution2D(32, 3, 3, border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Convolution2D(64, 3, 3, border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Convolution2D(128, 3, 3, border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Convolution2D(256, 3, 3, border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Convolution2D(512, 3, 3, border_mode='same', subsample = (2,2), activation = 'relu'))
#Flatten
model.add(Flatten())
#2 fully connected layers
model.add(Dense(4096, activation='relu'))
model.add(Dropout(p=0))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(p=0))
#define network architecture for decoder
#2 fully connected layers
model.add(Dense(4096, activation='relu'))
model.add(Dense(4096, activation='relu'))
#reshape to 2D
model.add(Reshape((8, 8, 64)))
#5 upconv layers
model.add(Deconvolution2D(256, 3, 3, (None, 15, 15,256), border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Deconvolution2D(128, 3, 3, (None, 29, 29, 128), border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Deconvolution2D(64, 3, 3, (None, 57, 57, 64), border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Deconvolution2D(32, 3, 3, (None, 113, 113, 32),border_mode='same', subsample = (2,2), activation = 'relu'))
model.add(Deconvolution2D(16, 3, 3, (None, 225, 225, 16), border_mode='same', subsample = (2,2), activation = 'relu'))
#output layer, add 3 RGB channels for reconstructed output view
model.add(Deconvolution2D(3, 3, 3, (None, 225, 225, 3), border_mode='same', subsample = (1,1), activation = 'relu'))
#add a resize layer to resize (225, 225) to (224, 224)
model.add(Reshape((225*225,3)))
model.add(Lambda(lambda x: x[:,:50176,])) # throw away some
model.add(Reshape((224,224,3)))
'''
build_autoencoder
model.add(Reshape((225*225,3)))
File "C:\ProgramData\Anaconda3\envs\myenv\lib\site-packages\keras\engine\sequential.py", line 181, in add
output_tensor = layer(self.outputs[0])
File "C:\ProgramData\Anaconda3\envs\myenv\lib\site-packages\keras\engine\base_layer.py", line 474, in __call__
output_shape = self.compute_output_shape(input_shape)
File "C:\ProgramData\Anaconda3\envs\myenv\lib\site-packages\keras\layers\core.py", line 398, in compute_output_shape
input_shape[1:], self.target_shape)
File "C:\ProgramData\Anaconda3\envs\myenv\lib\site-packages\keras\layers\core.py", line 386, in _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged
错误在第reshape ((225*225,3))
行