Ejemplo n.º 1
0
    def createModelBase(self, weights='imagenet'):

        model_name_for_call = self.decideModelName()
        modelCall = getattr(self, model_name_for_call)
        model, img_width, img_height = modelCall(weights)

        for layer in model.layers:
            print(layer.name)
            layer.trainable = True

        if weights == 'imagenet':
            model.layers.pop()
            pretrained_inputs = model.inputs
            model = Flatten()(model.output)
            model = Dense(512, activation='relu')(model)
            model = Dropout(0.5)(model)
            predictions = Dense(self.NUMBER_OF_CLASSES,
                                activation='softmax')(model)
        else:
            print(model.summary())
            pretrained_inputs = model.input
            predictions = Dense(self.NUMBER_OF_CLASSES,
                                activation='softmax',
                                name='dense_1')(model.layers[-2].output)
            print(model.summary())

        model_final = Model(inputs=pretrained_inputs, outputs=predictions)
        return model_final, img_height, img_width
Ejemplo n.º 2
0
# FC layers
model = Flatten()(model)

#model = Dense(1024, kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01))(model)
model = Dense(1024)(model)
#model = Dropout(0.2)(model)

#model = Dense(64, kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01))(model)
model = Dense(64)(model)
#model = Dropout(0.2)(model)

output = Dense(num_classes, activation='softmax')(model)

model = Model(inputs=[input_image], outputs=[output])

print(model.summary())

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

batch_size = 256
num_epochs = 20

# Train Model
history = model.fit(trainX, trainY, batch_size=batch_size,
                    epochs=num_epochs)  #, callbacks=[checkpoint])
model = Dense(dense_neurons / 2, activation='tanh')(model)

# Output Layer
output = Dense(10, activation="softmax")(model)

model = Model(input_layer, output)

# Compiling model
optimizer = keras.optimizers.SGD(lr=learning_rate, momentum=momentum)
model.compile(
    loss="sparse_categorical_crossentropy",
    optimizer=optimizer,
    metrics=["accuracy"]
)
model.summary()

# Train the model
history = model.fit(
    train_data,
    epochs=13,
    validation_data = test_data
)

"""# Saving and Recreating the trained model"""

## Save the whole model
model.save('./trained_CNN/imagewoof/my_model_imagewoof.h5')

## Recreate whole model
new_model=keras.models.load_model('./trained_CNN/imagewoof/my_model_imagewoof.h5')