Exemple #1
0
async def get_visualisation_digit(client: discord.Client,
                                  message: discord.Message):
    """
    Message the visual representation of the graph
    """
    dot = model_to_dot(DIGITS, show_shapes=True)
    image_bytes = dot.create(format='png')
    await message.channel.send(
        file=discord.File(io.BytesIO(image_bytes), filename='model.png'))
Exemple #2
0
def train_with_sampling_on_the_fly(segmentation_loaded, segmentation_path,
                                   distribution_set):

    (train_data, val_data,
     test_data), (train_label, val_label, test_label), no_classes = load_data(
         (0.05, 0.05, 0.9), distribution_set)
    print("Loaded Data.")

    size = int(max(segmentation_loaded[:, 0]) +
               1), int(max(segmentation_loaded[:, 1]) +
                       1), int(max(segmentation_loaded[:, 2]) + 1)

    model = build_model(no_classes, size)
    print("Build Model.")

    training_generator = data_generator_sampling_on_the_fly(
        train_data, train_label, segmentation_loaded, size, False)
    validation_generator = data_generator_sampling_on_the_fly(
        val_data, val_label, segmentation_loaded, size, False)
    results, model = training(model, training_generator, validation_generator,
                              train_data.shape[0], val_data.shape[0])
    print("Finished Training.")

    test_generator = data_generator_sampling_on_the_fly(
        test_data, test_label, segmentation_loaded, size, True)
    test_results = test(test_generator, model, test_data.shape[0])
    test_label = np.argmax(test_label, axis=1)
    prediction_results = np.stack((test_label, test_results), axis=-1)
    conf_matrix = confusion_matrix(test_label, test_results)
    print("Finished Testing.")

    model.save(os.path.join(segmentation_path, "model.h5"))
    plot_model(model, to_file=os.path.join(segmentation_path, "model.png"))
    with open(os.path.join(segmentation_path, "model_graph"),
              "wb") as graph_file:
        pickle.dump(model_to_dot(model), graph_file)
    with open(os.path.join(segmentation_path, "history"),
              "wb") as results_file:
        pickle.dump(results.history, results_file)
    np.save(os.path.join(segmentation_path, "prediction_results"),
            prediction_results)
    np.save(os.path.join(segmentation_path, "confusion_matrix"), conf_matrix)

    backend.clear_session()

    return accuracy_score(test_label, test_results)
def plot_model(keras_model, bgcolor, forecolor, framecolor, watermark_text,
               fillcolors, fontcolors) -> np.ndarray:
    """
    Parameters :
        - keras_model (keras.engine.training.Model) :
            the model the architecture of which is to be plotted.
        - bgcolor (str) :
            the hexadecimal expression of the background color.
        - forecolor (str) :
            the hexadecimal expression of the foreground color.
        - framecolor (str) :
            the hexadecimal expression of the color
            of the image border.
        - watermark_text (str) :
            the text to be watermarked at the bottom-right corner
            of the image.
        - fillcolors (dic) :
            a dictionnary with entries ('node name': 'fill hex color')
            for nodes to be filled with a color other than transparent.
        - fontcolors (dic) :
            a dictionnary with entries ('node name': 'font hex color')
            for nodes to be labell in a color other than 'forecolor'.

    Resuts :
        - an 'np.ndarray' of 3 colors channels
          and dimension (height x width)
    """

    graph = model_to_dot(keras_model, show_layer_names=False)
    graph.set_bgcolor(bgcolor)

    nodes = graph.get_node_list()
    edges = graph.get_edge_list()

    for node in nodes:
        if node.get_name() == 'node':
            node.obj_dict['attributes'] = \
                {'shape': 'record', 'style': "filled, rounded"
                 , 'fillcolor': bgcolor, 'color': forecolor
                 , 'fontcolor': forecolor, 'fontname': 'helvetica'}
        #print(str(node.get_label()) + " - " + str((node is not None) & (node.get_label() in colors)))
        if node.get_label() in fillcolors:
            node.set_fillcolor(fillcolors[node.get_label()])
        if node.get_label() in fontcolors:
            node.set_fontcolor(fontcolors[node.get_label()])
    # 'graph.set_edge_defaults' will fail (defaults being appended last) =>
    for edge in edges:  # apply successively to each edge
        edge.set_color(forecolor)
        edge.set_arrowhead("vee")

    #print(graph.to_string())
    tmp_filename = os.path.join('tmp', 'colored_tree.png')
    graph.write_png(tmp_filename)
    #from IPython.core.display import display, Image ; display(Image(graph.create_png()))

    image = cv2.cvtColor(cv2.imread(tmp_filename), cv2.COLOR_BGR2RGB)
    os.remove(tmp_filename)

    def hex_to_rgb(hex_color):
        return tuple(
            int(hex_color.lstrip('#')[i:i + 2], 16) for i in (0, 2, 4))

    #watermark
    texted_image = cv2.putText(
        img=np.copy(image),
        text=watermark_text,
        org=(image.shape[1] - (10 + int(7.33 * len(watermark_text))),
             image.shape[0] - 10),
        fontFace=cv2.FONT_HERSHEY_COMPLEX,
        fontScale=.4,
        color=hex_to_rgb(framecolor),
        lineType=cv2.LINE_AA,
        thickness=1)
    # padding
    outer_pad_top, outer_pad_bot, outer_pad_left, outer_pad_right = 4, 4, 4, 4
    outer_pad_color = hex_to_rgb(bgcolor)
    inner_pad_top, inner_pad_bot, inner_pad_left, inner_pad_right = 2, 2, 2, 2
    inner_pad_color = hex_to_rgb(framecolor)
    padded_image = cv2.copyMakeBorder(cv2.copyMakeBorder(
        texted_image,
        inner_pad_top,
        inner_pad_bot,
        inner_pad_left,
        inner_pad_right,
        borderType=cv2.BORDER_CONSTANT,
        value=inner_pad_color),
                                      outer_pad_top,
                                      outer_pad_bot,
                                      outer_pad_left,
                                      outer_pad_right,
                                      borderType=cv2.BORDER_CONSTANT,
                                      value=outer_pad_color)

    return cv2.cvtColor(padded_image, cv2.COLOR_RGB2BGR)
