Exemple #1
0
from inversegraphics_generator.resnet50 import MultiResNet, ContrastiveLoss

EPOCHS = 40
BATCH = 64
LEARNING_RATE = 0.0001
SIZE = 1000
MARGIN = 2

exp = Experiment("[ig] cnn-siamese2")
exp.param("epoch", EPOCHS)
exp.param("size", SIZE)
exp.param("batch", BATCH)
exp.param("learning rate", LEARNING_RATE)

# ds = IqImgDataset("/data/lisa/data/iqtest/iqtest-dataset-ambient.h5", "train/labeled", max_size=SIZE)
ds = IqImgDataset(os.path.join(get_data_dir(), "test.h5"),
                  "train/labeled",
                  max_size=SIZE)
dl = DataLoader(ds, batch_size=BATCH, shuffle=True, num_workers=0)

model = MultiResNet(siamese=True)  #.cuda()
# Loss and optimizer
criterion_contrast = ContrastiveLoss(MARGIN)
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)

# Train the model
total_step = int(len(ds) / BATCH)
for epoch in range(EPOCHS):
    for i, (questions, answers) in enumerate(dl):
        # Forward pass
        outputs, internals = model(questions)  #.cuda()
Exemple #2
0
import os
import pickle
import numpy as np
from tqdm import tqdm

from inversegraphics_generator.obj_generator import ObjGenerator
from inversegraphics_generator.iqtest_objs import get_data_dir

OUT = os.path.join(get_data_dir(), "aggregated.pkl")

SNEK_LEN_1 = 11
GRID_SIZE_1 = 5
OUT_PATH_1 = os.path.join(
    get_data_dir(),
    "list-v2-snek_{}-grid_{}-final.pkl".format(SNEK_LEN_1, GRID_SIZE_1))

SNEK_LEN_2 = 13
GRID_SIZE_2 = 5
OUT_PATH_2 = os.path.join(
    get_data_dir(),
    "list-v2-snek_{}-grid_{}-final.pkl".format(SNEK_LEN_2, GRID_SIZE_2))

SNEK_LEN_3 = 14
GRID_SIZE_3 = 6
OUT_PATH_3 = os.path.join(
    get_data_dir(),
    "list-v2-snek_{}-grid_{}-final.pkl".format(SNEK_LEN_3, GRID_SIZE_3))

