def toHLS(p,r,m,doQK=False,intbits_a=0,odir='cnn_projects'): if doQK: model = tf.keras.models.load_model('models/{}/model_best.h5'.format(m),custom_objects={'PruneLowMagnitude': pruning_wrapper.PruneLowMagnitude,'QDense': QDense, 'QConv2D': QConv2D, 'Clip': Clip, 'QActivation': QActivation,'QBatchNormalization':QBatchNormalization}) model = strip_pruning(model) hls_model = getQKeras(model=model,model_name=m,precision=p,reuse=r,intbits_a=intbits_a,odir=odir) else: model = tf.keras.models.load_model('models/{}/model_best.h5'.format(m),custom_objects={'PruneLowMagnitude': pruning_wrapper.PruneLowMagnitude,'QDense': QDense, 'QConv2D': QConv2D, 'Clip': Clip, 'QActivation': QActivation}) model = strip_pruning(model) hls_model = getBaseline(model=model,model_name=m,precision=p,reuse=r,intbits_a=intbits_a,odir=odir) (x_train, y_train), (x_test, y_test) = getNumpyData('svhn_cropped',oneHot=True) wp,ap = numerical(model=model, hls_model=hls_model, X=x_test[:1000]) ap.axes[0].set_title("") add_logo(ap.axes[0], ap, 0.3, position='upper left') labels = [item.get_text().replace('batch_normalization','Batch norm.').replace('max_pooling2d','Max Pooling').replace('_',' ').capitalize() for item in ap.axes[0].get_yticklabels()] ap.axes[0].set_yticklabels(labels) ap.axes[0].set_xlabel('Output') ap.axes[0].set_xlim([10.455191523E-13,64]) ap.savefig('plots/Profile_{}_activations.pdf'.format(model.name)) del ap wp.axes[0].set_title("") labels = [item.get_text().replace('batch_normalization','Batch norm.').replace('max_pooling2d','Max Pooling').replace('_',' ').replace('0 0','0, w').replace('0 1','0, b').replace('1 0','1, w').replace('1 1','1, b').replace('2 0','2, w').replace('2 1','2, b').replace('3 0','3, w').replace('3 1','3, b').replace('4 0','4, w').replace('4 1','4, b').replace('5 0','5, w').replace('5 1','5, b').replace('output dense 0','output, w').replace('output dense 1','output, b').replace('norm. 0','norm., w').replace('norm. 1','norm., b').replace(', b,',' 1,').replace('output 0','output, w').replace('output 1','output, b').capitalize() for item in wp.axes[0].get_yticklabels()] # labels = [item.replace('Dense','Fused dense + b n.').replace('Output Fused dense + b n.','Output dense') for item in labels] wp.axes[0].set_yticklabels(labels) wp.axes[0].set_xlim([0.0000009,64]) wp.axes[0].set_xlabel('Weight') add_logo(wp.axes[0], wp, 0.3, position='upper left') wp.savefig('plots/Profile_{}_weights.pdf'.format(model.name))
def toHLS(model,precision=32): (x_train, y_train), (x_test, y_test) = getNumpyData('svhn_cropped',oneHot=False) #get model precision pw = 'ap_fixed<{},{}>'.format(precision,intbits_w) po = 'ap_fixed<{},{}>'.format(precision,intbits_a) if precision < 10: pw = 'ap_fixed<{},{}>'.format(precision,4) po = 'ap_fixed<{},{}>'.format(precision,4) # hls config hls_cfg = {'Model' : {'Precision' : pw}} hls_cfg['LayerName'] = {'output_softmax' : {'Strategy' : 'Stable'}} hls_cfg['LayerType'] = {'Input' : {'Precision' : 'ap_fixed<16,6>'}, 'Dense' : {'Precision' : {'result' : po}}, 'Conv2D' : {'Precision' : {'result' : po}}, 'Pooling2D' : {'Precision' : po}, 'BatchNormalization' : {'Precision' : po}, 'Activation' : {'Precision' : {'result' : po}} } hls_cfg['Model']['PackFactor'] = 1 hls_cfg['Model']['ReuseFactor'] = 1 # vivado config cfg = hls4ml.converters.create_vivado_config() cfg['IOType'] = 'io_stream' cfg['HLSConfig'] = hls_cfg cfg['KerasModel'] = model cfg['OutputDir'] = model_name.replace(".h5","")+"_bw%i"%(precision) # wherever you want the project to go cfg['XilinxPart'] = 'xcvu9p-flgb2104-2l-e' print_dict(hls_cfg) #hls model hls_model = hls4ml.converters.keras_to_hls(cfg) for layer in hls_cfg['LayerName'].keys(): hls_cfg['LayerName'][layer]['Trace'] = True hls_model.compile() hls4ml_pred, hls4ml_trace = hls_model.trace(x_test[:1000]) keras_trace = hls4ml.model.profiling.get_ymodel_keras(model, x_test[:1000]) y_hls = hls_model.predict(x_test[:1000]) for layer in hls_cfg['LayerName'].keys(): print("Keras layer {}, first sample:".format(layer)) print(keras_trace[layer][0]) print("hls4ml layer {}, first sample:".format(layer)) print(hls4ml_trace[layer][0]) print('Compile and predict') x_test = x_test[:100] y_test = y_test[:100] y_predict = model .predict(x_test) y_predict_hls4ml = hls_model.predict(x_test) print("y_predict = ", y_predict[2]) print("y_predict_hls4ml = ", y_predict_hls4ml[2]) print("y_test = ", y_test[2]) print("arg ypred = ", np.argmax(y_predict[2])) print("arg ypredhls = ", np.argmax(y_predict_hls4ml[2])) data['accuracy_keras'] = accuracy_score (y_test, np.argmax(y_predict,axis=1)) data['accuracy_hls4ml'] = accuracy_score(y_test, np.argmax(y_predict_hls4ml,axis=1)) print("Accuracy: Keras={} hls4ml={}".format(data['accuracy_keras'],data['accuracy_hls4ml'])) hls4ml.utils.plot_model(hls_model, show_shapes=True, show_precision=True, to_file='plot_model_{}.png'.format(precision)) hls4ml.utils.plot_model(hls_model, show_shapes=True, show_precision=True, to_file='plot_model_{}.png'.format(precision)) wp,ap = numerical(keras_model=model, hls_model=hls_model, X=x_test[:1000]) wp.savefig('%s_profile_weights_LayerTypePrecision.pdf'%cfg['OutputDir']) ap.savefig('%s_profile_activations_LayerTypePrecision.pdf'%cfg['OutputDir'])
DropoutNoScaleForTernary, 'Clip': Clip, 'BinaryDense': BinaryDense, 'TernaryDense': TernaryDense, 'BinaryConv2D': BinaryConv2D, 'binary_tanh': binary_tanh, 'QDense': QDense, 'QConv2D': QConv2D, 'QActivation': QActivation, 'QBatchNormalization': QBatchNormalization }) model.load_weights(outdir + '/KERAS_check_best_model_weights.h5') # loop over each layer and get weights and biases' plt.figure() if options.doProfile: numerical(keras_model=model, X=X_test) plt.savefig('profile_%s.png' % (model_name)) if options.doWeights: allWeightsByLayer = {} for layer in model.layers: print("----") print(layer.name) if len(layer.get_weights()) < 1: continue weights = layer.get_weights()[0] weightsByLayer = [] for w in weights: weightsByLayer.append(w) if len(weightsByLayer) > 0: allWeightsByLayer[layer.name] = np.array(weightsByLayer) labelsW = [] histosW = []
def getReports(model,model_name,precision=32): outname = model_name.replace('.h5','')+'_bw{}'.format(precision) (x_train, y_train), (x_test, y_test) = getNumpyData('svhn_cropped',oneHot=False) data = {} data['w']= int(p) pw = 'ap_fixed<{},{}>'.format(precision,intbits_w) po = 'ap_fixed<{},{}>'.format(precision,intbits_a) if precision < 10: pw = 'ap_fixed<{},{}>'.format(precision,4) po = 'ap_fixed<{},{}>'.format(precision,4) hls_cfg = {'Model' : {'Precision' : pw}} hls_cfg['LayerType'] = {'Input' : {'Precision' : 'ap_fixed<16,6>'}, 'Dense' : {'Precision' : {'result' : po}}, 'Conv2D' : {'Precision' : {'result' : po}}, 'MaxPooling2D' : {'Precision' : {'result' : po}}, 'BatchNormalization' : {'Precision' : {'result' : po}}, 'Activation' : {'Precision' : {'result' : po}}} hls_cfg['Model']['PackFactor'] = 1 # an integer that divides the image width with no remained hls_cfg['Model']['ReuseFactor'] = 1 cfg = hls4ml.converters.create_vivado_config() cfg['IOType'] = 'io_stream' cfg['HLSConfig'] = hls_cfg cfg['KerasModel'] = model # the model cfg['OutputDir'] = outname cfg['XilinxPart'] = 'xcvu9p-flgb2104-2l-e' print("Configuration is \n") print_dict(hls_cfg) hls_model = hls4ml.converters.keras_to_hls(cfg) hls_model.compile() print('Compile and predict') x_test = x_test[:1000] y_test = y_test[:1000] y_predict = model .predict(x_test) y_predict_hls4ml = hls_model.predict(x_test) print("y_predict = ", y_predict[2]) print("y_predict_hls4ml = ", y_predict_hls4ml[2]) print("y_test = ", y_test[2]) print("arg ypred = ", np.argmax(y_predict[2])) print("arg ypredhls = ", np.argmax(y_predict_hls4ml[2])) data['accuracy_keras'] = accuracy_score (y_test, np.argmax(y_predict,axis=1).reshape(-1,1)) data['accuracy_hls4ml'] = accuracy_score(y_test, np.argmax(y_predict_hls4ml,axis=1).reshape(-1,1)) print(y_test- np.argmax(y_predict_hls4ml,axis=1)) print("Accuracy: Keras={} hls4ml={}".format(data['accuracy_keras'],data['accuracy_hls4ml'])) wp,ap = numerical(keras_model=model, hls_model=hls_model, X=x_test[:1000]) wp.savefig('plots/{}_profile_weights.pdf'.format(outname)) ap.savefig('plots/{}_profile_activations.pdf'.format(outname)) indir= '/eos/home-t/thaarres/hls4ml_cnns/models_synt/{}_bw{}/'.format(model_name,precision) # Get the resources from the logic synthesis report report = open('{}/vivado_synth.rpt'.format(indir)) lines = np.array(report.readlines()) data['lut'] = int(lines[np.array(['CLB LUTs*' in line for line in lines])][0].split('|')[2]) data['ff'] = int(lines[np.array(['CLB Registers' in line for line in lines])][0].split('|')[2]) data['bram'] = float(lines[np.array(['Block RAM Tile' in line for line in lines])][0].split('|')[2]) data['dsp'] = int(lines[np.array(['DSPs' in line for line in lines])][0].split('|')[2]) report.close() # Get the latency from the Vivado HLS report report = open('{}/myproject_prj/solution1/syn/report/myproject_csynth.rpt'.format(indir)) lines = np.array(report.readlines()) lat_line = lines[np.argwhere(np.array(['Latency (clock cycles)' in line for line in lines])).flatten()[0] + 6] data['latency_clks'] = int(lat_line.split('|')[2]) data['latency_ns'] = float(lat_line.split('|')[2])*5.0 data['latency_ii'] = int(lat_line.split('|')[4]) return data
def toHLS(model, model_name, precision=32): (img_train, label_train), (img_test, label_test) = tfds.load( "svhn_cropped", split=['train', 'test'], batch_size=-1, as_supervised=True, ) del (img_train, label_train) a = hls4ml.model.profiling.activations_keras(model, img_test[:1000], fmt='summary') intbits = ( np.ceil(max(np.log2(np.array(list(map(lambda x: x['whishi'], a)))))) + 1) hls_cfg = hls4ml.utils.config_from_keras_model(model, granularity='model') hls_cfg['Model'][ 'PackFactor'] = 1 # an integer that divides the image width with no remained hls_cfg['Model']['Precision'] = 'ap_fixed<%i,%i>' % (precision, int(intbits)) print(hls_cfg) hls_model = hls4ml.converters.convert_from_keras_model( model, hls_config=hls_cfg, output_dir='model_1/hls4ml_prj') # hls4ml.utils.plot_model(hls_model, show_shapes=True, show_precision=True, to_file=None) hls_model.compile() score = hls_model.evaluate(test_data) print("hlsModel Accuracy {} = {}".format(model_name, score[1])) wp, ap = numerical(keras_model=model, hls_model=hls_model, X=img_test[:1000]) wp.savefig('%s_profile_weights.pdf' % cfg['OutputDir']) ap.savefig('%s_profile_activations.pdf' % cfg['OutputDir']) cfg = hls4ml.converters.create_vivado_config() cfg['IOType'] = 'io_stream' cfg['HLSConfig'] = hls_cfg cfg['KerasModel'] = model # the model cfg['OutputDir'] = model_name.replace(".h5", "") + "_bw%i_int%i" % ( precision, intbits) # wherever you want the project to go cfg['XilinxPart'] = 'xcvu9p-flgb2104-2l-e' print("Configuration is \n") print(cfg) indir = '/eos/home-t/thaarres/hls4ml_cnns/synthesized/final_cnn_bw%i/' % precision data = {} data['w'] = int(p) # Get the resources from the logic synthesis report report = open('{}/vivado_synth.rpt'.format(indir)) lines = np.array(report.readlines()) data['lut'] = int(lines[np.array(['CLB LUTs*' in line for line in lines])][0].split('|')[2]) data['ff'] = int(lines[np.array( ['CLB Registers' in line for line in lines])][0].split('|')[2]) data['bram'] = float(lines[np.array( ['Block RAM Tile' in line for line in lines])][0].split('|')[2]) data['dsp'] = int(lines[np.array(['DSPs' in line for line in lines])][0].split('|')[2]) report.close() # Get the latency from the Vivado HLS report report = open( '{}/myproject_prj/solution1/syn/report/myproject_csynth.rpt'.format( indir)) lines = np.array(report.readlines()) lat_line = lines[np.argwhere( np.array(['Latency (clock cycles)' in line for line in lines])).flatten()[0] + 6] data['latency_clks'] = int(lat_line.split('|')[2]) data['latency_ns'] = float(lat_line.split('|')[2]) * 5.0 data['latency_ii'] = int(lat_line.split('|')[4]) return data
def main(args): # parameters our_config = yaml_load(args.config) save_dir = our_config['save_dir'] model_name = our_config['model']['name'] model_file_path = os.path.join(save_dir, 'model_best.h5') from tensorflow.keras.models import load_model from qkeras.utils import _add_supported_quantized_objects co = {} _add_supported_quantized_objects(co) model = load_model(model_file_path, custom_objects=co) if bool(our_config['convert']['RemoveSoftmax']): input_layer = model.inputs output_layer = None for layer in model.layers: if layer.name == 'softmax': output_layer = layer.input model = Model(inputs=input_layer, outputs=output_layer) model.summary() tf.keras.utils.plot_model(model, to_file="model.png", show_shapes=True, show_dtype=False, show_layer_names=False, rankdir="TB", expand_nested=False) _, (X_test, y_test) = cifar10.load_data() X_test = np.ascontiguousarray(X_test / 256.) num_classes = 10 y_test = tf.keras.utils.to_categorical(y_test, num_classes) # just use first 100 if bool(our_config['convert']['Trace']): X_test = X_test[:100] y_test = y_test[:100] y_keras = model.predict(X_test) np.save('y_keras.npy', y_keras) np.save('y_test.npy', y_test) np.save('X_test.npy', X_test) import hls4ml config = hls4ml.utils.config_from_keras_model(model, granularity='name') print("-----------------------------------") print_dict(config) print("-----------------------------------") config['Model'] = {} config['Model']['ReuseFactor'] = our_config['convert']['ReuseFactor'] config['Model']['Strategy'] = our_config['convert']['Strategy'] config['Model']['Precision'] = our_config['convert']['Precision'] for name in config['LayerName'].keys(): config['LayerName'][name]['Trace'] = bool( our_config['convert']['Trace']) config['LayerName'][name]['ReuseFactor'] = our_config['convert'][ 'ReuseFactor'] config['LayerName'][name]['Precision'] = our_config['convert'][ 'Precision'] if 'activation' in name: config['LayerName'][name]['Precision'] = {} config['LayerName'][name]['Precision']['result'] = our_config[ 'convert']['PrecisionActivation'] # custom configs for name in our_config['convert']['Override'].keys(): config['LayerName'][name].update( our_config['convert']['Override'][name]) cfg = hls4ml.converters.create_backend_config(fpga_part='xc7z020clg400-1') cfg['HLSConfig'] = config cfg['IOType'] = our_config['convert']['IOType'] cfg['Backend'] = our_config['convert']['Backend'] cfg['InputData'] = 'input_data.dat' cfg['OutputPredictions'] = 'output_predictions.dat' cfg['Interface'] = 's_axilite' # or 'm_axi' cfg['ClockPeriod'] = our_config['convert']['ClockPeriod'] cfg['KerasModel'] = model cfg['OutputDir'] = our_config['convert']['OutputDir'] print("-----------------------------------") print_dict(cfg) print("-----------------------------------") # profiling / testing hls_model = hls4ml.converters.keras_to_hls(cfg) hls4ml.utils.plot_model(hls_model, show_shapes=True, show_precision=True, to_file='model_hls4ml.png') if bool(our_config['convert']['Trace']): from hls4ml.model.profiling import compare, numerical import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt plt.figure() wp, ap = numerical(model=model, hls_model=hls_model, X=X_test) plt.show() plt.savefig('profiling_numerical.png', dpi=300) plt.figure() cp = compare(keras_model=model, hls_model=hls_model, X=X_test, plot_type="dist_diff") plt.show() plt.savefig('profiling_compare.png', dpi=300) y_hls, hls4ml_trace = hls_model.trace(X_test) np.save('y_hls.npy', y_hls) keras_trace = hls4ml.model.profiling.get_ymodel_keras(model, X_test) for layer in hls4ml_trace.keys(): plt.figure() klayer = layer if '_alpha' in layer: klayer = layer.replace('_alpha', '') plt.scatter(hls4ml_trace[layer].flatten(), keras_trace[klayer].flatten(), s=0.2) min_x = min(np.amin(hls4ml_trace[layer]), np.amin(keras_trace[klayer])) max_x = max(np.amax(hls4ml_trace[layer]), np.amax(keras_trace[klayer])) plt.plot([min_x, max_x], [min_x, max_x], c='gray') plt.xlabel('hls4ml {}'.format(layer)) plt.ylabel('QKeras {}'.format(klayer)) plt.show() plt.savefig('profiling_{}.png'.format(layer), dpi=300) else: hls_model.compile() y_hls = hls_model.predict(X_test) print("Keras Accuracy: {}".format( accuracy_score(np.argmax(y_test, axis=1), np.argmax(y_keras, axis=1)))) print("hls4ml Accuracy: {}".format( accuracy_score(np.argmax(y_test, axis=1), np.argmax(y_hls, axis=1)))) # Bitfile time if bool(our_config['convert']['Build']): np.savetxt('input_data.dat', X_test[:1].reshape(1, -1), fmt='%f', delimiter=' ') np.savetxt('output_predictions.dat', y_keras[:1].reshape(1, -1), fmt='%f', delimiter=' ') hls_model.build(csim=True, synth=True, vsynth=True) #,cosim=True,export=True) hls4ml.report.read_vivado_report(our_config['convert']['OutputDir']) if our_config['convert']['Backend'] == 'Pynq': hls4ml.templates.PynqBackend.make_bitfile(hls_model)