Exemple #4
0
    epochs=150
    # callbacks=callbacks_list
)

# %%

#-------------------------- Training history Analysis ------------------------------#
import pydot
from keras.utils import plot_model
plot_model(model, to_file='model.png')
# %%

from IPython.display import SVG
from keras.utils import model_to_dot

SVG(model_to_dot(model).create(prog='dot', format='svg'))

# %%


# Plot training & validation accuracy values
plt.plot(history.history['acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.title('Model loss')
sess = K.get_session()
uninitialized_variables = set(
    [i.decode('ascii') for i in sess.run(tf.report_uninitialized_variables())])
init = tf.variables_initializer([
    v for v in tf.global_variables()
    if v.name.split(':')[0] in uninitialized_variables
])
sess.run(init)

bert_model = get_bert_finetuning_model(model)

from IPython.display import SVG
from keras.utils import model_to_dot

SVG(model_to_dot(bert_model, dpi=65).create(prog='dot', format='svg'))

history = bert_model.fit(train_x,
                         train_y,
                         epochs=30,
                         batch_size=8,
                         verbose=1,
                         validation_data=(test_x, test_y),
                         shuffle=True)

# 학습 정확성 값과 검증 정확성 값을 플롯팅 합니다.
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
hidden_size = 64
output_size = 784

#定义神经网络层数,构建模型优化参数
model = Sequential()
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(output_size, activation='sigmoid'))
autoencoder = model.compile(optimizer='adam', loss='mse')

#-------------------------------------------------训练模型-------------------------------------
epochs = 5
batch_size = 128
history = autoencoder.fit(x_train, x_train, batch_size=batch_size, epochs=epochs, verbose=1,validation_data=(x_test, x_test))

#----------------------------------------------------模型可视化---------------------------------
SVG(model_to_dot(autoencoder).create(prog='dot',format='svg'))

#----------------------------------------------------查看自编码器的压缩效果------------------------
#只取编码器做模型(取输入层x和隐藏层h,作为网络结构)
encoded_imgs = model.predict(x_test)
#打印10张测试集手写体的压缩效果
n = 10
plt.figure(figsize=(20, 8))
for i in range(n):
    ax = plt.subplot(1, n, i+1)
    plt.imshow(encoded_imgs[i].reshape(4, 16).T) #将8*8的特征转化为4*16的图像
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()
feature_count = X_train.shape[1]

# ========================
#     parameters
# ========================
num_epochs = 10
layers = [16, 8]

# ========================
# define the keras model
# ========================
model = make_model(feature_count, layers)
print("# features : {0}".format(feature_count))
SVG(
    model_to_dot(model, show_shapes=True,
                 show_layer_names=False).create(prog='dot', format='svg'))
print(model.summary())

# ========================
#     train
# ========================
print("Fitting model")
history = model.fit(X_train,
                    y_train,
                    epochs=num_epochs,
                    validation_data=(X_test, y_test),
                    verbose=1)
print(model.summary())
# ========================
#     plot
# ========================
Exemple #8
0
#Train model with 40 epochs and 128 batch size
history_lstm = lstm_model.fit(X_train,
                              y_train,
                              batch_size=128,
                              epochs=40,
                              validation_data=(X_vali, y_vali),
                              callbacks=es)

score, acc = lstm_model.evaluate(X_vali, y_vali, batch_size=128)
print("Accuracy on the test set = {0:4.3f}".format(acc))
lstm_model.save('lstm.model')

# In[ ]:

SVG(
    model_to_dot(lstm_model, show_layer_names=True, show_shapes=True,
                 dpi=70).create(prog='dot', format='svg'))

# In[ ]:

import matplotlib.pyplot as plt

# In[ ]:

#Accuracy score visualization
plt.plot(history_lstm.history['accuracy'])
plt.plot(history_lstm.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
Exemple #9
0
 def display_model_svg(self):
     display(SVG(model_to_dot(self.model).create(prog='dot', format='svg')))