SNEK_LEN_4 = 15
GRID_SIZE_4 = 8
OUT_PATH_4 = os.path.join(
Exemple #3
0
import os
import matplotlib.pyplot as plt
from inversegraphics_generator.obj_generator import ObjGenerator
from inversegraphics_generator.iqtest_objs import get_data_dir

OUT_PATH = os.path.join(get_data_dir(), "object-{:06d}.obj")

# the size of the snake is randomly sampled
# from this interval
SNEK_LEN_MIN = 5
SNEK_LEN_MAX = 10

GRID_SIZE = 4  # the grid for the snake is N x N x N

CUBE_SIZE = 1.0  # cubes are unit-sized

### IMPORTANT
OBJECTS_TO_GENERATE = 1000

if __name__ == '__main__':
    og = ObjGenerator(GRID_SIZE, CUBE_SIZE)
    ## test with one cube
    # v, f = make_cube(CUBE_SIZE)
    # plot_cube(v, f)

    # test with one grid
    g = og.walk_snek(SNEK_LEN_MIN, SNEK_LEN_MAX)
    print(g)

    v, f = og.grid_to_cubes(g)
    og.write_obj(g, v, f, 0, OUT_PATH)
Exemple #4
0
    def __len__(self):
        return len(self.data["input"][:self.max_size])

    def __getitem__(self, idx):
        # out = {"input": self.data["input"][idx], "output": None}
        # if self.group in ["train/labeled", "test", "val"]:
        #     out["output"] = self.data["output"][idx]
        # return out

        answer = None
        if self.group in ["train/labeled", "test", "val"]:
            answer = np.zeros(3, dtype=np.float32)
            answer[self.data["output"][idx]] = 1  # one-hot encoding
            # print (self.data["output"][idx], answer)

        question = np.swapaxes(np.swapaxes(self.data["input"][idx], 1, 3), 2,
                               3)

        return (question, answer)


if __name__ == '__main__':
    ds = IqImgDataset(os.path.join(get_data_dir(), "test.h5"), "train/labeled")

    print(len(ds))

    question, answer = ds[0]

    print(question.shape)
    print(answer)
Exemple #5
0
import os

import matplotlib.pyplot as plt
from scipy.misc import imresize

from inversegraphics_generator.human_dataset import HumanDataset
from inversegraphics_generator.iqtest_objs import get_data_dir

ds = HumanDataset()

FACTOR = 400
OUT_PATH = os.path.join(get_data_dir(), "..", "3diqtt", "imgs")

corrects = []

for i in range(len(ds)):
    (question, answers, correct) = ds[i]
    q_scaled = imresize(question, FACTOR)
    plt.imsave(os.path.join(OUT_PATH,"{}-q.png".format(i)), q_scaled)

    for a in range(3):
        a_scaled = imresize(answers[a], FACTOR)
        plt.imsave(os.path.join(OUT_PATH,"{}-a{}.png".format(i,a)), a_scaled)

    corrects.append(str(correct))

with open(os.path.join(OUT_PATH,"_correct.csv"),"w") as f:
    f.writelines(corrects)


(question, answers, correct) = ds.get(1)
Exemple #6
0
import os

import h5py
import numpy as np

from inversegraphics_generator.iqtest_objs import get_data_dir

with h5py.File(os.path.join(get_data_dir(), "test.h5"), "w") as f:
    train = f.create_group("train")
    test = f.create_group("test")
    val = f.create_group("val")

    labeled = train.create_group("labeled")
    unlabeled = train.create_group("unlabeled")

    labeled_q = labeled.create_dataset("input",
                                       data=np.zeros((10, 4, 128, 128, 3),
                                                     dtype=np.float32))
    labeled_a = labeled.create_dataset("output",
                                       data=np.zeros(10),
                                       dtype=np.uint8)
Exemple #7
0
import pickle
import os
import matplotlib.pyplot as plt
from inversegraphics_generator.obj_generator import ObjGenerator
from inversegraphics_generator.iqtest_objs import get_data_dir

SNEK_LEN = 9
GRID_SIZE = 4
OUT_PATH = os.path.join(
    get_data_dir(), "tree-v2-snek_{}-grid_{}.pkl".format(SNEK_LEN, GRID_SIZE))
print("loading file:", OUT_PATH)

og = ObjGenerator(GRID_SIZE, 1.0)


def getLeafs(node):
    if len(node.children) == 0:
        return node
    else:
        out = [getLeafs(child) for child in node.children]
        out_flat = []
        for child in out:  # each layer removes some mess
            if type([]) == type(child):
                for child2 in child:
                    out_flat.append(child2)
            else:
                out_flat.append(child)
        return out_flat


root = pickle.load(open(OUT_PATH, "rb"))
Exemple #8
0
import random
import time

import numpy as np
import h5py
from tqdm import tqdm
from inversegraphics_generator.iqtest_objs import get_data_dir

OUT_PATH = get_data_dir() + "/3diqtt-v2-x2.h5"

f = h5py.File(OUT_PATH, 'r')

print(list(f.keys()))

q = f["train/labeled/questions"]

print(q.shape)
print(f["train/labeled/answers"].shape)
print(f["train/unlabeled/questions"].shape)

print(f["test/questions"].shape)

print(f["val/questions"].shape)
print(f["val/answers"].shape)

import matplotlib.pyplot as plt
idx = 0

for i in range(3):
    start = time.time()
    idx = random.randrange(0,len(q))
Exemple #9
0
import pickle
import os
from tqdm import tqdm

from inversegraphics_generator.obj_generator import ObjGenerator
from inversegraphics_generator.iqtest_objs import get_data_dir
import numpy as np

SNEK_LEN = 9
GRID_SIZE = 4
OUT_PATH = os.path.join(get_data_dir(),
                        "list-v2-snek_{}-grid_{}".format(SNEK_LEN, GRID_SIZE))

og = ObjGenerator(GRID_SIZE, 1.0)

final = []

with open(OUT_PATH + "-unique.pkl", "rb") as output_file:
    unique = pickle.load(output_file)

for u in tqdm(unique):
    # print (u)

    u_grid = og.str_to_grid(u)

    clean = True

    for i in range(3):
        tmp = og.grid_to_str(
            og.center_grid(np.rot90(u_grid, k=i + 1, axes=(0, 1))))
        if tmp in final:
Exemple #10
0
import numpy as np

from inversegraphics_generator.iqtest_objs import get_data_dir

ds = np.load(get_data_dir()+"/10000_baseline_data.npy")

print (ds.shape, ds.dtype, ds.max(), ds.min())
quit()

import matplotlib.pyplot as plt
for i in range(len(ds)):
    f, axarr = plt.subplots(1, 4, sharex=True, sharey=True, figsize=(20, 5))

    for img in range(4):
        img_data = ds[i,img]/255
        img_data = np.swapaxes(np.swapaxes(img_data,0,2),0,1)
        print(img_data.min(), img_data.max(), img_data.mean())

        axarr[img].imshow(img_data)

    plt.tight_layout()
    plt.show()
Exemple #11
0
import os
import matplotlib.pyplot as plt
import h5py
from inversegraphics_generator.iqtest_objs import get_data_dir
import numpy as np

OUT_PATH = os.path.join(get_data_dir(), "..", "3diqtt", "human-test.h5")

questions = []
answers = []
correct = []

folder = os.path.join(get_data_dir(), "..", "3diqtt")

for i in range(100):
    ref_file = os.path.join(folder, "sample-test-{}-ref.png".format(i))
    if os.path.isfile(ref_file):
        questions.append(plt.imread(ref_file)[:, :, :3])

        answers_buf = []
        for a in range(3):
            ans_file = os.path.join(folder, "sample-test-{}-ans{}".format(i, a + 1))

            if not os.path.isfile(ans_file + ".png"):
                correct.append(a)
                ans_file += "c"
            answers_buf.append(plt.imread(ans_file + ".png")[:, :, :3])
        answers.append(answers_buf)

from random import shuffle
Exemple #12
0
import pickle
import os
from tqdm import tqdm

from inversegraphics_generator.obj_generator import ObjGenerator
from inversegraphics_generator.iqtest_objs import get_data_dir

SNEK_LEN = 9
GRID_SIZE = 4
IN_PATH = os.path.join(
    get_data_dir(), "tree-v2-snek_{}-grid_{}.pkl".format(SNEK_LEN, GRID_SIZE))
print("loading file:", IN_PATH)
OUT_PATH = os.path.join(get_data_dir(),
                        "list-v2-snek_{}-grid_{}".format(SNEK_LEN, GRID_SIZE))

og = ObjGenerator(GRID_SIZE, 1.0)


def getLeafs(node):
    if len(node.children) == 0:
        return node
    else:
        out = [getLeafs(child) for child in node.children]
        out_flat = []
        for child in out:  # each layer removes some mess
            if type([]) == type(child):
                for child2 in child:
                    out_flat.append(child2)
            else:
                out_flat.append(child)
        return out_flat
Exemple #13
0
import os
import matplotlib.pyplot as plt
import numpy as np
from inversegraphics_generator.img_dataset import IqImgDataset
from inversegraphics_generator.iqtest_objs import get_data_dir

ds_train = IqImgDataset("/Users/florian/data/ig/iqtest-dataset-ambient.h5",
                        "train/labeled")
ds_test = IqImgDataset("/Users/florian/data/ig/iqtest-dataset-ambient.h5",
                       "test")

OUT = os.path.join(get_data_dir(), "sample-{traintest}-{idx}-{refans}.png")


def reshape(img):
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 0, 1)
    img *= 255
    img = np.clip(img, 0, 255)
    return img.astype(np.uint8)


