def train_dataloader(self):
     print("train_dataloader")
     self.ts = DataTraj(self.nfts,
                        conv(self.dgroup, self.dico, self.Batch._fields),
                        idtraj="aircraft")
     tsload = TensorDataLoader(self.ts,
                               batch_size=self.hparams.batch_size,
                               shuffle=False,
                               pin_memory=True)
     print("len(train_dataloader)", len(tsload))
     return Infinitecycle(tsload, self.hparams.steps_per_epoch)
Example #2
0
def backbone(input_layer):
    """
    Argument:
        input_layer:a 4-d tensor,[samples,rows,cols,channels]
    Return:
        three output
    """
    out_put = common.conv(input_layer, filter_shape=(3, 32))

    out_put = common.conv(out_put, filter_shape=(3, 64), padding="valid")
    for i in range(1):
        out_put = common.res_block(out_put, num_filter=(32, 64))

    out_put = common.conv(out_put, filter_shape=(3, 128), padding="valid")
    for i in range(2):
        out_put = common.res_block(out_put, num_filter=(64, 128))

    out_put = common.conv(out_put, filter_shape=(3, 256), padding="valid")
    for i in range(8):
        out_put = common.res_block(out_put, num_filter=(128, 256))
    branch_1 = out_put

    out_put = common.conv(out_put, filter_shape=(3, 512), padding="valid")
    for i in range(8):
        out_put = common.res_block(out_put, num_filter=(256, 512))
    branch_2 = out_put

    out_put = common.conv(out_put, filter_shape=(3, 1024), padding="valid")
    for i in range(4):
        out_put = common.res_block(out_put, num_filter=(512, 1024))

    return branch_1, branch_2, out_put
Example #3
0
def createTestStringMM(a, b, n, bits):
	A = common.conv(a, n)
	A_b = binaryString(A, bits)
	B = common.conv(b, n)
	B_b = binaryString(B, bits)
	n_b = binaryString(n, bits)

	wait_time = waitTime(bits, montgomery_multiplication)

	m_res = common.mont_result(a, b, n)
	m_res_b = binaryString(m_res, bits)

	return ("\tREPORT \"Begin test case for a=%(a)d (A=%(A)d), b=%(b)d (B=%(B)d), N=%(n)d\";\n"
			"\tREPORT \"Expected output is %(m_res)d, %(m_res_b)s\";\n"
			"\tA_in <= \"%(A_b)s\";\n"
			"\tB_in <= \"%(B_b)s\";\n"
			"\tN_in <= \"%(n_b)s\";\n"
			"\tlatch_in <= '1';\n"
			"\twait for 2 * clk_period;\n"
			"\tlatch_in <= '0';\n"
			"\twait for %(wait_time)d * clk_period;\n"
			"\tASSERT(M_out = \"%(m_res_b)s\") REPORT \"test failed\" SEVERITY ERROR;\n\n"
			% locals())
Example #4
0
def inference_resnet(images):
    with tf.variable_scope('1'):
        conv1 = common.conv(images, 64, ksize=7, stride=2)
        conv1 = common.bn(conv1)
        pool1 = common.max_pool(conv1)
    with tf.variable_scope('2'):
        stack2 = common.res_stack(pool1, [256, 256, 256], pool=False)
    with tf.variable_scope('3'):
        stack3 = common.res_stack(stack2, [512, 512, 512, 512])
    with tf.variable_scope('4'):
        stack4 = common.res_stack(stack3, [1024, 1024, 1024,
                                           1024, 1024, 1024])
    with tf.variable_scope('5'):
        stack5 = common.res_stack(stack4, [2048, 2048, 2048])
        pool5 = common.global_ave_pool(stack5)
    with tf.variable_scope('fc'):
        fc = common.fc(pool5, 1)
    return fc
