Ejemplo n.º 1
0
def visualize_network(
    network,
    scale_xy: int = 2,
    spacing: int = 10,
    scale_z=0.1,
    save_image_path: str = None,
):
    try:
        font = ImageFont.truetype(ImageFontPath.SKIA, 16)

        color_map = defaultdict(dict)
        color_map[Conv2D]['fill'] = '#CA6F1E'
        color_map[Activation]['fill'] = "#662851"
        color_map[Dropout]['fill'] = '#212F3D'
        color_map[MaxPooling2D]['fill'] = '#006fC1'
        color_map[Dense]['fill'] = '#145A32'
        color_map[Flatten]['fill'] = '#229954'
        color_map[BatchNormalization]['fill'] = '#BDC3C7'
        color_map[AveragePooling2D]['fill'] = '#4EACF2'
        color_map[Concatenate]['fill'] = "#4A235A"
        return visualkeras.layered_view(
            network,
            color_map=color_map,
            font=font,
            legend=True,
            scale_xy=scale_xy,
            spacing=spacing,
            to_file=save_image_path,
            scale_z=scale_z,
        )
    except Exception as e:
        raise e
Ejemplo n.º 2
0
def save_report_keras(model, datasets, classes, history, name, save_path):

    report_dist_path = "../reports/" + save_path

    title_page = get_title_page(name)

    model_summary = get_model_summary(model)

    loss = plot_graph(
        [[history['loss'], 'Training data']
         ],  # [history['val_loss'], 'Validation data']
        name="Training losses",
        legend=['Loss', 'No. epoch'])

    accuracy = plot_graph(
        [[history['accuracy'], 'Training data']
         ],  # [history['val_accuracy'], 'Validation data']
        name="Model accuracy",
        legend=['Accuracy', 'No. epoch'])

    if not os.path.exists(report_dist_path):
        os.mkdir(report_dist_path)

    pp = PdfPages(report_dist_path + "/" + name + '.pdf')
    pp.savefig(title_page)
    pp.savefig(model_summary)

    model_image = set_image(
        visualkeras.layered_view(model,
                                 legend=True,
                                 scale_xy=2,
                                 scale_z=1,
                                 draw_funnel=False,
                                 min_z=10,
                                 min_xy=10,
                                 max_z=200,
                                 max_xy=1000))
    pp.savefig(model_image)

    pp.savefig(loss)
    pp.savefig(accuracy)

    for ds, title in datasets:
        y_true, y_pred = generate_y_with_dense(model, ds)
        confusion_matrix = plot_confusion_matrix(y_true,
                                                 y_pred,
                                                 title=title,
                                                 classes=classes)
        pp.savefig(confusion_matrix)

    pp.close()
Ejemplo n.º 3
0
 def visualize_model(self):
     color_map = defaultdict(dict)
     color_map[Convolution2D]['fill'] = 'orange'
     color_map[Lambda]['fill'] = 'gray'
     color_map[Dropout]['fill'] = 'pink'
     color_map[Cropping2D]['fill'] = 'red'
     color_map[Dense]['fill'] = 'green'
     color_map[Flatten]['fill'] = 'blue'
     color_map[Activation]['fill'] = 'yellow'
     file_name = os.path.join(self.output_dir, "color_output.png")
     check_dir(file_name)
     visualkeras.layered_view(self.model, color_map=color_map, spacing=50, to_file=file_name)
     file_name = os.path.join(self.output_dir, "output.png")
     check_dir(file_name)
     visualkeras.layered_view(self.model, spacing=50, to_file=file_name)  # write to disk
     file_name = os.path.join(self.output_dir, "flat_output.png")
     check_dir(file_name)
     visualkeras.layered_view(self.model, draw_volume=False, spacing=50, to_file=file_name)
     file_name = os.path.join(self.output_dir, "model_plot.png")
     check_dir(file_name)
     plot_model(self.model, to_file=file_name, show_shapes=True, show_layer_names=True)
Ejemplo n.º 4
0
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))

# Now visualize the model!

color_map = defaultdict(dict)
color_map[Conv2D]['fill'] = 'orange'
color_map[ZeroPadding2D]['fill'] = 'gray'
color_map[Dropout]['fill'] = 'pink'
color_map[MaxPooling2D]['fill'] = 'red'
color_map[Dense]['fill'] = 'green'
color_map[Flatten]['fill'] = 'teal'

font = ImageFont.truetype("arial.ttf", 32)

visualkeras.layered_view(model, to_file='../figures/vgg16.png', type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='../figures/vgg16_legend.png', type_ignore=[visualkeras.SpacingDummyLayer],
                         legend=True, font=font)
