Num Rec app and save & loading
This commit is contained in:
parent
fc20f3594d
commit
b7a89b714e
12 changed files with 8171 additions and 12 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "Archiver.hpp"
|
||||
#include "ConnectionCommon.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
|
@ -49,4 +50,17 @@ namespace tp {
|
|||
Location mLocation;
|
||||
BytePointer mPointer = 0;
|
||||
};
|
||||
|
||||
template <bool tRead>
|
||||
class ArchiverLocalConnection : public ArchiverTemplate<tRead> {
|
||||
public:
|
||||
ArchiverLocalConnection() = default;
|
||||
|
||||
protected:
|
||||
void writeBytes(const int1* val, ualni size) override { connection.writeBytes(val, size); }
|
||||
void readBytes(int1* val, ualni size) override { connection.readBytes(val, size); }
|
||||
|
||||
public:
|
||||
LocalConnection connection;
|
||||
};
|
||||
}
|
||||
|
|
@ -414,7 +414,7 @@ namespace tp {
|
|||
void archiveWrite(tArchiver& ar) const {
|
||||
ar << mLoad;
|
||||
for (auto item : *this) {
|
||||
ar << item.data();
|
||||
ar % item.data();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -428,7 +428,8 @@ namespace tp {
|
|||
if (mLoad == mSize) {
|
||||
resizeBuffer(tResizePolicy(mSize));
|
||||
}
|
||||
ar >> mBuff[mLoad];
|
||||
new (mBuff + mLoad) tType();
|
||||
ar % mBuff[mLoad];
|
||||
mLoad++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ set(CMAKE_CXX_STANDARD 23)
|
|||
|
||||
project(DataAnalysis)
|
||||
|
||||
### ---------------------- Externals --------------------- ###
|
||||
set(BINDINGS_INCLUDE ${EXTERNALS}/glfw/include ${EXTERNALS}/glew/include)
|
||||
set(BINDINGS_LIBS glfw glew_s Imgui Nanovg)
|
||||
|
||||
### ---------------------- Static Library --------------------- ###
|
||||
file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp")
|
||||
file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp" "./applications/*.hpp")
|
||||
|
|
@ -15,10 +19,13 @@ target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
|
|||
target_link_libraries(${PROJECT_NAME} PUBLIC Math Containers Allocators)
|
||||
|
||||
### -------------------------- Applications -------------------------- ###
|
||||
add_executable(NumberRec ./applications/NumberRecognition.cpp)
|
||||
target_link_libraries(NumberRec ${PROJECT_NAME} Connection)
|
||||
add_executable(NumRecTrain applications/NumRecTraining.cpp)
|
||||
target_link_libraries(NumRecTrain ${PROJECT_NAME} Connection ImageIO)
|
||||
file(COPY "applications/rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/")
|
||||
|
||||
add_executable(NumRecApp applications/NumRecApp.cpp)
|
||||
target_link_libraries(NumRecApp ${PROJECT_NAME} Connection ImageIO)
|
||||
|
||||
### -------------------------- Tests -------------------------- ###
|
||||
enable_testing()
|
||||
file(GLOB TEST_SOURCES "./tests/*.cpp" "./tests/*/*.cpp")
|
||||
|
|
|
|||
77
DataAnalysis/applications/NumRecApp.cpp
Normal file
77
DataAnalysis/applications/NumRecApp.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
|
||||
#include "FCNN.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
// #include "NewPlacement.hpp"
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_write.h"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
void loadImage(Buffer<halnf>& output, const char* name) {
|
||||
int x, y, channels_in_file;
|
||||
unsigned char* loadedImage = stbi_load(name, &x, &y, &channels_in_file, 4);
|
||||
|
||||
if (!loadedImage) return;
|
||||
|
||||
output.reserve(x * y);
|
||||
|
||||
for (auto i : Range(output.size())) {
|
||||
output[i] = loadedImage[i * 4] / 255.f;
|
||||
}
|
||||
|
||||
stbi_image_free(loadedImage);
|
||||
}
|
||||
|
||||
void loadNN(FCNN& nn) {
|
||||
ArchiverLocalConnection<true> archiver;
|
||||
|
||||
archiver.connection.connect(LocalConnection::Location("NumRec.wb"), LocalConnection::Type(true));
|
||||
|
||||
if (archiver.connection.getConnectionStatus().isOpened()) {
|
||||
archiver % nn;
|
||||
} else {
|
||||
Buffer<halni> layers = { 784, 10 };
|
||||
nn.initializeRandom(layers);
|
||||
}
|
||||
}
|
||||
|
||||
void executeCmd(const char* imageName) {
|
||||
FCNN nn;
|
||||
Buffer<halnf> output(10);
|
||||
Buffer<halnf> input;
|
||||
|
||||
loadNN(nn);
|
||||
loadImage(input, imageName);
|
||||
|
||||
nn.evaluate(input, output);
|
||||
|
||||
printf("Output - ");
|
||||
for (auto val : output) {
|
||||
printf("%f ", val.data());
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const char* imageName = "digit.png";
|
||||
|
||||
if (argc == 2) {
|
||||
imageName = argv[1];
|
||||
}
|
||||
|
||||
ModuleManifest* deps[] = { &gModuleDataAnalysis, &gModuleConnection, nullptr };
|
||||
ModuleManifest module = ModuleManifest("NumRec", nullptr, nullptr, deps);
|
||||
|
||||
if (!module.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
executeCmd(imageName);
|
||||
|
||||
module.deinitialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,9 +1,27 @@
|
|||
#include "FCNN.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "stb_image_write.h"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
void writeImage(const Buffer<halnf>& image, const char* name) {
|
||||
struct Tmp {
|
||||
uint1 r, g, b, a;
|
||||
};
|
||||
|
||||
Buffer<Tmp> converted;
|
||||
converted.reserve(image.size());
|
||||
|
||||
for (auto i = 0; i < image.size(); i++) {
|
||||
auto val = uint1(image[i] * 255);
|
||||
converted[i] = { val, val, val, 255 };
|
||||
}
|
||||
|
||||
stbi_write_png(name, 28, 28, 4, converted.getBuff(), 28 * 4);
|
||||
}
|
||||
|
||||
struct Dataset {
|
||||
ualni length = 0;
|
||||
Pair<ualni, ualni> imageSize = { 0, 0 };
|
||||
|
|
@ -48,8 +66,20 @@ bool loadDataset(Dataset& out, const String& location) {
|
|||
|
||||
struct NumberRec {
|
||||
NumberRec() {
|
||||
|
||||
// try to load wb file
|
||||
{
|
||||
ArchiverLocalConnection<true> archiver;
|
||||
|
||||
archiver.connection.connect(LocalConnection::Location("NumRec.wb"), LocalConnection::Type(true));
|
||||
|
||||
if (archiver.connection.getConnectionStatus().isOpened()) {
|
||||
archiver % nn;
|
||||
} else {
|
||||
Buffer<halni> layers = { 784, 10 };
|
||||
nn.initializeRandom(layers);
|
||||
}
|
||||
}
|
||||
|
||||
Dataset dataset;
|
||||
|
||||
|
|
@ -79,6 +109,20 @@ struct NumberRec {
|
|||
}
|
||||
|
||||
output.reserve(10);
|
||||
|
||||
writeImage(mTestcases.first().input, "tmp1.png");
|
||||
writeImage(mTestcases.last().input, "tmp2.png");
|
||||
}
|
||||
|
||||
~NumberRec() {
|
||||
// save aas wb file
|
||||
{
|
||||
ArchiverLocalConnection<false> archiver;
|
||||
archiver.connection.connect(LocalConnection::Location("NumRec.wb"), LocalConnection::Type(false));
|
||||
if (archiver.connection.getConnectionStatus().isOpened()) {
|
||||
archiver % nn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
halnf eval(ualni idx) {
|
||||
|
|
@ -179,7 +223,7 @@ int main() {
|
|||
|
||||
auto batchSize = trainRange.idxDiff() / numBatches;
|
||||
|
||||
for (auto epoch : Range(10)) {
|
||||
for (auto epoch : Range(1)) {
|
||||
printf("Epoch %i\n", epoch.index());
|
||||
|
||||
for (auto batchIdx : Range(trainRange.idxDiff() / batchSize)) {
|
||||
|
|
@ -204,7 +248,7 @@ int main() {
|
|||
// app.displayImage(i);
|
||||
}
|
||||
|
||||
printf("\n\nIncorrect - %i out of %i (%f)\n\n", errors, testRange.idxDiff(), (halnf) errors / (halnf) testRange.idxDiff() );
|
||||
printf("\n\nIncorrect - %i out of %i (%f)\n\n", errors, testRange.idxDiff(), (halnf) errors / (halnf) testRange.idxDiff());
|
||||
}
|
||||
|
||||
module.deinitialize();
|
||||
BIN
DataAnalysis/applications/rsc/mnist.gz
Normal file
BIN
DataAnalysis/applications/rsc/mnist.gz
Normal file
Binary file not shown.
|
|
@ -18,6 +18,12 @@ namespace tp {
|
|||
|
||||
halnf val = 0;
|
||||
halnf grad = 0;
|
||||
|
||||
public:
|
||||
template <class tArchiver>
|
||||
void archive(tArchiver& ar) {
|
||||
ar % val;
|
||||
}
|
||||
};
|
||||
|
||||
Parameter bias;
|
||||
|
|
@ -27,9 +33,22 @@ namespace tp {
|
|||
halnf activationValueLinear = 0;
|
||||
|
||||
halnf cache;
|
||||
|
||||
public:
|
||||
template <class tArchiver>
|
||||
void archive(tArchiver& ar) {
|
||||
ar % bias;
|
||||
ar % weights;
|
||||
}
|
||||
};
|
||||
|
||||
Buffer<Neuron> neurons;
|
||||
|
||||
public:
|
||||
template <class tArchiver>
|
||||
void archive(tArchiver& ar) {
|
||||
ar % neurons;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
|
|
@ -45,6 +64,12 @@ namespace tp {
|
|||
void calcGrad(const Buffer<halnf>& output);
|
||||
void applyGrad(halnf step);
|
||||
|
||||
public:
|
||||
template <class tArchiver>
|
||||
void archive(tArchiver& ar) {
|
||||
ar % mLayers;
|
||||
}
|
||||
|
||||
public:
|
||||
Buffer<Layer> mLayers;
|
||||
halni mAvgCount = 0;
|
||||
|
|
|
|||
4
Externals/CMakeLists.txt
vendored
4
Externals/CMakeLists.txt
vendored
|
|
@ -46,3 +46,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
|
|||
C_STANDARD 99
|
||||
C_STANDARD_REQUIRED ON
|
||||
)
|
||||
|
||||
project(ImageIO)
|
||||
add_library(${PROJECT_NAME} INTERFACE)
|
||||
target_include_directories(${PROJECT_NAME} INTERFACE ./imageIO/)
|
||||
7987
Externals/imageIO/stb_image.h
vendored
Normal file
7987
Externals/imageIO/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -18,7 +18,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Math CommandLine Connection)
|
|||
add_executable(rayt ./applications/Rayt.cpp applications/SceneLoad.cpp
|
||||
applications/Rayt.hpp)
|
||||
|
||||
target_link_libraries(rayt ${PROJECT_NAME} Lua)
|
||||
target_link_libraries(rayt ${PROJECT_NAME} Lua ImageIO)
|
||||
|
||||
file(COPY "applications/rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/")
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
|
|||
|
||||
target_include_directories(${PROJECT_NAME}Tests PUBLIC ./applications/)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils)
|
||||
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils ImageIO)
|
||||
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/lib)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue