Ejemplo n.º 1
0
    def __init__ ( self, name, mapping, parent=None ) :
        portlist = dict( [("A%d"%i, gate.IN) for i in range (0,4)] +
                         [("B%d"%i, gate.IN) for i in range (0,4)] +
                         [("Bb%d"%i, gate.IN) for i in range (0,4)] +
                         [("S%d"%i, gate.IN) for i in range (0,4)] +
                         [("D%d"%i, gate.OUT) for i in range (0,4)])

        gate.module.__init__( self, name, portlist, mapping, parent)
        
        for i in range(0, 4):
            n = "BbS1%d" % i
            self.netlist[n] = net.net(n, parent=self)   
            n = "BS0%d" % i
            self.netlist[n] = net.net(n, parent=self)   


        self.gatelist.extend([
                AND("BbS10gate", [self.netlist["BbS10"], self.port["Bb0"].netconn, self.port["S1"].netconn], self),
                AND("BbS11gate", [self.netlist["BbS11"], self.port["Bb1"].netconn, self.port["S1"].netconn], self),
                AND("BbS12gate", [self.netlist["BbS12"], self.port["Bb2"].netconn, self.port["S1"].netconn], self),
                AND("BbS13gate", [self.netlist["BbS13"], self.port["Bb3"].netconn, self.port["S1"].netconn], self),

                AND("BS00gate", [self.netlist["BS00"], self.port["B0"].netconn, self.port["S0"].netconn], self),
                AND("BS01gate", [self.netlist["BS01"], self.port["B1"].netconn, self.port["S0"].netconn], self),
                AND("BS02gate", [self.netlist["BS02"], self.port["B2"].netconn, self.port["S0"].netconn], self),
                AND("BS03gate", [self.netlist["BS03"], self.port["B3"].netconn, self.port["S0"].netconn], self),

                NOR("D0gate", [self.port["D0"].netconn, self.netlist["BbS10"], self.netlist["BS00"], self.port["A0"].netconn], self),
                NOR("D1gate", [self.port["D1"].netconn, self.netlist["BbS11"], self.netlist["BS01"], self.port["A1"].netconn], self),
                NOR("D2gate", [self.port["D2"].netconn, self.netlist["BbS12"], self.netlist["BS02"], self.port["A2"].netconn], self),
                NOR("D3gate", [self.port["D3"].netconn, self.netlist["BbS13"], self.netlist["BS03"], self.port["A3"].netconn], self)])
Ejemplo n.º 2
0
    def __init__(self, name, mapping, parent=None):
        portlist = dict([("COUT", gate.OUT), ("CIN", gate.IN),
                         ("RSTB", gate.IN), ("PHI", gate.IN)])
        for i in range(0, 16):
            portlist["Q%d" % i] = gate.OUT

        gate.module.__init__(self, name, portlist, mapping, parent)

        self.netlist['c0'] = net.net('c0', parent=self)
        self.netlist['c1'] = net.net('c1', parent=self)
        self.netlist['c2'] = net.net('c2', parent=self)

        self.gatelist = [
            count4_nmos(
                "count4_nmos_0_u",
                dict([('Q0', self.port['Q0'].netconn),
                      ('Q1', self.port['Q1'].netconn),
                      ('Q2', self.port['Q2'].netconn),
                      ('Q3', self.port['Q3'].netconn),
                      ('PHI', self.port['PHI'].netconn),
                      ('RSTB', self.port['RSTB'].netconn),
                      ('CIN', self.port['CIN'].netconn),
                      ('COUT', self.netlist['c0'])]), self),
            count4_nmos(
                "count4_nmos_1_u",
                dict([('Q0', self.port['Q4'].netconn),
                      ('Q1', self.port['Q5'].netconn),
                      ('Q2', self.port['Q6'].netconn),
                      ('Q3', self.port['Q7'].netconn),
                      ('PHI', self.port['PHI'].netconn),
                      ('RSTB', self.port['RSTB'].netconn),
                      ('CIN', self.netlist['c0']),
                      ('COUT', self.netlist['c1'])]), self),
            count4_nmos(
                "count4_nmos_2_u",
                dict([('Q0', self.port['Q8'].netconn),
                      ('Q1', self.port['Q9'].netconn),
                      ('Q2', self.port['Q10'].netconn),
                      ('Q3', self.port['Q11'].netconn),
                      ('PHI', self.port['PHI'].netconn),
                      ('RSTB', self.port['RSTB'].netconn),
                      ('CIN', self.netlist['c1']),
                      ('COUT', self.netlist['c2'])]), self),
            count4_nmos(
                "count4_nmos_3_u",
                dict([('Q0', self.port['Q12'].netconn),
                      ('Q1', self.port['Q13'].netconn),
                      ('Q2', self.port['Q14'].netconn),
                      ('Q3', self.port['Q15'].netconn),
                      ('PHI', self.port['PHI'].netconn),
                      ('RSTB', self.port['RSTB'].netconn),
                      ('CIN', self.netlist['c2']),
                      ('COUT', self.port['COUT'].netconn)]), self)
        ]
Ejemplo n.º 3
0
    def __init__(self, name, mapping, parent=None):
        portlist = dict([('Q', gate.OUT), ('D', gate.IN), ('RSTB', gate.IN),
                         ('PHI', gate.IN)])
        gate.module.__init__(self,
                             name=name,
                             portlist=portlist,
                             mapping=mapping,
                             parent=parent)

        for i in ('phib', 'rst', 'n0'):
            self.netlist[i] = net.net(i, parent=self, pullup_str=100)
        self.netlist['n1'] = net.net('n1', parent=self, charge_storage=True)
        self.netlist['n2'] = net.net('n2', parent=self, charge_storage=True)
        self.port['Q'].netconn.pullup_str += 100

        self.gatelist.extend([
            # Inverters for the clock and reset signals
            UNMOS("NMOS_0_u", [
                self.netlist['phib'], self.netlist['vss'],
                self.port['PHI'].netconn
            ], self),
            UNMOS("NMOS_1_u", [
                self.netlist['rst'], self.netlist['vss'],
                self.port['RSTB'].netconn
            ], self),
            # 2 half latches
            UNMOS(
                "NMOS_2_u",
                [self.netlist['n0'], self.netlist['vss'], self.netlist['n1']],
                self),
            UNMOS("NMOS_3_u", [
                self.netlist['n1'], self.port['D'].netconn,
                self.netlist['phib']
            ], self),
            UNMOS("NMOS_4_u", [
                self.port['Q'].netconn, self.netlist['vss'], self.netlist['n2']
            ], self),
            UNMOS("NMOS_5_u", [
                self.netlist['n2'], self.netlist['n0'],
                self.port['PHI'].netconn
            ], self),
            # Reset first and second latches in opposite directions - must be strong drivers to win contention vs pass gates
            UNMOS(
                "NMOS_6_u",
                [self.netlist['n1'], self.netlist['vss'], self.netlist['rst']],
                self),
            UNMOS(
                "NMOS_7_u",
                [self.netlist['n2'], self.netlist['vdd'], self.netlist['rst']],
                self),
        ])
