Exemplo n.º 1
0
def train():
    net = model.model(128).model()
    net.compile(optimizer=SGD(lr=0.001),
                loss='categorical_crossentropy',
                metrics=['acc'])
    net.summary()
    x_train, y_train = loader.loader("./data/FF-PP/train")
    net.fit(x_train, y_train, epochs=100, batch_size=50, validation_split=0.2)

    x_test, y_test = loader.loader("./data/FF-PP/test")

    score = net.evaluate(x_test, y_test)
    print(score)
    ## Save the Weights
    json_string = net.to_json()
    open('./models/FF-PP/AlexNet.json', 'w').write(json_string)
    net.save_weights("./models/FF-PP/weights.h5")
Exemplo n.º 2
0
from keras import backend as K
from keras.models import load_model, model_from_json
import numpy as np
from md import loader
import cv2
from tqdm import tqdm
import copy
import os
# Read model
json_string = open('./models/FF-FM/AlexNet.json', 'r').read()
model = model_from_json(json_string)
model.load_weights('./models/FF-FM/weights.h5')
model.summary()

# Read images and names
ld = loader.loader("./data/FF-FM/train", option=True)
x = ld[0][3]
cv2.imwrite("./sample.png", x * 255)
x = np.array([x])
for x, path in zip(ld[0], ld[2]):
    x = np.array([x])
    name = os.path.basename(path)
    # define functions
    func_Yc = model.output[:, 0]
    func_Aij6 = model.get_layer("conv2d_6").output
    func_Aij7 = model.get_layer("conv2d_7").output
    func_Aij8 = model.get_layer("conv2d_8").output
    func_Aij9 = model.get_layer("conv2d_9").output
    func_Aij10 = model.get_layer("conv2d_10").output

    func_grads6 = K.gradients(func_Yc, func_Aij6)[0]