Skip to content

Commit

Permalink
Upsampling2D test case (fastmachinelearning#520)
Browse files Browse the repository at this point in the history
* using keras upsampling2d method

* Update nnet_image_stream.h

* reindent and remove commends

* add UpSampling2D to the supported layers

* typo

* testing upsampling2d iostream and io parallel

* Delete test_upsampling2d.py

* pytest for upsampling2d iostream and io parallel

both are passed

* Delete test_upsampling2d.py
  • Loading branch information
ChiRuiChen authored Apr 28, 2022
1 parent 501650b commit dde1f80
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
107 changes: 64 additions & 43 deletions hls4ml/templates/vivado/nnet_utils/nnet_image_stream.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
#ifndef NNET_IMAGE_STREAM_H_
#define NNET_IMAGE_STREAM_H_

#include "nnet_common.h"
#include "hls_stream.h"

namespace nnet {

template<class data_T, typename CONFIG_T>
void resize_nearest(
hls::stream<data_T> &image,
hls::stream<data_T> &resized
) {
assert(CONFIG_T::new_height % CONFIG_T::height == 0);
assert(CONFIG_T::new_width % CONFIG_T::width == 0);
constexpr unsigned ratio_height = CONFIG_T::new_height / CONFIG_T::height;
constexpr unsigned ratio_width = CONFIG_T::new_width / CONFIG_T::width;
constexpr unsigned ii = ratio_height * ratio_width;

ResizeImage: for (unsigned i = 0; i < CONFIG_T::height * CONFIG_T::width; i++) {
#pragma HLS PIPELINE II=ii

data_T in_data = image.read();

ResizeNew: for (unsigned j = 0; j < ratio_height * ratio_width; j++) {
#pragma HLS UNROLL

data_T out_data;
#pragma HLS DATA_PACK variable=out_data

ResizeChan: for (unsigned k = 0; k < CONFIG_T::n_chan; k++) {
#pragma HLS UNROLL
out_data[k] = in_data[k];
}

resized.write(out_data);
}
}
}

}

#endif
#ifndef NNET_IMAGE_STREAM_H_
#define NNET_IMAGE_STREAM_H_

#include "nnet_common.h"
#include "hls_stream.h"

namespace nnet {

template<class data_T, typename CONFIG_T>
void resize_nearest(
hls::stream<data_T> &image,
hls::stream<data_T> &resized
) {
assert(CONFIG_T::new_height % CONFIG_T::height == 0);
assert(CONFIG_T::new_width % CONFIG_T::width == 0);
constexpr unsigned ratio_height = CONFIG_T::new_height / CONFIG_T::height;
constexpr unsigned ratio_width = CONFIG_T::new_width / CONFIG_T::width;


ImageHeight: for (unsigned h = 0; h < CONFIG_T::height; h++) {
#pragma HLS PIPELINE

data_T data_in_row[CONFIG_T::width];

ImageWidth: for (unsigned i = 0; i < CONFIG_T::width; i++) {
#pragma HLS UNROLL

data_T in_data = image.read();

ImageChan: for (unsigned j = 0; j < CONFIG_T::n_chan; j++) {
#pragma HLS UNROLL

data_in_row[i][j] = in_data[j];
}
}

ResizeHeight: for (unsigned i = 0; i <ratio_height; i++) {
#pragma HLS UNROLL

ImageWidth2: for (unsigned l = 0; l < CONFIG_T::width; l++) {
#pragma HLS UNROLL

ResizeWidth: for (unsigned j = 0; j < ratio_width; j++) {
#pragma HLS UNROLL

data_T out_data;
#pragma HLS DATA_PACK variable=out_data

ResizeChan: for (unsigned k = 0; k < CONFIG_T::n_chan; k++) {
#pragma HLS UNROLL

out_data[k] = data_in_row[l][k];
}

resized.write(out_data);
}
}
}
}
}

}

#endif
2 changes: 1 addition & 1 deletion hls4ml/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def config_from_keras_model(model, granularity='model', default_precision='ap_fi
model_arch = json.loads(model.to_json())

#Define supported layers
core_layers = ['InputLayer', 'Dropout', 'Flatten', 'Reshape', 'Permute']
core_layers = ['InputLayer', 'Dropout', 'Flatten', 'Reshape', 'Permute', 'UpSampling2D']
dense_layers = ['Dense', 'BinaryDense', 'TernaryDense']
conv_layers = ['Conv1D', 'Conv2D', 'BinaryConv2D']
pooling_layers = ['MaxPooling1D', 'MaxPooling2D', 'GlobalMaxPooling1D', 'GlobalMaxPooling2D', 'AveragePooling1D', 'AveragePooling2D', 'GlobalAveragePooling1D', 'GlobalAveragePooling2D']
Expand Down

0 comments on commit dde1f80

Please sign in to comment.