Ejemplo n.º 4
0
    def __init__ ( self, name, mapping, parent=None ) :
        portlist = dict( [("Gb%d"%i, gate.IN) for i in range (0,4)] +
                         [("Pb%d"%i, gate.IN) for i in range (0,4)] +
                         [("C%d"%i, gate.OUT) for i in range (0,4)] +
                         [("CNb", gate.IN),("CN4", gate.OUT),("X", gate.OUT),("Y", gate.OUT)] )
        gate.module.__init__( self, name, portlist, mapping, parent)

        for i in range(0, 4):
            n = "Pbuf%d" % i
            self.netlist[n] = net.net(n, parent=self)   

        for n in [ "CNbGb0","Pb0Gb1","CNbGb01", "Pb1Gb2", "Pb0Gb12", "Pb1Gb23", "Pb0Gb123", "CNbGb012", "Pb2Gb3", "XCNb"] :
            self.netlist[n] = net.net(n, parent=self)

        self.gatelist.extend([
                INV("C0gate", [self.port["C0"].netconn, self.port["CNb"].netconn], self),

                BUF("Pb0gate", [self.netlist['Pbuf0'], self.port["Pb0"].netconn], self),
                AND("CNbGb0gate", [self.netlist["CNbGb0"], self.port["CNb"].netconn, self.port["Gb0"].netconn], self),

                BUF("Pb1gate", [self.netlist['Pbuf1'], self.port["Pb1"].netconn], self),
                AND("Pb0Gb1gate", [self.netlist["Pb0Gb1"], self.port["Pb0"].netconn, self.port["Gb1"].netconn], self),
                AND("CNbGb01gate", [self.netlist["CNbGb01"], self.port["CNb"].netconn, self.port["Gb0"].netconn, self.port["Gb1"].netconn], self),

                BUF("Pb2gate", [self.netlist['Pbuf2'], self.port["Pb2"].netconn], self),
                AND("Pb1Gb2gate", [self.netlist["Pb1Gb2"], self.port["Pb1"].netconn, self.port["Gb2"].netconn], self),
                AND("Pb0Gb12gate", [self.netlist["Pb0Gb12"], self.port["Pb0"].netconn, self.port["Gb1"].netconn, self.port["Gb2"].netconn], self),
                AND("CNbGb012gate", [self.netlist["CNbGb012"], self.port["CNb"].netconn, self.port["Gb0"].netconn, self.port["Gb1"].netconn, self.port["Gb2"].netconn], self),

                BUF("Pb3gate", [self.netlist['Pbuf3'], self.port["Pb3"].netconn], self),
                AND("Pb2Gb3gate", [self.netlist["Pb2Gb3"], self.port["Pb2"].netconn, self.port["Gb3"].netconn], self),
                AND("Pb1Gb23gate", [self.netlist["Pb1Gb23"], self.port["Pb1"].netconn, self.port["Gb2"].netconn, self.port["Gb3"].netconn], self),
                AND("Pb0Gb123gate", [self.netlist["Pb0Gb123"], self.port["Pb0"].netconn, self.port["Gb1"].netconn, self.port["Gb2"].netconn, self.port["Gb3"].netconn], self),


                NAND("Xgate", [self.port["X"].netconn, self.port["Gb0"].netconn, self.port["Gb1"].netconn, self.port["Gb2"].netconn, self.port["Gb3"].netconn], self),

                NOR("Ygate", [ self.port["Y"].netconn, self.netlist["Pbuf3"],self.netlist["Pb2Gb3"],self.netlist["Pb1Gb23"],self.netlist["Pb0Gb123"]], self),

                NAND("XCNbgate", [self.netlist["XCNb"], self.port["Gb0"].netconn, self.port["Gb1"].netconn, self.port["Gb2"].netconn, self.port["Gb3"].netconn, self.port["CNb"].netconn], self),

                AND("CN4gate", [self.port["CN4"].netconn, self.port["Y"].netconn, self.netlist["XCNb"]], self),

                NOR("C3gate", [self.port["C3"].netconn, self.netlist["Pbuf2"], self.netlist["Pb1Gb2"], self.netlist["Pb0Gb12"], self.netlist["CNbGb012"]], self),

                NOR("C2gate", [self.port["C2"].netconn, self.netlist["Pbuf1"], self.netlist["Pb0Gb1"], self.netlist["CNbGb01"]], self),

                NOR("C1gate", [self.port["C1"].netconn, self.netlist["Pbuf0"], self.netlist["CNbGb0"]], self)])
Ejemplo n.º 5
0
def test(net, testloader, cost, use_cuda):
    net.eval()
    test_loss = 0.0
    correct = 0.0
    total = 0
    # temp = []
    for data in testloader:
        x_test, y_test = data
        if use_cuda:
            x_test, y_test = x_test.cuda(), y_test.cuda()
        x_test, y_test = Variable(x_test), Variable(y_test)
        output = net(x_test)
        # if use_cuda:
        #     temp.append(output.data.cpu().numpy())
        # else:
        #     temp.append(output.data.numpy())
        test_loss += cost(output, y_test).data[0]
        if use_cuda:
            _, pred = torch.max(
                output.data.cpu(),
                1)  # pred: get the index of the max probability
            correct += pred.eq(y_test.data.cpu().view_as(pred)).sum()
        else:
            _, pred = torch.max(output.data, 1)
            correct += pred.eq(y_test.data.view_as(pred)).sum()

        total += y_test.size(0)

    Loss = test_loss / len(testloader)
    acc = 100 * correct / total
    print("Loss {}, Acc {}".format(Loss, acc))
    print("-" * 10)
    return Loss, acc
