Ejemplo n.º 1
0
def plot_history(history, types):
    # summarize history for accuracy
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.xlim([0, max(history.epoch)])
    plt.legend(['train', 'test'], loc='upper left')
    os.makedirs(os.path.dirname(ml_utils.get_dir_path()), exist_ok=True)
    plt.savefig(ml_utils.get_dir_path() + types + '_accuracy.png')
    plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
    plt.clf()

    # summarize history for loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.xlim([0, max(history.epoch)])
    plt.legend(['train', 'test'], loc='upper left')
    os.makedirs(os.path.dirname(ml_utils.get_dir_path()), exist_ok=True)
    plt.savefig(ml_utils.get_dir_path() + types + '_loss.png')
    plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
    plt.clf()
Ejemplo n.º 2
0
def plot_color_separation(data_x, data_y, data_z, color, title, x_label='X', y_label='Y', z_label='Z'):
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.scatter(data_x, data_y, data_z, c=color, s=50, edgecolors='black', alpha=0.8, marker='s')  # linestyle='dotted',
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    ax.set_zlabel(z_label)
    ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
    os.makedirs(os.path.dirname(ml_utils.get_dir_path()), exist_ok=True)
    plt.title(title)
    plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
    plt.savefig(ml_utils.get_dir_path() + title + '.png')
    # plt.show()
    plt.clf()
Ejemplo n.º 3
0
def draw_bar_chart(dictionary, title, x_label, y_label):
    bar_chart = plt.bar(range(len(dictionary)), dictionary.values(), align='center',
                        color=cm.OrRd(np.linspace(.4, .8, 10)),
                        zorder=3)
    value_list = list(dictionary.values())
    high = max(value_list)
    plt.ylim([0, high + 10])
    plt.title(title)
    plt.ylabel(y_label)
    plt.xlabel(x_label)
    ax = plt.gca()
    ax.grid()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    # ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
    for tick in ax.xaxis.get_major_ticks() + ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(8)

    plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True)
    plt.xticks(range(len(dictionary)), list(dictionary.keys()), rotation=90)
    plt.tight_layout()
    # plt.subplots_adjust(left=0.1, right=0.95, top=0.9, bottom=0.25)
    auto_label(bar_chart)
    plt.savefig(ml_utils.get_dir_path() + title + '.png')
    plt.clf()
Ejemplo n.º 4
0
def plot_confusion_matrix_2(cm,
                            normalize=False,
                            title='Confusion matrix', removeNullClass=True,
                            cmap=plt.cm.OrRd):
    FONT_SIZE = 8

    accuracy = np.trace(cm) / float(np.sum(cm))
    misclass = 1 - accuracy

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(8 * 2, 6 * 2))  # 8, 6
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    # plt.title(title)
    plt.colorbar()

    classes = np.arange(0, len(cm[0]) + 1)
    tick_marks = np.arange(len(cm[0]))
    plt.xticks(tick_marks, classes, rotation=90, fontsize=FONT_SIZE)
    plt.yticks(tick_marks, classes, fontsize=FONT_SIZE)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.4f}".format(cm[i, j]),
                     horizontalalignment="center",
                     fontsize=FONT_SIZE,
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     fontsize=FONT_SIZE,
                     color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
    # plt.show()

    os.makedirs(os.path.dirname(ml_utils.get_dir_path()), exist_ok=True)
    plt.savefig(ml_utils.get_dir_path() + title + '_confusion_matrix.png')
    plt.clf()
