def test_pretrained_vgg():
    print("Running process to load and test a pre-trained VGG model:")
    batch_size = 16
    model_class = cifar100vgg(train=False)
    model = model_class.model

    print('Start Test')
    X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR100_color()
    #test_prediction = model.predict(X_test, batch_size=batch_size, verbose=2)
    test_prediction = model_class.predict_c(X_test, batch_size=batch_size)
    score = log_loss(y_test, test_prediction)
    print('Test Score log_loss: ', score)
    score_test2 = accuracy_score(y_test, np.argmax(test_prediction, axis=1))
    print('Test Score accuracy: ', score_test2)

    weights_step1 = model.get_weights()
    conv_weights = weights_step1[0]
    print("Saving conv weights into file...")
    now = datetime.datetime.now()
    filename_weights_step1 = "weights/conv_weights_pretrained_" + str(
        now.strftime("%Y-%m-%d-%H-%M")) + ".npy"
    np.save(filename_weights_step1, conv_weights)
    print("Done.")

    return filename_weights_step1, score, score_test2
def run_test_loading_models(filename_model, filename_conv_weights=None):
    print("Running process to load and test a model:")
    batch_size = 16

    print('Loading weights from {}'.format(filename_model))
    model_class = cifar100vgg(train=False)
    model = model_class.model
    model.load_weights(filename_model)

    if filename_conv_weights:
        conv_bias = model.layers[0].get_weights()[1]
        print('Loading conv weights from {}'.format(filename_conv_weights))
        conv_kernel = np.load(filename_conv_weights)
        model.layers[0].set_weights([conv_kernel, conv_bias])

    model_class.model = model
    print('Start Test')
    X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR100_color()
    #test_prediction = model.predict(X_test, batch_size=batch_size, verbose=2)
    test_prediction = model_class.predict_c(X_test, batch_size=batch_size)
    score = log_loss(y_test, test_prediction)
    print('Test Score log_loss: ', score)
    score_test2 = accuracy_score(y_test, np.argmax(test_prediction, axis=1))
    print('Test Score accuracy: ', score_test2)

    return score, score_test2
예제 #3
0
def main(dataset_name="cifar100", model_name="cifar100"):
    if dataset_name == "cifar100":
        dataset = keras.datasets.cifar100
        labels = CIFAR100_LABELS
    elif dataset_name == "cifar10":
        dataset = keras.datasets.cifar10
        labels = CIFAR10_LABELS
    else:
        raise ValueError(dataset_name)

    print("Loaded dataset={}".format(dataset_name))
    if model_name == "cifar100":
        model = cifar100vgg(train=False)
    elif model_name == "cifar10":
        model = cifar10vgg(train=False)
    else:
        raise ValueError(model_name)
    print("Loaded model={}".format(model_name))

    (x_train, y_train), (x_test, y_test) = dataset.load_data()
    x_train_norm = model.normalize_production(x_train.astype('float32'))

    if dataset_name == model_name:
        print("Check validation error:")
        check_validation(dataset_name, x_test, y_test, model)

    print("evaluating first flat layer using dataset...")
    inter_model = keras.Model(
        inputs=model.model.input,
        outputs=model.model.get_layer("flatten_1").output)
    X = x_train
    Y = y_train
    X_processed = inter_model.predict(x_train_norm)
    data = (X, X_processed, Y)
    print("done.")

    print("saving processed images...")
    filename = "{}_{}_processed.p".format(dataset_name, model_name)
    with open(filename, "wb") as f:
        pickle.dump(data, f)
    print("saved results to {}".format(filename))

    print("saving a separated version of the data...")
    # dataset is a map (label_str => (X, X_processed))
    dataset = {}
    for i, label in enumerate(labels):
        ii = (Y.flatten() == i)
        imgs = X[ii]
        imgs_processed = X_processed[ii]
        dataset[label] = (imgs, imgs_processed)

    filename = "{}_{}_separated.p".format(dataset_name, model_name)
    with open(filename, "wb") as f:
        pickle.dump(dataset, f)
    print("saved results to {}".format(filename))

    # Finally, clear the session.
    keras.backend.clear_session()
def run_load_finetune_models(filename_model, filename_conv_weights):
    print("Running process to load and finetune a model:")
    batch_size = 16
    #nb_epoch = 50

    print('Loading model from {}'.format(filename_model))

    model_class = cifar100vgg(train=False)
    model = model_class.model
    model.load_weights(filename_model)

    conv_bias = model.layers[0].get_weights()[1]
    print('Loading conv weights from {}'.format(filename_conv_weights))
    conv_kernel = np.load(filename_conv_weights)
    model.layers[0].set_weights([conv_kernel, conv_bias])
    model.layers[0].trainable = False
    #model.compile(optimizer = Adam(lr=5.0e-4), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])

    X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR100_color()
    #callbacks = [EarlyStopping(monitor='val_loss', patience=15, verbose=0)]

    print("Fine-tuning model...")
    #model.fit(X_train, y_train, batch_size=batch_size, epochs=nb_epoch,
    #      shuffle=True, verbose=1, validation_data=(X_val, y_val),
    #      callbacks=callbacks)
    model_class.model = model
    model = model_class.train(model)

    print('Saving weights of model...')
    now = datetime.datetime.now()
    filename_model_step3 = "weights/model_finetuned_" + str(
        now.strftime("%Y-%m-%d-%H-%M")) + ".h5"
    model.save_weights(filename_model_step3)
    print("Done.")

    #print('Making predictions for model')
    #predictions_valid = model.predict(X_val, batch_size=batch_size, verbose=2)
    #score = log_loss(y_val, predictions_valid)
    #print('Score log_loss: ', score)

    #print("Log_loss train: ", score)

    #info_string = 'loss_' + str(score) + '_ep_' + str(nb_epoch)
    #print(info_string)

    model_class.model = model
    print('Start Test:')
    #test_prediction = model.predict(X_test, batch_size=batch_size, verbose=2)
    test_prediction = model_class.predict_c(X_test, batch_size=batch_size)
    score_test = log_loss(y_test, test_prediction)
    print('Test Score log_loss: ', score_test)
    score_test2 = accuracy_score(y_test, np.argmax(test_prediction, axis=1))
    print('Test Score accuracy: ', score_test2)

    return filename_model_step3, score_test, score_test2
