def main(model_dir=None):
    netconfig, hyperparams_config, data_config = config()
    if model_dir is None:
        model_num = int(netconfig['model_dir'][-1]) - 1
        model_dir = netconfig['model_dir'][:-1] + str(model_num)
    netconfig['model_dir'] = model_dir
    train_data, validate_data, test_data = construct_data(netconfig)
Esempio n. 2
0
def main():

    netconfig, _, _ = config()
    model = SELUNet(dropout=netconfig["alphadropout"])
    netconfig["model_resume_file"] = "model3/saved_models/model_20.pt"
    model = load_model(model, netconfig["model_resume_file"])
    model.eval()

    train_data = AudioDataset("bdl_speech.npz", "bdl_egg.npz",
                              netconfig["window"], netconfig["window"])
    data = [train_data[i] for i in range(10)]
    speech = th.cat([d[0] for d in data])
    peak_arr = th.cat([d[1] for d in data])

    idx = np.random.choice(len(speech), 2000, replace=False)
    speech = speech[idx].unsqueeze_(1)

    with th.no_grad():
        features = model.feature_extractor(speech)

    print(f"speech shape {speech.shape} features shape {features.shape}")
    features = features.view(features.size(0), -1).numpy()

    labels = peak_arr[:, 1][idx].numpy()

    features_tsne = TSNE().fit_transform(features)
    print(f"features tsne shape {features_tsne.shape}")

    positive_idx = labels == 1
    negative_idx = labels == 0

    print(
        f"positive samples: {np.count_nonzero(positive_idx)} negative samples: {np.count_nonzero(negative_idx)}"
    )

    positive_coords = features_tsne[positive_idx]
    negative_coords = features_tsne[negative_idx]
    positive_distances = peak_arr[:, 0][idx].numpy()[positive_idx]

    bins = np.linspace(0, netconfig["window"], 5)
    markers = [".", "v", "^", "*"]
    colors = ["b", "r", "k", "m"]
    inds = np.digitize(positive_distances, bins)

    fig, ax = plt.subplots()

    ax.set_title("TSNE L2 plot")
    for i, m, c in zip(range(len(bins) - 1), markers, colors):
        indices = inds == i
        ax.scatter(
            positive_coords[indices, 0],
            positive_coords[indices, 1],
            c=c,
            label=f"++ {(bins[i], bins[i+1])}",
            marker=m,
        )
    ax.scatter(negative_coords[:, 0], negative_coords[:, 1], c="g", label="--")
    ax.legend(loc=1)
    fig.set_tight_layout(True)
    ax.set_xlabel("Dimension 1")
    ax.set_ylabel("Dimension 2")

    mng = plt.get_current_fig_manager()
    mng.window.showMaximized()
    plt.show()
Esempio n. 3
0
import os
import time
import copy

import torch
import torch.nn as nn
import torch.nn.functional as F
#from torch.autograd import Variable
from torch.utils.data import DataLoader

import utils
from main import config
from tqdm.auto import tqdm
#  base config
args = config()

# normally train & evaluation consistantly going together


def train(dataloaders, model, criterion, optimizer, scheduler, args):
    since = time.time()
    epochs = args.num_epoch
    save_dir = args.save_dir
    device = args.device

    # best model find
    best_model = copy.deepcopy(model.state_dict())
    best_acc = 0.0

    for epoch in range(epochs):
        print(f'Epoch {epoch}/{epochs - 1}')
Esempio n. 4
0
print()
print(">> Variables:")
print(df[variables])

grid_search = dict(adv_epsilon=[0.01,0.05,0.1,0.5,1.0], adv_norm=['infty', 'l2'])


outputs = ['test_accuracy','adv_accuracy']

epoch = 20
df = pd.DataFrame()

