繁体   English   中英

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

[英]error while running code on machine/deep learning

我有这个错误:

ValueError   :   Traceback (most recent call last)


1 #print( n_classes)

2 m = modelFN( n_classes , input_height=input_height, input_width=input_width   )

----> 3 m.load_weights(args.save_weights_path + "." +"h"+ str(  epoch_number ))[---error in this line----]

ValueError: You are trying to load a weight file containing 16 layers into a model with 19 layers.

我的代码:

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')

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.h5与您在VGGSegnet定义的净值不匹配。 他们有不同的层次。
您应该检查您的weight filemodel定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM