예제 #1
0
                  metrics=["accuracy"])

    model.build()
    model.summary()
    history = model.fit_generator(
        train_generator,
        epochs=1,
        validation_data=validation_generator,
        class_weight={
            0: 0.5,  # false
            1: 0.5  # true
        },
        callbacks=[EarlyStopping(monitor="val_loss", patience=5)])

    def plot_history(history):
        loss = history.history["loss"]
        val_loss = history.history["val_loss"]
        epochs = range(1, len(loss) + 1)
        plt.figure()
        plt.plot(epochs, loss, "bo", label="Training loss")
        plt.plot(epochs, val_loss, "b", label="Validation loss")
        plt.title("Training and validation loss")
        plt.legend()
        # plt.show()

    plot_history(history)

    predictions_pos = model.predict_generator(test_generator_positive)
    predictions_neg = model.predict_generator(test_generator_negative)
    pdb.set_trace()
예제 #2
0
    shuffle=False
)

val_steps = test_generator.n//test_generator.batch_size
loss, acc = model.evaluate_generator( test_generator, verbose=1, steps = val_steps)
print('Test loss: %f' %loss)
print('Test accuracy: %f' %acc)

"""Precision, recall, F-score"""

import sklearn.metrics
from sklearn.metrics import classification_report, confusion_matrix

print('Loaded %d test samples from %d classes.' %(test_generator.n, test_generator.num_classes))

preds = model.predict_generator(test_generator, verbose=1, steps=val_steps)

Ypred = np.argmax(preds, axis=1)
Ytest = test_generator.classes  # shuffle=False in test_generator

print(classification_report(Ytest, Ypred, labels=None, target_names=classnames, digits=3))

"""Graphs"""

import matplotlib.pyplot as plt

# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
예제 #3
0
# print model
model.summary()

# Same compile as in original model
model.compile(optimizer=Adam(learning_rate=.0001), loss='categorical_crossentropy', metrics=['accuracy'])

# Learning on 40 images -> 4 steps and 10 images per step
model.fit_generator(generator=train_batches, steps_per_epoch=4,
    validation_data=valid_batches, validation_steps=4, epochs=5, verbose=2)

test_imgs, test_labels = next(test_batches)
test_labels = test_labels[:,0]

# Taking one batch so will be 10 images
predictions = model.predict_generator(generator=test_batches, steps=1, verbose=0)
print(np.round(predictions[:,0]))

cm = confusion_matrix(y_true=test_labels, y_pred=np.round(predictions[:,0]))
print('Configuration matrix')
print(cm)


"""

Epoch 5/5
4/4 - 39s - loss: 0.4781 - accuracy: 0.8250 - val_loss: 0.5529 - val_accuracy: 0.7750

Configuration matrix
[[5 1]
 [0 4]]