Community Conundrums are live! Learn more

Cannot reproduce the result of cats dogs classification

Level 1
Cannot reproduce the result of cats dogs classification

I follow the document to apply transfer learning on xception model to classify cats and dogs:



https://www.dataiku.com/learn/guide/visual/machine-learning/deep-learning-images.html



But the results I achieved is very pool. 



Here is the architecture code:



from keras.layers import Input, Dense, Flatten

from keras.models import Model

from keras.applications import Xception

import os

import dataiku



def build_model(input_shapes, n_classes=None):



    #### DEFINING INPUT AND BASE ARCHITECTURE

    # You need to modify the name and shape of the "image_input" 

    # according to the preprocessing and name of your 

    # initial feature.

    # This feature should to be preprocessed as an "Image", with a 

    # custom preprocessing.



    image_input = Input(shape=(299,299,3), name="path_preprocessed")





    base_model = Xception(include_top=False, weights=None, input_tensor=image_input)



    #### LOADING WEIGHTS OF PRE TRAINED MODEL

    # To leverage this architecture, it is better to use weights

    # computed on a previous training on a large dataset (Imagenet).

    # To do so, you need to download the file containing the weights

    # and load them into your model.

    # You can do it by using the macro "Download pre-trained model"

    # of the "Deep Learning image" plugin (CPU or GPU version depending

    # on your setup) available in the plugin store. For this architecture,

    # you need to select:

    #    "Xception trained on Imagenet"

    # This will download the weights and put them into a managed folder

    folder = dataiku.Folder("xception_weights")

    weights_path = "xception_imagenet_weights_notop.h5"



    base_model.load_weights(os.path.join(folder.get_path(), weights_path),

                       by_name=True, skip_mismatch=True)

    

    for layer in base_model.layers:

        layer.trainable = False

    

    #### ADDING FULLY CONNECTED CLASSIFICATION LAYER

    x = base_model.layers[-1].output

    x = Flatten()(x)

    predictions = Dense(n_classes, activation="softmax")(x)



    model = Model(input=base_model.input, output=predictions)

    return model



def compile_model(model):

    model.compile(

        optimizer="adam",

        loss="categorical_crossentropy"

    )

    return model



Here is code for training:



from dataiku.doctor.deep_learning.sequences import DataAugmentationSequence

from keras.preprocessing.image import ImageDataGenerator

from keras import callbacks



# A function that builds train and validation sequences.

# You can define your custom data augmentation based on the original train and validation sequences



#   build_train_sequence_with_batch_size        - function that returns train data sequence depending on

#                                                 batch size

#   build_validation_sequence_with_batch_size   - function that returns validation data sequence depending on

#                                                 batch size

def build_sequences(build_train_sequence_with_batch_size, build_validation_sequence_with_batch_size):

    

    batch_size = 16

    

    augmentator = ImageDataGenerator(

        zoom_range=0.2,

        shear_range=0.2,

        rotation_range=20,

        width_shift_range=0.2,

        height_shift_range=0.2,

        horizontal_flip=True

    )

    train_sequence = build_train_sequence_with_batch_size(batch_size)

    validation_sequence = build_validation_sequence_with_batch_size(batch_size)

    

    augmented_sequence = DataAugmentationSequence(

        train_sequence,

        'path_preprocessed',

        augmentator,

        1

    )

    return augmented_sequence, validation_sequence





# A function that contains a call to fit a model.



#   model                 - compiled model

#   train_sequence        - train data sequence, returned in build_sequence

#   validation_sequence   - validation data sequence, returned in build_sequence

#   base_callbacks        - a list of Dataiku callbacks, that are not to be removed. User callbacks can be added to this list

def fit_model(model, train_sequence, validation_sequence, base_callbacks):

    epochs = 10

    

    callback = callbacks.ReduceLROnPlateau(

        monitor='val_loss',

        factor=0.2,

        patience=5

    )



    base_callbacks.append(callback)

    model.fit_generator(train_sequence,

                        epochs=epochs,

                        callbacks=base_callbacks,

                        shuffle=True)



From the training curve, I do not think to increase epochs will be helpful. What could be the problem?

0 Kudos
1 Reply
Level 1

@Haifeng : I'm having the same problem you had and my code looks very similar to yours. Have you been able to fix the analysis?

0 Kudos