<commit_before>from keras.models import Sequential
from keras.layers import Conv2D, Activation, Flatten, Dropout
from keras.layers.pooling import GlobalAveragePooling2D, AveragePooling2D
from keras.applications.mobilenet import MobileNet


class Model(object):

    @classmethod
    def custom(cls, inputShape, numClasses):

        model = Sequential()

        # Convolutions with stride 2 are like convolution + pooling
        # 2x smaller on width and height
        model.add(Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=64,
                         input_shape=inputShape,
                         padding='same', activation='relu', name="conv1"))

        # 2x smaller on width and height
        model.add(
            Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=128, padding='same', activation='relu', name="conv2"))

        # 2x smaller on width and height
        model.add(
            Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=256, padding='same', activation='relu', name="conv3"))

        # 2x smaller on width and height
        model.add(
            Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=512, padding='same', activation='relu', name="conv4"))

        # Final classifier
        model.add(Conv2D(kernel_size=(1, 1), strides=(1, 1), filters=numClasses))
        model.add(GlobalAveragePooling2D())
        model.add(Activation('softmax', name="softmax"))

        return model

    @classmethod
    def mobilenet(cls, inputShape, numClasses, alpha, retrainAll=False):

        # Load MobileNet Full, with output shape of (None, 7, 7, 1024), final classifier is excluded
        baseModel = MobileNet(input_shape=inputShape, alpha=alpha,
                              depth_multiplier=1, dropout=1e-3, include_top=False,
                              weights='imagenet', input_tensor=None, pooling=None)

        # Adding final classifier
        fineTunedModel = Sequential()

        fineTunedModel.add(baseModel)

        # Global average output shape (None, 1, 1, 1024).
        # Global pooling with AveragePooling2D to have a 4D Tensor and apply Conv2D.
        fineTunedModel.add(AveragePooling2D(pool_size=(baseModel.output_shape[1], baseModel.output_shape[2]),
                                            strides=(1, 1), padding='valid', name="global_pooling"))

        fineTunedModel.add(Dropout(rate=0.5))

        # Convolution layer that acts like fully connected, with 2 classes, output shape (None, 1, 1, 2)
        fineTunedModel.add(Conv2D(filters=numClasses, kernel_size=(1, 1), name="fc_conv"))

        # Reshape to (None, 2) to match the one hot encoding target and final softmax
        fineTunedModel.add(Flatten())

        # Final sofmax for deploy stage
        fineTunedModel.add(Activation('softmax'))

        # Freeze the base model layers, train only the last convolution
        if retrainAll is False:
            for layer in fineTunedModel.layers[0].layers:
                layer.trainable = False
<commit_msg>Layer naming fix to export backend model<commit_after>from keras.models import Sequential
from keras.layers import Conv2D, Activation, Flatten, Dropout
from keras.layers.pooling import GlobalAveragePooling2D, AveragePooling2D
from keras.applications.mobilenet import MobileNet


class Model(object):

    @classmethod
    def custom(cls, inputShape, numClasses):

        model = Sequential()

        # Convolutions with stride 2 are like convolution + pooling
        # 2x smaller on width and height
        model.add(Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=64,
                         input_shape=inputShape,
                         padding='same', activation='relu', name="conv1"))

        # 2x smaller on width and height
        model.add(
            Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=128, padding='same', activation='relu', name="conv2"))

        # 2x smaller on width and height
        model.add(
            Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=256, padding='same', activation='relu', name="conv3"))

        # 2x smaller on width and height
        model.add(
            Conv2D(kernel_size=(3, 3), strides=(2, 2), filters=512, padding='same', activation='relu', name="conv4"))

        # Final classifier
        model.add(Conv2D(kernel_size=(1, 1), strides=(1, 1), filters=numClasses))
        model.add(GlobalAveragePooling2D())
        model.add(Activation('softmax', name="softmax"))

        return model

    @classmethod
    def mobilenet(cls, inputShape, numClasses, alpha, retrainAll=False):

        # Load MobileNet Full, with output shape of (None, 7, 7, 1024), final classifier is excluded
        baseModel = MobileNet(input_shape=inputShape, alpha=alpha,
                              depth_multiplier=1, dropout=1e-3, include_top=False,
                              weights='imagenet', input_tensor=None, pooling=None)

        # Adding final classifier
        fineTunedModel = Sequential()

        fineTunedModel.add(baseModel)

        # Global average output shape (None, 1, 1, 1024).
        # Global pooling with AveragePooling2D to have a 4D Tensor and apply Conv2D.
        fineTunedModel.add(AveragePooling2D(pool_size=(baseModel.output_shape[1], baseModel.output_shape[2]),
                                            strides=(1, 1), padding='valid', name="global_pooling"))

        fineTunedModel.add(Dropout(rate=0.5))

        # Convolution layer that acts like fully connected, with 2 classes, output shape (None, 1, 1, 2)
        fineTunedModel.add(Conv2D(filters=numClasses, kernel_size=(1, 1), name="fc_conv"))

        # Reshape to (None, 2) to match the one hot encoding target and final softmax
        fineTunedModel.add(Flatten())

        # Final sofmax for deploy stage
        fineTunedModel.add(Activation('softmax', name="softmax"))

        # Freeze the base model layers, train only the last convolution
        if retrainAll is False:
            for layer in fineTunedModel.layers[0].layers:
                layer.trainable = False

{'fraction_non_alphanumeric': 0.10561165369971859, 'fraction_numerical': 0.027478894222810794, 'mean_word_length': 2.8, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6704306', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1677, 'n_words': 475}