Example #1
0
def default_latent(num_outputs, input_shape):

    drop = 0.2

    img_in = Input(shape=input_shape, name='img_in')
    x = img_in
    x = Lambda(lambda x: x / 255.)(x)  # normalize
    x = Convolution2D(24, (5, 5),
                      strides=(2, 2),
                      activation='relu',
                      name="conv2d_1")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(32, (5, 5),
                      strides=(2, 2),
                      activation='relu',
                      name="conv2d_2")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(32, (5, 5),
                      strides=(2, 2),
                      activation='relu',
                      name="conv2d_3")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(32, (3, 3),
                      strides=(1, 1),
                      activation='relu',
                      name="conv2d_4")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(32, (3, 3),
                      strides=(1, 1),
                      activation='relu',
                      name="conv2d_5")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(64, (3, 3),
                      strides=(2, 2),
                      activation='relu',
                      name="conv2d_6")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(64, (3, 3),
                      strides=(2, 2),
                      activation='relu',
                      name="conv2d_7")(x)
    x = Dropout(drop)(x)
    x = Convolution2D(10, (1, 1),
                      strides=(2, 2),
                      activation='relu',
                      name="latent")(x)

    y = Conv2DTranspose(filters=64,
                        kernel_size=(3, 3),
                        strides=2,
                        name="deconv2d_1")(x)
    y = Conv2DTranspose(filters=64,
                        kernel_size=(3, 3),
                        strides=2,
                        name="deconv2d_2")(y)
    y = Conv2DTranspose(filters=32,
                        kernel_size=(3, 3),
                        strides=2,
                        name="deconv2d_3")(y)
    y = Conv2DTranspose(filters=32,
                        kernel_size=(3, 3),
                        strides=2,
                        name="deconv2d_4")(y)
    y = Conv2DTranspose(filters=32,
                        kernel_size=(3, 3),
                        strides=2,
                        name="deconv2d_5")(y)
    y = Conv2DTranspose(filters=1,
                        kernel_size=(3, 3),
                        strides=2,
                        name="img_out")(y)

    x = Flatten(name='flattened')(x)
    x = Dense(256, activation='relu')(x)
    x = Dropout(drop)(x)
    x = Dense(100, activation='relu')(x)
    x = Dropout(drop)(x)
    x = Dense(50, activation='relu')(x)
    x = Dropout(drop)(x)

    outputs = [y]

    for i in range(num_outputs):
        outputs.append(
            Dense(1, activation='linear', name='n_outputs' + str(i))(x))

    model = Model(inputs=[img_in], outputs=outputs)

    return model
Example #2
0
def train(review_data):
    ################################################################
    # declare input embeddings to the model
    #User input
    user_id_input = Input(shape=[1], name='user')
    #Item Input
    item_id_input = Input(shape=[1], name='item')
    price_id_input = Input(shape=[1], name='price')
    title_id_input = Input(shape=[1], name='title')

    # define the size of embeddings as a parameter
    # ****H: size_of_embedding - 5, 10 , 15, 20, 50
    size_of_embedding = 15
    user_embedding_size = size_of_embedding
    item_embedding_size = size_of_embedding
    price_embedding_size = size_of_embedding
    title_embedding_size = size_of_embedding

    # apply an embedding layer to all inputs
    user_embedding = Embedding(output_dim=user_embedding_size,
                               input_dim=users.shape[0],
                               input_length=1,
                               name='user_embedding')(user_id_input)

    item_embedding = Embedding(output_dim=item_embedding_size,
                               input_dim=items_reviewed.shape[0],
                               input_length=1,
                               name='item_embedding')(item_id_input)

    price_embedding = Embedding(output_dim=price_embedding_size,
                                input_dim=price.shape[0],
                                input_length=1,
                                name='price_embedding')(price_id_input)

    title_embedding = Embedding(output_dim=title_embedding_size,
                                input_dim=titles.shape[0],
                                input_length=1,
                                name='title_embedding')(title_id_input)

    # reshape from shape (batch_size, input_length,embedding_size) to (batch_size, embedding_size).
    user_vecs = Reshape([user_embedding_size])(user_embedding)
    item_vecs = Reshape([item_embedding_size])(item_embedding)
    price_vecs = Reshape([price_embedding_size])(price_embedding)
    title_vecs = Reshape([title_embedding_size])(title_embedding)

    ################################################################
    # Concatenate the item embeddings :
    item_vecs_complete = Concatenate()([item_vecs, price_vecs, title_vecs])

    # Concatenate user and item embeddings and use them as features for the neural network:
    input_vecs = Concatenate()([user_vecs, item_vecs_complete
                                ])  # can be changed by Multiply
    #input_vecs = Concatenate()([user_vecs, item_vecs]) # can be changed by Multiply

    # Multiply user and item embeddings and use them as features for the neural network:
    #input_vecs = Multiply()([user_vecs, item_vecs]) # can be changed by concat

    # Dropout is a technique where randomly selected neurons are ignored during training to prevent overfitting
    input_vecs = Dropout(0.1)(input_vecs)

    # Check one dense 128 or two dense layers (128,128) or (128,64) or three denses layers (128,64,32))

    # First layer
    # Dense(128) is a fully-connected layer with 128 hidden units.
    # Use rectified linear units (ReLU) f(x)=max(0,x) as an activation function.
    x = Dense(128, activation='relu')(input_vecs)
    x = Dropout(0.1)(x)  # Add droupout or not # To improve the performance

    # Next Layers
    #x = Dense(128, activation='relu')(x) # Add dense again or not
    x = Dense(64, activation='relu')(x)  # Add dense again or not
    x = Dropout(0.1)(x)  # Add droupout or not # To improve the performance
    x = Dense(32, activation='relu')(x)  # Add dense again or not #
    x = Dropout(0.1)(x)  # Add droupout or not # To improve the performance

    # The output
    y = Dense(1)(x)

    ################################################################
    model = Model(
        inputs=[user_id_input, item_id_input, price_id_input, title_id_input],
        outputs=y)

    ################################################################
    # ****H: loss
    # ****H: optimizer
    model.compile(loss='mse', optimizer="adam")

    ################################################################

    save_path = "./"
    mytime = time.strftime("%Y_%m_%d_%H_%M")
    # modname = 'dense_2_15_embeddings_2_epochs' + mytime
    modname = 'dense_2_15_embeddings_2_epochs'
    thename = save_path + '/' + modname + '.h5'
    mcheck = ModelCheckpoint(thename, monitor='val_loss', save_best_only=True)

    ################################################################

    # ****H: batch_size
    # ****H: epochs
    # ****H:
    # ****H:

    history = model.fit([
        ratings_train["user_id"], ratings_train["item_id"],
        ratings_train["price_id"], ratings_train["title_id"]
    ],
                        ratings_train["score"],
                        batch_size=64,
                        epochs=2,
                        validation_split=0.2,
                        callbacks=[mcheck],
                        shuffle=True)

    print("MSE: ", history.history)

    return model
Example #3
0
x_data = np.array(x_data)
y_data = np.array(y_data)

print(x_data.shape)

# if x_data.shape[0] > y_data.shape[0]:
#     x_data = x_data[:y_data.shape[0] - x_data.shape[0]]

split = int(y_data.shape[0] * 0.8)
x_train, x_test = x_data[:split], x_data[split:]
y_train, y_test = y_data[:split], y_data[split:]

model = Sequential()
model.add(GRU(128, kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer="adam",
              loss="binary_crossentropy",
              metrics=['binary_accuracy'])  # accuracy,mse,binary_crossentropy

history = model.fit(x_train, y_train, epochs=10, verbose=2)

y_pred = model.predict(x_test)
model.evaluate(x_test, y_test)
last_data = np.expand_dims(x_test[-1], axis=0)
print(model.predict(last_data))
# print("test AUC", round(roc_auc_score(y_test, y_pred), 4))
Example #4
0
                  tamano_filtro1,
                  input_shape=input_shape,
                  padding='same',
                  activation='relu'))
cnn.add(
    Convolution2D(filtrosConv2,
                  tamano_filtro1,
                  activation='relu',
                  padding='same'))

cnn.add(MaxPooling2D(pool_size=tamano_pool, strides=(2, 2)))
#cnn.add(Dropout(0.25))