예제 #5
0
def main():
    np.random.seed(42)
    tf.set_random_seed(42)

    x_train, y_train, x_test, y_test = load_data()

    source_model = cifar100vgg(train=False)
    x_train = source_model.normalize_production(x_train)
    x_test = source_model.normalize_production(x_test)

    fine_tuning_tests((x_train, y_train, x_test, y_test), source_model)
    embedding_logistic_regression((x_train, y_train, x_test, y_test),
                                  source_model)
    embedding_tests((x_train, y_train, x_test, y_test), source_model)
    fine_tuning_tests((x_train, y_train, x_test, y_test), source_model, True,
                      25, True)
def run_end_to_end_models(filename_model, filename_phases):
    X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR100_color()
    print(
        "Starting end-to-end co-training of phase profile and suffix layers..."
    )

    print("Training dataset shape: ", X_train.shape)
    print("Training label shape:", y_train.shape)

    print('Loading model from {}'.format(filename_model))
    model_class = cifar100vgg(train=False)
    model = model_class.model
    model.load_weights(filename_model)

    X_train, X_test = model_class.normalize(X_train, X_test)

    EPOCHS = 25
    BS = 64
    INIT_LR = 5e-4
    opt = Adam(lr=INIT_LR)
    PhaseLR = 5e-6

    print('Loading phase profile from {}'.format(filename_phases))
    phi_in = np.load(filename_phases)
    phase2Kernel = Phase2Kernels(phi_in)

    def step(X, y):
        # get the weights

        weights = phase2Kernel.forward()
        conv_bias = model.layers[0].get_weights()[1]
        model.layers[0].set_weights([weights, conv_bias])

        # keep track of our gradients
        with tf.GradientTape() as tape:
            # make a prediction using the model and then calculate the
            # loss
            pred = model(X)
            print("Pred shape:", pred.shape)
            loss = sparse_categorical_crossentropy(y, pred)
            accuracy = accuracy_score(y, np.argmax(pred, axis=1))
            print("loss", tf.math.reduce_mean(loss))
            print("accuracy:", accuracy)
        grads = tape.gradient(loss, model.trainable_variables)
        print("gradients shapes: ", grads[0].shape, grads[1].shape)
        final_grad = phase2Kernel.backward(grads[0])
        phase2Kernel.phi -= PhaseLR * cp.asnumpy(final_grad)

        opt.apply_gradients(zip(grads, model.trainable_variables))
        # print(PhaseLR * cp.asnumpy(final_grad))
        # time.sleep(1)

    numUpdates = int(X_train.shape[0] / BS)
    # loop over the number of epochs
    for epoch in range(0, EPOCHS):
        # show the current epoch number
        print("[INFO] starting epoch {}/{}...".format(epoch + 1, EPOCHS))
        X_train, y_train = shuffle(X_train, y_train, random_state=0)
        sys.stdout.flush()
        epochStart = time.time()
        # loop over the data in batch size increments
        for i in range(0, numUpdates):
            # determine starting and ending slice indexes for the current
            # batch
            print("starting batch: ", i)
            start = i * BS
            end = start + BS
            # take a step
            step(X_train[start:end], y_train[start:end])
        # show timing information for the epoch
        epochEnd = time.time()
        elapsed = (epochEnd - epochStart) / 60.0
        print("took {:.4} minutes".format(elapsed))
    print("Saving co-trained phases and network parameters...")
    now = datetime.datetime.now()
    filename_model_step4 = "weights/model_co-trained_" + str(
        now.strftime("%Y-%m-%d-%H-%M")) + ".h5"
    model.save_weights(filename_model_step4)
    filename_phases_step4 = "phases/phases_co-trained" + str(
        now.strftime("%Y-%m-%d-%H-%M")) + ".npy"
    np.save(filename_phases_step4, np.asarray(phase2Kernel.phi))
    print("Done.")
    print("Co-training finished!")
    print('Start Test:')
    test_prediction = model.predict(X_test, batch_size=BS, verbose=2)
    score_test = log_loss(y_test, test_prediction)
    print('Test Score log_loss: ', score_test)
    score_test2 = accuracy_score(y_test, np.argmax(test_prediction, axis=1))
    print('Test Score accuracy: ', score_test2)

    print("Measuring metrics using Class' predict() method:")
    model_class.model = model
    print('Start Test:')
    #test_prediction = model.predict(X_test, batch_size=batch_size, verbose=2)
    test_prediction = model_class.predict_c(X_test,
                                            normalize=False,
                                            batch_size=BS)
    score_test3 = log_loss(y_test, test_prediction)
    print('Test Score log_loss: ', score_test3)
    score_test4 = accuracy_score(y_test, np.argmax(test_prediction, axis=1))
    print('Test Score accuracy: ', score_test4)

    return filename_model_step4, filename_phases_step4, score_test, score_test2, score_test3, score_test4
def create_classifier():
    model_class = cifar100vgg(train=False)
    model = model_class.model
    return model