for exp_name, exp_dir in zip(exp_names, exp_dirs):
    print(f">> Experiment {exp_name}")
    kwargs = load_dict(os.path.join(exp_dir, defaults.ARG_FILE))
    args = main.config(**kwargs)
    (train_loader, test_loader), device = main.load(args)
    models, _, _= main.init(args, device, shape=train_loader.shape)

    checkpoints = [os.path.join(exp_dir, fstr(f, args=args))
        for f in defaults.SAVE_FILES]

    try:
        for m,c in zip(models, checkpoints):
            m.load_state_dict(torch.load(c))
    except:
        print(">> Warning: Files missing for {exp_dir}")
        continue
                
    for values in itertools.product(*grid_search.values()):
        grid = {}
def main():

    netconfig, _, _ = config()
    model = SELUNet(dropout=netconfig["alphadropout"])
    netconfig["model_resume_file"] = "model3/saved_models/model_20.pt"
    model = load_model(model, netconfig["model_resume_file"])
    model.eval()

    train_data = AudioDataset(
        "bdl_speech.npz", "bdl_egg.npz", netconfig["window"], netconfig["window"]
    )
    data = [train_data[i] for i in range(10)]
    speech = th.cat([d[0] for d in data])
    peak_arr = th.cat([d[1] for d in data])

    idx = np.random.choice(len(speech), 2000, replace=False)
    speech = speech[idx].unsqueeze_(1)

    with th.no_grad():
        features = model.feature_extractor(speech)

    print(f"speech shape {speech.shape} features shape {features.shape}")
    features = features.view(features.size(0), -1).numpy()

    labels = peak_arr[:, 1][idx].numpy()

    features_tsne = TSNE(perplexity=70, n_iter=2000).fit_transform(features)
    print(f"features tsne shape {features_tsne.shape}")

    positive_idx = labels == 1
    negative_idx = labels == 0

    print(
        f"positive samples: {np.count_nonzero(positive_idx)} negative samples: {np.count_nonzero(negative_idx)}"
    )

    positive_coords = features_tsne[positive_idx]
    negative_coords = features_tsne[negative_idx]
    positive_distances = peak_arr[:, 0][idx].numpy()[positive_idx]

    # bins = np.linspace(0, netconfig["window"], 5)
    # markers = [".", "v", "^", "*"]
    # colors = ["b", "r", "k", "m"]
    # inds = np.digitize(positive_distances, bins)

    fig, ax = plt.subplots()

    # ax.set_title("TSNE L2 plot")
    sc = ax.scatter(
        positive_coords[:, 0],
        positive_coords[:, 1],
        c=positive_distances / (netconfig["window"] - 1),
        # label=f"++",
        marker="o",
        cmap="viridis",
    )
    # ax.scatter(
    #     negative_coords[:, 0], negative_coords[:, 1], c="g", label="--", marker="."
    # )
    cbar = plt.colorbar(sc)
    cbar.set_ticks([0, 1])
    cbar.set_ticklabels(["$1$", "$w_d$"])

    # ax.legend(
    #     ncol=2,
    #     framealpha=0.2,
    #     loc="upper center",
    #     bbox_to_anchor=(0.5, 1.15),
    #     mode=None,
    #     markerscale=4,
    # )
    ax.tick_params(
        axis="both",
        which="both",
        bottom="off",
        top="off",
        labelbottom="off",
        right="off",
        left="off",
        labelleft="off",
    )
    fig.set_tight_layout(True)
    # ax.set_xlabel("Dimension 1")
    # ax.set_ylabel("Dimension 2")

    # mng = plt.get_current_fig_manager()
    # mng.window.showMaximized()
    # plt.show()
    model_folder = os.path.join(netconfig["model_resume_file"].split("/")[0], "plots")
    os.makedirs(model_folder, exist_ok=True)

    curtime = dt.now()
    plt.savefig(
        os.path.join(
            model_folder,
            f"fig_{curtime.day}_{curtime.hour}_{curtime.minute}_{curtime.second}.eps",
        ),
        format="eps",
        dpi=300,
    )