def sample(ds, idx, traintest):
    plt.imsave(OUT.format(traintest=traintest, refans="ref", idx=idx),
               reshape(ds[idx][0][0]))

    ans_idx = ds[idx][1].argmax()
    for ans in range(3):
        label = "ans" + str(ans + 1)
        if ans == ans_idx:
            label += "c"
Exemple #14
0
import os
import numpy as np
import h5py
from torch.utils.data import Dataset
import matplotlib.pyplot as plt
from inversegraphics_generator.iqtest_objs import get_data_dir

default_path = os.path.join(get_data_dir(), "..", "3diqtt", "human-test.h5")


class HumanDataset(Dataset):
    def __init__(self, h5_path=default_path):
        file_handle = h5py.File(h5_path, 'r')
        self.references = file_handle["references"]
        self.answers = file_handle["answers"]
        self.correct = file_handle["correct"]
        self.tru_len = len(self.correct)

    def __len__(self):
        return 20

    def __getitem__(self, idx):
        if idx == 1:  # we are not using sample 1 because it's used for the tutorial
            idx = 20
        return (self.references[idx, :, :, :3], self.answers[idx, :, :, :, :3], self.correct[idx])

    def get(self, idx):
        return (self.references[idx, :, :, :3], self.answers[idx, :, :, :, :3], self.correct[idx])


if __name__ == '__main__':
Exemple #15
0
    def get_training_questions_answers(self, n=1):
        return self.sample_qa(self.train, 0, n)

    def get_testing_questions_answers(self, n=1):
        return self.sample_qa(self.test, 2, n)

    def get_validation_questions_answers(self, n=1):
        return self.sample_qa(self.val, 3, n)

    def cleanup(self):
        for tmp in self.paths:
            tmp.cleanup()


if __name__ == '__main__':
    # iq = IqDataset(os.path.expanduser("~/data/ig/iqtest-v1.npz"))
    iq = IqObjDataset(os.path.join(get_data_dir(), "iqtest-v1.npz"))
    print(iq.train.shape)
    print(iq.test.shape)
    print(iq.val.shape)

    print(iq.get_training_samples_unordered(5))
    print(iq.get_training_questions_answers(5))
    print(iq.get_testing_questions_answers(5))
    print(iq.get_validation_questions_answers(5))

    time.sleep(
        3000
    )  # use this time to do `ls` on the directories printed above or pull them into Blender
    iq.cleanup()