[Python] Developing Deep Learning image recognition system using Pre-trained models

Discussão em 'Python' iniciado por Stack, Outubro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I want to use Pre-trained models such as Xception, VGG16, ResNet50, etc for my Deep Learning image recognition project to quick train the model on the training set with high accuracy. I am having trouble to find an exact code to implement my model. Firstly, as per the requirement of the VGG16 model, I modified the input shape of training data from (256,256,3) to (224,224,3). I have used keras programming environment. My model codes are as follows

    train_x = np.expand_dims(train_X, axis=2)
    train_y = np.expand_dims(train_Y, axis=2)
    print(train_X.shape) # output - (670, 224, 224, 3)
    print(train_Y.shape) # output - (670, 224, 224, 1)
    print(train_x.shape) # output - (670, 224, 1, 224, 3)
    print(train_y.shape) # output - (670, 224, 1, 224, 1)


    def vgg16_(IMG_WIDTH=224,IMG_HEIGHT=224,IMG_CHANNELS=3):
    inputs = Input(shape=(len(train_x[0]), 1))
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(inputs)
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

    # Block 2
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

    # Block 3
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

    # Block 4
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

    # Block 5
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

    x = Flatten()(x)
    x = Dropout(0.2)(x)
    x = Dense(100, activation='tanh')(x)
    x = Reshape([len(train_x[0]),1])(x)
    model = Model(inputs, reshape)
    model.compile(loss='mse', optimizer='rmsprop')
    return model


    But, Unfortunately I am getting this error by fitting this model on training data

    ValueError: Input 0 is incompatible with layer block1_conv1: expected ndim=4, found ndim=3


    What should I do to get the right output?

    Also, I tried to run from the following code by changing only output layer. I got this error ValueError: Error when checking target: expected predictions to have 2 dimensions, but got array with shape (670, 224, 224, 1)

    model_vgg16_conv = VGG16(input_shape=(IMG_WIDTH,IMG_HEIGHT,3),weights='imagenet', include_top=False,pooling=max)
    model_vgg16_conv.summary()
    #print("ss")
    #Create your own input format
    input = Input(shape=(IMG_WIDTH,IMG_HEIGHT,3),name = 'image_input')
    #print("ss2")
    #Use the generated model
    output_vgg16_conv = model_vgg16_conv(input)
    print("ss3")
    #Add the fully-connected layers
    x = Flatten(name='flatten')(output_vgg16_conv)
    x = Dense(512, activation='relu', name='fc1')(x)
    x = Dense(128, activation='relu', name='fc2')(x)
    x = Dense(1, activation='sigmoid', name='predictions')(x)

    #Create your own model
    my_model = Model(input=input, output=x)

    #In the summary, weights and layers from VGG part will be hidden, but they will be fit during the training
    my_model.summary()



    my_model.compile(loss='categorical_crossentropy',
    optimizer='adam',
    metrics=['accuracy'])


    How to resolve this issue?

    Continue reading...

Compartilhe esta Página