Ejemplo n.º 6
0
def parse_file(filename):
    # open the file
    inputfile = open(filename)

    # parse the first line
    first_line = inputfile.readline()
    first_line = first_line.split()

    num_cells = int(first_line[0])
    num_connections = int(first_line[1])
    num_rows = int(first_line[2])
    num_cols = int(first_line[3])

    # parse the nets
    list_of_nets = []
    for i in range(num_connections):
        cur_net_unparsed = inputfile.readline().split()

        cur_net = net.net(int(cur_net_unparsed[1]),
                          int(cur_net_unparsed[0]) - 1)

        for j in range(2, int(cur_net_unparsed[0]) + 1):
            cur_net.add_connection(int(cur_net_unparsed[j]))

        # error checking, did we parse correctly?
        # print(len(cur_net.connections), cur_net.num_connections)
        assert (len(cur_net.connections) == cur_net.num_connections)
        list_of_nets.append(cur_net)

    # error checking
    # print(len(list_of_nets), num_cells)
    assert (len(list_of_nets) == num_connections)

    # returned the parsed information
    return [num_cells, num_connections, num_rows, num_cols, list_of_nets]
Ejemplo n.º 7
0
def main():
    key_word = 'testdata' + '/'
    file_path1 = '/home/ruslan/prac/planogramm/' + key_word
    for name in os.listdir(file_path1):
        if name == 'else':
            continue
        print(name)
        image = cv2.imread(key_word + name)
        img = imread(key_word + name)
        saved = img.copy()

        image, image1, flag = first_try_to_detect(image, saved, name)

        if flag == 0:
            image, image1 = second_try_to_detect(img, saved, name)

        images = array(image)
        images1 = array(image1)

        # Подаем матрицу нейросети
        list_of_list_of_class, list_of_list_of_proba = net.net(images, mode=1)

        for ind in range(len(list_of_list_of_class)):
            list_of_proba = list_of_list_of_proba[ind]
            list_of_class = list_of_list_of_class[ind]
            print_class(list_of_class)
            maximum(list_of_proba, list_of_class)
Ejemplo n.º 8
0
    def __init__(self, name, mapping, parent=None):
        portlist = dict([('OUT', gate.OUT), ('I0', gate.IN), ('I1', gate.IN),
                         ('I2', gate.IN)])
        gate.module.__init__(self,
                             name=name,
                             portlist=portlist,
                             mapping=mapping,
                             parent=parent)

        for i in ('n0', 'n1', 'n2'):
            self.netlist[i] = net.net(i, parent=self)
        # Add depln mode pullups
        self.netlist['n0'].pullup_str = 100
        self.port['OUT'].netconn.pullup_str = 100

        self.gatelist.extend([
            NMOS("NMOS_0_u", [
                self.netlist['n0'], self.netlist['n1'], self.port['I0'].netconn
            ], self),
            NMOS("NMOS_1_u", [
                self.netlist['n1'], self.netlist['n2'], self.port['I1'].netconn
            ], self),
            NMOS("NMOS_2_u", [
                self.netlist['n2'], self.netlist['vss'],
                self.port['I2'].netconn
            ], self),
            NMOS("NMOS_3_u", [
                self.port['OUT'].netconn, self.netlist['vss'],
                self.netlist['n0']
            ], self),
        ])
Ejemplo n.º 9
0
def test_random_U(net, data_size, batch_size, channel, dim, use_cuda):
    net.eval()
    print("-" * 10)
    print("test process")
    print("-" * 10)

    result = {}
    if data_size % batch_size == 0:
        time = data_size // batch_size
    else:
        time = data_size // batch_size + 1

    for i in range(time):
        print('time {}/{}'.format(i + 1, time))
        if i == time - 1:
            x_test = random_data.get_U_random_data(channel=channel, size=data_size - (time - 1) * batch_size, dim=dim)
        else:
            x_test = random_data.get_U_random_data(channel=channel, size=batch_size, dim=dim)

        if use_cuda:
            x_test = x_test.cuda()
        x_test = Variable(x_test)
        output = net(x_test)
        _, pred = torch.max(output.data, 1)
        pred = pred.view(-1)
        for j in pred:
            if j in result.keys():
                result[j] += 1
            else:
                result[j] = 0
    return result
Ejemplo n.º 10
0
def test_net(test_loader=None, path=None, batch_size=128):
    #run test loop here

    n_batches = len(test_loader)
    model = torch.load(path)
    net = model['model']
    net.load_state_dict(model['state_dict'])
    for par in net.parameters():
        par.requires_grad = False
    net.eval()

    #writing results to spreadsheet

    fname = "test_pred.csv"
    f_out = open(fname, "w")
    wrt = csv.writer(f_out)
    net = net.cpu()
    #testing metrics
    corr_cnt = 0
    total_iter = 0
    for data in test_loader:
        [inputs, labels, snr] = data
        snr = snr.numpy()
        #inputs, labels,snr = Variable(inputs), Variable(labels), Variable(snr)
        pred = net(inputs.float()).numpy()
        pred = np.argmax(pred, axis=1)
        labels = np.argmax(labels.numpy(), axis=1)
        for s, p, l in zip(snr, pred, labels):
            if (p == l):
                corr_cnt += 1
                wrt.writerow([s, p, l])
            total_iter += 1
    print("Test done, accr = :" + str(corr_cnt / total_iter))
    f_out.close()