Example #5
0
def inference_small(images):
    with tf.variable_scope('1'):
        conv1 = common.conv(images, 64, ksize=7, stride=2)
        conv1 = common.bn(conv1)
        pool1 = common.max_pool(conv1)
    with tf.variable_scope('2'):
        stack2 = common.stack(pool1, common.res_block, [64, 64])
        pool2 = common.max_pool(stack2)
    with tf.variable_scope('3'):
        stack3 = common.stack(pool2, common.res_block, [128, 128])
        pool3 = common.max_pool(stack3)
    with tf.variable_scope('4'):
        stack4 = common.stack(pool3, common.res_block, [256, 256, 256])
        pool4 = common.max_pool(stack4)
    with tf.variable_scope('5'):
        stack5 = common.stack(pool4, common.res_block, [512, 512])
        pool5 = common.global_ave_pool(stack5)
    with tf.variable_scope('fc'):
        fc = common.fc(pool5, 1)
    return fc
        def predict(dataset):
            vs = DataTraj(dataset,
                          conv(self.dgroup, self.dico, self.Batch._fields),
                          idtraj="aircraft")
            val_data = TensorDataLoader(vs,
                                        batch_size=self.hparams.batch_size,
                                        shuffle=False,
                                        pin_memory=True)

            def prepare_batch(batch):
                return self.Batch(*tuple([xi.cuda() for xi in x]
                                         for x in batch))

            pred = torchmapf(lambda x: self.forward(x).squeeze(1),
                             prepare_batch, val_data)
            pred = pred.cpu().numpy()
            r = torchmapf(lambda x: self.compute_error(x)[0], prepare_batch,
                          val_data).cpu().numpy()
            r = r / (dataset.countmeasure.values *
                     (dataset.countmeasure.values - 1) / 2)
            dataset.loc[:, "nnpredlatitude"] = pred[:, [0]]
            dataset.loc[:, "nnpredlongitude"] = pred[:, [1]]
            dataset.loc[:, "error"] = r
            return r
Example #7
0
def inference_nvidianet(images):
    with tf.variable_scope('conv1'):
        conv1 = common.activation(common.conv(images, 24, ksize=5, stride=2,
                                              padding='VALID'))
    with tf.variable_scope('conv2'):
        conv2 = common.activation(common.conv(conv1, 36, ksize=5, stride=2,
                                              padding='VALID'))
    with tf.variable_scope('conv3'):
        conv3 = common.activation(common.conv(conv2, 48, ksize=5, stride=2,
                                              padding='VALID'))
    with tf.variable_scope('conv4'):
        conv4 = common.activation(common.conv(conv3, 64, ksize=3, stride=1,
                                              padding='VALID'))
    with tf.variable_scope('conv5'):
        conv5 = common.activation(common.conv(conv4, 64, ksize=3, stride=1,
                                              padding='VALID'))
    with tf.variable_scope('conv6'):
        conv6 = common.activation(common.conv(conv5, 64, ksize=3, stride=1,
                                              padding='VALID'))
    with tf.variable_scope('conv7'):
        conv7 = common.activation(common.conv(conv6, 64, ksize=3, stride=1,
                                              padding='VALID'))
    conv7_flat = common.flatten(conv7)
    with tf.variable_scope('fc1'):
        fc1 = common.dropout(common.activation(common.fc(conv7_flat, 512)),
                             0.5)
    with tf.variable_scope('fc2'):
        fc2 = common.dropout(common.activation(common.fc(fc1, 128)),
                             0.625)
    with tf.variable_scope('fc3'):
        fc3 = common.dropout(common.activation(common.fc(fc2, 32)),
                             0.75)
    with tf.variable_scope('fc4'):
        fc4 = common.dropout(common.activation(common.fc(fc3, 8)),
                             0.875)
    with tf.variable_scope('fc5'):
        fc5 = tf.atan(common.fc(fc4, 1))
    return fc5