Ejemplo n.º 5
0
def build_model(number_class, input_shape=36, plot_model_arch=False):
    model = tf.keras.Sequential()
    # Must define the input shape in the first layer of the neural network
    # model.add(tf.keras.layers.Dense(1024, input_shape=(36,), activation=tf.nn.relu,kernel_regularizer=keras.regularizers.l2(0.001)))
    # model.add(tf.keras.layers.Dropout(0.3))
    # model.add(tf.keras.layers.Dense(1024, activation=tf.nn.relu, kernel_regularizer=keras.regularizers.l2(0.001)))
    # model.add(tf.keras.layers.Dropout(0.3))
    model.add(
        tf.keras.layers.Dense(512,
                              input_shape=(input_shape, ),
                              activation=tf.nn.relu,
                              kernel_regularizer=keras.regularizers.l2(0.001)))
    # model.add(tf.keras.layers.Flatten())
    # model.add(tf.keras.layers.Dropout(0.5))
    model.add(
        tf.keras.layers.Dense(256,
                              activation=tf.nn.relu,
                              kernel_regularizer=keras.regularizers.l2(0.001)))
    # model.add(tf.keras.layers.Dropout(0.3))
    model.add(
        tf.keras.layers.Dense(128,
                              activation=tf.nn.relu,
                              kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Dropout(0.3))
    model.add(tf.keras.layers.Dense(number_class, activation='softmax'))
    # Take a look at the model summary
    model.summary()
    model.compile(
        loss='categorical_crossentropy',
        optimizer='Adamax',  # rmsprop, adam, Adamax
        metrics=['accuracy'])
    if plot_model_arch:
        plot_model(model,
                   show_shapes=True,
                   to_file=ml_utils.get_dir_path() +
                   'color_classifier_model.png')

    return model
import traceback
from collections import Counter

import keras
import ml_utils
import numpy as np
import serial as serial

import colorSpaceUtil

if __name__ == "__main__":
    model_bottom = keras.models.load_model(ml_utils.get_dir_path() +
                                           'model_bottom.h5')
    model_left = keras.models.load_model(ml_utils.get_dir_path() +
                                         'model_left.h5')
    model_right = keras.models.load_model(ml_utils.get_dir_path() +
                                          'model_right.h5')
    text_class_model = keras.models.load_model(ml_utils.get_dir_path() +
                                               'text_class_model.h5')

    # device="/dev/tty.usbmodem2853891"
    port = "COM3"  # COM for windows, it changes when we use unix system
    ser = serial.Serial(port, 115200, timeout=None)

    bottom_color = 0.0
    left_color = 0.0
    right_color = 0.0

    bottom_color_list = [0]
    left_color_list = [0]
    right_color_list = [0]
Ejemplo n.º 7
0

if __name__ == "__main__":
    (train_container_data, train_container_labels,
     train_container_labels_raw), (
         train_rack_data, train_rack_labels,
         train_rack_labels_raw) = ml_utils.get_trainig_data(
             ml_utils.get_start_column(), ml_utils.get_feature_type())
    (test_rack_data, test_rack_labels, test_rack_labels_raw), (
        test_container_data, test_container_labels,
        test_container_labels_raw) = ml_utils.get_testing_data(
            ml_utils.get_start_column(), ml_utils.get_feature_type())

    # if ml_utils.load_configurations_from_files():
    if False:  # remove the default fault operation and enable above statement
        model_rack = keras.models.load_model(ml_utils.get_dir_path() +
                                             'model_rack.h5')
        model_container = keras.models.load_model(ml_utils.get_dir_path() +
                                                  'model_container.h5')
        print("Loaded configurations from files !! ")

    else:

        earlyStopping = keras.callbacks.EarlyStopping(monitor='val_loss',
                                                      patience=10,
                                                      verbose=1,
                                                      mode='auto')

        model_rack = build_model(5)
        history_rack = model_rack.fit(train_rack_data,
                                      train_rack_labels,
Ejemplo n.º 8
0
import traceback
from collections import Counter

import keras
import ml_utils
import numpy as np
import serial as serial

import colorSpaceUtil

if __name__ == "__main__":
    model_bottom = keras.models.load_model(ml_utils.get_dir_path() + 'model_bottom.h5')
    model_left = keras.models.load_model(ml_utils.get_dir_path() + 'model_left.h5')
    model_right = keras.models.load_model(ml_utils.get_dir_path() + 'model_right.h5')
    text_class_model = keras.models.load_model(ml_utils.get_dir_path() + 'text_class_model.h5')

    # device="/dev/tty.usbmodem2853891"
    port = "COM3"  # COM for windows, it changes when we use unix system
    ser = serial.Serial(port, 115200, timeout=None)

    bottom_color = 0.0
    left_color = 0.0
    right_color = 0.0

    bottom_color_list = [0]
    left_color_list = [0]
    right_color_list = [0]

    isBottomPredicted = False

    no_records = 100
Ejemplo n.º 9
0
        source_dir_path + 'test_bottom.csv', start_column=4, end_column=10)
    test_left_data, test_left_labels_raw, test_left_labels = ml_utils.parse_file(
        source_dir_path + 'test_left.csv', start_column=7, end_column=13)
    test_right_data, test_right_labels_raw, test_right_labels = ml_utils.parse_file(
        source_dir_path + 'test_right.csv', start_column=7, end_column=13)

    if ml_utils.get_feature_type() == 'PREPROCESSED':
        bottom_preprocessor = StandardScaler()
        left_preprocessor = StandardScaler()
        right_preprocessor = StandardScaler()

        bottom_preprocessor.fit(train_bottom_data)
        left_preprocessor.fit(train_left_data)
        right_preprocessor.fit(train_right_data)

        os.makedirs(os.path.dirname(ml_utils.get_dir_path()), exist_ok=True)
        pickle.dump(bottom_preprocessor, open(ml_utils.get_dir_path() + "bottom_preprocessor.p", "wb"))
        pickle.dump(left_preprocessor, open(ml_utils.get_dir_path() + "left_preprocessor.p", "wb"))
        pickle.dump(right_preprocessor, open(ml_utils.get_dir_path() + "right_preprocessor.p", "wb"))

        train_bottom_data = bottom_preprocessor.transform(train_bottom_data)
        train_left_data = left_preprocessor.transform(train_left_data)
        train_right_data = right_preprocessor.transform(train_right_data)

        test_bottom_data = bottom_preprocessor.transform(test_bottom_data)
        test_left_data = left_preprocessor.transform(test_left_data)
        test_right_data = right_preprocessor.transform(test_right_data)

    # earlyStopping = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='auto')

    model_bottom = dl_clf.build_model(5, 6)
Ejemplo n.º 10
0
def plot_confusion_matrix(con_matx,
                          normalize=False,
                          title='Confusion matrix', removeNullClass=True,
                          cmap=plt.cm.OrRd):  # Blues,OrRd, YlOrRd
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if removeNullClass:
        con_matx = con_matx[1:, 1:]  # remove class 0 - do nothing
        classes = np.arange(1, len(con_matx[0]) + 1)
    else:
        classes = np.arange(0, len(con_matx[0]) + 1)

    # plt.figure(figsize=(12, 12))

    plt.imshow(con_matx, interpolation='nearest', cmap=cmap)

    if normalize:
        updated_title = title + ' Identification (normalized)'
    else:
        updated_title = title + ' Identification (non normalized)'

    plt.title(updated_title)
    plt.colorbar()

    tick_marks = np.arange(len(con_matx[0]))
    plt.xticks(tick_marks, classes, rotation=90, fontsize=8)
    plt.yticks(tick_marks, classes, fontsize=8)

    if normalize:
        con_matx = con_matx.astype('float') / con_matx.sum(axis=1)[:, np.newaxis] * 100.0
        for x in range(0, len(con_matx[0, :])):
            xv = con_matx[x, :]
            for y in range(0, len(xv)):
                vv = xv[y]

                con_matx[x, y] = int(np.round(vv * 10.0)) / 10.0

    thresh = con_matx.max() / 2.
    for i, j in itertools.product(range(con_matx.shape[0]), range(con_matx.shape[1])):
        plt.text(j, i, con_matx[i, j],
                 horizontalalignment="center",
                 color="white" if con_matx[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.1)

    ax = plt.gca()
    ax.grid()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    # ax.spines['bottom'].set_visible(False)
    # ax.spines['left'].set_visible(False)

    # plt.show()
    os.makedirs(os.path.dirname(ml_utils.get_dir_path()), exist_ok=True)
    plt.savefig(ml_utils.get_dir_path() + title + '_confusion_matrix.png')
    plt.clf()
scalar_rack = StandardScaler()
scalar_container = StandardScaler()

if __name__ == "__main__":
    (train_container_data, train_container_labels,
     train_container_labels_raw), (
         train_rack_data, train_rack_labels,
         train_rack_labels_raw) = ml_utils.get_trainig_data(
             ml_utils.get_start_column(), ml_utils.get_feature_type())
    (test_rack_data, test_rack_labels, test_rack_labels_raw), (
        test_container_data, test_container_labels,
        test_container_labels_raw) = ml_utils.get_testing_data(
            ml_utils.get_start_column(), ml_utils.get_feature_type())

    if ml_utils.load_configurations_from_files():
        model_rack = keras.models.load_model(ml_utils.get_dir_path() +
                                             'model_rack.h5')
        model_container = keras.models.load_model(ml_utils.get_dir_path() +
                                                  'model_container.h5')

        if ml_utils.get_feature_type() == 'PREPROCESSED':
            scalar_rack = pickle.load(
                open(ml_utils.get_dir_path() + "scalar_rack.p", "rb"))
            scalar_container = pickle.load(
                open(ml_utils.get_dir_path() + "scalar_container.p", "rb"))

        print("Loaded configurations from files !! ")

    else:

        model_rack = dl_clf.build_model(3)