Ejemplo n.º 11
0
def train(net, dataloader, cost, optimizer, epoch, n_epochs, use_cuda):
    # the model of training
    net.train()
    running_loss = 0.0
    print("-" * 10)
    print('Epoch {}/{}'.format(epoch, n_epochs))
    num = 0
    for item in dataloader:
        num += len(item)
        for data in item:
            x_train, y_train = data
            if use_cuda:
                x_train, y_train = x_train.cuda(), y_train.cuda()

            # change data to Variable, a wrapper of Tensor
            x_train, y_train = Variable(x_train), Variable(y_train)

            # zero the parameter gradients
            optimizer.zero_grad()

            outputs = net(x_train)
            loss = cost(outputs, y_train)
            loss.backward()
            # optimize the weight of this net
            optimizer.step()

            running_loss += loss.data[0]

    print("Loss {}".format(running_loss / len(num)))
    print("-" * 10)
Ejemplo n.º 12
0
    def __init__ ( self, name, mapping, parent=None ) :
        portlist = dict( [("E%d"%i, gate.IN) for i in range (0,4)] +
                         [("D%d"%i, gate.IN) for i in range (0,4)] +
                         [("C%d"%i, gate.IN) for i in range (0,4)] +
                         [("F%d"%i, gate.OUT) for i in range (0,4)] +
                         [("AEB", gate.OUT), ("M", gate.IN)] )
        gate.module.__init__( self, name, portlist, mapping, parent)

        for name in [ 'EXD', 'CM' ]:
            for i in range(0, 4):
                n = "%s%d" % (name,i)
                self.netlist[n] = net.net(n, parent=self)   

        self.gatelist.extend([
                XOR("EXD0gate", [self.netlist['EXD0'], self.port['E0'].netconn, self.port['D0'].netconn], self),
                XOR("EXD1gate", [self.netlist['EXD1'], self.port['E1'].netconn, self.port['D1'].netconn], self),
                XOR("EXD2gate", [self.netlist['EXD2'], self.port['E2'].netconn, self.port['D2'].netconn], self),
                XOR("EXD3gate", [self.netlist['EXD3'], self.port['E3'].netconn, self.port['D3'].netconn], self),
                
                OR("CM0gate", [self.netlist['CM0'], self.port['C0'].netconn, self.port['M'].netconn], self),
                OR("CM1gate", [self.netlist['CM1'], self.port['C1'].netconn, self.port['M'].netconn], self),
                OR("CM2gate", [self.netlist['CM2'], self.port['C2'].netconn, self.port['M'].netconn], self),
                OR("CM3gate", [self.netlist['CM3'], self.port['C3'].netconn, self.port['M'].netconn], self),

                XOR("F0gate", [self.port['F0'].netconn, self.netlist['EXD0'], self.netlist['CM0']], self),
                XOR("F1gate", [self.port['F1'].netconn, self.netlist['EXD1'], self.netlist['CM1']], self),
                XOR("F2gate", [self.port['F2'].netconn, self.netlist['EXD2'], self.netlist['CM2']], self),
                XOR("F3gate", [self.port['F3'].netconn, self.netlist['EXD3'], self.netlist['CM3']], self),

                AND("AEBgate", [self.port["AEB"].netconn, self.port["F0"].netconn, self.port["F1"].netconn, self.port["F2"].netconn, self.port["F3"].netconn], self)])
Ejemplo n.º 13
0
    def __init__(self, name, mapping, parent=None):
        portlist = dict([("Q0", gate.OUT), ("Q1", gate.OUT), ("RSTB", gate.IN),
                         ("PHI", gate.IN)])
        gate.module.__init__(self,
                             name=name,
                             portlist=portlist,
                             mapping=mapping,
                             parent=parent)

        for n in ['d0', 'd1', 'q0b', 'q1b', 'd1a', 'd1b']:
            self.netlist[n] = net.net(n, parent=self)

        for i in range(0, 2):
            n = "n%d" % i
            self.netlist[n] = net.net(n, parent=self)

        self.gatelist.extend([
            NMOS_DFF(
                "dff0_u",
                dict([('Q', self.port['Q0'].netconn),
                      ('D', self.netlist['d0']),
                      ('RSTB', self.port['RSTB'].netconn),
                      ('PHI', self.port['PHI'].netconn)]), self),
            NMOS_INV(
                "inv6_u",
                dict([('OUT', self.netlist['d0']),
                      ('IN', self.port['Q0'].netconn)]), self),
            DFF("dff1_u", [
                self.port['Q1'].netconn, self.netlist['d1'],
                self.port['RSTB'].netconn, self.port['PHI'].netconn
            ], self),
            INV("inv4_u", [self.netlist['q0b'], self.port['Q0'].netconn],
                self),
            INV("inv5_u", [self.netlist['q1b'], self.port['Q1'].netconn],
                self),
            AND("and7_u", [
                self.netlist['d1a'], self.port['Q0'].netconn,
                self.netlist['q1b']
            ], self),
            AND("and8_u", [
                self.netlist['d1b'], self.port['Q1'].netconn,
                self.netlist['q0b']
            ], self),
            OR("or9_u",
               [self.netlist['d1'], self.netlist['d1a'], self.netlist['d1b']],
               self)
        ])
Ejemplo n.º 14
0
def wire(net_names, pullup_str=0, parent=None, charge_storage=False) :
    '''
    convenience function for declaring nets in a gate object
    '''
    newnets = dict()    
    for name in net_names:
        newnets[name] = net.net(name, pullup_str=pullup_str, parent=parent, charge_storage=False)
    return newnets
