def __init__(self, lambd=4, lr=0.1, max_iter=50, kernel="linear", degree=None, length_word=16, length_frac=8) -> None: super().__init__() self.degree = degree self.kernel_type = kernel self.max_iter = max_iter self.length_word = length_word self.length_frac = length_frac self.FXP_FORMAT = Fxp( val=None, signed=True, n_word=self.length_word, n_frac=self.length_frac, rounding="trunc", # overflow='wrap' ) self.FXP_FORMAT.config.op_sizing = "same" self.FXP_FORMAT.config.const_op_sizing = "same" self.lr = Fxp(val=lr, like=self.FXP_FORMAT) self.lambd = Fxp(val=lambd, like=self.FXP_FORMAT)
def optimize_fxp(scan, robot_r, reference_scan, closest_index): _k = 0.1 # _k = 0.1 k = Fxp([_k, _k, _k / 10], n_word=n_word, n_int=1) cost_min = Fxp(9999, n_word=n_word) cost_old = cost_function_fxp(scan, robot_r, reference_scan[closest_index]) print(cost_old) robot_r_best = robot_r count = 0 while True: dd = differential_fxp(scan, robot_r, reference_scan[closest_index]) print(dd) dd = npf.format(dd, n_word=n_word, n_int=3) robot_r = robot_r - k * dd robot_r = npf.format(robot_r, n_word=n_word, n_int=3) cost_new = cost_function_fxp(scan, robot_r, reference_scan[closest_index]) count += 1 # print(cost_old - cost_new) print(cost_new) if cost_min > cost_new: cost_min = cost_new robot_r_best = robot_r # if np.abs(cost_old - cost_new) < 0.0001: if np.abs((cost_old - cost_new)()) < 0.0000001: break cost_old = cost_new return robot_r_best, cost_min, count
def save_model_bin(self, binFilePath, modelPath=None): from fxpmath import Fxp if modelPath != None: self.model = load_model(modelPath, compile="False") with open(binFilePath, 'wb') as binFile: largest_inaccuracy = 0 for layer in self.model.layers: g = layer.get_config() h = layer.get_weights() print(g) #binFile.write(json.dumps(g).encode(encoding="ascii",errors="unknown char")) # embedding = 1 * 68 * 8 # simple rnn = 8 * 8, 8 * 8, 8 * 1 # drop out: none # dense: 8 * 1, 1 * 1 # activation: sigmoid for i in h: i = np.array(i) for index, x in np.ndenumerate(i): print(x) h_fxp = Fxp(x, signed=True, n_word=16, n_frac=8) difference = abs(h_fxp.get_val() - x) if difference > largest_inaccuracy: largest_inaccuracy = difference print(h_fxp.bin()) binFile.write(h_fxp.bin().encode( encoding="ascii", errors="unknown char")) print("largest difference") print(str(largest_inaccuracy))
def compute_A(self, omega): identity = Fxp(val=np.identity(self.data.shape[0]), like=self.FXP_FORMAT) omega_lamba_id = omega + self.lambd * identity upper_A = np.concatenate((np.array([[0]]), -self.y.T()), axis=1) lower_A = np.concatenate((self.y(), omega_lamba_id()), axis=1) A = Fxp(val=np.concatenate((upper_A, lower_A), axis=0), like=self.FXP_FORMAT) return A
def param_convert(x, a): y = Fxp(x, signed=True, n_word=a + 2, n_frac=a, overflow='saturate', rounding='around') y = y.get_val() if (a == 100): y = torch.from_numpy(x) else: y = torch.from_numpy(y) y = y.type(torch.FloatTensor) return y
def compute_omega(self): omega = Fxp(val=np.zeros(shape=(self.data.shape[0], self.data.shape[0])), like=self.FXP_FORMAT) for i in range(self.data.shape[0]): for j in range(self.data.shape[0]): Xi = Fxp(val=np.expand_dims(self.data[i](), axis=1), like=self.FXP_FORMAT) Xj = Fxp(val=np.expand_dims(self.data[j](), axis=1), like=self.FXP_FORMAT) omega[i][j] = self.y[i][0] * self.y[j][0] * self.kernel(Xi, Xj) return omega
def rotation_matrix_fxp(robot_r, n_word=None, n_int=None): theta = robot_r[..., 2]() robot_rotation = np.array([ [np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)] ]).transpose(tuple(range(2, robot_r().ndim + 1)) + (1, 0)) return Fxp(robot_rotation, n_word=n_word, n_int=n_int)
def icp_fxp(scan, robot_r, reference_scan): cost_min = Fxp(9999, n_word=n_word) cost_old = cost_min robot_r_best = robot_r optimize_counts = [] icp_count = 0 while True: # print(icp_count) closest_index = closest_points_index_fxp(scan, robot_r, reference_scan) robot_r, cost_new, optimize_count = optimize_fxp( scan, robot_r, reference_scan, closest_index ) if cost_min > cost_new: cost_min = cost_new robot_r_best = robot_r # if np.abs(cost_old - cost_new) < 0.0001: if np.abs((cost_old - cost_new)()) < 0.0000001: break cost_old = cost_new print(optimize_count) optimize_counts.append(optimize_count) icp_count += 1 return robot_r_best, icp_count, np.array(optimize_counts)
def cost_function_fxp(scan, robot_r, reference_scan): current_scan = coord.robot2map_fxp(scan, robot_r, n_word=n_word, n_int=3) cost = npf.square_sum(current_scan - reference_scan) / \ Fxp(reference_scan().shape[:-1], n_word=n_word, n_int=5) cost = npf.format(cost, n_word=n_word, n_int=1) # print('cost: {}', cost.dtype) return cost
def forward_conversion(self, input_data, signed, total_bits, fractional_bits): ''' Converts input data from float/int python data types to ap_fixed with total bits and fractional_bits and returns its uint32 equivalent :param input_data: can be both a single int/float number or a numpy array :param signed: Boolean, if the input data is signed or not :param total_bits: numer of total bits used to represent the input data :param fractional_bits: number of fractional bits used to represent the input data. Integer bits = total bits - fractional bits :return: input data converted to uint32 format. 0.5 can be represented with 4 bits as 0.100. This is converted into 0100 (fractional point removed) and then converted to int. 0.5 as input is converted to 4 as uint32. ''' fixed_point_representation = Fxp(input_data, signed=signed, n_word=total_bits, n_frac=fractional_bits) uint_coverted = np.uint32(fixed_point_representation.uraw()) return uint_coverted
def fit(self, X, y): self.data = Fxp(val=X, like=self.FXP_FORMAT) self.y = Fxp(val=y, like=self.FXP_FORMAT) self.steps = 0 # Computation of matrix Omega and A omega = self.compute_omega() A = self.compute_A(omega) # Computation of the vectors involved in the optimization opt_matrix = A.T.dot(A) ones_hat = Fxp(val=np.concatenate( (np.array([[0]]), np.ones(shape=(self.data.shape[0], 1))), axis=0), like=self.FXP_FORMAT) opt_vect = A.T.dot(ones_hat) # Initialization of beta_k beta_k = Fxp(val=np.random.random(size=(self.data.shape[0] + 1, 1)), like=self.FXP_FORMAT) for i in range(self.max_iter): p_k = opt_vect - opt_matrix.dot(beta_k) r_k = p_k.T.dot(p_k) / p_k.T.dot(opt_matrix.dot(p_k)) beta_k = beta_k + (1 - self.lr) * r_k * p_k self.alphas = beta_k[1:] self.b = beta_k[0][0] self.steps += 1 self.alphas = beta_k[1:] self.b = beta_k[0][0] return self.alphas, self.b
def save_model_txt_binary(self, txtPath, modelPath=None): from fxpmath import Fxp import math import json if modelPath != None: self.model = load_model(modelPath, compile="False") with open(txtPath, 'w') as txtFile: for layer in self.model.layers: g = layer.get_config() h = layer.get_weights() txtFile.write(json.dumps(g)) txtFile.write("\n") for i in h: i = np.array(i) for index, x in np.ndenumerate(i): if g["name"] == "dropout": continue #if g["name"] == "embedding": # print(index) # row = math.floor(index / 4) # col = index % 4 # txtFile.write("row:"+str(row)+"col:"+col) # else: # row = math.floor(index / 32) # col = index % 32 # txtFile.write("row:"+str(row)+"col:"+col) if len(index) > 1: row = index[0] col = index[1] txtFile.write("row:" + str(row) + "col:" + str(col)) else: row = index[0] txtFile.write("row:" + str(row)) h_fxp = Fxp(x, signed=True, n_word=16, n_frac=8) txtFile.write("val:" + h_fxp.bin()) txtFile.write("\n")
def to_fxp_array(input, output, fractbit): if len(output) != 0: print("ERROR") for i in range(len(input) // 2): x1 = Fxp(input[2 * i], True, 16, fractbit) bdata1 = int(x1.hex(), 16).to_bytes(2, byteorder='big') x2 = Fxp(input[2 * i + 1], True, 16, fractbit) bdata2 = int(x2.hex(), 16).to_bytes(2, byteorder='big') bdata = bdata2 + bdata1 idata = int.from_bytes(bdata, 'big') output.append(idata)
def differential_fxp(scan, robot_r, reference_scan): robot_rotation = coord.rotation_matrix_fxp(robot_r, n_word=n_word, n_int=1) # print(robot_rotation.dtype) rotated_scan = npf.matmul(scan, robot_rotation) rotated_scan = npf.format(rotated_scan, n_word=n_word, n_int=3) linear_trans_vector = robot_r[..., np.newaxis, 0:2] - reference_scan[..., 0:2] linear_trans_vector = npf.format(linear_trans_vector, n_word=n_word, n_int=3) # print(rotated_scan.info()) # print(linear_trans_vector.dtype) dxy = rotated_scan + linear_trans_vector dt = npf.cross(rotated_scan, linear_trans_vector) # dxy = Fxp(dxy).like(Fxp(None, n_word=32, n_frac=28)) # dt = Fxp(dt).like(Fxp(None, n_word=32, n_frac=28)) d = npf.concatenate([dxy, dt[..., np.newaxis]], -1) d = npf.format(d, n_word=n_word, n_int=0) # print('d: {}', d.dtype) return npf.sum(d, axis=-2) / Fxp(reference_scan().shape[:-1])
def format(x, n_word=16, n_int=None): return Fxp(x).like(Fxp(None, n_word=n_word, n_int=n_int))
def cross(a, b): (a_int, b_int), shift = fxp2int(a, b) z_int = np.cross(a_int, b_int) # TODO: オーバーフロー z = Fxp(z_int.astype(np.float)) z.resize(n_frac=shift * 2) return z
files = [f for f in listdir('./') if ".csv" in f] word_bits = 16 frac_bits = 11 error = [] for f_ in files: with open(f_, 'r') as handle: data = np.genfromtxt(handle, delimiter=',') fxp_val = np.zeros_like(data) fxp_bin = np.ndarray(data.shape, dtype='U16') for line in range(len(data)): if "bias" in f_: fxp_sample = Fxp(data[line], True, word_bits, frac_bits) fxp_val[line] = fxp_sample.get_val() fxp_bin[line] = fxp_sample.bin() error.append( np.nan_to_num((data[line] - fxp_val[line]) / data[line])) else: for column in range(len(data[line])): fxp_sample = Fxp(data[line][column], True, word_bits, frac_bits) fxp_val[line][column] = fxp_sample.get_val() fxp_bin[line][column] = fxp_sample.bin() error.append( np.nan_to_num( (data[line][column] - fxp_val[line][column]) / data[line][column])) with open('report.txt', 'a') as r:
channels = 3 elif image.mode == "L": channels = 1 else: print("Unknown mode: %s" % image.mode) return None pixel_values = numpy.array(pixel_values).reshape((width, height, channels)) return pixel_values, width, height image, x, y = get_image("empty-heart.jpg") file1.write('v2.0 raw') for j in range(y): for i in range(x): #print (image[i][j] >> 4) fxppixel = Fxp(image[j][i], 0, 24, 0) fxppixel = Fxp(fxppixel >> 4, 0, 4, 0) print(str(fxppixel.hex()).replace("0x", "")) tempstr = str(fxppixel.hex()).replace("0x", "") tempstr = tempstr.replace("'", "") tempstr = tempstr.replace("[", "") tempstr = tempstr.replace("]", "") tempstr = tempstr.replace(",", "") tempstr = tempstr.replace(" ", "") file1.write('\n' + tempstr) #file1.write('\n' + str(image[j][i] >> 4)) file1.close()
pixel_values = list(image.getdata()) if image.mode == "RGB": channels = 3 elif image.mode == "L": channels = 1 else: print("Unknown mode: %s" % image.mode) return None pixel_values = numpy.array(pixel_values).reshape((width, height, channels)) return pixel_values, width, height image, x, y = get_image("fancy256x256.jpg") file1.write('v2.0 raw') for j in range(y): for i in range(x): #print (image[i][j] >> 4) fxppixel = (Fxp((image[j][i] >> 4), 0, 4, 0)) print(str(fxppixel.hex()).replace("0x", "")) tempstr = str(fxppixel.hex()).replace("0x", "") tempstr = tempstr.replace("'", "") tempstr = tempstr.replace("[", "") tempstr = tempstr.replace("]", "") tempstr = tempstr.replace(",", "") tempstr = tempstr.replace(" ", "") file1.write('\n' + tempstr) #file1.write('\n' + str(image[j][i] >> 4)) file1.close()
from fxpmath import Fxp inputs = input('sign, integers, fractional:').split(', ') sign = bool(inputs[0]) integers = int(inputs[1]) fractional = int(inputs[2]) words = integers + fractional + sign #x = (Fxp(number, bool(sign), words, fractional)) #print(x.bin(frac_dot=True)) while True: number = float(input('Enter a signed Decimal value to convert:')) x = (Fxp(number, sign, words, fractional)) print(x.bin(frac_dot=True)) print(x.hex())
threshold = 1 fmo = [[[ 0 for _ in range(Nox)] for _ in range(Noy)] for _ in range(Nof)] fmo_obtained = [[[ 0 for _ in range(Nox)] for _ in range(Noy)] for _ in range(Nof)] ############################################################################### # Script # ############################################################################### # A : Fill FMI with open("fmo.txt", "r") as file1: for f in range(0, Nof): for y in range(0, Noy): for x in range(0, Nox): line = "0x" + file1.readline().split('\n')[0].upper() fmo[f][y][x] = Fxp(line, True, BW_px, fr_px, overflow = 'wrap') with open("result_fmo.txt", "r") as file2: for ty in range(0, Noy//Toy): for tx in range(0, Nox//Tox): for f in range(0, Nof): for y in range(0, Toy): for x in range(0, Tox): res = file2.readline().split(':') val = "0x" + res[0].upper() pos = int(res[1]) f_pos = pos // (Nox * Noy) y_pos = (pos % (Nox * Noy)) // Noy x_pos = (pos % (Nox * Noy)) % Noy fmo_obtained[f_pos][y_pos][x_pos] = Fxp(val, True, BW_px, fr_px, overflow = 'wrap') # Check pos
from fxpmath import Fxp import numpy as np x = np.array([[1 / 3, 1 / 3]]) y = np.array([[1 / 3], [1 / 3]]) print("X original = ", x) print("Y original = ", y) print("-----------") x_fxp = Fxp(x, signed=False, n_word=20, n_frac=15) y_fxp = Fxp(y, signed=False, n_word=20, n_frac=15) print("X fxp =", x_fxp.get_val()) print("Y fxp =", y_fxp.get_val()) print("-----------") print(x_fxp.info(verbose=3)) print("-----------") print("Dot product without scaling = ", x_fxp.get_val().dot(y_fxp.get_val())) print("Dot prod scaled = ", Fxp(x_fxp.get_val().dot(y_fxp.get_val()), n_word=20, n_frac=15)) print("Dot prod without specifications = ", Fxp(x_fxp.get_val().dot(y_fxp.get_val()))) dot_fpx = Fxp(None, signed=True, n_word=20, n_frac=15) dot_fpx.equal(x_fxp().dot(y_fxp()))
import sys sys.path.insert(1, '/Users/jingyuan/Desktop/dga/dga_detection_rnn') from fxpmath import Fxp import numpy as np tanh_table_path = "conf/tanh_table.bin" with open(tanh_table_path, 'wb') as binFile: for entry in range(10): xVal = -4 + entry * 8 / 9 print(xVal) x_fxp = Fxp(xVal, signed=True, n_word=16, n_frac=12) print(x_fxp.bin()) xTanh = np.tanh(xVal) print(xTanh) xTanh_fxp = Fxp(xTanh, signed=True, n_word=16, n_frac=12) print(xTanh_fxp.bin()) binFile.write(x_fxp.bin().encode(encoding="ascii", errors="unknown char")) binFile.write(xTanh_fxp.bin().encode(encoding="ascii", errors="unknown char"))
def convertToIq(num): return Fxp(num, signed=True, n_word=16, n_frac=15)
i_f = float(val) / float(maxval) * (len(colors) - 1) i, f = int(i_f // 1), i_f % 1 # Split into whole & fractional parts. # Does it fall exactly on one of the color points? if f < EPSILON: return colors[i] else: # Otherwise return a color within the range between them. (r1, g1, b1), (r2, g2, b2) = colors[i], colors[i + 1] return int(r1 + f * (r2 - r1)), int(g1 + f * (g2 - g1)), int(b1 + f * (b2 - b1)) maxval = 767 colors = [(0, 15, 15), (0, 0, 15), (15, 0, 15)] # [Beginning, Middle , End] print(' Val R G B') file1.write('v2.0 raw') for i in range(maxval + 1): r, g, b = convert_to_rgb(maxval, i, colors) fxpred = (Fxp(r, 0, 16, 0)) fxpgreen = (Fxp(g, 0, 16, 0)) fxpblue = (Fxp(b, 0, 16, 0)) const = (Fxp(1, 0, 16, 0)) fxpword = Fxp(None, 0, 16, 0) fxpword.equal(((const & 0x0001) << 15) | ((fxpred & 0x001f) << 10) | ((fxpgreen & 0x001f) << 5) | ((fxpblue & 0x001f))) print('{:3d} -> ({:3d}, {:3d}, {:3d})'.format(i, r, g, b)) file1.write('\n' + str(fxpword.hex()).replace("0x", "")) file1.close()
def concatenate(args, axis=None): args_int, shift = fxp2int(*args) z_int = np.concatenate(args_int, axis=axis) z = Fxp(z_int.astype(np.float)) z.resize(n_frac=shift) return z
def sum(a, axis=None): a_int, shift = fxp2int(a) z_int = np.sum(a_int[0], axis=axis) # TODO: オーバーフロー z = Fxp(z_int.astype(float)) z.resize(n_frac=shift) return z
import torch import sys from fxpmath import Fxp import numpy as np x = Fxp(-5.25789021) print(x.info()) # network = sys.argv[1] network = "trained-models/cornell-randsplit-rgbd-grconvnet3-drop1-ch32/epoch_19_iou_0.98" net = torch.load(network) # net_q = torch.quantization.quantize_dynamic(net, qconfig_spec=None, dtype=torch.qint8, mapping=None, inplace=False) # net.eval() model_dict = net.state_dict() # model_dict_q = net_q.state_dict() # print ("model_dict = ",model_dict) keys = model_dict.keys() print("keys = ", keys) conv1_weight = model_dict['conv1.weight'] # conv1_weigdt_fixed = model_dict_q['conv1.weight'] # print("origin = ",conv1_weight[0][0][0]) # print("quantize = ",conv1_weigdt_fixed[0][0][0]) # print ("conv1_weight = ",conv1_weight.size()) # bn1_weight = model_dict["bn1.weight"] # print ("bn1_weight = ",bn1_weight) for key in model_dict:
with open(join(out_folder, mod + '_bin.txt'), 'a') as f_bin: with open(join(out_folder, mod + '_float.txt'), 'a') as f_float: for snr in range(len(snr_array)): for frame in range(frames): signal = parsed_signal[snr, frame, 0:frame_size] for r in signal: signal_real = np.real(r) signal_img = np.imag(r) f_float.write(str(signal_real) + ' ') f_float.write(str(signal_img) + '\n') f_bin.write( str( Fxp(signal_real, True, fxp_bits, fxp_frac).bin()) + ' ') f_bin.write( str( Fxp(signal_img, True, fxp_bits, fxp_frac).bin()) + '\n') #DEV TEST PURPOSE ONLY absolute = [] for i in range(2048): signal_real = np.real(signal[i]) signal_img = np.imag(signal[i]) absolute.append((signal_real**2) + (signal_img**2)) #print(absolute) absolute_bin = [ Fxp(i, True, 16, 11).bin() for i in absolute ]
#-10dB=0; -8dB=1; -6dB=2; -4dB=3; # -2dB=4; 0dB=5; 2dB=6; 4dB=7; # 6dB=8; 8dB=9; 10dB=10; 12dB=11; # 14dB=12; 16dB=13; 18dB=14; 20dB=15 snr=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] frames=500 ft_array = np.empty((frames,6)) for mod in modulations: data_file = mod + "_features.pickle" with open(mod + '_data_bin.txt', 'a') as f_bin: with open(mod + '_data_float.txt', 'a') as f_float: with open(data_file, 'rb') as handle: ft_file = pickle.load(handle) for snr_value in snr: for frame in range(frames): ft_array[frame][:] = ft_file[snr_value][frame] ft_array_norm = normalize(ft_array, norm='l2') for ar in range(len(ft_array_norm)): for result in ft_array_norm[ar]: f_float.write(str(result) + " ") f_float.write("\n") for ar in range(len(ft_array_norm)): for result in ft_array_norm[ar]: f_bin.write(str(Fxp(result, True, 16, 11).bin() + " ")) f_bin.write("\n") print('All done. Go grab a beer!')