[Python] Question about implementing Transfer Learning Correctly in Python Keras

Discussão em 'Python' iniciado por Stack, Maio 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have an assignment where I have to use transfer learning on iris images (the dataset is very small). The task is to build a regularized NN to classify the images into their respective classes.

    My entire code is below:

    import keras
    from keras.preprocessing.image import ImageDataGenerator

    #importing images trhrough ImageDataGenerator

    train_gen = ImageDataGenerator(rescale = 1./255,
    shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
    test_gen = ImageDataGenerator(rescale = 1./255)

    #Generating training and test sets

    training_set = train_gen.flow_from_directory(r"Iris_Imgs",
    target_size = (224, 224), shuffle=True, batch_size = 15, class_mode = 'categorical')
    train_imgs, train_labels = next(training_set)
    test_set = test_gen.flow_from_directory(r"Iris_Imgs",
    target_size = (224, 224), shuffle=True, class_mode = 'categorical')
    test_imgs, test_labels = next(test_set)

    from keras.applications import VGG16
    from keras.models import Model

    #Importing VGG16 and getting weights

    vgg_conv = VGG16(weights='imagenet', input_shape=(224, 224, 3))

    vgg_conv = VGG16(weights='imagenet', include_top=False,
    input_shape=(224, 224, 3))
    part_model = Model(inputs = vgg_conv.input,
    outputs = vgg_conv.get_layer('block4_pool').output)

    #These are the features I would like to connect to my NN

    block4_pool_features = part_model.predict(training_set)
    vgg_conv.layers.pop()


    This is the part I'm most confused about - when i built my NN (below) I want to make sure I'm connecting the layers from the vgg_conv.output layer to the NN I've built.

    from tensorflow import keras
    from tensorflow.keras import layers
    from keras.layers.convolutional import Conv2D
    from keras.models import Sequential
    from keras.layers.convolutional import MaxPooling2D
    from keras.layers.normalization import BatchNormalization
    from keras.layers import Flatten
    from keras.layers import Dense
    from keras.layers import Dropout


    imageSize=224
    classifier=Sequential()

    classifier.add(Conv2D(3, (3, 3), input_shape = (imageSize, imageSize, 3), activation = 'relu'))
    classifier.add(MaxPooling2D(pool_size = (1, 1)))

    classifier.add(BatchNormalization())


    classifier.add(Conv2D(3, (3, 3), activation = 'relu'))
    classifier.add(MaxPooling2D(pool_size = (1, 1)))

    classifier.add(BatchNormalization())

    classifier.add(Flatten())

    classifier.add(Dense(128, activation='relu'))

    classifier.add(Dropout(1e-3))

    classifier.add(Dense(3, activation='softmax'))

    opt = keras.optimizers.Adam(learning_rate=3e-2)

    #Connecting last layer - Am I doing this correctly?

    last = vgg_conv.output
    x = Flatten()(last) ## functional API
    x2 = Dense(1024, activation='relu')(x) # Fully Connect
    my_preds = Dense(200, activation='softmax')(x2)


    for layer in classifier.layers[:10000]:
    layer.trainable = False

    classifier.compile(loss='categorical_crossentropy', optimizer='adam', metrics = ['acc'])

    classifier.fit(train_imgs, train_labels, batch_size = 10, epochs = 30)


    I've also noticed that the model I've built is performing very poorly.

    Continue reading...

Compartilhe esta Página