Example #8
0
def yolo3(input_layer):
    branch_1,branch_2,conv = backbone.backbone(input_layer)

    conv = common.conv(conv,(1,512))
    conv = common.conv(conv,(3,1024))
    conv = common.conv(conv,(1,512))
    conv = common.conv(conv,(3,1024))
    conv = common.conv(conv,(1,512))

    conv_l_branch = common.conv(conv,(3,1024))
    l_output = common.conv(conv_l_branch,(1,3 * (NUM_CLASS + 5)),activation = False,bn = False)

    conv = common.conv(conv,(1,256))
    conv = common.upsample(conv,"resize")

    conv = tf.concat([conv,branch_2],axis = -1)
    conv = common.conv(conv,(1,256))
    conv = common.conv(conv,(3,512))
    conv = common.conv(conv,(1,256))
    conv = common.conv(conv,(3,512))
    conv = common.conv(conv,(1,256))

    conv_m_branch = common.conv(conv,(3,512))
    m_output = common.conv(conv_m_branch,(1,3 * (NUM_CLASS + 5)),activation = False,bn = False)

    conv = common.conv(conv,(1,128))
    conv = common.upsample(conv,"resize")
    
    conv = tf.concat([conv,branch_1],axis = -1)
    conv = common.conv(conv,(1,128))
    conv = common.conv(conv,(3,256))
    conv = common.conv(conv,(1,128))
    conv = common.conv(conv,(3,256))
    conv = common.conv(conv,(1,128))
    
    conv_s_branch = common.conv(conv,(3,256))
    s_output = common.conv(conv_s_branch,(1,3 * (NUM_CLASS + 5)),activation = False,bn = False)
    
    return [s_output,m_output,l_output]
    def put_saltions_inside(self, pz: int, nz: int, concentration: float,
                            positive_diameter_in: float,
                            negative_diameter_in: float, counterions: int,
                            valency_counterion: int,
                            counterion_diameter_in: float,
                            bigger_ion_diameter: float, crystal_pack: bool):
        # establish the number of inside salt ions first
        # Note: salt concentration is the concentration of one kind of ions, also the factor of 0.6 is there in order to be consistent with units.

        volume_box = self.lx * self.ly * self.lz

        total_nions_inside = int((concentration * 0.6022) *
                                 (volume_box * utility.unitlength *
                                  utility.unitlength * utility.unitlength))
        if (total_nions_inside % pz != 0):
            total_nions_inside = total_nions_inside - (total_nions_inside %
                                                       pz) + pz

        total_pions_inside = abs(nz) * total_nions_inside / pz
        total_saltions_inside = total_nions_inside + total_pions_inside + counterions
        print("total_saltions_inside", total_saltions_inside)
        # express diameter in consistent units
        bigger_ion_diameter = bigger_ion_diameter / utility.unitlength  # the bigger_ion_diameter can be cation or anion depending on their sizes
        positive_diameter_in = positive_diameter_in / utility.unitlength
        negative_diameter_in = negative_diameter_in / utility.unitlength
        counterion_diameter_in = counterion_diameter_in / utility.unitlength

        # distance of closest approach between the ion and the interface
        # choosing bigger_ion_diameter to define distance of closest approach helps us to avoid overlapping the ions when we generate salt ions inside
        r0_x = 0.5 * self.lx - 0.5 * bigger_ion_diameter
        r0_y = 0.5 * self.ly - 0.5 * bigger_ion_diameter
        r0_z = 0.5 * self.lz - 0.5 * bigger_ion_diameter

        # generate salt ions inside
        ion_pos = []
        ion_diameter = []
        ion_valency = []
        ion_charges = []
        ion_masses = []
        ion_epsilon = []
        if not crystal_pack:
            while (len(ion_pos) != total_saltions_inside):
                x = np.random.random()
                x = (1 - x) * (-r0_x) + x * (r0_x)

                y = np.random.random()
                y = (1 - y) * (-r0_y) + y * (r0_y)

                z = np.random.random()
                z = (1 - z) * (-r0_z) + z * (r0_z)

                posvec = np.asarray([x, y, z])
                continuewhile = False
                i = 0
                while (i < len(ion_pos) and continuewhile
                       == False):  # ensure ions are far enough apart
                    if (common.magnitude_np(posvec - ion_pos[i], axis=0) <=
                        (0.5 * bigger_ion_diameter + 0.5 * ion_diameter[i])):
                        continuewhile = True
                    i += 1
                if (continuewhile == True):
                    continue
                if (len(ion_pos) < counterions):
                    ion_diameter.append(counterion_diameter_in)
                    ion_valency.append(valency_counterion)
                    ion_charges.append(valency_counterion * 1.0)
                    ion_masses.append(1.0)
                    ion_epsilon.append(self.ein)
                elif (len(ion_pos) >= counterions and len(ion_pos) <
                      (total_pions_inside + counterions)):
                    ion_diameter.append(positive_diameter_in)
                    ion_valency.append(pz)
                    ion_charges.append(pz * 1.0)
                    ion_masses.append(1.0)
                    ion_epsilon.append(self.ein)
                else:
                    ion_diameter.append(negative_diameter_in)
                    ion_valency.append(nz)
                    ion_charges.append(nz * 1.0)
                    ion_masses.append(1.0)
                    ion_epsilon.append(self.ein)
                ion_pos.append(
                    posvec)  # copy the salt ion to the stack of all ions
        else:
            num_ions_in_lx = int(self.lx / bigger_ion_diameter)
            num_ions_in_ly = int(self.ly / bigger_ion_diameter)
            num_ions_in_lz = int(self.lz / bigger_ion_diameter)

            for i in range(num_ions_in_lx):
                x = (-self.lx / 2 +
                     (0.5 * bigger_ion_diameter)) + i * bigger_ion_diameter
                for j in range(num_ions_in_ly):
                    y = (-self.ly / 2 +
                         (0.5 * bigger_ion_diameter)) + j * bigger_ion_diameter
                    for k in range(num_ions_in_lz):
                        if len(ion_pos) < total_saltions_inside:
                            z = (-self.lz / 2 + (0.5 * bigger_ion_diameter)
                                 ) + k * bigger_ion_diameter
                            posvec = np.array([x, y, z])
                            if (x >
                                ((self.lx / 2) - (0.5 * bigger_ion_diameter))
                                    or y >
                                ((self.ly / 2) - (0.5 * bigger_ion_diameter))
                                    or z >
                                ((self.lz / 2) - (0.5 * bigger_ion_diameter))):
                                continue
                            if (len(ion_pos) < counterions):
                                ion_diameter.append(counterion_diameter_in)
                                ion_valency.append(valency_counterion)
                                ion_charges.append(valency_counterion * 1.0)
                                ion_masses.append(1.0)
                                ion_epsilon.append(self.ein)
                            elif (len(ion_pos) >= counterions
                                  and len(ion_pos) <
                                  (total_pions_inside + counterions)):
                                ion_diameter.append(positive_diameter_in)
                                ion_valency.append(pz)
                                ion_charges.append(pz * 1.0)
                                ion_masses.append(1.0)
                                ion_epsilon.append(self.ein)
                            else:
                                ion_diameter.append(negative_diameter_in)
                                ion_valency.append(nz)
                                ion_charges.append(nz * 1.0)
                                ion_masses.append(1.0)
                                ion_epsilon.append(self.ein)
                            ion_pos.append(posvec)

        return {ion_pos_str:conv(ion_pos), ion_charges_str:conv(ion_charges),\
                 ion_masses_str:conv(ion_masses), ion_diameters_str:conv(ion_diameter), ion_epsilon_str:conv(ion_epsilon)}
    def discretize(self, smaller_ion_diameter: float, f: float,
                   charge_meshpoint: float):
        print("charge_meshpoint", charge_meshpoint)
        self.width = f * self.lx
        nx = int(self.lx / self.width)
        ny = int(self.ly / self.width)
        left_plane = {
            "posvec": [],
            "q": [],
            "epsilon": [],
            "a": [],
            "normalvec": []
        }
        right_plane = {
            "posvec": [],
            "q": [],
            "epsilon": [],
            "a": [],
            "normalvec": []
        }
        area = self.width * self.width

        # creating a discretized hard wall interface at z = - l/2
        for j in range(ny):
            for i in range(nx):
                position = conv([
                    -0.5 * self.lx + 0.5 * smaller_ion_diameter +
                    i * self.width, -0.5 * self.ly +
                    0.5 * smaller_ion_diameter + j * self.width, -0.5 * self.lz
                ])
                normal = conv([0, 0, -1])
                left_plane["posvec"].append(position)
                left_plane["q"].append(charge_meshpoint)
                left_plane["epsilon"].append(self.eout)
                left_plane["a"].append(area)
                left_plane["normalvec"].append(normal)

        # creating a discretized hard wall interface at z = l/2
        for j in range(ny):
            for i in range(nx):
                position = conv([
                    -0.5 * self.lx + 0.5 * smaller_ion_diameter +
                    i * self.width, -0.5 * self.ly +
                    0.5 * smaller_ion_diameter + j * self.width, 0.5 * self.lz
                ])
                normal = conv([0, 0, 1])
                right_plane["posvec"].append(position)
                right_plane["q"].append(charge_meshpoint)
                right_plane["epsilon"].append(self.eout)
                right_plane["a"].append(area)
                right_plane["normalvec"].append(normal)
        for key in left_plane.keys():
            left_plane[key] = conv(left_plane[key])
        for key in right_plane.keys():
            right_plane[key] = conv(right_plane[key])
        self.left_plane = left_plane
        self.right_plane = right_plane
        self.tf_left_plane, self.tf_place_left_plane = common.make_tf_versions_of_dict(
            left_plane)
        self.tf_right_plane, self.tf_place_right_plane = common.make_tf_versions_of_dict(
            right_plane)