Frozzenizzy 发表于 2020-4-29 15:06:08

keras写resnet34应用时输出不变

网络结构如下
def Conv2d_BN(x, nb_filter, kernel_size, strides=(1, 1), padding='same', name=None):
    if name is not None:
      bn_name = name + '_bn'
      conv_name = name + '_conv'
    else:
      bn_name = None
      conv_name = None
    x = Conv2D(nb_filter, kernel_size, padding=padding, strides=strides, activation='relu', name=conv_name)(x)
    x = BatchNormalization(axis=3, name=bn_name)(x)
    return x

#对于identity_Block的nb_filter以及bottleneck_Block的nb_filters进行说明:如上图所示:resNet34里面,每一个卷积层当中的nb_filter仅仅是一种,而resnet50里面每一层的filter有多种
def identity_Block(inpt, nb_filter, kernel_size, strides=(1, 1), with_conv_shortcut=False):
    x = Conv2d_BN(inpt, nb_filter=nb_filter, kernel_size=kernel_size, strides=strides, padding='same')
    x = Conv2d_BN(x, nb_filter=nb_filter, kernel_size=kernel_size, padding='same')
    if with_conv_shortcut:#shortcut的含义是:将输入层x与最后的输出层y进行连接,如上图所示
      shortcut = Conv2d_BN(inpt, nb_filter=nb_filter, strides=strides, kernel_size=kernel_size)
      x = add()
      return x
    else:
      x = add()
      return x

def resnet_34(width,height,channel,classes):
    inpt = Input(shape=(width, height, channel))
    x = ZeroPadding2D((3, 3))(inpt)
    #conv1
    x = Conv2d_BN(x, nb_filter=64, kernel_size=(7, 7), strides=(2, 2), padding='valid')
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)
    #conv2_x
    x = identity_Block(x, nb_filter=64, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=64, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=64, kernel_size=(3, 3))
    #conv3_x
    x = identity_Block(x, nb_filter=128, kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True)
    x = identity_Block(x, nb_filter=128, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=128, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=128, kernel_size=(3, 3))
    #conv4_x
    x = identity_Block(x, nb_filter=256, kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True)
    x = identity_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=256, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=256, kernel_size=(3, 3))
    #conv5_x
    x = identity_Block(x, nb_filter=512, kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True)
    x = identity_Block(x, nb_filter=512, kernel_size=(3, 3))
    x = identity_Block(x, nb_filter=512, kernel_size=(3, 3))
    x = AveragePooling2D(pool_size=(4, 4))(x)
    x = Flatten()(x)
    x = Dense(classes, activation='softmax')(x)
    model = Model(inputs=inpt, outputs=x)
    return model
但是通过
x = load_img(file, target_size=(img_width,img_height))
x = img_to_array(x)
x = np.expand_dims(x, axis=0)
array = model.predict(x)
result = array
不论输入什么图片都会输出相同的结果这是为什么

之前用model.add的时候就不会有这种问题,是不是结构写法跟
train_datagen = ImageDataGenerator(
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    rescale=1./255
)
train_generator = train_datagen.flow_from_directory(
    train_root,
    target_size=(IM_WIDTH, IM_HEIGHT),
    batch_size=batch_size,
    shuffle=True
)
# vaild data
vaild_datagen = ImageDataGenerator(
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    rescale=1./255
)
vaild_generator = train_datagen.flow_from_directory(
    vaildation_root,
    target_size=(IM_WIDTH, IM_HEIGHT),
    batch_size=batch_size,
)
        model.compile(loss='categorical_crossentropy',
            optimizer=optimizers.RMSprop(lr=lr),
            metrics=['accuracy'])
    print ('Model Compiled')   

    target_best_valacc_dir = './modelresnetbest/'
    if not os.path.exists(target_best_valacc_dir):
      os.mkdir(target_best_valacc_dir)
    checkpoint = ModelCheckpoint(target_best_valacc_dir,
      monitor='val_acc', save_weights_only=True,verbose=1,save_best_only=True, period=1)
   
    model.fit_generator(
      train_generator,
      steps_per_epoch=steps_per_epoch,
      epochs=EPOCH,
      validation_data=vaild_generator,
      validation_steps=validation_steps)
不兼容导致的

沸腾沸腾 发表于 2020-8-2 17:42:07

您好,看到你的关于测试模式 的问题 不知道您解决了没有?我最近用pytorch也遇到了相同的问题?想请教一下
页: [1]
查看完整版本: keras写resnet34应用时输出不变