cnn.add(Flatten())
cnn.add(Dense(512, activation='relu'))
cnn.add(Dropout(0.5))
cnn.add(Dense(clases, activation='softmax'))
"""cnn=Sequential()
cnn.add(Convolution2D(filtrosConv2,tamano_filtro1,input_shape=input_shape,padding='same',activation='relu'))
cnn.add(Convolution2D(filtrosConv2,tamano_filtro1,activation='relu',padding='same'))
cnn.add(MaxPooling2D(pool_size=tamano_pool, strides=(2,2)))

cnn.add(Convolution2D(filtrosConv3,tamano_filtro1,activation='relu',padding='same'))
cnn.add(Convolution2D(filtrosConv3,tamano_filtro1,activation='relu',padding='same'))
cnn.add(MaxPooling2D(pool_size=tamano_pool, strides=(2,2)))

cnn.add(Convolution2D(filtrosConv4,tamano_filtro1,activation='relu',padding='same'))
cnn.add(Convolution2D(filtrosConv4,tamano_filtro1,activation='relu',padding='same'))
cnn.add(Convolution2D(filtrosConv4,tamano_filtro1,activation='relu',padding='same'))
cnn.add(MaxPooling2D(pool_size=tamano_pool, strides=(2,2)))
Example #5
0
def model_from_paper(multi_gpu=True, num_gpus=4):
    """
    
    Defines hyperparameters and compiles the CNN model used in the paper:
        https://arxiv.org/abs/1804.06812

    Returns
    -------
    A Keras sequential model object

    """

    model = Sequential()
    #    model.add(InputLayer(input_shape=(128,128,1)))
    model.add(
        Conv2D(64, (3, 3),
               strides=(1, 1),
               input_shape=(128, 128, 1),
               kernel_initializer='glorot_uniform'))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(
        Conv2D(64, (3, 3), strides=(1, 1),
               kernel_initializer='glorot_uniform'))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(
        Conv2D(128, (3, 3),
               strides=(1, 1),
               kernel_initializer='glorot_uniform'))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(
        Conv2D(128, (3, 3),
               strides=(1, 1),
               kernel_initializer='glorot_uniform'))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(
        Conv2D(256, (3, 3),
               strides=(1, 1),
               kernel_initializer='glorot_uniform'))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(
        Conv2D(256, (3, 3),
               strides=(1, 1),
               kernel_initializer='glorot_uniform'))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(Flatten())

    model.add(Dense(2048))

    model.add(Activation('elu'))

    model.add(BatchNormalization())

    model.add(Dropout(0.5))

    model.add(Dense(8, activation='softmax'))

    # compile model
    adam = optimizers.Adam(lr=1e-3, decay=0.95e-3)
    model.compile(optimizer=adam,
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    if (multi_gpu == True):
        parallel_model = multi_gpu_model(model, gpus=num_gpus)

        parallel_model.compile(optimizer=adam,
                               loss='sparse_categorical_crossentropy',
                               metrics=['accuracy'])
        return model, parallel_model

    return model
Example #6
0
    plt.tight_layout()
    plt.show()

val_data_gen = image_gen_val.flow_from_directory(batch_size=batch_size,
                                                 directory=validation_dir,
                                                 target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                 class_mode='categorical')

augmented_images = [train_data_gen[0][0][0] for i in range(5)]
#plotImages(augmented_images)

model_enfa = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', #convolution with 16 filterns of 3 of size
           input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Dropout(0.2),                                     #drop out of 20% to turn off 20% on neurons
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),                                   #drop out of 20% to turn off 20% on neurons
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(256, activation='relu'),
    Dense(3)
])

model_enfa.compile(optimizer='adam',
                  loss=tf.keras.losses.CategoricalCrossentropy(
    name='categorical_crossentropy'),
                  metrics=['accuracy'])
    def __init__(self, training_batch_size=100, existing_weight=None, test_percentage=0.02, learning_rate=0.002, save_every_x_epoch=5, number_of_training_sample=sys.maxsize, fine_tuning_model=None, memory_safe=True, validate=True):

        if memory_safe == False:
            self.Y = []
            self.X = []

        self.save_every_x_epoch = save_every_x_epoch
        self.validate = validate
        self.memory_safe = memory_safe
        self.number_of_sample = number_of_training_sample
        self.training_batch_size = training_batch_size

        # We know that MNIST images are 28 pixels in each dimension.
        self.img_size = 160

        self.img_size_flat = self.img_size * self.img_size * 3

        self.img_shape_full = (self.img_size, self.img_size, 3)

        self.test = {}
        test_X = []
        test_Y = []
        with open('train_labels.csv', 'r') as csvfile:
            reader = csv.reader(csvfile)
            all_class_samples = []
            for row in reader:
                all_class_samples.append(row)

            print(len(all_class_samples))
            test_count = int(test_percentage * len(all_class_samples))
            print("Training with: " + str(int((1 - test_percentage) * len(all_class_samples))) + ", Testing with: " + str(test_count))
            index = 0
            for row in all_class_samples:

                y = []
                for index in range(1, 7):
                    if index < 3:
                        y.append(float(row[index]))
                    else:
                        y.append(float(row[index]) / 160)
                print(y)

                if index > test_count:
                    if memory_safe == False and len(self.Y) < number_of_training_sample:
                        image = Image.open(row[0])
                        img_array = np.asarray(image)
                        if img_array.shape != self.img_shape_full:
                            continue
                        self.X.append(img_array.flatten())
                        self.Y.append(y)
                        continue
                    else:
                        break

                self.test[row[0]] = y
                index += 1

            for (key, value) in self.test.items():

                image = Image.open(key)
                img_array = np.asarray(image)
                if img_array.shape != self.img_shape_full:
                    continue
                test_X.append(img_array.flatten())
                test_Y.append(value)

            self.test_X = np.array(test_X) / 255
            self.test_Y = np.array(test_Y)

            if memory_safe == False:
                self.Y = np.array(self.Y)
                self.X = np.array(self.X) / 255
                print(self.X.shape, self.Y.shape)

        # Start construction of the Keras Sequential model.
        model = Sequential()
        self.model = model

        model.add(InputLayer(input_shape=(self.img_size_flat,)))

        # The input is a flattened array with 784 elements,
        # but the convolutional layers expect images with shape (28, 28, 1)
        model.add(Reshape(self.img_shape_full))

        # First convolutional layer with ReLU-activation and max-pooling.
        model.add(Conv2D(kernel_size=5, strides=1, filters=16, padding='same',
                         activation='relu', name='layer_conv1'))
        model.add(MaxPooling2D(pool_size=2, strides=2))

        # Second convolutional layer with ReLU-activation and max-pooling.
        model.add(Conv2D(kernel_size=5, strides=1, filters=32, padding='same',
                         activation='relu', name='layer_conv2'))
        model.add(MaxPooling2D(pool_size=2, strides=2))

        model.add(Conv2D(kernel_size=5, strides=2, filters=64, padding='same',
                         activation='relu', name='layer_conv3'))
        model.add(MaxPooling2D(pool_size=2, strides=2))

        # Flatten the 4-rank output of the convolutional layers
        # to 2-rank that can be input to a fully-connected / dense layer.
        model.add(Flatten())

        model.add(Dense(512, activation='relu'))
        model.add(Dropout(0.2))
        model.add(Dense(6))

        def normalize_loss(yTrue, yPred):
            total_loss = 0
            total_loss += K.sum(K.square(yTrue[0:2] - yPred[0:2]))
            # if yTrue[0] == 1:
            #     total_loss += K.sum(K.square(yTrue[3:5] - yPred[3:5]))
            if yTrue[0] == 1:
                total_loss += K.sum(K.square(yTrue[2:4] - yPred[2:4]))
            else:
                total_loss += K.sum(K.square(yTrue[0] - yPred[0]))

            if yTrue[1] == 1:
                total_loss += K.sum(K.square(yTrue[4:6] - yPred[4:6]))
            else:
                total_loss += K.sum(K.square(yTrue[1] - yPred[1]))

            return K.sqrt(total_loss)

        self.optimizer = optimizers.Adadelta(lr=learning_rate, clipnorm=2.)

        model.compile(optimizer=self.optimizer, loss=normalize_loss, metrics=[metrics.cosine,  metrics.mse])
        if existing_weight != None:
            model.load_weights(existing_weight)
        else:
            all_weights = os.listdir(os.path.join("models", "keypoint_model"))
            if ".DS_Store" in all_weights:
                all_weights.remove(".DS_Store")

            if len(all_weights) > 0:
                last_copy = sorted(all_weights)[-1]
                model.load_weights(os.path.join("models", "keypoint_model", last_copy))

        if fine_tuning_model != None:
            for index in range(3):
                weights = fine_tuning_model.layers[index].get_weights()
                model.layers[index].set_weights(weights)
            print("Fine tuning model copied")

        model.save(os.path.join("models", "keypoint_model.h5"))
Example #8
0
def build_unet(input_shape, num_classes):
    inputs = tf.keras.Input(shape=input_shape)
    conv1 = Conv2D(64,
                   3,
                   activation='relu',
                   dilation_rate=2,
                   padding='same',
                   kernel_initializer='he_normal')(inputs)
    conv1 = BatchNormalization()(conv1)
    conv1 = Conv2D(64,
                   3,
                   activation='relu',
                   dilation_rate=2,
                   padding='same',
                   kernel_initializer='he_normal')(conv1)
    conv1 = BatchNormalization()(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    conv2 = Conv2D(128,
                   3,
                   activation='relu',
                   dilation_rate=2,
                   padding='same',
                   kernel_initializer='he_normal')(pool1)
    conv2 = BatchNormalization()(conv2)
    conv2 = Conv2D(128,
                   3,
                   activation='relu',
                   dilation_rate=2,
                   padding='same',
                   kernel_initializer='he_normal')(conv2)
    conv2 = BatchNormalization()(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    conv3 = Conv2D(256,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(pool2)
    conv3 = BatchNormalization()(conv3)
    conv3 = Conv2D(256,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv3)
    conv3 = BatchNormalization()(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
    conv4 = Conv2D(512,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(pool3)
    conv4 = BatchNormalization()(conv4)
    conv4 = Conv2D(512,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv4)
    conv4 = BatchNormalization()(conv4)
    drop4 = Dropout(0.5)(conv4, training=True)
    pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)

    conv5 = Conv2D(1024,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(pool4)
    conv5 = BatchNormalization()(conv5)
    conv5 = Conv2D(1024,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv5)
    conv5 = BatchNormalization()(conv5)
    drop5 = Dropout(0.5)(conv5, training=True)

    up6 = Conv2D(512,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(UpSampling2D(size=(2,
                                                                    2))(drop5))
    merge6 = concatenate([drop4, up6], axis=3)
    conv6 = Conv2D(512,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(merge6)
    conv6 = Conv2D(512,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv6)

    up7 = Conv2D(256,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(UpSampling2D(size=(2,
                                                                    2))(conv6))
    merge7 = concatenate([conv3, up7], axis=3)
    conv7 = Conv2D(256,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(merge7)
    conv7 = Conv2D(256,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv7)

    up8 = Conv2D(128,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(UpSampling2D(size=(2,
                                                                    2))(conv7))
    merge8 = concatenate([conv2, up8], axis=3)
    conv8 = Conv2D(128,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(merge8)
    conv8 = Conv2D(128,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv8)

    up9 = Conv2D(64,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(UpSampling2D(size=(2,
                                                                    2))(conv8))
    merge9 = concatenate([conv1, up9], axis=3)
    conv9 = Conv2D(64,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(merge9)
    conv9 = Conv2D(32,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv9)

    x = Flatten()(conv9)
    x = Dense(512, activation='relu', kernel_initializer='he_uniform')(x)
    x = Dropout(0.3)(x)
    x = Dense(num_classes, activation='softmax', name='predictions')(x)
    return Model(inputs=inputs, outputs=x)

    return model
Example #9
0
def new_model(nb_classes, dropout=True, dropout_rate=0.2):
    """
    Creates a vgg11 network.

    Parameters
    ----------
    input_shape : tuple
        The shape of the input tensor not including the sample axis.
        Tensorflow uses the NHWC dimention ordering convention.
    nb_class : int
        The number of output class. The network will have this number of
        output nodes for one-hot encoding.
    dropout : bool
        Where or not to implement dropout in the fully-connected layers.
    dropout_rate : float
        Dropout rate.

    Returns
    -------
    keras.models.Sequential() :
        The create vgg11 network.
    """
    model = Sequential()

    # sub-net 1
    model.add(
        Conv2D(filters=16,
               kernel_size=3,
               padding='same',
               activation='relu',
               input_shape=img_shape))
    model.add(
        Conv2D(filters=16, kernel_size=3, padding='same', activation='relu'))
    model.add(MaxPool2D(pool_size=2))

    # sub-net 2
    model.add(
        Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
    model.add(
        Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
    model.add(MaxPool2D(pool_size=2))

    # sub-net 3
    model.add(
        Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
    model.add(
        Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
    model.add(MaxPool2D(pool_size=2))

    # dense layers
    model.add(Flatten())
    model.add(Dense(units=64, activation='relu'))
    model.add(Dropout(dropout_rate)) if dropout else None
    model.add(Dense(units=64, activation='relu'))
    model.add(Dropout(dropout_rate)) if dropout else None
    model.add(Dense(units=nb_classes, activation='softmax'))

    # Compile Model
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
Example #10
0
def data_pre(data):
    # 得到标签
    label = [[i] * len(data[i]) for i in range(len(data))][0]
    label = to_categorical(label)
    # 切词
    context = []
    for i in data:
        for j in i:
            context.append(jieba.lcut(j))

    # 构建词典
    tokenizer = Tokenizer(num_words=20000)
    tokenizer.fit_on_texts(context)

    train_tags_title = tokenizer.texts_to_sequences(context)
    train_tags_title_preprocessed = pad_sequences(train_tags_title,
                                                  maxlen=45,
                                                  padding='post')

    # 预训练词向量
    # embedding_matrix = np.zeros((278028, 30), dtype=np.float32)
    # f = open('wiki.zh.text.vector', encoding='utf-8')
    # f = f.readlines()
    # for text in f:
    #     text = text.split()
    #     if text[0] in context:
    #         embedding_matrix[context[text[0]]] = text[1:]

    # 模型
    x_1 = Input(shape=(45, ))  # 输入数据维度
    embed_1 = Embedding(input_dim=45,
                        output_dim=45)(x_1)  # 将索引值转化为稠密向量,且只能做第一层
    L_1 = (LSTM(64))(embed_1)  # 第一个括号构建一个层 64是输出空间的维度,第二个括号用该层做计算
    L_1 = Dropout(0.5)(L_1)  # 防止过拟合,0.5在这里是需要丢弃的输入比例
    L_1 = Dense(9, activation='softmax')(L_1)  # 3是输出空间维度
    model_one = Model(x_1, L_1)  # x_1输入,L_1输出
    model_one.compile(loss='categorical_crossentropy',
                      optimizer='rmsprop',
                      metrics=['acc'])  # 'binary_crossentropy'
    history = model_one.fit(train_tags_title_preprocessed,
                            label,
                            batch_size=512,
                            epochs=20,
                            validation_split=0.1,
                            shuffle=True)
    # 汇总acc函数历史数据
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model acc')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()
    # 汇总损失函数历史数据
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()
Example #11
0
                  padding='same'),
    BatchNormalization(),
    MaxPool2D((2, 2)),
    Convolution2D(filters=16,
                  kernel_size=(3, 3),
                  activation='relu',
                  padding='same'),
    BatchNormalization(),
    Convolution2D(filters=16,
                  kernel_size=(3, 3),
                  activation='relu',
                  padding='same'),
    BatchNormalization(),
    Flatten(),
    Dense(units=32, activation="relu"),
    Dropout(0.15),
    Dense(units=16, activation="relu"),
    Dropout(0.05),
    Dense(units=10, activation="softmax")
])
optim = RMSprop(lr=0.001)
model.compile(optimizer=optim,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(x_train,
                    to_categorical(y_train),
                    epochs=80,
                    validation_split=0.15,
                    verbose=1)
eval = model.evaluate(x_test, to_categorical(y_test))
plt.plot(history.history['accuracy'])
Example #12
0
    def build(self):

        # 1 -- CodeNN
        methodname = Input(shape=(self.methname_len, ),
                           dtype='int32',
                           name='methodname')
        apiseq = Input(shape=(self.apiseq_len, ), dtype='int32', name='apiseq')
        tokens = Input(shape=(self.tokens_len, ), dtype='int32', name='tokens')

        # methodname
        # embedding layer
        init_emd_weights = np.load(
            self.data_dir + self.init_embed_weights_methodname
        ) if self.init_embed_weights_methodname is not None else None
        init_emd_weights = init_emd_weights if init_emd_weights is None else [
            init_emd_weights
        ]

        embedding = Embedding(input_dim=self.vocab_size,
                              output_dim=self.embed_dims,
                              weights=init_emd_weights,
                              mask_zero=False,
                              name='embedding_methodname')

        methodname_embedding = embedding(methodname)

        # dropout
        dropout = Dropout(0.25, name='dropout_methodname_embed')
        methodname_dropout = dropout(methodname_embedding)

        # forward rnn
        fw_rnn = LSTM(self.lstm_dims,
                      recurrent_dropout=0.2,
                      return_sequences=True,
                      name='lstm_methodname_fw')

        # backward rnn
        bw_rnn = LSTM(self.lstm_dims,
                      recurrent_dropout=0.2,
                      return_sequences=True,
                      go_backwards=True,
                      name='lstm_methodname_bw')

        methodname_fw = fw_rnn(methodname_dropout)
        methodname_bw = bw_rnn(methodname_dropout)

        dropout = Dropout(0.25, name='dropout_methodname_rnn')
        methodname_fw_dropout = dropout(methodname_fw)
        methodname_bw_dropout = dropout(methodname_bw)

        # max pooling
        maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False),
                         output_shape=lambda x: (x[0], x[2]),
                         name='maxpooling_methodname')
        methodname_pool = Concatenate(name='concat_methodname_lstm')(
            [maxpool(methodname_fw_dropout),
             maxpool(methodname_bw_dropout)])
        activation = Activation('tanh', name='active_methodname')
        methodname_repr = activation(methodname_pool)

        # apiseq
        # embedding layer
        embedding = Embedding(input_dim=self.vocab_size,
                              output_dim=self.embed_dims,
                              mask_zero=False,
                              name='embedding_apiseq')

        apiseq_embedding = embedding(apiseq)

        # dropout
        dropout = Dropout(0.25, name='dropout_apiseq_embed')
        apiseq_dropout = dropout(apiseq_embedding)

        # forward rnn
        fw_rnn = LSTM(self.lstm_dims,
                      return_sequences=True,
                      recurrent_dropout=0.2,
                      name='lstm_apiseq_fw')

        # backward rnn
        bw_rnn = LSTM(self.lstm_dims,
                      return_sequences=True,
                      recurrent_dropout=0.2,
                      go_backwards=True,
                      name='lstm_apiseq_bw')

        apiseq_fw = fw_rnn(apiseq_dropout)
        apiseq_bw = bw_rnn(apiseq_dropout)

        dropout = Dropout(0.25, name='dropout_apiseq_rnn')
        apiseq_fw_dropout = dropout(apiseq_fw)
        apiseq_bw_dropout = dropout(apiseq_bw)

        # max pooling

        maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False),
                         output_shape=lambda x: (x[0], x[2]),
                         name='maxpooling_apiseq')
        apiseq_pool = Concatenate(name='concat_apiseq_lstm')(
            [maxpool(apiseq_fw_dropout),
             maxpool(apiseq_bw_dropout)])
        activation = Activation('tanh', name='active_apiseq')
        apiseq_repr = activation(apiseq_pool)

        # tokens
        # embedding layer
        init_emd_weights = np.load(
            self.data_dir + self.init_embed_weights_tokens
        ) if self.init_embed_weights_tokens is not None else None
        init_emd_weights = init_emd_weights if init_emd_weights is None else [
            init_emd_weights
        ]

        embedding = Embedding(input_dim=self.vocab_size,
                              output_dim=self.embed_dims,
                              weights=init_emd_weights,
                              mask_zero=False,
                              name='embedding_tokens')

        tokens_embedding = embedding(tokens)

        # dropout
        dropout = Dropout(0.25, name='dropout_tokens_embed')
        tokens_dropout = dropout(tokens_embedding)

        # forward rnn
        fw_rnn = LSTM(self.lstm_dims,
                      recurrent_dropout=0.2,
                      return_sequences=True,
                      name='lstm_tokens_fw')

        # backward rnn
        bw_rnn = LSTM(self.lstm_dims,
                      recurrent_dropout=0.2,
                      return_sequences=True,
                      go_backwards=True,
                      name='lstm_tokens_bw')

        tokens_fw = fw_rnn(tokens_dropout)
        tokens_bw = bw_rnn(tokens_dropout)

        dropout = Dropout(0.25, name='dropout_tokens_rnn')
        tokens_fw_dropout = dropout(tokens_fw)
        tokens_bw_dropout = dropout(tokens_bw)

        # max pooling
        maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False),
                         output_shape=lambda x: (x[0], x[2]),
                         name='maxpooling_tokens')
        tokens_pool = Concatenate(name='concat_tokens_lstm')(
            [maxpool(tokens_fw_dropout),
             maxpool(tokens_bw_dropout)])
        # tokens_pool = maxpool(tokens_dropout)
        activation = Activation('tanh', name='active_tokens')
        tokens_repr = activation(tokens_pool)

        # fusion methodname, apiseq, tokens
        merge_methname_api = Concatenate(name='merge_methname_api')(
            [methodname_repr, apiseq_repr])
        merge_code_repr = Concatenate(name='merge_code_repr')(
            [merge_methname_api, tokens_repr])

        code_repr = Dense(self.hidden_dims,
                          activation='tanh',
                          name='dense_coderepr')(merge_code_repr)

        self.code_repr_model = Model(inputs=[methodname, apiseq, tokens],
                                     outputs=[code_repr],
                                     name='code_repr_model')
        self.code_repr_model.summary()

        #  2 -- description
        desc = Input(shape=(self.desc_len, ), dtype='int32', name='desc')

        # desc
        # embedding layer
        init_emd_weights = np.load(
            self.data_dir + self.init_embed_weights_desc
        ) if self.init_embed_weights_desc is not None else None
        init_emd_weights = init_emd_weights if init_emd_weights is None else [
            init_emd_weights
        ]

        embedding = Embedding(input_dim=self.vocab_size,
                              output_dim=self.embed_dims,
                              weights=init_emd_weights,
                              mask_zero=False,
                              name='embedding_desc')

        desc_embedding = embedding(desc)

        # dropout
        dropout = Dropout(0.25, name='dropout_desc_embed')
        desc_dropout = dropout(desc_embedding)

        # forward rnn
        fw_rnn = LSTM(self.lstm_dims,
                      recurrent_dropout=0.2,
                      return_sequences=True,
                      name='lstm_desc_fw')

        # backward rnn
        bw_rnn = LSTM(self.lstm_dims,
                      recurrent_dropout=0.2,
                      return_sequences=True,
                      go_backwards=True,
                      name='lstm_desc_bw')

        desc_fw = fw_rnn(desc_dropout)
        desc_bw = bw_rnn(desc_dropout)

        dropout = Dropout(0.25, name='dropout_desc_rnn')
        desc_fw_dropout = dropout(desc_fw)
        desc_bw_dropout = dropout(desc_bw)

        # max pooling

        maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False),
                         output_shape=lambda x: (x[0], x[2]),
                         name='maxpooling_desc')
        desc_pool = Concatenate(name='concat_desc_lstm')(
            [maxpool(desc_fw_dropout),
             maxpool(desc_bw_dropout)])
        activation = Activation('tanh', name='active_desc')
        desc_repr = activation(desc_pool)

        self.desc_repr_model = Model(inputs=[desc],
                                     outputs=[desc_repr],
                                     name='desc_repr_model')
        self.desc_repr_model.summary()

        #  3 -- cosine similarity
        code_repr = self.code_repr_model([methodname, apiseq, tokens])
        desc_repr = self.desc_repr_model([desc])

        cos_sim = Dot(axes=1, normalize=True,
                      name='cos_sim')([code_repr, desc_repr])

        sim_model = Model(inputs=[methodname, apiseq, tokens, desc],
                          outputs=[cos_sim],
                          name='sim_model')

        self.sim_model = sim_model

        self.sim_model.summary()

        #  4 -- build training model
        good_sim = sim_model(
            [self.methodname, self.apiseq, self.tokens, self.desc_good])
        bad_sim = sim_model(
            [self.methodname, self.apiseq, self.tokens, self.desc_bad])

        loss = Lambda(lambda x: K.maximum(1e-6, self.margin - x[0] + x[1]),
                      output_shape=lambda x: x[0],
                      name='loss')([good_sim, bad_sim])

        self.training_model = Model(inputs=[
            self.methodname, self.apiseq, self.tokens, self.desc_good,
            self.desc_bad
        ],
                                    outputs=[loss],
                                    name='training_model')

        self.training_model.summary()
Example #13
0
def train_bidirectional_baseline_seq2seq_model(mfcc_features,
                                               target_length,
                                               latent_dim,
                                               word_level,
                                               gpu_enabled=False):
    """
        trains Encoder/Decoder architecture and prepares encoder_model and decoder_model for prediction part
        :param mfcc_features: int
        :param target_length: int
        :param latent_dim: int
        :return: Model, Model, Model
        """
    # Encoder training

    encoder_inputs = Input(shape=(None, mfcc_features), name="encoder_input")

    decoder_inputs = Input(shape=(None, target_length), name="decoder_input")

    pre_decoder_dense_layer = Dense(44,
                                    activation="relu",
                                    name="pre_decoder_dense")
    decoder_entries = pre_decoder_dense_layer(decoder_inputs)
    dropout_layer = Dropout(0.1, name="decoder_dropout")
    decoder_entries = dropout_layer(decoder_entries)

    if gpu_enabled:
        encoder_states = encoder_bi_GRU_gpu(input_shape=mfcc_features,
                                            encoder_inputs=encoder_inputs,
                                            latent_dim=latent_dim)

        # Decoder training, using 'encoder_states' as initial state.

        decoder_outputs = decoder_for_bidirectional_encoder_GRU_gpu(
            target_length=target_length,
            encoder_states=encoder_states,
            decoder_inputs=decoder_entries,
            latent_dim=latent_dim)

    else:
        encoder_states = encoder_bi_GRU(input_shape=mfcc_features,
                                        encoder_inputs=encoder_inputs,
                                        latent_dim=latent_dim)

        # Decoder training, using 'encoder_states' as initial state.

        decoder_outputs = decoder_for_bidirectional_encoder_GRU(
            target_length=target_length,
            encoder_states=encoder_states,
            decoder_inputs=decoder_entries,
            latent_dim=latent_dim)

    # Dense Output Layers
    if word_level:
        target_length = len(settings.CHARACTER_SET) + 1
        decoder_outputs = get_multi_output_dense(decoder_outputs,
                                                 target_length)
    else:
        decoder_dense = Dense(target_length,
                              activation='softmax',
                              name="decoder_dense")
        decoder_outputs = decoder_dense(decoder_outputs)

    # Generating Keras Model
    model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
    return model
Example #14
0
def cifar10_student_strong(n_classes: int,
                           input_shape=None,
                           input_tensor=None,
                           weights_path: Union[None, str] = None) -> Model:
    """
    Defines a cifar10 strong student network.

    :param n_classes: the number of classes.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained cifar10 tiny network's weights.
    :return: Keras functional Model.
    """
    inputs = create_inputs(input_shape, input_tensor)

    # Define a weight decay for the regularisation.
    weight_decay = 1e-4

    # Block1.
    x = Conv2D(32, (3, 3),
               padding='same',
               activation='elu',
               name='block1_conv1',
               kernel_regularizer=l2(weight_decay))(inputs)

    x = BatchNormalization(name='block1_batch-norm1')(x)
    x = Conv2D(32, (3, 3),
               padding='same',
               activation='elu',
               name='block1_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block1_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block1_pool')(x)
    x = Dropout(0.2, name='block1_dropout', seed=0)(x)

    # Block2.
    x = Conv2D(64, (3, 3),
               padding='same',
               activation='elu',
               name='block2_conv1',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block2_batch-norm1')(x)
    x = Conv2D(64, (3, 3),
               padding='same',
               activation='elu',
               name='block2_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block2_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block2_pool')(x)
    x = Dropout(0.3, name='block2_dropout', seed=0)(x)

    # Add top layers.
    x = Flatten()(x)
    x = Dense(n_classes)(x)
    outputs = Activation('softmax', name='softmax')(x)

    # Create model.
    model = Model(inputs, outputs, name='cifar10_student_strong')

    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
Example #15
0
red_neuronal.add(MaxPooling2D(pool_size=tamano_pool))
red_neuronal.add(
    Convolution2D(filtrosConv2,
                  tamano_filtro2,
                  padding='same',
                  activation='relu'))
red_neuronal.add(MaxPooling2D(pool_size=tamano_pool))

red_neuronal.add(Flatten())
#cnn.add(Dense(256,activation='relu'))
#cnn.add(Dense(315,activation='relu'))
#cnn.add(Dense(516,activation='relu'))
#cnn.add(Dense(555,activation='relu')) #Modelo 7
#red_neuronal.add(Dense(650,activation='relu'))#Modelo 10,11
red_neuronal.add(Dense(150, activation='relu'))
red_neuronal.add(Dropout(0.5))
red_neuronal.add(Dense(clases, activation='softmax'))
red_neuronal.compile(loss='categorical_crossentropy',
                     optimizer=optimizers.Adam(lr=lr),
                     metrics=['accuracy'])

historial = red_neuronal.fit(imagen_entrenamiento,
                             steps_per_epoch=pasos,
                             epochs=epocas)
#******************************************************************************************************************************

#***************************************************** Almecenamineto del modelo ya entrenado**********************************

direccion = 'C:/Users/carlo/Desktop/Entrenamiento_Frutas/Modelo/'  #Ruta de la carpeta donde se almacenara el modelo

if not os.path.exists(direccion):
Example #16
0
def context_encoder_variational_autoencoder(x, x_ce, dropout_rate, dropout,
                                            config):
    outputs = {}

    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(),
                                        config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

        temp_out_ce = x_ce
        for layer in encoder:
            temp_out_ce = layer(temp_out_ce)

    with tf.variable_scope("Bottleneck"):
        intermediate_conv = Conv2D(temp_out.get_shape().as_list()[3] // 8,
                                   1,
                                   padding='same')
        intermediate_conv_reverse = Conv2D(temp_out.get_shape().as_list()[3],
                                           1,
                                           padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)
        temp_out_ce = intermediate_conv(temp_out_ce)
        reshape = temp_out.get_shape().as_list()[1:]

        mu_layer = Dense(config.zDim)
        sigma_layer = Dense(config.zDim)
        dec_dense = Dense(np.prod(reshape))

        flatten = Flatten()(temp_out)
        outputs['z_mu'] = z_mu = dropout_layer(mu_layer(flatten), dropout)
        outputs['z_mu_ce'] = z_mu_ce = dropout_layer(
            mu_layer(Flatten()(temp_out_ce)), dropout)
        outputs['z_log_sigma'] = z_log_sigma = dropout_layer(
            sigma_layer(flatten), dropout)
        outputs['z_sigma'] = z_sigma = tf.exp(z_log_sigma)
        z_vae = z_mu + tf.random_normal(tf.shape(z_sigma)) * z_sigma
        reshaped = tf.reshape(dropout_layer(dec_dense(z_vae), dropout),
                              [-1, *reshape])
        temp_out = intermediate_conv_reverse(reshaped)
        temp_out_ce = intermediate_conv_reverse(
            tf.reshape(dropout_layer(dec_dense(z_mu_ce), dropout),
                       [-1, *reshape]))

    with tf.variable_scope('Decoder'):
        decoder = build_unified_decoder(config.outputWidth,
                                        config.intermediateResolutions,
                                        config.numChannels)

        # Decode: z -> x_hat
        for layer in decoder:
            temp_out = layer(temp_out)

        outputs['x_hat'] = temp_out

        for layer in decoder:
            temp_out_ce = layer(temp_out_ce)

        outputs['x_hat_ce'] = temp_out_ce

    return outputs
Example #17
0
            else:
                base_model = network(include_top=False,
                                     weights='imagenet',
                                     classes=num_classes)

            x = base_model.output
            x = GlobalAveragePooling2D()(x)

            #  two fully-connected layers
            if l2_reg > 0.:
                x = Dense(512,
                          activation='relu',
                          kernel_regularizer=regularizers.l2(l2_reg))(x)
                # add a dropout layer
                x = Dropout(dropout_rate)(x)
                x = Dense(256,
                          activation='relu',
                          kernel_regularizer=regularizers.l2(l2_reg))(x)
            else:
                x = Dense(512, activation='relu')(x)
                # add a dropout layer
                x = Dropout(dropout_rate)(x)
                x = Dense(256, activation='relu')(x)

            # and a softmax layer -- 6 classes
            predictions = Dense(num_classes, activation='softmax')(x)

            optimizer = Adam(lr=learning_rate, epsilon=epsilon)

            # this is the model we will train
Example #18
0
        num = LETTERSTR.find(letter)
        onehot[num] = 1
        labellist.append(onehot)
    return labellist


# Create CNN Model
print("Creating CNN model...")
inp = Input((60, 200, 3))
out = inp
out = Conv2D(filters=32, kernel_size=(3, 3), padding='same',
             activation='relu')(out)
out = Conv2D(filters=32, kernel_size=(3, 3), activation='relu')(out)
out = BatchNormalization()(out)
out = MaxPooling2D(pool_size=(2, 2))(out)
out = Dropout(0.3)(out)
out = Conv2D(filters=64, kernel_size=(3, 3), padding='same',
             activation='relu')(out)
out = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(out)
out = BatchNormalization()(out)
out = MaxPooling2D(pool_size=(2, 2))(out)
out = Dropout(0.3)(out)
out = Conv2D(filters=128,
             kernel_size=(3, 3),
             padding='same',
             activation='relu')(out)
out = Conv2D(filters=128, kernel_size=(3, 3), activation='relu')(out)
out = BatchNormalization()(out)
out = MaxPooling2D(pool_size=(2, 2))(out)
out = Dropout(0.3)(out)
out = Conv2D(filters=256, kernel_size=(3, 3), activation='relu')(out)
Example #19
0
xest, yest = prepp(test_data, windows)
print(xt2.shape, yt2.shape, xest.shape, yest.shape)

# window = 60
# xt2, yt2 = pppp(pd.DataFrame(train_data), pd.DataFrame(test_data), window)
# xest, yest = pppp(xtest, ytest, window)

xt3 = np.array(xt2).reshape(xt2.shape[0], xt2.shape[1], 1)
yt3 = np.array(yt2)

xt4 = np.array(xest).reshape(xest.shape[0], xest.shape[1], 1)
yt4 = np.array(yest)

model2 = Sequential()
model2.add(LSTM(100, return_sequences=True, input_shape=(len(xt3[0]), 1)))
model2.add(Dropout(0.2))

# model2.add(LSTM(100, return_sequences=True))
# model2.add(Dropout(0.2))

model2.add(LSTM(50))

model2.add(Dense(1, activation='linear'))

model2.compile(loss='mse', optimizer='Adam')

model_history = model2.fit(xt3,
                           yt3,
                           epochs=20,
                           batch_size=100,
                           verbose=1,
cnn_model.add(MaxPooling2D(pool_size=(2, 4)))

# Adding convolutional layer
cnn_model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))

# Adding max pooling layer
cnn_model.add(MaxPooling2D(pool_size=(2, 4)))

# Adding a flattened layer to input our image data
cnn_model.add(Flatten())

# Adding a dense layer with 64 neurons
cnn_model.add(Dense(64, activation='relu'))

# Adding a dropout layer for regularization
cnn_model.add(Dropout(0.25))

# Adding an output layer
cnn_model.add(Dense(10, activation='softmax'))

# Compiling our neural network
cnn_model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

# Fitting our neural network
history = cnn_model.fit(X_train,
                        y_train,
                        batch_size=16,
                        validation_data=(X_test, y_test),
                        epochs=15)
Example #21
0
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784, )))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

history = model.fit(x_train,
                    y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
def Inception_Inflated3d(include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=None,
                dropout_prob=0.0,
                endpoint_logit=True,
                classes=400):
    """Instantiates the Inflated 3D Inception v1 architecture.

    Optionally loads weights pre-trained
    on Kinetics. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format='channels_last'` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.
    Note that the default input frame(image) size for this model is 224x224.

    # Arguments
        include_top: whether to include the the classification 
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or 'kinetics_only' (pre-training on Kinetics dataset only).
            or 'imagenet_and_kinetics' (pre-training on ImageNet and Kinetics datasets).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(NUM_FRAMES, 224, 224, 3)` (with `channels_last` data format)
            or `(NUM_FRAMES, 3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels.
            NUM_FRAMES should be no smaller than 8. The authors used 64
            frames per example for training and testing on kinetics dataset
            Also, Width and height should be no smaller than 32.
            E.g. `(64, 150, 150, 3)` would be one valid value.
        dropout_prob: optional, dropout probability applied in dropout layer
            after global average pooling layer. 
            0.0 means no dropout is applied, 1.0 means dropout is applied to all features.
            Note: Since Dropout is applied just before the classification
            layer, it is only useful when `include_top` is set to True.
        endpoint_logit: (boolean) optional. If True, the model's forward pass
            will end at producing logits. Otherwise, softmax is applied after producing
            the logits to produce the class probabilities prediction. Setting this parameter 
            to True is particularly useful when you want to combine results of rgb model
            and optical flow model.
            - `True` end model forward pass at logit output
            - `False` go further after logit to produce softmax predictions
            Note: This parameter is only useful when `include_top` is set to True.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if not (weights in WEIGHTS_NAME or weights is None or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or %s' % 
                         str(WEIGHTS_NAME) + ' ' 
                         'or a valid path to a file containing `weights` values')

    if weights in WEIGHTS_NAME and include_top and classes != 400:
        raise ValueError('If using `weights` as one of these %s, with `include_top`'
                         ' as true, `classes` should be 400' % str(WEIGHTS_NAME))

    # Determine proper input shape
    input_shape = _obtain_input_shape(
        input_shape,
        default_frame_size=224, 
        min_frame_size=32, 
        default_num_frames=64,
        min_num_frames=8,
        data_format=K.image_data_format(),
        require_flatten=include_top,
        weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = 4

    # Downsampling via convolution (spatial and temporal)
    x = conv3d_bn(img_input, 64, 7, 7, 7, strides=(2, 2, 2), padding='same', name='Conv3d_1a_7x7')

    # Downsampling (spatial only)
    x = MaxPooling3D((1, 3, 3), strides=(1, 2, 2), padding='same', name='MaxPool2d_2a_3x3')(x)
    x = conv3d_bn(x, 64, 1, 1, 1, strides=(1, 1, 1), padding='same', name='Conv3d_2b_1x1')
    x = conv3d_bn(x, 192, 3, 3, 3, strides=(1, 1, 1), padding='same', name='Conv3d_2c_3x3')

    # Downsampling (spatial only)
    x = MaxPooling3D((1, 3, 3), strides=(1, 2, 2), padding='same', name='MaxPool2d_3a_3x3')(x)

    # Mixed 3b
    branch_0 = conv3d_bn(x, 64, 1, 1, 1, padding='same', name='Conv3d_3b_0a_1x1')

    branch_1 = conv3d_bn(x, 96, 1, 1, 1, padding='same', name='Conv3d_3b_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 128, 3, 3, 3, padding='same', name='Conv3d_3b_1b_3x3')

    branch_2 = conv3d_bn(x, 16, 1, 1, 1, padding='same', name='Conv3d_3b_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 32, 3, 3, 3, padding='same', name='Conv3d_3b_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_3b_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 32, 1, 1, 1, padding='same', name='Conv3d_3b_3b_1x1')
    
    ######edit##############
    q = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_3b_3a_3x3')(x)
    q = conv3d_bn(q, 32, 1, 1, 1, padding='same', name='Conv3d_3b_3b_1x1')
    ########################
    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_3b')

    # Mixed 3c
    branch_0 = conv3d_bn(x, 128, 1, 1, 1, padding='same', name='Conv3d_3c_0a_1x1')

    branch_1 = conv3d_bn(x, 128, 1, 1, 1, padding='same', name='Conv3d_3c_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 192, 3, 3, 3, padding='same', name='Conv3d_3c_1b_3x3')

    branch_2 = conv3d_bn(x, 32, 1, 1, 1, padding='same', name='Conv3d_3c_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 96, 3, 3, 3, padding='same', name='Conv3d_3c_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_3c_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 64, 1, 1, 1, padding='same', name='Conv3d_3c_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_3c')


    # Downsampling (spatial and temporal)
    x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), padding='same', name='MaxPool2d_4a_3x3')(x)

    # Mixed 4b
    branch_0 = conv3d_bn(x, 192, 1, 1, 1, padding='same', name='Conv3d_4b_0a_1x1')

    branch_1 = conv3d_bn(x, 96, 1, 1, 1, padding='same', name='Conv3d_4b_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 208, 3, 3, 3, padding='same', name='Conv3d_4b_1b_3x3')

    branch_2 = conv3d_bn(x, 16, 1, 1, 1, padding='same', name='Conv3d_4b_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 48, 3, 3, 3, padding='same', name='Conv3d_4b_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_4b_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 64, 1, 1, 1, padding='same', name='Conv3d_4b_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_4b')

    # Mixed 4c
    branch_0 = conv3d_bn(x, 160, 1, 1, 1, padding='same', name='Conv3d_4c_0a_1x1')

    branch_1 = conv3d_bn(x, 112, 1, 1, 1, padding='same', name='Conv3d_4c_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 224, 3, 3, 3, padding='same', name='Conv3d_4c_1b_3x3')

    branch_2 = conv3d_bn(x, 24, 1, 1, 1, padding='same', name='Conv3d_4c_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 64, 3, 3, 3, padding='same', name='Conv3d_4c_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_4c_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 64, 1, 1, 1, padding='same', name='Conv3d_4c_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_4c')

    # Mixed 4d
    branch_0 = conv3d_bn(x, 128, 1, 1, 1, padding='same', name='Conv3d_4d_0a_1x1')

    branch_1 = conv3d_bn(x, 128, 1, 1, 1, padding='same', name='Conv3d_4d_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 256, 3, 3, 3, padding='same', name='Conv3d_4d_1b_3x3')

    branch_2 = conv3d_bn(x, 24, 1, 1, 1, padding='same', name='Conv3d_4d_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 64, 3, 3, 3, padding='same', name='Conv3d_4d_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_4d_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 64, 1, 1, 1, padding='same', name='Conv3d_4d_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_4d')

    # Mixed 4e
    branch_0 = conv3d_bn(x, 112, 1, 1, 1, padding='same', name='Conv3d_4e_0a_1x1')

    branch_1 = conv3d_bn(x, 144, 1, 1, 1, padding='same', name='Conv3d_4e_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 288, 3, 3, 3, padding='same', name='Conv3d_4e_1b_3x3')

    branch_2 = conv3d_bn(x, 32, 1, 1, 1, padding='same', name='Conv3d_4e_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 64, 3, 3, 3, padding='same', name='Conv3d_4e_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_4e_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 64, 1, 1, 1, padding='same', name='Conv3d_4e_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_4e')

    # Mixed 4f
    branch_0 = conv3d_bn(x, 256, 1, 1, 1, padding='same', name='Conv3d_4f_0a_1x1')

    branch_1 = conv3d_bn(x, 160, 1, 1, 1, padding='same', name='Conv3d_4f_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 320, 3, 3, 3, padding='same', name='Conv3d_4f_1b_3x3')

    branch_2 = conv3d_bn(x, 32, 1, 1, 1, padding='same', name='Conv3d_4f_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 128, 3, 3, 3, padding='same', name='Conv3d_4f_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_4f_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 128, 1, 1, 1, padding='same', name='Conv3d_4f_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_4f')


    # Downsampling (spatial and temporal)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding='same', name='MaxPool2d_5a_2x2')(x)

    # Mixed 5b
    branch_0 = conv3d_bn(x, 256, 1, 1, 1, padding='same', name='Conv3d_5b_0a_1x1')

    branch_1 = conv3d_bn(x, 160, 1, 1, 1, padding='same', name='Conv3d_5b_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 320, 3, 3, 3, padding='same', name='Conv3d_5b_1b_3x3')

    branch_2 = conv3d_bn(x, 32, 1, 1, 1, padding='same', name='Conv3d_5b_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 128, 3, 3, 3, padding='same', name='Conv3d_5b_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_5b_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 128, 1, 1, 1, padding='same', name='Conv3d_5b_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_5b')

    # Mixed 5c
    branch_0 = conv3d_bn(x, 384, 1, 1, 1, padding='same', name='Conv3d_5c_0a_1x1')

    branch_1 = conv3d_bn(x, 192, 1, 1, 1, padding='same', name='Conv3d_5c_1a_1x1')
    branch_1 = conv3d_bn(branch_1, 384, 3, 3, 3, padding='same', name='Conv3d_5c_1b_3x3')

    branch_2 = conv3d_bn(x, 48, 1, 1, 1, padding='same', name='Conv3d_5c_2a_1x1')
    branch_2 = conv3d_bn(branch_2, 128, 3, 3, 3, padding='same', name='Conv3d_5c_2b_3x3')

    branch_3 = MaxPooling3D((3, 3, 3), strides=(1, 1, 1), padding='same', name='MaxPool2d_5c_3a_3x3')(x)
    branch_3 = conv3d_bn(branch_3, 128, 1, 1, 1, padding='same', name='Conv3d_5c_3b_1x1')

    x = layers.concatenate(
        [branch_0, branch_1, branch_2, branch_3],
        axis=channel_axis,
        name='Mixed_5c')

    if include_top:
        # Classification block
        x = AveragePooling3D((2, 7, 7), strides=(1, 1, 1), padding='valid', name='global_avg_pool')(x)
        x = Dropout(dropout_prob)(x)

        x = conv3d_bn(x, classes, 1, 1, 1, padding='same', 
                use_bias=True, use_activation_fn=False, use_bn=False, name='Conv3d_6a_1x1')
 
        num_frames_remaining = int(x.shape[1])
        x = Reshape((num_frames_remaining, classes))(x)

        # logits (raw scores for each class)
        x = Lambda(lambda x: K.mean(x, axis=1, keepdims=False),
                   output_shape=lambda s: (s[0], s[2]))(x)

        if not endpoint_logit:
            x = Activation('softmax', name='prediction')(x)
    else:
        h = int(x.shape[2])
        w = int(x.shape[3])
        x = AveragePooling3D((2, h, w), strides=(1, 1, 1), padding='valid', name='global_avg_pool')(x)



    inputs = img_input
    # create model
    model = Model(inputs, x, name='i3d_inception')

    # load weights
    if weights in WEIGHTS_NAME:
        if weights == WEIGHTS_NAME[0]:   # rgb_kinetics_only
            if include_top:
                weights_url = WEIGHTS_PATH['rgb_kinetics_only']
                model_name = 'i3d_inception_rgb_kinetics_only.h5'
            else:
                weights_url = WEIGHTS_PATH_NO_TOP['rgb_kinetics_only']
                model_name = 'i3d_inception_rgb_kinetics_only_no_top.h5'

        elif weights == WEIGHTS_NAME[1]: # flow_kinetics_only
            if include_top:
                weights_url = WEIGHTS_PATH['flow_kinetics_only']
                model_name = 'i3d_inception_flow_kinetics_only.h5'
            else:
                weights_url = WEIGHTS_PATH_NO_TOP['flow_kinetics_only']
                model_name = 'i3d_inception_flow_kinetics_only_no_top.h5'

        elif weights == WEIGHTS_NAME[2]: # rgb_imagenet_and_kinetics
            if include_top:
                weights_url = WEIGHTS_PATH['rgb_imagenet_and_kinetics']
                model_name = 'i3d_inception_rgb_imagenet_and_kinetics.h5'
            else:
                weights_url = WEIGHTS_PATH_NO_TOP['rgb_imagenet_and_kinetics']
                model_name = 'i3d_inception_rgb_imagenet_and_kinetics_no_top.h5'

        elif weights == WEIGHTS_NAME[3]: # flow_imagenet_and_kinetics
            if include_top:
                weights_url = WEIGHTS_PATH['flow_imagenet_and_kinetics']
                model_name = 'i3d_inception_flow_imagenet_and_kinetics.h5'
            else:
                weights_url = WEIGHTS_PATH_NO_TOP['flow_imagenet_and_kinetics']
                model_name = 'i3d_inception_flow_imagenet_and_kinetics_no_top.h5'

        downloaded_weights_path = get_file(model_name, weights_url, cache_subdir='models')
        model.load_weights(downloaded_weights_path)

        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)

        if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow':
            warnings.warn('You are using the TensorFlow backend, yet you '
                          'are using the Theano '
                          'image data format convention '
                          '(`image_data_format="channels_first"`). '
                          'For best performance, set '
                          '`image_data_format="channels_last"` in '
                          'your keras config '
                          'at ~/.keras/keras.json.')

    elif weights is not None:
        model.load_weights(weights)

    return model
Example #23
0
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# Construct CNN architecture
model_cnn = Sequential()
model_cnn.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]))
model_cnn.add(Activation('relu'))
model_cnn.add(Conv2D(32, (3, 3), padding='same'))
model_cnn.add(Activation('relu'))
model_cnn.add(MaxPooling2D(pool_size=(2, 2)))
model_cnn.add(Dropout(0.25))

model_cnn.add(Conv2D(64, (3, 3), padding='same'))
model_cnn.add(Activation('relu'))
model_cnn.add(Conv2D(64, (3, 3), padding='same'))
model_cnn.add(Activation('relu'))
model_cnn.add(MaxPooling2D(pool_size=(2, 2)))
model_cnn.add(Dropout(0.25))

model_cnn.add(Flatten())
model_cnn.add(Dense(512))
model_cnn.add(Activation('relu'))
model_cnn.add(Dropout(0.5))
model_cnn.add(Dense(num_classes))
model_cnn.add(Activation('softmax'))
Example #24
0
def caltech_complicated_ensemble_submodel1(input_shape=None, input_tensor=None, n_classes=None,
                                           weights_path: Union[None, str] = None) -> Model:
    """
    Defines a caltech network.

    :param n_classes: used in order to be compatible with the main script.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained custom network's weights.
    :return: Keras functional API Model.
    """
    inputs = create_inputs(input_shape, input_tensor)

    # Define a weight decay for the regularisation.
    weight_decay = 1e-4

    x = Conv2D(64, (3, 3), padding='same', activation='relu',
               input_shape=input_shape, kernel_regularizer=l2(weight_decay))(inputs)
    x = BatchNormalization()(x)
    x = Dropout(0.3, seed=0)(x)

    x = Conv2D(64, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(128, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(128, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)
    x = Dropout(0.4, seed=0)(x)

    x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.5, seed=0)(x)

    x = Flatten()(x)
    x = Dense(512, kernel_regularizer=l2(weight_decay))(x)

    x = BatchNormalization()(x)

    x = Dropout(0.5, seed=0)(x)
    outputs = Dense(n_classes, activation='softmax', name='softmax_outputs')(x)

    # Create Submodel 1.
    model = Model(inputs, outputs, name='caltech_complicated_ensemble_submodel1')
    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
Example #25
0
def Deeplabv3(input_shape=(512, 512, 3),
              weights=None,
              input_tensor=None,
              classes=21,
              backbone='mobilenetv2',
              OS=16,
              alpha=.8,
              activation='sigmoid'):
    """ Instantiates the Deeplabv3+ architecture
    Optionally loads weights pre-trained
    on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
    # Arguments
        weights: one of 'pascal_voc' (pre-trained on pascal voc),
            'cityscapes' (pre-trained on cityscape) or None (random initialization)
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
        classes: number of desired classes. PASCAL VOC has 21 classes, Cityscapes has 19 classes.
            If number of classes not aligned with the weights used, last layer is initialized randomly
        backbone: backbone to use. one of {'xception','mobilenetv2'}
        activation: optional activation to add to the top of the network.
            One of 'softmax', 'sigmoid' or None
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - If `alpha` < 1.0, proportionally decreases the number
                    of filters in each layer.
                - If `alpha` > 1.0, proportionally increases the number
                    of filters in each layer.
                - If `alpha` = 1, default number of filters from the paper
                    are used at each layer.
            Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.
    # Returns
        A Keras model instance.
    # Raises
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
        ValueError: in case of invalid argument for `weights` or `backbone`
    """

    if not (weights in {'pascal_voc', 'cityscapes', None}):
        raise ValueError(
            'The `weights` argument should be either '
            '`None` (random initialization), `pascal_voc`, or `cityscapes` '
            '(pre-trained on PASCAL VOC)')

    if not (backbone in {'xception', 'mobilenetv2'}):
        raise ValueError('The `backbone` argument should be either '
                         '`xception`  or `mobilenetv2` ')

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        img_input = input_tensor

    if backbone == 'xception':
        if OS == 8:
            entry_block3_stride = 1
            middle_block_rate = 2  # ! Not mentioned in paper, but required
            exit_block_rates = (2, 4)
            atrous_rates = (12, 24, 36)
        else:
            entry_block3_stride = 2
            middle_block_rate = 1
            exit_block_rates = (1, 2)
            atrous_rates = (6, 12, 18)

        x = Conv2D(32, (3, 3),
                   strides=(2, 2),
                   name='entry_flow_conv1_1',
                   use_bias=False,
                   padding='same')(img_input)
        x = BatchNormalization(name='entry_flow_conv1_1_BN')(x)
        x = Activation('relu')(x)

        x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1)
        x = BatchNormalization(name='entry_flow_conv1_2_BN')(x)
        x = Activation('relu')(x)

        x = _xception_block(x, [128, 128, 128],
                            'entry_flow_block1',
                            skip_connection_type='conv',
                            stride=2,
                            depth_activation=False)
        x, skip1 = _xception_block(x, [256, 256, 256],
                                   'entry_flow_block2',
                                   skip_connection_type='conv',
                                   stride=2,
                                   depth_activation=False,
                                   return_skip=True)

        x = _xception_block(x, [728, 728, 728],
                            'entry_flow_block3',
                            skip_connection_type='conv',
                            stride=entry_block3_stride,
                            depth_activation=False)
        for i in range(16):
            x = _xception_block(x, [728, 728, 728],
                                'middle_flow_unit_{}'.format(i + 1),
                                skip_connection_type='sum',
                                stride=1,
                                rate=middle_block_rate,
                                depth_activation=False)

        x = _xception_block(x, [728, 1024, 1024],
                            'exit_flow_block1',
                            skip_connection_type='conv',
                            stride=1,
                            rate=exit_block_rates[0],
                            depth_activation=False)
        x = _xception_block(x, [1536, 1536, 2048],
                            'exit_flow_block2',
                            skip_connection_type='none',
                            stride=1,
                            rate=exit_block_rates[1],
                            depth_activation=True)
        x = Dropout(0.4)(x)

    else:
        OS = 8
        first_block_filters = _make_divisible(32 * alpha, 8)
        x = Conv2D(first_block_filters,
                   kernel_size=3,
                   strides=(2, 2),
                   padding='same',
                   use_bias=False,
                   name='Conv')(img_input)
        x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)
        x = Activation(relu6, name='Conv_Relu6')(x)

        x = _inverted_res_block(x,
                                filters=16,
                                alpha=alpha,
                                stride=1,
                                expansion=1,
                                block_id=0,
                                skip_connection=False)

        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=1,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=2,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=3,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=4,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=5,
                                skip_connection=True)

        # stride in block 6 changed from 2 -> 1, so we need to use rate = 2
        x = _inverted_res_block(
            x,
            filters=64,
            alpha=alpha,
            stride=1,  # 1!
            expansion=6,
            block_id=6,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=7,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=8,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=9,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=10,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=11,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=12,
                                skip_connection=True)

        x = _inverted_res_block(
            x,
            filters=160,
            alpha=alpha,
            stride=1,
            rate=2,  # 1!
            expansion=6,
            block_id=13,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=14,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=15,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=320,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=16,
                                skip_connection=False)
        x = Dropout(0.4)(x)
    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling

    # Image Feature branch
    shape_before = tf.shape(x)
    b4 = GlobalAveragePooling2D()(x)
    # from (b_size, channels)->(b_size, 1, 1, channels)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Conv2D(256, (1, 1),
                padding='same',
                use_bias=False,
                name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation('relu')(b4)
    # upsample. have to use compat because of the option align_corners
    size_before = tf.keras.backend.int_shape(x)
    b4 = Lambda(lambda x: tf.compat.v1.image.resize(
        x, size_before[1:3], method='bilinear', align_corners=True))(b4)
    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation('relu', name='aspp0_activation')(b0)

    # there are only 2 branches in mobilenetV2. not sure why
    if backbone == 'xception':
        # rate = 6 (12)
        b1 = SepConv_BN(x,
                        256,
                        'aspp1',
                        rate=atrous_rates[0],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 12 (24)
        b2 = SepConv_BN(x,
                        256,
                        'aspp2',
                        rate=atrous_rates[1],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 18 (36)
        b3 = SepConv_BN(x,
                        256,
                        'aspp3',
                        rate=atrous_rates[2],
                        depth_activation=True,
                        epsilon=1e-5)

        # concatenate ASPP branches & project
        x = Concatenate()([b4, b0, b1, b2, b3])
    else:
        x = Concatenate()([b4, b0])

    x = Conv2D(256, (1, 1),
               padding='same',
               use_bias=False,
               name='concat_projection')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation('relu')(x)
    x = Dropout(0.4)(x)
    # DeepLab v.3+ decoder

    if backbone == 'xception':
        # Feature projection
        # x4 (x2) block
        size_before2 = tf.keras.backend.int_shape(x)
        x = Lambda(lambda xx: tf.compat.v1.image.resize(xx,
                                                        size_before2[1:3] * tf.
                                                        constant(OS // 4),
                                                        method='bilinear',
                                                        align_corners=True))(x)

        dec_skip1 = Conv2D(48, (1, 1),
                           padding='same',
                           use_bias=False,
                           name='feature_projection0')(skip1)
        dec_skip1 = BatchNormalization(name='feature_projection0_BN',
                                       epsilon=1e-5)(dec_skip1)
        dec_skip1 = Activation('relu')(dec_skip1)
        x = Concatenate()([x, dec_skip1])
        x = SepConv_BN(x,
                       256,
                       'decoder_conv0',
                       depth_activation=True,
                       epsilon=1e-5)
        x = SepConv_BN(x,
                       256,
                       'decoder_conv1',
                       depth_activation=True,
                       epsilon=1e-5)
        x = Dropout(0.4)(x)
    # you can use it with arbitary number of classes
    if (weights == 'pascal_voc' and classes == 21) or (weights == 'cityscapes'
                                                       and classes == 19):
        last_layer_name = 'logits_semantic'
    else:
        last_layer_name = 'custom_logits_semantic'

    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)
    size_before3 = tf.keras.backend.int_shape(img_input)
    x = Lambda(lambda xx: tf.compat.v1.image.resize(
        xx, size_before3[1:3], method='bilinear', align_corners=True))(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    if activation in {'softmax', 'sigmoid'}:
        x = tf.keras.layers.Activation(activation)(x)

    model = Model(inputs, x, name='deeplabv3plus')

    # load weights

    if weights == 'pascal_voc':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_X,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_MOBILE,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    elif weights == 'cityscapes':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_X_CS,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_MOBILE_CS,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    return model
Example #26
0
def get_encoded_predictions(input_code):
    return Dropout(0.125, name='encoded_predictions_dropout')(input_code)
Example #27
0
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(
    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train,
          y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
Example #28
0
def Deeplabv3(weights_info=None,
              input_tensor=None,
              input_shape=(512, 512, 3),
              classes=21,
              backbone='mobilenetv2',
              OS=16,
              alpha=1.,
              activation='softmax'):
    """ Instantiates the Deeplabv3+ architecture

    Optionally loads weights pre-trained
    on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
    # Arguments
        weights_info: this dict is consisted of `classes` and `weghts`.
            `classes` is number of `weights` output units.
            `weights` is one of 'imagenet' (pre-training on ImageNet), 'pascal_voc', 'cityscapes',
            original weights path (pre-training on original data) or None (random initialization)
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
        classes: number of desired classes. PASCAL VOC has 21 classes, Cityscapes has 19 classes.
            If number of classes not aligned with the weights used, last layer is initialized randomly
        backbone: backbone to use. one of {'xception','mobilenetv2'}
        activation: optional activation to add to the top of the network.
            One of 'softmax', 'sigmoid' or None
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - If `alpha` < 1.0, proportionally decreases the number
                    of filters in each layer.
                - If `alpha` > 1.0, proportionally increases the number
                    of filters in each layer.
                - If `alpha` = 1, default number of filters from the paper
                    are used at each layer.
            Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.

    # Returns
        A Keras model instance.

    # Raises
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
        ValueError: in case of invalid argument for `weights` or `backbone`

    """

    if not (backbone in {'xception', 'mobilenetv2'}):
        raise ValueError('The `backbone` argument should be either '
                         '`xception`  or `mobilenetv2` ')

    if weights_info.get("weights") is None:
        weights = 'pascal_voc'
    else:
        weights = weights_info["weights"]

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        img_input = input_tensor

    if OS == 8:
        atrous_rates = (12, 24, 36)
    else:
        atrous_rates = (6, 12, 18)

    if backbone == 'xception':
        base_model, skip1 = DilatedXception(input_tensor=img_input,
                                            input_shape=input_shape,
                                            weights_info=weights_info,
                                            OS=OS,
                                            return_skip=True,
                                            include_top=False)

    else:
        base_model = MobileNetV2(input_tensor=img_input,
                                 input_shape=input_shape,
                                 weights_info=weights_info,
                                 OS=OS,
                                 include_top=False)

    x = base_model.output

    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling

    # Image Feature branch
    shape_before = tf.shape(x)
    b4 = GlobalAveragePooling2D()(x)
    # from (b_size, channels)->(b_size, 1, 1, channels)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Conv2D(256, (1, 1),
                padding='same',
                use_bias=False,
                name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation('relu')(b4)
    # upsample. have to use compat because of the option align_corners
    size_before = tf.keras.backend.int_shape(x)
    b4 = Lambda(lambda x: tf.compat.v1.image.resize(
        x, size_before[1:3], method='bilinear', align_corners=True))(b4)
    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation('relu', name='aspp0_activation')(b0)

    # there are only 2 branches in mobilenetV2. not sure why
    if backbone == 'xception':
        # rate = 6 (12)
        b1 = SepConv_BN(x,
                        256,
                        'aspp1',
                        rate=atrous_rates[0],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 12 (24)
        b2 = SepConv_BN(x,
                        256,
                        'aspp2',
                        rate=atrous_rates[1],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 18 (36)
        b3 = SepConv_BN(x,
                        256,
                        'aspp3',
                        rate=atrous_rates[2],
                        depth_activation=True,
                        epsilon=1e-5)

        # concatenate ASPP branches & project
        x = Concatenate()([b4, b0, b1, b2, b3])
    else:
        x = Concatenate()([b4, b0])

    x = Conv2D(256, (1, 1),
               padding='same',
               use_bias=False,
               name='concat_projection')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation('relu')(x)
    x = Dropout(0.1)(x)
    # DeepLab v.3+ decoder

    if backbone == 'xception':
        # Feature projection
        # x4 (x2) block
        size_before2 = tf.keras.backend.int_shape(x)
        x = Lambda(lambda xx: tf.compat.v1.image.resize(xx,
                                                        size_before2[1:3] * tf.
                                                        constant(OS // 4),
                                                        method='bilinear',
                                                        align_corners=True))(x)

        dec_skip1 = Conv2D(48, (1, 1),
                           padding='same',
                           use_bias=False,
                           name='feature_projection0')(skip1)
        dec_skip1 = BatchNormalization(name='feature_projection0_BN',
                                       epsilon=1e-5)(dec_skip1)
        dec_skip1 = Activation('relu')(dec_skip1)
        x = Concatenate()([x, dec_skip1])
        x = SepConv_BN(x,
                       256,
                       'decoder_conv0',
                       depth_activation=True,
                       epsilon=1e-5)
        x = SepConv_BN(x,
                       256,
                       'decoder_conv1',
                       depth_activation=True,
                       epsilon=1e-5)

    # you can use it with arbitary number of classes
    if (weights == 'pascal_voc' and classes == 21) or (weights == 'cityscapes'
                                                       and classes == 19):
        last_layer_name = 'logits_semantic'
    else:
        last_layer_name = 'custom_logits_semantic'

    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)
    size_before3 = tf.keras.backend.int_shape(img_input)
    x = Lambda(lambda xx: tf.compat.v1.image.resize(
        xx, size_before3[1:3], method='bilinear', align_corners=True))(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    if activation in {'softmax', 'sigmoid', 'linear'}:
        x = Activation(activation)(x)

    model = Model(inputs, x, name='deeplabv3plus')

    # load weights
    if weights == 'pascal_voc':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_X,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_MOBILE,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    elif weights == 'cityscapes':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_X_CS,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_MOBILE_CS,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    elif os.path.exists(weights):
        if weights_info.get("classes") is None:
            model.load_weights(weights)
    return model
Example #29
0
def default_categorical(input_shape=(120, 160, 3), roi_crop=(0, 0)):

    opt = keras.optimizers.Adam()
    drop = 0.2

    #we now expect that cropping done elsewhere. we will adjust our expeected image size here:
    input_shape = adjust_input_shape(input_shape, roi_crop)

    img_in = Input(
        shape=input_shape, name='img_in'
    )  # First layer, input layer, Shape comes from camera.py resolution, RGB
    x = img_in
    x = Convolution2D(
        24, (5, 5), strides=(2, 2), activation='relu', name="conv2d_1"
    )(
        x
    )  # 24 features, 5 pixel x 5 pixel kernel (convolution, feauture) window, 2wx2h stride, relu activation
    x = Dropout(drop)(
        x
    )  # Randomly drop out (turn off) 10% of the neurons (Prevent overfitting)
    x = Convolution2D(
        32, (5, 5), strides=(2, 2), activation='relu', name="conv2d_2"
    )(x)  # 32 features, 5px5p kernel window, 2wx2h stride, relu activatiion
    x = Dropout(drop)(
        x
    )  # Randomly drop out (turn off) 10% of the neurons (Prevent overfitting)
    if input_shape[0] > 32:
        x = Convolution2D(
            64, (5, 5), strides=(2, 2), activation='relu', name="conv2d_3")(
                x)  # 64 features, 5px5p kernal window, 2wx2h stride, relu
    else:
        x = Convolution2D(
            64, (3, 3), strides=(1, 1), activation='relu', name="conv2d_3")(
                x)  # 64 features, 5px5p kernal window, 2wx2h stride, relu
    if input_shape[0] > 64:
        x = Convolution2D(
            64, (3, 3), strides=(2, 2), activation='relu', name="conv2d_4")(
                x)  # 64 features, 3px3p kernal window, 2wx2h stride, relu
    elif input_shape[0] > 32:
        x = Convolution2D(
            64, (3, 3), strides=(1, 1), activation='relu', name="conv2d_4")(
                x)  # 64 features, 3px3p kernal window, 2wx2h stride, relu
    x = Dropout(drop)(
        x
    )  # Randomly drop out (turn off) 10% of the neurons (Prevent overfitting)
    x = Convolution2D(
        64, (3, 3), strides=(1, 1), activation='relu', name="conv2d_5")(
            x)  # 64 features, 3px3p kernal window, 1wx1h stride, relu
    x = Dropout(drop)(
        x
    )  # Randomly drop out (turn off) 10% of the neurons (Prevent overfitting)
    # Possibly add MaxPooling (will make it less sensitive to position in image).  Camera angle fixed, so may not to be needed

    x = Flatten(name='flattened')(x)  # Flatten to 1D (Fully connected)
    x = Dense(100, activation='relu', name="fc_1")(
        x)  # Classify the data into 100 features, make all negatives 0
    x = Dropout(drop)(
        x
    )  # Randomly drop out (turn off) 10% of the neurons (Prevent overfitting)
    x = Dense(50, activation='relu', name="fc_2")(
        x)  # Classify the data into 50 features, make all negatives 0
    x = Dropout(drop)(
        x)  # Randomly drop out 10% of the neurons (Prevent overfitting)
    #categorical output of the angle
    angle_out = Dense(15, activation='softmax', name='angle_out')(
        x
    )  # Connect every input with every output and output 15 hidden units. Use Softmax to give percentage. 15 categories and find best one based off percentage 0.0-1.0

    #continous output of throttle
    throttle_out = Dense(20, activation='softmax', name='throttle_out')(
        x)  # Reduce to 1 number, Positive number only

    model = Model(inputs=[img_in], outputs=[angle_out, throttle_out])
    return model
Example #30
0
def build_3d_cnn(w, h, d, s, num_outputs):
    #Credit: https://github.com/jessecha/DNRacing/blob/master/3D_CNN_Model/model.py
    '''
        w : width
        h : height
        d : depth
        s : n_stacked
    '''
    input_shape = (s, h, w, d)

    model = Sequential()
    #First layer
    #model.add(Cropping3D(cropping=((0,0), (50,10), (0,0)), input_shape=input_shape) ) #trim pixels off top

    # Second layer
    model.add(
        Conv3D(filters=16,
               kernel_size=(3, 3, 3),
               strides=(1, 3, 3),
               data_format='channels_last',
               padding='same',
               input_shape=input_shape))
    model.add(Activation('relu'))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     padding='valid',
                     data_format=None))
    # Third layer
    model.add(
        Conv3D(filters=32,
               kernel_size=(3, 3, 3),
               strides=(1, 1, 1),
               data_format='channels_last',
               padding='same'))
    model.add(Activation('relu'))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     padding='valid',
                     data_format=None))
    # Fourth layer
    model.add(
        Conv3D(filters=64,
               kernel_size=(3, 3, 3),
               strides=(1, 1, 1),
               data_format='channels_last',
               padding='same'))
    model.add(Activation('relu'))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     padding='valid',
                     data_format=None))
    # Fifth layer
    model.add(
        Conv3D(filters=128,
               kernel_size=(3, 3, 3),
               strides=(1, 1, 1),
               data_format='channels_last',
               padding='same'))
    model.add(Activation('relu'))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     padding='valid',
                     data_format=None))
    # Fully connected layer
    model.add(Flatten())

    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(num_outputs))
    #model.add(Activation('tanh'))

    return model