Ejemplo n.º 15
0
def train_net():
    # 参数定义
    lr = 1e-1  # 学习率
    epoches = 55  # 迭代次数
    device = torch.device(
        'cuda') if torch.cuda.is_available() else torch.device('cpu')
    model = net().to(device)
    criterion = nn.CrossEntropyLoss()  # 目标函数
    optimizer = optim.SGD(model.parameters(), lr=lr)  # 定义优化器种类,使用随机梯度下降
    data = dataPreparation()
    train_loader = data.train_loader  # 设置训练数据
    test_loader = data.test_loader  # 设置测试数据

    checkpoints = './checkpoints/net/'
    writer = SummaryWriter()

    accuracy_best = 0

    for epoch in range(epoches):
        print("第%d轮训练" % (epoch + 1))
        for i, (images, labels) in enumerate(tqdm(train_loader, ncols=50)):
            images = images.to(device)
            labels = labels.to(device)
            images = Variable(images.view(-1, 28 * 28))
            labels = Variable(labels)

            outputs = model(images)  # 计算模型输出结果
            loss = criterion(outputs, labels)  # 计算误差loss
            optimizer.zero_grad()  # 在反向传播之前清除网络状态
            loss.backward()  # loss反向传播
            optimizer.step()  # 更新参数

        print("epoch_" + str(epoch) + "_loss:", loss.item())
        writer.add_scalar('Train_loss', loss,
                          epoch)  #第一个参数为图形名称,第二个参数为y值,第三个为x值

        model.eval()
        with torch.no_grad():
            total = 0
            correct = 0
            for test_images, test_labels in test_loader:
                test_images = test_images.to(device)
                test_labels = test_labels.to(device)
                test_images = Variable(test_images.view(-1, 28 * 28))
                test_outputs = model(test_images)
                _, predicts = torch.max(test_outputs.data, 1)
                total += test_labels.size(0)
                correct += (predicts == test_labels).sum().numpy()
                accuracy = correct / total
                if accuracy > accuracy_best:
                    accuracy_best = accuracy
                    torch.save(model.state_dict(),
                               os.path.join(checkpoints, 'best.pth'))
            writer.add_scalar("accuracy", accuracy, epoch)
            print("accuracy_best:", accuracy_best, '\n')
Ejemplo n.º 16
0
    def __init__(self, name, mapping, parent=None):
        portlist = dict([('OUT', gate.OUT), ('I0', gate.IN), ('I1', gate.IN)])
        gate.module.__init__(self,
                             name=name,
                             portlist=portlist,
                             mapping=mapping,
                             parent=parent)

        # Add depln mode pullups
        self.port['OUT'].netconn.pullup_str = 100
        self.netlist['i0b'] = net.net('i0b', parent=self, pullup_str=100)
        self.netlist['i1b'] = net.net('i1b', parent=self, pullup_str=100)
        self.netlist['n0'] = net.net('n0', parent=self)
        self.netlist['n1'] = net.net('n1', parent=self)

        self.gatelist.extend([
            UNMOS("NMOS_0_u", [
                self.netlist['i0b'], self.netlist['vss'],
                self.port['I0'].netconn
            ], self),
            UNMOS("NMOS_1_u", [
                self.netlist['i1b'], self.netlist['vss'],
                self.port['I1'].netconn
            ], self),
            UNMOS("NMOS_2_u", [
                self.port['OUT'].netconn, self.netlist['n0'],
                self.netlist['i0b']
            ], self),
            UNMOS(
                "NMOS_3_u",
                [self.netlist['n0'], self.netlist['vss'], self.netlist['i1b']],
                self),
            UNMOS("NMOS_4_u", [
                self.port['OUT'].netconn, self.netlist['n1'],
                self.port['I0'].netconn
            ], self),
            UNMOS("NMOS_5_u", [
                self.netlist['n1'], self.netlist['vss'],
                self.port['I1'].netconn
            ], self),
        ])
Ejemplo n.º 17
0
def test():
    weight_root = './checkpoints/net/best.pth'
    device = torch.device(
        'cuda') if torch.cuda.is_available() else torch.device('cpu')
    model = net().to(device)
    model.load_state_dict(torch.load(weight_root))
    data = dataPreparation()
    test_loader = data.test_loader_final
    # test_loader=data.test_loader
    num = 0
    error = 0
    right = 0
    # print(test_loader.dataset.data.shape)
    for i, (test_images, test_labels) in enumerate(test_loader):
        # test_images = test_images[i].to(device)
        # test_labels = test_labels[i].to(device)
        test_images = test_images.to(device)
        test_labels = test_labels.to(device)
        test_images = Variable(test_images.view(-1, 28 * 28))
        test_outputs = model(test_images)
        _, predicts = torch.max(test_outputs.data, 1)

        # print("truth:", test_labels, '\t', "predict:", predicts, '\n')
        if test_labels == predicts:
            num += 1

        # 显示错误50张图片
        if error < 50 and test_labels != predicts:
            plt.figure("error")
            plt.subplot(5, 10, error + 1)
            img = test_images.numpy().reshape(28, 28)
            plt.imshow(img, cmap=CM.gray)
            plt.xticks([])
            plt.yticks([])
            plt.xlabel("truth:" + str(test_labels.numpy()) + "  predict:" +
                       str(predicts.numpy()))
            error += 1

        # 显示正确50张图片
        if right < 50 and test_labels == predicts:
            plt.figure("right")
            plt.subplot(5, 10, right + 1)
            img = test_images.numpy().reshape(28, 28)
            plt.imshow(img, cmap=CM.gray)
            plt.xticks([])
            plt.yticks([])
            plt.xlabel("truth:" + str(test_labels.numpy()) + "  predict:" +
                       str(predicts.numpy()))
            right += 1

    accuracy = num / 10000
    print("accuracy:", accuracy)
    plt.show()
