Ejemplo n.º 1
0
# Loading dataset
X = load_x_dataset1()
y = load_y_dataset1()

# building the mode
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=X.shape[1:]))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(5, activation='softmax'))

# Stops the training early of the val_loss has stopped improving
early_stop = EarlyStopping(monitor="val_loss", min_delta=0, patience=2, verbose=0, mode="auto",
                           baseline=None, restore_best_weights=False)

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

history = model.fit(X, y, batch_size=150, epochs=6, verbose=1, validation_split=0.2, callbacks=[early_stop])

# Plot training & validation accuracy & loss values
plot_model(history, 'acc', 'McQueen', '/Users/william/Documents/gitHub/B20IT38/mcqueen_models/models/mcqueen_phase_three/base/acc.png')
plot_model(history, 'loss', 'McQueen', '/Users/william/Documents/gitHub/B20IT38/mcqueen_models/models/base/loss.png')

# Saving the model
model.save(NAME + '.model')
test_model(NAME + '.model')
Ejemplo n.º 2
0
# Load dataset
X = load_x_1()
y = load_y_1()

# Load model, create early stop callback & train model
history = train_model(
    X=X,
    y=y,
    epochs=30,
    batch_size=150,
    val_split=0.3,
    patience=1,
    model=
    "/Users/william/Documents/gitHub/B20IT38/mcqueen_models/mcqueen_pase_two/McQueen.model",
    save_location=FOLDER + NAME + '.model')

# Plot training & validation acc & loss
plot_model(history=history,
           metric='acc',
           name=NAME,
           save_location=FOLDER + 'acc.png')
plot_model(history=history,
           metric='loss',
           name=NAME,
           save_location=FOLDER + 'loss.png')

# Test the trained model against the testing set
# Function prints how many correct predictions that was made.
# test_model(FOLDER+NAME+'.model')
              metrics=['accuracy'])

# Stops the training early of the val_loss has stopped improving
early_stop = EarlyStopping(monitor="val_loss",
                           min_delta=0,
                           patience=7,
                           verbose=0,
                           mode="auto",
                           baseline=None,
                           restore_best_weights=False)

# Trains the model and saves the history of the training
history = model.fit(X,
                    y,
                    batch_size=150,
                    epochs=100,
                    validation_split=0.2,
                    callbacks=[early_stop])

# Plotting the acc & loss of the training and validation
plot_model(
    history, 'acc', 'McQueen_Fully_connected',
    '/Users/william/Documents/gitHub/B20IT38/mcqueen_models/mcqueen_FF/base/acc.png'
)
plot_model(
    history, 'loss', 'McQueen_Fully_connected',
    '/Users/william/Documents/gitHub/B20IT38/mcqueen_models/mcqueen_FF/base/loss.png'
)

model.save(NAME + '.model')  # Saving the model
test_model(NAME + '.model')  # Testing the model with testset1
# Stops the training early of the val_loss has stopped improving
early_stop = EarlyStopping(monitor="val_loss",
                           min_delta=0,
                           patience=3,
                           verbose=0,
                           mode="auto",
                           baseline=None,
                           restore_best_weights=False)

# Trains the model and saves the history of the training
history = model.fit(X,
                    y,
                    batch_size=320,
                    epochs=15,
                    validation_split=0.2,
                    callbacks=[early_stop])

model.summary()
# Plotting the acc & loss of the training and validation
plot_model(
    history, 'acc', 'McQueen_Phase_Two_CNN',
    '/Users/william/Documents/gitHub/B20IT38/mcqueen_models/mcqueen_pase_two/acc.png'
)
plot_model(
    history, 'loss', 'McQueen_Phase_Two_CNN',
    '/Users/william/Documents/gitHub/B20IT38/mcqueen_models/mcqueen_pase_two/loss.png'
)

model.save(NAME + '.model')  # Saving the model
# test_model(NAME+'.model')  # Testing the model with testset1
model.add(Dense(350, activation='relu'))
model.add(Dense(6, activation='softmax'))

early_stop = EarlyStopping(monitor="val_loss",
                           min_delta=0,
                           patience=2,
                           verbose=0,
                           mode="auto",
                           baseline=None,
                           restore_best_weights=True)

model.compile(optimizer=SGD(lr=0.001, momentum=0.8, decay=1e-6),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Trains the model and saves the history of the training
history = model.fit(X,
                    y,
                    batch_size=200,
                    epochs=10,
                    validation_split=0.3,
                    callbacks=[early_stop])

model.summary()
# Plotting the acc & loss of the training and validation
plot_model(history, 'acc', 'McQueen_p3_t1', FOLDER + 'acc.png')
plot_model(history, 'loss', 'McQueen_P3_t1', FOLDER + 'loss.png')

model.save(NAME + '.model')  # Saving the model
# test_model(NAME+'.model')  # Testing the model with testset1