Esempio n. 1
0
 def __init__(self, net_dir, use_bn=True, is_static_batch_norm=False):
     super().__init__()
     sys.path.append(net_dir)
     import pznet
     # self.net = pznet.znet(model_file_name, net_file_name)
     self.net = pznet.znet()
     self.net.load_net(net_dir)
Esempio n. 2
0
 def __init__(self, net_dir):
     super().__init__(dtype='float32')
     sys.path.append(net_dir)
     import pznet
     # self.net = pznet.znet(model_file_name, net_file_name)
     self.net = pznet.znet()
     self.net.load_net(net_dir)
Esempio n. 3
0
def timing_test(base, N, recompile, architecture, core_options, cpu_offset, optimization, ignore, time_each, input_mode, run):
    test_name = list(filter(None, base.split('/')))[-1]

    net_path = os.path.join(base, "net.prototxt")
    weights_path = os.path.join(base, "weights.h5")
    input_path =  os.path.join(base, "in.h5")

    z = pznet.znet()
    znet_path = "/opt/znets/{}_{}cores_{}ht_{}off_{}".format(test_name, core_options["conv"][0], core_options["conv"][1], cpu_offset, optimization)
    lib_path  = "/opt/znets/lib"#os.path.join(znet_path, "lib")
    if recompile:
        print ("Recompiling...")
        z.create_net(net_path, weights_path, znet_path, architecture, core_options, cpu_offset,
                    opt_mode=optimization, ignore=ignore, time_each=time_each)

    if run:
        print ("Loading net...")
        z.load_net(znet_path, lib_path)

        in_shape = z.get_in_shape()
        if input_mode == 'read':
            in_file  = h5py.File(input_path)
            in_a     = in_file["main"][:]


        print ("Running net...")
        durations = []
        for i in range(N):
            if input_mode == 'random':
               in_a = np.random.random_sample(in_shape)
            elif input_mode == 'zero':
               in_a = np.zeroes(in_shape)
            elif input_mode == 'one':
               in_a = np.ones(in_shape)
            s = time()
            z.forward(in_a)
            e = time()
            durations.append(e-s)
        return sum(durations) / len(durations)
Esempio n. 4
0
input_file = 'data/inputs/input_{}_{}_{}_{}_{}_r.h5'.format(
    input_dim[0], input_dim[1], input_dim[2], input_dim[3], input_dim[4])

reference_file = 'data/reference/reference_{}_{}_{}_{}_{}_{}_x{}_{}_{}_o{}_r.h5'.format(
    layers_prefix, input_dim[0], input_dim[1], input_dim[2], input_dim[3],
    input_dim[4], kernel_dim[0], kernel_dim[1], kernel_dim[2], ofm)
net_path = os.path.join(base, net_file)
weights_path = os.path.join(base, weights_file)
input_path = os.path.join(base, input_file)
reference_path = os.path.join(base, reference_file)

#input_path  = sys.argv[1]
#output_path = sys.argv[2]

z = pznet.znet(net_path, weights_path)

in_file = h5py.File(input_path)
in_a = in_file["input"][:]
out_a = z.forward(in_a)
reference_file = h5py.File(reference_path)
reference_a = reference_file["data"][:]

diff_a = reference_a - out_a
error = ssq = np.sum(diff_a**2)

fd = diff_a.flatten()
fo = out_a.flatten()
fr = reference_a.flatten()
boo = np.argmax(fd)
if error > 0.1:
Esempio n. 5
0
if len(sys.argv) > 2:
    create = False

test_name = filter(None, base.split('/'))[-1]

net_path = os.path.join(base, "net.prototxt")
weights_path = os.path.join(base, "weights.h5")
input_path =  os.path.join(base, "in.h5")
reference_path =  os.path.join(base, "out.h5")

in_file  = h5py.File(input_path)
in_a     = in_file["main"][:]
znet_path = "/opt/znets/{}_{}cores".format(test_name, cores)
lib_path  = os.path.join(znet_path, "lib")
lib_path  = "/opt/znets/lib"
z = pznet.znet()
if create:
    print "Creating net..."
    z.create_net(net_path, weights_path, znet_path, architecture, cores, ht, cpu_offset)
#sys.exit(1)
print "Running net..."
z.load_net(znet_path, lib_path)

for i in range(1):
    out_a    = z.forward(in_a)

    reference_file = h5py.File(reference_path)
    reference_a = reference_file["main"][:]
    np.set_printoptions(precision=2)

    diff_a = reference_a - out_a
Esempio n. 6
0
def correctness_test(base, N, recompile, architecture, core_options,
                     cpu_offset, optimization):
    test_name = list(filter(None, base.split('/')))[-1]

    net_path = os.path.join(base, "net.prototxt")
    weights_path = os.path.join(base, "weights.h5")
    input_path = os.path.join(base, "in.h5")
    reference_path = os.path.join(base, "out.h5")

    z = pznet.znet()
    znet_path = "/opt/znets/{}_{}cores_{}".format(test_name,
                                                  core_options["conv"][0],
                                                  optimization)
    lib_path = "/opt/znets/lib"  #os.path.join(znet_path, "lib")
    if recompile:
        print("Recompiling...")
        z.create_net(net_path,
                     weights_path,
                     znet_path,
                     architecture,
                     core_options,
                     cpu_offset,
                     opt_mode=optimization)

    print("Loading net...")
    z.load_net(znet_path, lib_path)

    in_file = h5py.File(input_path)
    in_a = in_file["main"][:]

    print("Running net...")
    out_a = z.forward(in_a)

    reference_file = h5py.File(reference_path)
    reference_a = reference_file["main"][:]
    np.set_printoptions(precision=2)

    diff_a = reference_a - out_a
    rel_d = np.abs(diff_a) / (out_a + 0.0000000001)
    mask1 = diff_a > 1e-5
    mask2 = rel_d > 1e-5

    #fd = diff_a.flatten()
    #fo = out_a.flatten()
    fr = reference_a.flatten()
    #diffs0 = [np.sum(np.abs(diff_a[0][0][i])) for i in range(18)]
    #diffs1 = [np.sum(np.abs(diff_a[1][0][i])) for i in range(18)]
    errors = rel_d * mask1 * mask2 * reference_a
    error = np.sum((errors * 10)**2)

    max_d = np.max(np.abs(diff_a))
    #i = np.argmax(np.abs(diff_a.flatten()))
    max_rel_d = max_d / fr
    print("Max rel d: {}".format(max_rel_d))
    print("Max d: {}".format(max_d))
    print("Average d: {}".format(np.average(rel_d)))

    if np.isnan(error):
        print("Not congrats! Error == {}".format(error))
        import pdb
        pdb.set_trace()
    elif error > 0.010:
        print("Not congrats! Error == {}".format(error))
        import pdb
        pdb.set_trace()
    else:
        print("Congrats! All pass. Error == {}".format(error))