Exemple #1
0
def init_goal_misc(p, init_image, goal_image, init, goal):
    print("init:",init_image.min(),init_image.max(),)
    print("goal:",goal_image.min(),goal_image.max(),)
    print(init)
    print(goal)
    rec = sae.decode_binary(np.array([init,goal]))
    init_rec, goal_rec = rec
    print("init (reconstruction):",init_rec.min(),init_rec.max(),)
    print("goal (reconstruction):",goal_rec.min(),goal_rec.max(),)

    def r(i):
        s = i.shape
        return i.reshape((s[0]//2, 2, s[1]//2, 2)).mean(axis=(1,3))
    
    plot_grid([init_image,init_rec,init_image-init_rec,(init_image-init_rec).round(),
               init_image.round(),init_rec.round(),init_image.round()-init_rec.round(),(init_image.round()-init_rec.round()).round(),
               r(init_image),r(init_rec),r(init_image)-r(init_rec),(r(init_image)-r(init_rec)).round(),
               # r(init_image).round(),r(init_rec).round(),r(init_image).round()-r(init_rec).round(),(r(init_image).round()-r(init_rec).round()).round(),
               
               goal_image,goal_rec,goal_image-goal_rec,(goal_image-goal_rec).round(),
               goal_image.round(),goal_rec.round(),goal_image.round()-goal_rec.round(),(goal_image.round()-goal_rec.round()).round(),
               r(goal_image),r(goal_rec),r(goal_image)-r(goal_rec),(r(goal_image)-r(goal_rec)).round(),
               # r(goal_image).round(),r(goal_rec).round(),r(goal_image).round()-r(goal_rec).round(),(r(goal_image).round()-r(goal_rec).round()).round(),
               ],
              w=4,
              path=problem(network("init_goal_reconstruction.png")),verbose=True)

    import sys
    print("init BCE:",bce(init_image,init_rec))
    print("init MAE:",mae(init_image,init_rec))
    print("init MSE:",mse(init_image,init_rec))
    # if image_diff(init_image,init_rec) > image_threshold:
    #     print("Initial state reconstruction failed!")
    #     sys.exit(3)
    print("goal BCE:",bce(goal_image,goal_rec))
    print("goal MAE:",mae(goal_image,goal_rec))
    print("goal MSE:",mse(goal_image,goal_rec))
    # if image_diff(goal_image,goal_rec) > image_threshold:
    #     print("Goal state reconstruction failed!")
    #     sys.exit(4)
    if not np.all(p.validate_states(rec)):
        print("Init/Goal state reconstruction failed!")
        # sys.exit(3)
        print("But we continue anyways...")
Exemple #2
0
 def regenerate_many(sae, data):
     loss = 1000000000
     for i in range(max_repeat):
         data_rec = regenerate(sae, data)
         prev_loss = loss
         loss = bce(data, data_rec)
         if len(data) > 3000:
             print(loss, loss / prev_loss)
         data = data_rec
         if loss / prev_loss > rate_threshold:
             if len(data) > 3000:
                 print("improvement saturated: loss / prev_loss = ",
                       loss / prev_loss, ">", rate_threshold)
             break
         # if loss < threshold:
         #     print("good amount of loss:", loss, "<", threshold)
         #     break
     return data.round().astype(np.int8)
Exemple #3
0
def main(network_dir, problem_dir, searcher):
    global sae, oae, ad, ad2, sd, sd2, sd3, cae, combined_discriminator, available_actions

    p = latplan.util.puzzle_module(network_dir)

    sae = default_networks[get_ae_type(network_dir)](network_dir).load(
        allow_failure=True)
    oae = ActionAE(sae.local("_aae/")).load(allow_failure=True)
    try:
        ad = PUDiscriminator(sae.local("_ad/")).load(allow_failure=True)
    except:
        ad = Discriminator(sae.local("_ad/")).load(allow_failure=True)
    # sd  = Discriminator(sae.local("_sd/")).load(allow_failure=True)
    # ad2 = Discriminator(sae.local("_ad2/")).load(allow_failure=True)
    # sd2 = Discriminator(sae.local("_sd2/")).load(allow_failure=True)
    sd3 = PUDiscriminator(sae.local("_sd3/")).load()
    try:
        cae = default_networks['SimpleCAE'](sae.local("_cae/")).load()
        combined_discriminator = default_networks['CombinedDiscriminator'](sae,
                                                                           cae,
                                                                           sd3)
    except:
        combined_discriminator = default_networks['CombinedDiscriminator2'](
            sae, sd3)

    def problem(path):
        return os.path.join(problem_dir, path)

    def network(path):
        root, ext = os.path.splitext(path)
        return "{}_{}{}".format(
            ensure_directory(network_dir).split("/")[-2], root, ext)

    def search(path):
        root, ext = os.path.splitext(path)
        return "{}_{}{}".format(searcher, root, ext)

    from scipy import misc
    from latplan.puzzles.util import preprocess, normalize
    # is already enhanced, equalized
    init_image = normalize(misc.imread(problem("init.png")))
    goal_image = normalize(misc.imread(problem("goal.png")))
    print(
        "init:",
        init_image.min(),
        init_image.max(),
    )
    print(
        "goal:",
        goal_image.min(),
        goal_image.max(),
    )
    init = sae.encode_binary(np.expand_dims(init_image,
                                            0))[0].round().astype(int)
    goal = sae.encode_binary(np.expand_dims(goal_image,
                                            0))[0].round().astype(int)
    print(init)
    print(goal)
    rec = sae.decode_binary(np.array([init, goal]))
    init_rec, goal_rec = rec
    print(
        "init (reconstruction):",
        init_rec.min(),
        init_rec.max(),
    )
    print(
        "goal (reconstruction):",
        goal_rec.min(),
        goal_rec.max(),
    )

    def r(i):
        s = i.shape
        return i.reshape((s[0] // 2, 2, s[1] // 2, 2)).mean(axis=(1, 3))

    plot_grid(
        [
            init_image,
            init_rec,
            init_image - init_rec,
            (init_image - init_rec).round(),
            init_image.round(),
            init_rec.round(),
            init_image.round() - init_rec.round(),
            (init_image.round() - init_rec.round()).round(),
            r(init_image),
            r(init_rec),
            r(init_image) - r(init_rec),
            (r(init_image) - r(init_rec)).round(),
            # r(init_image).round(),r(init_rec).round(),r(init_image).round()-r(init_rec).round(),(r(init_image).round()-r(init_rec).round()).round(),
            goal_image,
            goal_rec,
            goal_image - goal_rec,
            (goal_image - goal_rec).round(),
            goal_image.round(),
            goal_rec.round(),
            goal_image.round() - goal_rec.round(),
            (goal_image.round() - goal_rec.round()).round(),
            r(goal_image),
            r(goal_rec),
            r(goal_image) - r(goal_rec),
            (r(goal_image) - r(goal_rec)).round(),
            # r(goal_image).round(),r(goal_rec).round(),r(goal_image).round()-r(goal_rec).round(),(r(goal_image).round()-r(goal_rec).round()).round(),
        ],
        w=4,
        path=problem(network("init_goal_reconstruction.png")),
        verbose=True)

    import sys
    print("init BCE:", bce(init_image, init_rec))
    print("init MAE:", mae(init_image, init_rec))
    print("init MSE:", mse(init_image, init_rec))
    # if image_diff(init_image,init_rec) > image_threshold:
    #     print("Initial state reconstruction failed!")
    #     sys.exit(3)
    print("goal BCE:", bce(goal_image, goal_rec))
    print("goal MAE:", mae(goal_image, goal_rec))
    print("goal MSE:", mse(goal_image, goal_rec))
    # if image_diff(goal_image,goal_rec) > image_threshold:
    #     print("Goal state reconstruction failed!")
    #     sys.exit(4)
    if not np.all(p.validate_states(rec)):
        print("Init/Goal state reconstruction failed!")
        # sys.exit(3)
        print("But we continue anyways...")

    known_transisitons = np.loadtxt(sae.local("actions.csv"), dtype=np.int8)
    actions = oae.encode_action(known_transisitons, batch_size=1000).round()
    histogram = np.squeeze(actions.sum(axis=0, dtype=int))
    print(histogram)
    print(np.count_nonzero(histogram), "actions valid")
    print("valid actions:")
    print(np.where(histogram > 0)[0])
    identified, total = np.squeeze(histogram.sum()), len(actions)
    if total != identified:
        print("network does not explain all actions: only {} out of {} ({}%)".
              format(identified, total, identified * 100 // total))
    available_actions = np.zeros(
        (np.count_nonzero(histogram), actions.shape[1], actions.shape[2]),
        dtype=int)

    for i, pos in enumerate(np.where(histogram > 0)[0]):
        available_actions[i][0][pos] = 1

    # available_actions = available_actions.repeat(inflation,axis=0)

    for i, found_goal_state in enumerate(
            eval(searcher)().search(init, goal, goalcount)):
        plan = np.array(found_goal_state.path())
        print(plan)
        plot_grid(sae.decode_binary(plan),
                  path=problem(network(search("path_{}.png".format(i)))),
                  verbose=True)

        validation = p.validate_transitions(
            [sae.decode_binary(plan[0:-1]),
             sae.decode_binary(plan[1:])])
        print(validation)
        print(
            ad.discriminate(np.concatenate((plan[0:-1], plan[1:]),
                                           axis=-1)).flatten())

        print(p.validate_states(sae.decode_binary(plan)))
        print(combined_discriminator(plan).flatten())
        import subprocess
        subprocess.call(
            ["rm", "-f",
             problem(network(search("path_{}.valid".format(i))))])
        if np.all(validation):
            subprocess.call(
                ["touch",
                 problem(network(search("path_{}.valid".format(i))))])
            sys.exit(0)
            "************************* to_configs on gpu, batch=100 ***************************"
        )):
    p.to_configs(s, batch_size=100)

with Timer(
        style(
            "************************* to_configs on gpu, batch=1000 ***************************"
        )):
    p.to_configs(s, batch_size=1000)

from latplan.util import bce, mae
_c = p.to_configs(s, batch_size=1000).round().astype(int)
print("sum(abs(config - to_configs(generate(config)))) =",
      np.sum(np.abs(c - _c)))
print("MAE(config, to_configs(generate(config))) =", mae(c, _c))
print("BCE(config, to_configs(generate(config))) =", bce(c, _c))
for i in range(120, 125):
    print("original     :", c[i])
    print("reconstructed:", _c[i])

c = c[:10]

with Timer(
        style(
            "************************* transitions_old ***************************"
        )):
    transitions = p.transitions_old(4, configs=c)

with Timer(
        style(
            "************************* transitions ***************************"
Exemple #5
0
                widgets=[
                    pb.Timer("Elap: %(elapsed) "),
                    pb.AbsoluteETA("Est: %(elapsed) "),
                    pb.Bar(),
                ])
            for pre_state,suc_state,pre_image,suc_image in bar(zip(pre_states,suc_states,pre_images,suc_images)):
                
                generated_transitions = aae.decode([
                    np.repeat([pre_state],128,axis=0),
                    all_labels,
                ],batch_size=1000)
                generated_suc_states = generated_transitions[:,N:]
                generated_suc_images = ae.decode_binary(generated_suc_states,batch_size=1000)

                from latplan.util import bce
                errors = bce(generated_suc_images, np.repeat([suc_image],128,axis=0), axis=(1,2))
                min_error = np.amin(errors)
                if min_error < 0.01:
                    count += 1
        finally:
            print({"count": count, "total":len(all_actions)})
    
    actions = aae.encode_action(data, batch_size=1000)
    actions_r = actions.round()

    histogram = actions.sum(axis=0)
    print(histogram)
    histogram_r = actions_r.sum(axis=0,dtype=int)
    print(histogram_r)
    print (np.count_nonzero(histogram_r > 0))
        
Exemple #6
0
def main(network_dir, problem_dir):
    global sae, oae, ad, ad2, sd, sd2, sd3, cae, combined_discriminator, available_actions

    p = latplan.util.puzzle_module(network_dir)

    sae = default_networks[get_ae_type(network_dir)](network_dir).load(
        allow_failure=True)
    try:
        ad = PUDiscriminator(sae.local("_ad/")).load(allow_failure=True)
    except:
        ad = Discriminator(sae.local("_ad/")).load(allow_failure=True)
    # sd  = Discriminator(sae.local("_sd/")).load(allow_failure=True)
    # ad2 = Discriminator(sae.local("_ad2/")).load(allow_failure=True)
    # sd2 = Discriminator(sae.local("_sd2/")).load(allow_failure=True)
    sd3 = PUDiscriminator(sae.local("_sd3/")).load()
    discriminator = default_networks['CombinedDiscriminator2'](sae, sd3)

    def problem(path):
        return os.path.join(problem_dir, path)

    def network(path):
        root, ext = os.path.splitext(path)
        return "{}_{}{}".format(
            ensure_directory(network_dir).split("/")[-2], root, ext)

    def search(path):
        root, ext = os.path.splitext(path)
        return "{}_{}{}".format(searcher, root, ext)

    from scipy import misc
    from latplan.puzzles.util import preprocess, normalize
    # is already enhanced, equalized
    init_image = normalize(misc.imread(problem("init.png")))
    goal_image = normalize(misc.imread(problem("goal.png")))
    print(
        "init:",
        init_image.min(),
        init_image.max(),
    )
    print(
        "goal:",
        goal_image.min(),
        goal_image.max(),
    )
    init = sae.encode_binary(np.expand_dims(init_image,
                                            0))[0].round().astype(int)
    goal = sae.encode_binary(np.expand_dims(goal_image,
                                            0))[0].round().astype(int)
    print(init)
    print(goal)
    rec = sae.decode_binary(np.array([init, goal]))
    init_rec, goal_rec = rec
    print(
        "init (reconstruction):",
        init_rec.min(),
        init_rec.max(),
    )
    print(
        "goal (reconstruction):",
        goal_rec.min(),
        goal_rec.max(),
    )

    print("init BCE:", bce(init_image, init_rec))
    print("init MAE:", mae(init_image, init_rec))
    print("init MSE:", mse(init_image, init_rec))

    print("goal BCE:", bce(goal_image, goal_rec))
    print("goal MAE:", mae(goal_image, goal_rec))
    print("goal MSE:", mse(goal_image, goal_rec))
Exemple #7
0
 def prune_unreconstructable(sae, data):
     rec = regenerate(sae, data)
     loss = bce(data, rec, (1, ))
     return data[np.where(loss < threshold)[0]]