def main():
	#set hyperparameter at here 
	hiddenlayerlist = [[18, 20, 25, 16, 15]]	#change the number of hidden layer, and nodes in the layer
	
	ss = 1e-4			#step Size
	numofiter = 20000	#iterations
	size = 200			#input size
	dim = 2				#input dimension
	margin = 0			#change Margin at here, change this value to 0 to make the data not linear separable
	
	#generate the input and output
	inputdata, outputdata = generatedata(size, dim, margin)
	
	#plot to viaualize if it is 1D
	print("Training Data Plot: ")
	plt.figure(1)
	if dim == 1:
		
		plt.scatter(inputdata[: size // 2, 0],np.ones((size // 2, 1)), color="r")
		plt.scatter(inputdata[size // 2 :, 0],np.ones((size // 2, 1)), color="b")
		plt.legend(['Label 1', 'Label 0'], loc='upper right')
	elif dim == 2:
		
		plt.scatter(inputdata[: size // 2, 0],inputdata[: size // 2, 1], color="r")
		plt.scatter(inputdata[size // 2 :, 0],inputdata[size // 2 :, 1], color="b")
		plt.legend(['Label 1', 'Label 0'], loc='upper right')
	
	network = net(inputdata, outputdata, size, ss, numofiter, dim, hiddenlayerlist)
	network.backpropagation()
	output = network.forwardewithcomputedW(inputdata)
	
	#plot network computed result
	output = np.append(inputdata,output, axis=1)
	print("Network computed output: ")
	
	plt.figure(4)
	if dim ==1:
		
		output1 = output[output[:, -1] == 1]
		output2 = output[output[:, -1] == 0]
		plt.scatter(output1[:, 0],np.ones((np.shape(output1)[0], 1)), color="r")
		plt.scatter(output2[:, 0],np.ones((np.shape(output2)[0], 1)), color="b")
		plt.legend(['Label 1', 'Label 0'], loc='upper right')
		
	if dim ==2:
		output1 = output[output[:, -1] == 1]
		output2 = output[output[:, -1] == 0]
		plt.scatter(output1[:, 0], output1[:, 1], color="r")
		plt.scatter(output2[:, 0], output2[:, 1], color="b")
		plt.legend(['Label 1', 'Label 0'], loc='upper right')
	
	plt.show()
Ejemplo n.º 19
0
    def __init__(self, options):
        testbench.__init__(self, options)

        for name in ['in0', 'in1', 'in2', 'en0', 'en1', 'en2', 'n0', 'n1']:
            self.netlist[name] = net.net(name)

        dut0 = BUFIF1(
            "bufif1_0_u",
            [self.netlist['n0'], self.netlist['in0'], self.netlist['en0']],
            strength=net.SUPPLY_STR)
        dut1 = BUFIF1(
            "bufif1_1_u",
            [self.netlist['n0'], self.netlist['in1'], self.netlist['en1']],
            strength=net.SUPPLY_STR)
        dut2 = BUFIF1(
            "bufif1_2_u",
            [self.netlist['n0'], self.netlist['in2'], self.netlist['en2']],
            strength=net.SUPPLY_STR)

        dut3 = BUF("buf_0_u", [self.netlist['n1'], self.netlist['n0']])

        self.gatelist = [dut0, dut1, dut2, dut3]

        vector_string = '''
PI in0
PI in1
PI in2
PI en0
PI en1
PI en2
PO n0
PO n1

 000   000 XX
 000   001 00
 000   010 00
 000   011 00
 000   100 00
 000   101 00
 000   110 00
 000   111 00
 100   000 XX
 100   001 00
 100   010 00
 100   011 00
 100   100 11
 100   101 XX
 100   110 XX
 100   111 00
'''

        self.events = self.read_flex_string(vector_string)
Ejemplo n.º 20
0
def predict(net, batch_size, test_set):
    test_predictions = []
    for start_index in range(0, test_set.shape[0] - batch_size + 1,
                             batch_size):
        #get batches
        X = test_set[start_index:start_index + batch_size, :-5].view(
            batch_size, 1, 285)
        y = test_set[start_index:start_index + batch_size, -5:]
        #forward pass
        outputs = net(X).view(batch_size, 5).detach()
        test_predictions.append(outputs)

    return torch.cat(test_predictions, dim=0).numpy()
Ejemplo n.º 21
0
def correct_photo():
    files = flask.request.files
    images = []

    mimetype = 'application/zip'

    uniq_name = str(uuid.uuid4())
    zf = zipfile.ZipFile(f'{uniq_name}_images.zip', 'w')

    for name, value in files.items():
        ext = str(name).split('.')[-1]
        if ext.lower() in ['jpg', 'jpeg', 'png']:
            value.save(name)
            images.append((name, ))
            processed_img = net.net(name)
            imageio.imwrite(name, processed_img)
            zf.write(name, name)
            logger.info(f'removing tmp: {name}')
            os.remove(name)
        elif ext.lower() in ['arw', 'dng']:
            value.save(name)
            images.append((name,))
            processed_img = net.net(name)
            name = name.split('.')[0] + '.png'
            imageio.imwrite(name, processed_img)
            zf.write(name, name)
            logger.info(f'removing tmp: {name}')
            os.remove(name)
            os.remove(name.split('.')[0] + '.' + ext)

    logger.info(f'processing {len(images)} images')
    zf.close()

    if not is_empty(images):
        return Response(json.loads(json.dumps('{"filename": "' + uniq_name + '"}')), status=200, mimetype=mimetype)
    else:  # error, return error code 400
        return Response(json.dumps('{msg: "no images", code: 400}'), status=400, mimetype='application/json')
Ejemplo n.º 22
0
    def __init__ ( self, name, mapping, parent=None ) :
        portlist = dict( [("A%d"%i, gate.IN) for i in range (0,4)] +
                         [("B%d"%i, gate.IN) for i in range (0,4)] +
                         [("S%d"%i, gate.IN) for i in range (0,4)] +
                         [("E%d"%i, gate.OUT) for i in range (0,4)] +
                         [("Bb%d"%i, gate.OUT) for i in range (0,4)])

        gate.module.__init__( self, name, portlist, mapping, parent)
        
        for i in range(0, 4):
            n = "ABS3%d" % i
            self.netlist[n] = net.net(n, parent=self)   
            n = "ABbS2%d" % i
            self.netlist[n] = net.net(n, parent=self)   


        self.gatelist = [
                INV("Bb0gate",[self.port["Bb0"].netconn, self.port["B0"].netconn], self),
                INV("Bb1gate",[self.port["Bb1"].netconn, self.port["B1"].netconn], self),
                INV("Bb2gate",[self.port["Bb2"].netconn, self.port["B2"].netconn], self),
                INV("Bb3gate",[self.port["Bb3"].netconn, self.port["B3"].netconn], self),
                
                AND("ABS30gate",[self.netlist["ABS30"], self.port["A0"].netconn, self.port["B0"].netconn, self.port["S3"].netconn], self),
                AND("ABS31gate",[self.netlist["ABS31"], self.port["A1"].netconn, self.port["B1"].netconn, self.port["S3"].netconn], self),
                AND("ABS32gate",[self.netlist["ABS32"], self.port["A2"].netconn, self.port["B2"].netconn, self.port["S3"].netconn], self),
                AND("ABS33gate",[self.netlist["ABS33"], self.port["A3"].netconn, self.port["B3"].netconn, self.port["S3"].netconn], self),
                
                AND("ABbS20gate", [self.netlist["ABbS20"], self.port["A0"].netconn, self.port["Bb0"].netconn, self.port["S2"].netconn], self),
                AND("ABbS21gate", [self.netlist["ABbS21"], self.port["A1"].netconn, self.port["Bb1"].netconn, self.port["S2"].netconn], self),
                AND("ABbS22gate", [self.netlist["ABbS22"], self.port["A2"].netconn, self.port["Bb2"].netconn, self.port["S2"].netconn], self),
                AND("ABbS23gate", [self.netlist["ABbS23"], self.port["A3"].netconn, self.port["Bb3"].netconn, self.port["S2"].netconn], self),
                
                NOR("E0gate", [self.port["E0"].netconn, self.netlist["ABS30"], self.netlist["ABbS20"]], self),
                NOR("E1gate", [self.port["E1"].netconn, self.netlist["ABS31"], self.netlist["ABbS21"]], self),
                NOR("E2gate", [self.port["E2"].netconn, self.netlist["ABS32"], self.netlist["ABbS22"]], self),
                NOR("E3gate", [self.port["E3"].netconn, self.netlist["ABS33"], self.netlist["ABbS23"]], self)]
Ejemplo n.º 23
0
    def __init__(self,
                 n=1,
                 num=2,
                 lr=1e-5,
                 report=10,
                 batch_size=16,
                 img_side=2):

        # omino type
        self.n = n
        # number of objects
        self.num = num
        # learning rate
        self.lr = lr
        # report steps
        self.report = report
        # batch size
        self.batch = batch_size
        # image size
        self.img_side = img_side

        self.types = 2

        # training steps
        self.steps = 5000

        # iterate steps
        self.episodes = 50

        # start to build the graph

        tf.reset_default_graph()
        # initialize a network
        self.network = net()

        # training placeholders
        self.input = tf.placeholder(tf.float32,
                                    [None, self.img_side, self.img_side, 1])
        self.target = tf.placeholder(tf.float32,
                                     [None, self.types, self.img_side**2])

        # save map generated
        self.maps = []
        # one test image could be used
        self.test_img = np.reshape(np.concatenate(
            [np.zeros((1, 2)), np.ones((1, 2))], axis=0),
                                   newshape=[1, 2, 2, 1])
Ejemplo n.º 24
0
 def init(self):
     # load the config file
     self.nettester_config = config()
     # initialize the gui
     self.nettester_gui = gui(
         self.nettester_config.config["resolution"],
         self.nettester_config.config["fg_color"],
         self.nettester_config.config["bg_color"],
         self.nettester_config.config["font_size_correction"],
         self.nettester_config.config["show_mouse_cursor"],
     )
     # initialize the networking
     self.nettester_net = net()
     # create the buttons
     self.create_buttons()
     self.switch_to_wired()
     return True
Ejemplo n.º 25
0
    def __init__ ( self,  name, portlist, mapping,  parent=None, strength=1, pullup_str=None, pulldown_str=None,is_a_primitive=False): 
        self.name = name
        self.port = dict()
        self.parent = parent
        self.outputpins = dict()
        
        if not pulldown_str:
            self.pulldown_str = strength
        else:
            self.pulldown_str = pulldown_str
        if not pullup_str:
            self.pullup_str = strength
        else:
            self.pullup_str = pullup_str
        self.strength = min( [self.pulldown_str, self.pullup_str] )
            
        self.is_a_primitive = is_a_primitive
        # Map each port to a simulation net and update the simulation
        # net's fanin and fanout lists appropriately
        for portname in mapping:
            n = mapping[portname]
            if portname in portlist :
                direction = portlist[portname]
                p = pin.pin(name=portname, parent=self, netconn=n, direction=direction ) 
                self.port[portname] = p
                if  direction == IN or direction == INOUT :                    
                    #n.all_fanout.add( p )
                    n.all_fanout.add( self ) # point at the gate rather than a pin
                    if is_a_primitive:
                        # n.prim_fanout.add( p )
                        n.prim_fanout.add( self ) # point at the gate rather than a pin
                        if self.simulate not in n.efanout:
                            n.efanout.append(self.simulate)
                if  direction == OUT or direction == INOUT :
                    self.outputpins[portname] = p
                    n.all_fanin.add(p)    
                    n.all_gate_fanin.add(self)
                    # all drivers default to drive 'X' on startup
                    if is_a_primitive:
                        n.drivers.append(p)

        for portname in portlist:
            if not portname in self.port:
                self.port[portname] = pin.pin(name=portname, parent=self, netconn=net.net('unconnected'))
Ejemplo n.º 26
0
            def warpper(*args, **kwargs):
                self.net = net(1, ip, port)
                funcName, msg = function.__name__, ""
                func = (funcName, args, kwargs)
                self.net.send('RF', func)

                while 1:
                    recived = self.net.recv()
                    msg = recived[0]
                    data = recived[1]
                    msg = msg.replace(
                        ' ', '')  # to remove the white space in the msg

                    if msg == "end":
                        return data

                    if msg == "SF":
                        data = func

                    self.net.msgToFunc[msg](data)
Ejemplo n.º 27
0
Archivo: main.py Proyecto: ag8/magic
def train(sess):
    logits = net.net(images_batch, num_fully_connected_layers=1)

    # Initialize all variables
    sess.run(tf.global_variables_initializer())

    labels = tf.cast(labels_batch, dtype=tf.float32)
    cost = tf.reduce_mean(tf.squared_difference(logits, labels))


    # Initialize all variables
    sess.run(tf.global_variables_initializer())

    train_op = tf.train.AdamOptimizer(FLAGS.LEARNING_RATE).minimize(cost)



    # Initialize all variables
    sess.run(tf.global_variables_initializer())

    training_epochs = 10

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(n_samples / FLAGS.BATCH_SIZE)
        # Loop over all batches
        for i in range(total_batch):
            # batch_xs, _ = mnist.train.next_batch(batch_size)
            # batch_xs = images_batch.eval()

            # Fit training using batch data
            # cost = vae.partial_fit(batch_xs)

            _, my_cost  = sess.run([train_op, cost])

            print("Epoch: (" + str(epoch) + "/" + str(training_epochs) + "); i: (" + str(i) + "/" + str(total_batch) + ").  Current cost: " + float_to_str(my_cost) + "")

            # Compute average loss
            avg_cost += cost / n_samples * FLAGS.BATCH_SIZE
Ejemplo n.º 28
0
def train(net, dataloader, cost, optimizer, epoch, n_epochs, use_cuda):
    # the model of training
    net.train()
    running_loss = 0.0
    correct = 0.0
    total = 0
    print('Epoch {}/{}'.format(epoch, n_epochs))
    for data in dataloader:
        x_train, y_train = data
        if use_cuda:
            x_train, y_train = x_train.cuda(), y_train.cuda()

        # change data to Variable, a wrapper of Tensor
        x_train, y_train = Variable(x_train), Variable(y_train)

        # zero the parameter gradients
        optimizer.zero_grad()

        outputs = net(x_train)
        loss = cost(outputs, y_train)
        loss.backward()
        # optimize the weight of this net
        optimizer.step()

        running_loss += loss.data[0]
        if use_cuda:
            _, pred = torch.max(
                outputs.data.cpu(),
                1)  # pred: get the index of the max probability
            correct += pred.eq(y_train.data.cpu().view_as(pred)).sum()
        else:
            _, pred = torch.max(outputs.data, 1)
            correct += pred.eq(y_train.data.view_as(pred)).sum()
        total += y_train.size(0)
    Loss = running_loss / len(dataloader)
    train_acc = 100 * correct / total
    print("Loss {}, acc {}".format(Loss, train_acc))
    print("-" * 10)
    return Loss, train_acc
Ejemplo n.º 29
0
    def mainNet(self):
        host = socket.gethostbyname(socket.gethostname())

        self.net = net(0, host, 1705)

        stdout = output(self.net)
        stderr = error(self.net)

        sys.stdout = stdout
        sys.stderr = stderr

        while 1:
            msg, data = self.net.recv()
            msg = msg.replace(' ', '')  #remove white spaces from the message

            if len(msg):
                end = self.net.msgToFunc[msg](data)

            if end:
                break

            self.net.socket.close()
            self.net.socket.close()
Ejemplo n.º 30
0
def train_net(train_loader=None,
              net=None,
              batch_size=128,
              n_epochs=5,
              learning_rate=0.001,
              saved_model=None,
              fname=None):
    #Print all of the hyperparameters of the training iteration:
    print("===== HYPERPARAMETERS =====")
    print("batch_size=", batch_size)
    print("epochs=", n_epochs)
    print("learning_rate=", learning_rate)
    print("=" * 30)

    #Get training and test data
    n_batches = len(train_loader)

    #Create our loss and optimizer functions
    loss, optimizer = get_loss_optimizer(net, learning_rate)

    #Time for printing
    training_start_time = time.time()

    f_out = open(fname, "w")
    wrt = csv.writer(f_out)

    total_train_loss = 0

    scheduler = StepLR(optimizer, step_size=250, gamma=0.1)
    net = net.float()
    net = net.to('cuda')
    #Loop for n_epochs
    for epoch in range(n_epochs):

        running_loss = 0.0
        print_every = n_batches // 10
        start_time = time.time()

        wrt.writerow([epoch, total_train_loss])

        total_train_loss = 0

        if (((epoch + 1) % 250) == 0):
            checkpoint = {
                'model': net,
                'state_dict': net.state_dict(),
                'optimizer': optimizer.state_dict()
            }
            file_name = 'checkpoint.pt'
            torch.save(checkpoint, file_name)

        i = 0

        for data in train_loader:

            [inputs, labels, snr] = data
            #print(inputs.shape)
            #Wrap them in a Variable object

            inputs, labels, snr = Variable(inputs).to('cuda'), Variable(
                labels).to('cuda'), Variable(snr).to('cuda')

            #inputs,labels,snr = Variable(inputs), Variable(labels), Variable(snr)
            #Set the parameter gradients to zero
            optimizer.zero_grad()
            #Forward pass, backward pass, optimize
            outputs = net(inputs.float())
            labels = labels.squeeze_().cpu()
            loss_size = loss(outputs.cpu(), np.argmax(labels, axis=1))
            #loss_size = loss(outputs, np.argmax(labels,axis=1))
            loss_size.backward()
            optimizer.step()

            #Print statistics

            running_loss += loss_size.data
            total_train_loss += loss_size.data

            #Print loss from every 10% (then resets to 0) of a batch of an epoch
            if (i + 1) % (print_every + 1) == 0:
                print("Epoch {}, {:d}% \t train_loss: {:.4f} took: {:.2f}s".
                      format(epoch + 1, int(100 * (i + 1) / n_batches),
                             total_train_loss / print_every,
                             time.time() - start_time))
                #Reset running loss and time
                running_loss = 0.0
                start_time = time.time()

            i += 1
        scheduler.step()

    print("Training finished, took {:.2f}s".format(time.time() -
                                                   training_start_time))
    final = {
        'model': net,
        'state_dict': net.state_dict(),
        'optimizer': optimizer.state_dict()
    }

    torch.save(final, saved_model)
    f_out.close()
Ejemplo n.º 31
0
filename, infile, alg, query = argv

try:
        algType = ["enum", "elim"].index(alg)
except ValueError:
        exit("Unknown algorithm; please use enum or elim")
try:
        parts = (
        lambda x: x.strip("P").strip("()"))(# remove the leading P for probability, then the parens
                (lambda x: x[0] if len(x) is 1 else x[1])(# remove the unquoted part
                        (lambda x: x.split('"'))(# allow extra quotes
                                query
                        )
                )
        ).split("|")

        event = parts[0]

        evidence = {}
        if len(parts) > 1: # allow empty conditionals
                for term in parts[1].split(","):
                        if term:
                                name, value = tuple(term.split("="))
                                evidence[name] = value
except Exception:
        exit('Could not parse query string. Make sure it is of this form, INCLUDING quotes: "P(C|A=f,E=t)" or "P(C)"')
        
bayes = net(infile)
original = dict(evidence)
output.display(bayes.probability(event, evidence, algType), event, original)