visualkeras.layered_view(model, to_file='../figures/vgg16_spacing_layers.png', spacing=0)
visualkeras.layered_view(model, to_file='../figures/vgg16_type_ignore.png',
                         type_ignore=[ZeroPadding2D, Dropout, Flatten, visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='../figures/vgg16_color_map.png',
                         color_map=color_map, type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='../figures/vgg16_flat.png',
                         draw_volume=False, type_ignore=[visualkeras.SpacingDummyLayer])
visualkeras.layered_view(model, to_file='../figures/vgg16_scaling.png',
                         scale_xy=1, scale_z=1, max_z=1000, type_ignore=[visualkeras.SpacingDummyLayer])
Ejemplo n.º 5
0
baseModel = VGG16(weights="imagenet", include_top=False,
                     input_tensor=Input(shape=(224, 224, 3)))

headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dense(len(lb.classes_), activation="softmax")(headModel)

model = Model(inputs=baseModel.input, outputs=headModel)

for layer in baseModel.layers:
    layer.trainable = False

model.summary()
visualkeras.layered_view(model, to_file='vgg16_arc.png', legend=True)

# compile model
print("[INFO] compiling model...")
opt = SGD(lr=1e-4, momentum=0.9, decay=1e-4 / args["epochs"])
model.compile(loss="categorical_crossentropy", optimizer=opt,
              metrics=["accuracy"])

# train model
print("[INFO] training head...")
H = model.fit(
    x=trainAug.flow(trainX, trainY, batch_size=32),
    steps_per_epoch=len(trainX) // 32,
    validation_data=valAug.flow(testX, testY),
    validation_steps=len(testX) // 32,
    epochs=args["epochs"])
Ejemplo n.º 6
0
  #                  validation_steps = TRAIN_VAL.shape[0] // 64,
                  verbose=1,
                  callbacks = [early_stopping_callback, ckpt]) 

  scores = model.evaluate(inputs[test], targets[test], verbose=0)
  print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')
  acc_per_fold.append(scores[1] * 100)
  loss_per_fold.append(scores[0])

  # Increase fold number
  fold_no = fold_no + 1

!pip install visualkeras
import visualkeras

visualkeras.layered_view(model)

"""## Evaluation"""

print(TRAIN_VAL.shape)
plt.title('Model Accuracy')
plt.plot(range(1,11),history.history['accuracy'])
plt.plot(range(1,11),history.history['val_accuracy'])
plt.xlabel('Epoch')
plt.ylabel('accuracy')
plt.legend(labels=['train','validation'])
plt.show()

plt.title('Model Loss')
plt.plot(range(1,11),history.history['loss'])
plt.plot(range(1,11),history.history['val_loss'])
Ejemplo n.º 7
0
from tensorflow.python.keras.models import Sequential
from tensorflow.keras import layers
import visualkeras

model = Sequential()
model.add(layers.Dense(8, activation='relu', input_shape=(4000, )))
model.add(layers.Dense(8, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])
visualkeras.layered_view(model,
                         to_file='../figures/spam.png',
                         min_xy=10,
                         min_z=10,
                         scale_xy=100,
                         scale_z=100,
                         one_dim_orientation='x')
Ejemplo n.º 8
0
from tensorflow import keras
from tensorflow.keras import layers
import visualkeras

encoder_input = keras.Input(shape=(28, 28, 1), name='img')
x = layers.Conv2D(16, 3, activation='relu')(encoder_input)
x = layers.Conv2D(32, 3, activation='relu')(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation='relu')(x)
x = layers.Conv2D(16, 3, activation='relu')(x)
encoder_output = layers.GlobalMaxPooling2D()(x)
encoder = keras.Model(encoder_input, encoder_output, name='encoder')

visualkeras.layered_view(encoder, to_file='../figures/encoder.png')

x = layers.Reshape((4, 4, 1))(encoder_output)
x = layers.Conv2DTranspose(16, 3, activation='relu')(x)
x = layers.Conv2DTranspose(32, 3, activation='relu')(x)
x = layers.UpSampling2D(3)(x)
x = layers.Conv2DTranspose(16, 3, activation='relu')(x)
decoder_output = layers.Conv2DTranspose(1, 3, activation='relu')(x)
autoencoder = keras.Model(encoder_input, decoder_output, name='autoencoder')

visualkeras.layered_view(autoencoder, to_file='../figures/autoencoder.png')
    conv9 = tf.keras.layers.Conv2DTranspose(64,
                                            kshape,
                                            activation='relu',
                                            padding='same',
                                            trainable=base_trainable)(up9)
    conv9 = tf.keras.layers.BatchNormalization()(conv9)
    conv9 = tf.keras.layers.Conv2DTranspose(64,
                                            kshape,
                                            activation='relu',
                                            padding='same',
                                            trainable=base_trainable)(conv9)
    conv9 = tf.keras.layers.BatchNormalization()(conv9)

    out = tf.keras.layers.Conv2D(num_classes, (1, 1),
                                 activation='softmax')(conv9)
    model = tf.keras.Model(input_img, out)

    opt = tf.keras.optimizers.Adam(lr=learning_rate, decay=learning_decay)
    model.compile(optimizer=opt, loss='categorical_crossentropy')

    return model


model = get_model()
visualkeras.layered_view(model,
                         to_file='model.png',
                         type_ignore=[
                             tf.keras.layers.BatchNormalization,
                             tf.keras.layers.AlphaDropout
                         ])