在机器/深度学习上运行代码时出错

时间:2019-06-07 11:23:08

标签: python python-3.x

我遇到此错误:

import VGGSegnet
#import LoadBatches
from keras.models import load_model
modelFns = { 'vgg_segnet':VGGSegnet.VGGSegnet}
modelFN = modelFns[ model_name ]
m = modelFN( n_classes , input_height=input_height, input_width=input_width   )
m.load_weights(args.save_weights_path + "." +"h"+ str(  epoch_number ))

我的代码:

VGGSegnet.py

我的from keras.layers.convolutional import Conv2D, ZeroPadding2D, UpSampling2D from keras.layers.core import Flatten, Dense, Reshape, Permute, Activation from keras.layers.normalization import BatchNormalization from keras.layers.pooling import MaxPooling2D from keras.models import * import os file_path = os.path.dirname(os.path.abspath(__file__)) VGG_Weights_path = file_path + "/data/vgg16_weights_th_dim_ordering_th_kernels.h5" def VGGSegnet(n_classes, input_height=416, input_width=608, vgg_level=3): img_input = Input(shape=(3, input_height, input_width)) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format='channels_first')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format='channels_first')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool1', data_format='channels_first')(x) f1 = x x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format='channels_first')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format='channels_first')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format='channels_first')(x) f2 = x x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format='channels_first')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format='channels_first')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format='channels_first')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool1', data_format='channels_first')(x) f3 = x x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format='channels_first')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', data_format='channels_first')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', data_format='channels_first')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool1', data_format='channels_first')(x) f4 = x x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format='channels_first')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', data_format='channels_first')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format='channels_first')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool1', data_format='channels_first')(x) f5 = x x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(1000, activation='relu', name='predictions')(x) vgg = Model(img_input, x) vgg.load_weights(VGG_Weights_path) levels = [f1, f2, f3, f4, f5] o = levels[vgg_level] o = ZeroPadding2D((1,1),data_format='channels_first')(o) o = Conv2D(512,(3,3),padding='valid',data_format='channels_first')(o) o = BatchNormalization()(o) o = UpSampling2D((2,2),data_format='channels_first')(o) o = ZeroPadding2D((1,1),data_format='channels_first')(o) o = Conv2D(256,(3,3),padding='valid',data_format='channels_first')(o) o = BatchNormalization()(o) o = UpSampling2D((2,2),data_format='channels_first')(o) o = ZeroPadding2D((1,1),data_format='channels_first')(o) o = Conv2D(128,(3,3),padding='valid',data_format='channels_first')(o) o = BatchNormalization()(o) o = UpSampling2D((2, 2), data_format='channels_first')(o) o = ZeroPadding2D((1, 1), data_format='channels_first')(o) o = Conv2D(64, (3, 3), padding='valid', data_format='channels_first')(o) o = BatchNormalization()(o) o = Conv2D(n_classes,(3,3),padding='same',data_format='channels_first')(o) o_shape = Model(img_input,o).output_shape outputHeight = o_shape[2] outputWidth = o_shape[3] o = (Reshape((-1,outputHeight*outputWidth)))(o) o = (Permute((2,1)))(o) o = (Activation('softmax'))(o) model = Model(img_input,o) model.outputWidth = outputWidth model.outputHeight = outputHeight return model if __name__ == '__main__': m = VGGSegnet(101) from keras.utils import plot_model plot_model(m,show_shapes=True,to_file='model.png') 文件

onEnd: function(event) {            
    if (($('#lists:hover').length == 0)) {
        alert("delete");
    }
}

1 个答案:

答案 0 :(得分:0)

ValueError: You are trying to load a weight file containing 16 layers into a model with 19 layers.
您的权重文件/data/vgg16_weights_th_dim_ordering_th_kernels.h5VGGSegnet中定义的网络不匹配。它们有不同的层次。
您应该检查weight filemodel的定义。