コード例 #1
0
ファイル: train.py プロジェクト: drNymbus/grgb
def convert(net, input, view=False):
    # img should be a flatten tensor
    out = net(input)
    img = to_image(out, "RGB")

    if view:
        view_data(input, 'L')
        view_data(out, "RGB")

    return img
コード例 #2
0
ファイル: train.py プロジェクト: drNymbus/grgb
def test(net, testloader, cuda=False):
    net.eval()
    if cuda:
        net.cuda()

    accuracy, jaccard, mse, maxe = 0, 0, 0, 0
    for i, item in enumerate(testloader, 0):
        if item is None:
            break
        # UNPACKING item
        path = item["path"]
        data = item["data"]
        label = item["label"]

        # REFORMAT DATA AND LABEL
        data = torch.tensor(data, dtype=torch.float32)
        data = data.reshape(-1)
        label = torch.tensor(label, dtype=torch.float32)
        label = label.reshape(-1)

        if cuda:
            # LOADS MODEL ON GPU
            device = torch.device('cuda:0')
            data = data.to(device)
            label = label.to(device)

        out = net(data)

        label = label.cpu().detach().numpy()
        out = out.cpu().detach().numpy()
        # accuracy += sklearn.metrics.accuracy_score(label, out)
        # jaccard  += sklearn.metrics.jaccard_score(label, out)
        mse += sklearn.metrics.mean_squared_error(label, out)
        maxe += sklearn.metrics.max_error(label, out)

    # accuracy = accuracy / len(testloader)
    # jaccard = jaccard / len(testloader)
    mse = mse / len(testloader)
    maxe = maxe / len(testloader)
    # print("Accuracy ==> [%d] on %d images" % (accuracy*100, len(testloader)))
    # print("Jaccard ==> [%d] on %d images" % (jaccard*100, len(testloader)))
    print("MEAN_SQUARED_ERROR ==> [%.5f] on %d images" %
          (mse, len(testloader)))
    print("MAX_ERROR ==> [%.5f] on %d images" % (maxe, len(testloader)))
コード例 #3
0
from neural import net

nn = net('test.ann')

コード例 #4
0
NO_LEARNING_THRESHOLD = 200

# Time cycle of the simulation
time = 0

# These variables perform bookkeeping (how many cycles was the pole
# balanced for before it fell). Useful for plotting learning curves.
time_steps_to_failure = []
num_failures = 0
time_at_start_of_current_trial = 0

# You should reach convergence well before this
max_failures = 400

# Initialize a cart pole
nn = neural.net()
nn.load_params('trained7.npz')
p = physics.pendulum()
p.reset()

nn_states = []
states = []

use_p = False
use_nn = False
if use_nn:
	state = p.get_state_img(nn)
else:
	state = p.get_state()

コード例 #5
0
ファイル: train.py プロジェクト: drNymbus/grgb
def train(net, epochs, trainloader, cuda=False, plot=False):
    JSON_PATH = "data/json/loss.json"
    with open(JSON_PATH, "w+") as f:
        json.dump({}, f)

    # criterion = nn.CrossEntropyLoss()
    criterion = nn.MSELoss()
    # criterion = nn.KLDivLoss()
    # criterion = nn.NLLLoss()

    # optimizer = optim.ASGD(net.parameters(), lr=0.0000001)
    optimizer = optim.Adam(net.parameters(), lr=0.00001)

    if cuda:
        criterion.cuda()
        net.cuda()

    e_losses = []
    for epoch in range(epochs):  # loop over the dataset multiple times

        losses = []
        running_loss = 0.0
        for i, item in enumerate(trainloader, 0):

            # UNPACKING item
            path = item["path"]
            data = item["data"]
            label = item["label"]

            # REFORMAT DATA AND LABEL
            data = torch.tensor(data, dtype=torch.float32)
            data = data.reshape(-1)
            label = torch.tensor(label, dtype=torch.float32)
            # label = label.reshape(-1)

            # print(path, ": data", data.shape[0], "| label", label.shape[0])

            if cuda:
                # LOADS MODEL ON GPU
                device = torch.device('cuda:0')
                data = data.to(device)
                label = label.to(device)

            optimizer.zero_grad()

            out = net(data)
            out = torch.reshape(out, (IMG_RES[0], IMG_RES[1], 3))

            loss = criterion(out, label)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()
            losses.append(running_loss)

            if i % 300 == 299:  # print every 300 mini-batches
                print('[e: %d, i: %d] loss: %.3f' %
                      (epoch + 1, i + 1, running_loss / 50))
                # running_loss = 0.0

        if plot:
            plt.plot(losses, label="running loss for epoch(%d)" % (epoch + 1))
            plt.show()

        avg = 0  # compute average loss over the epoch
        for loss in losses:
            avg += loss
        avg = avg / len(trainloader)
        e_losses.append(avg)

        # loads data in data/json/loss.json
        all_data = {}
        try:
            with open(JSON_PATH, 'r') as f:
                all_data = json.loads(f.read())
        except Exception as e:
            print("/!\\ Warning /!\\ " + str(e))
            pass

        # append the loss data of the epoch in data/json/loss.json
        all_data[epoch + 1] = {"average": avg, "losses": losses}
        with open(JSON_PATH, "w+") as f:
            json.dump(all_data, f)

        print("\n\t(AVERAGE) e=%d: loss=%.3f\n" % (epoch + 1, avg))
        print("#########################################################")

    if plot:
        plt.plot(e_losses, label="epoch (average) running loss")
        plt.show()