Skip to content

Commit

Permalink
Fix bad link between cells
Browse files Browse the repository at this point in the history
  • Loading branch information
vtemplier committed May 3, 2023
2 parents 7f19303 + 46fb934 commit 84c41e5
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
6 changes: 6 additions & 0 deletions include/Cell/Cell_Frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class Cell_Frame : public virtual Cell, public Cell_Frame_Top {
virtual void clearOutputTensors();
virtual void initializeParameters(unsigned int /*nbChannels*/, unsigned int /*nbInputs*/){};
virtual void initializeDataDependent();
virtual void addLinkInput(Cell* cell);
virtual void addLinkInput(StimuliProvider& sp,
unsigned int x0,
unsigned int y0,
unsigned int width,
unsigned int height);
virtual void linkInput(Cell* cell);
virtual void linkInput(StimuliProvider& sp,
unsigned int x0,
Expand Down
6 changes: 6 additions & 0 deletions include/Cell/Cell_Frame_CUDA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class Cell_Frame_CUDA : public virtual Cell, public Cell_Frame_Top {
virtual void clearOutputTensors();
virtual void initializeParameters(unsigned int /*nbChannels*/, unsigned int /*nbInputs*/){};
virtual void initializeDataDependent();
virtual void addLinkInput(Cell* cell);
virtual void addLinkInput(StimuliProvider& sp,
unsigned int x0,
unsigned int y0,
unsigned int width,
unsigned int height);
virtual void linkInput(Cell* cell);
virtual void linkInput(StimuliProvider& sp,
unsigned int x0,
Expand Down
12 changes: 7 additions & 5 deletions python/n2d2/cells/nn/abstract_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def add_input(self, inputs: Union[Interface, Tensor]):
# Check for z dimension consistency
#print('dim_z = ', dim_z)
#print('and self.get_nb_channels()', self.get_nb_channels())
#if not dim_z == self.get_nb_channels():
# raise RuntimeError("Total number of input dimZ != cell '" + self.get_name() + "' number channels")
if not dim_z == self.get_nb_channels():
raise RuntimeError("Total number of input dimZ != cell '" + self.get_name() + "' number channels")
elif self.mappable and self._N2D2_object.getMapping().empty():
self._N2D2_object.setMapping(Tensor([self.get_nb_outputs(), inputs.dimZ()], value=True,
datatype="bool", dim_format="N2D2").N2D2())
Expand All @@ -252,9 +252,11 @@ def add_input(self, inputs: Union[Interface, Tensor]):
# cells created by Interfaces out of any deepnet at initialization have an empty
# getData() method so data are manually passed to the input tensor
if isinstance(cell, MultipleOutputsProvider) or isinstance(inputs, Interface):
diffOutput = Tensor(ipt.dims(), value=0, dim_format="N2D2")
self._N2D2_object.addInputBis(ipt.N2D2(), diffOutput.N2D2())
#self._N2D2_object.linkInput(cell.N2D2())
if isinstance(cell.N2D2(), N2D2.StimuliProvider):
diffOutput = Tensor(ipt.dims(), value=0, dim_format="N2D2")
self._N2D2_object.addInputBis(ipt.N2D2(), diffOutput.N2D2())
else:
self._N2D2_object.addLinkInput(cell.N2D2())
else:
self._N2D2_object.linkInput(cell.N2D2())
if not initialized:
Expand Down
68 changes: 68 additions & 0 deletions src/Cell/Cell_Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,74 @@ void N2D2::Cell_Frame<T>::initializeDataDependent()
}
}

template <class T>
void N2D2::Cell_Frame<T>::addLinkInput(Cell* cell)
{
// Define input-output sizes
setInputsDims(cell->getOutputsDims());

Cell_Frame_Top* cellFrame = dynamic_cast<Cell_Frame_Top*>(cell);

if (cellFrame != NULL) {
mInputs.push_back(&cellFrame->getOutputs());
mDiffOutputs.push_back(&cellFrame->getDiffInputs());
}
else {
throw std::runtime_error(
"Cell_Frame<T>::addLinkInput(): cell is NULL!");
}

setOutputsDims();

if (mOutputs.empty()) {
std::vector<size_t> outputsDims(mOutputsDims);
outputsDims.push_back(mInputs.dimB());

mOutputs.resize(outputsDims);
mDiffInputs.resize(outputsDims);
}

mMapping.resize({getNbOutputs(), getNbChannels()}, true);
}

template <class T>
void N2D2::Cell_Frame<T>::addLinkInput(StimuliProvider& sp,
unsigned int x0,
unsigned int y0,
unsigned int width,
unsigned int height)
{
if (width == 0)
width = sp.getSizeX() - x0;
if (height == 0)
height = sp.getSizeY() - y0;

if (x0 > 0 || y0 > 0 || width < sp.getSizeX() || height < sp.getSizeY())
throw std::runtime_error("Cell_Frame<T>::linkInput(): adding a cropped "
"environment channel map as input is not "
"supported");

/*if (sp.getNbChannels() != getNbChannels()){
throw std::runtime_error("Cell has different number of channels than input");
}*/

// Define input-output sizes
setInputsDims(sp.getSize());
mInputs.push_back(&sp.getData());
mDiffOutputs.push_back(new Tensor<T>(mInputs[0].dims()), 0);

setOutputsDims();

if (mOutputs.empty()) {
std::vector<size_t> outputsDims(mOutputsDims);
outputsDims.push_back(mInputs.dimB());

mOutputs.resize(outputsDims);
mDiffInputs.resize(outputsDims);
}

}

/**
* Link an input that has to be of the same size as the current input dimensions of the cell.
* If the current input dimensions are empty, the input dimensions are initialized to
Expand Down
68 changes: 68 additions & 0 deletions src/Cell/Cell_Frame_CUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,74 @@ void N2D2::Cell_Frame_CUDA<T>::initializeDataDependent()
}
}

template <class T>
void N2D2::Cell_Frame_CUDA<T>::addLinkInput(Cell* cell)
{
// Define input-output sizes
setInputsDims(cell->getOutputsDims());

Cell_Frame_Top* cellFrame = dynamic_cast<Cell_Frame_Top*>(cell);

if (cellFrame != NULL) {
mInputs.push_back(&cellFrame->getOutputs());
mDiffOutputs.push_back(&cellFrame->getDiffInputs());
}
else {
throw std::runtime_error(
"Cell_Frame<T>::addLinkInput(): cell is NULL!");
}

setOutputsDims();

if (mOutputs.empty()) {
std::vector<size_t> outputsDims(mOutputsDims);
outputsDims.push_back(mInputs.dimB());

mOutputs.resize(outputsDims);
mDiffInputs.resize(outputsDims);
}

mMapping.resize({getNbOutputs(), getNbChannels()}, true);
}

template <class T>
void N2D2::Cell_Frame_CUDA<T>::addLinkInput(StimuliProvider& sp,
unsigned int x0,
unsigned int y0,
unsigned int width,
unsigned int height)
{
if (width == 0)
width = sp.getSizeX() - x0;
if (height == 0)
height = sp.getSizeY() - y0;

if (x0 > 0 || y0 > 0 || width < sp.getSizeX() || height < sp.getSizeY())
throw std::runtime_error("Cell_Frame<T>::linkInput(): adding a cropped "
"environment channel map as input is not "
"supported");

/*if (sp.getNbChannels() != getNbChannels()){
throw std::runtime_error("Cell has different number of channels than input");
}*/

// Define input-output sizes
setInputsDims(sp.getSize());
mInputs.push_back(&sp.getData());
mDiffOutputs.push_back(new Tensor<T>(mInputs[0].dims()), 0);

setOutputsDims();

if (mOutputs.empty()) {
std::vector<size_t> outputsDims(mOutputsDims);
outputsDims.push_back(mInputs.dimB());

mOutputs.resize(outputsDims);
mDiffInputs.resize(outputsDims);
}

}

/**
* Link an input that has to be of the same size as the current input dimensions of the cell.
* If the current input dimensions are empty, the input dimensions are initialized to
Expand Down
3 changes: 3 additions & 0 deletions src/python/Cell/pybind_Cell_Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void declare_Cell_Frame(py::module &m, const std::string& typeStr) {
.def("clearInputTensors", &Cell_Frame<T>::clearInputTensors)
.def("clearOutputTensors", &Cell_Frame<T>::clearOutputTensors)
.def("initializeParameters", &Cell_Frame<T>::initializeParameters, py::arg("nbInputChannels"), py::arg("nbInputs"))
.def("addLinkInput", (void (Cell_Frame<T>::*)(Cell*)) &Cell_Frame<T>::addLinkInput, py::arg("cell"))
.def("addLinkInput", (void (Cell_Frame<T>::*)(StimuliProvider&, unsigned int, unsigned int, unsigned int, unsigned int)) &Cell_Frame<T>::addLinkInput,
py::arg("sp"), py::arg("x0")=0, py::arg("y0")=0, py::arg("width")=0, py::arg("height")=0)
.def("linkInput", (void (Cell_Frame<T>::*)(Cell*)) &Cell_Frame<T>::linkInput, py::arg("cell"))
.def("linkInput", (void (Cell_Frame<T>::*)(StimuliProvider&, unsigned int, unsigned int, unsigned int, unsigned int)) &Cell_Frame<T>::linkInput,
py::arg("sp"), py::arg("x0")=0, py::arg("y0")=0, py::arg("width")=0, py::arg("height")=0)
Expand Down
3 changes: 3 additions & 0 deletions src/python/Cell/pybind_Cell_Frame_CUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ void declare_Cell_Frame_CUDA(py::module &m, const std::string& typeStr) {
.def("clearInputTensors", &Cell_Frame_CUDA<T>::clearInputTensors)
.def("clearOutputTensors", &Cell_Frame_CUDA<T>::clearOutputTensors)
.def("initializeParameters", &Cell_Frame_CUDA<T>::initializeParameters, py::arg("nbInputChannels"), py::arg("nbInputs"))
.def("addLinkInput", (void (Cell_Frame_CUDA<T>::*)(Cell*)) &Cell_Frame_CUDA<T>::addLinkInput, py::arg("cell"))
.def("addLinkInput", (void (Cell_Frame_CUDA<T>::*)(StimuliProvider&, unsigned int, unsigned int, unsigned int, unsigned int)) &Cell_Frame_CUDA<T>::addLinkInput,
py::arg("sp"), py::arg("x0")=0, py::arg("y0")=0, py::arg("width")=0, py::arg("height")=0)
.def("linkInput", (void (Cell_Frame_CUDA<T>::*)(Cell*)) &Cell_Frame_CUDA<T>::linkInput, py::arg("cell"))
.def("linkInput", (void (Cell_Frame_CUDA<T>::*)(StimuliProvider&, unsigned int, unsigned int, unsigned int, unsigned int)) &Cell_Frame_CUDA<T>::linkInput,
py::arg("sp"), py::arg("x0")=0, py::arg("y0")=0, py::arg("width")=0, py::arg("height")=0)
Expand Down

0 comments on commit 84c41e5

Please sign in to comment.