bhiziroglu / image-classification-with-deep-convolutional-neural-networks Goto Github PK
View Code? Open in Web Editor NEWC++ / LibTorch implementation of AlexNet
C++ / LibTorch implementation of AlexNet
Hi, thanks for sharing your code. It really helps me a lot.
I try to add following code in the function pair<Tensor, Tensor> Cifar::read_data
, and expect to get one of the image files :
Tensor tmp=images[i].clone();
tmp=tmp.permute({1,2,0}).to(torch::kU8);
cv::Mat image(32,32,CV_8UC3);
std::memcpy((void *)image.data,tmp.data_ptr(),sizeof(torch::kU8)*tmp.numel());
char imgpath[814];
sprintf(imgpath,"/my/path/%d.png",i+1);
cv::imwrite(imgpath,image);
However, I always get an image file that looks non sense.
I don't know what's wrong, because transforming between Tensor and Mat is too complex for me. So I try to implement my own read_data function, and the problem is gone. It turns out the image above is from a frog.
Here is my code
pair<Tensor, Tensor> Cifar::read_data(const string &root, int train){
int num_samples;
vector<string> files;
if(train==0){
files = trainBatchFiles;
num_samples = 50000;
}else{
files = testBatchFiles;
num_samples = 10000;
}
auto images =torch::empty({num_samples, 3,32,32}, torch::kByte);
auto targets = torch::empty(num_samples, torch::kByte);
int labelr(0);
Mat image(32,32,CV_8UC3,cv::Scalar::all(0));
char* pData=(char*)image.data;
int tot_img=0;
for (const auto &file : files)
{
const auto path = join_paths(root, file);
FILE * fptr=fopen(path.c_str(),"rb");
if(fptr== nullptr){
cout<<"Open error!"<<endl;
fclose(fptr);
continue;
}
for(int id_img=0; id_img<1e4;id_img++,tot_img++){
fread(&labelr,sizeof(char),1,fptr);
//cout<<labelr<<endl;
for(int id_pic=0;id_pic<32*32;id_pic++){
fread(pData+id_pic*3+2,sizeof(char),1,fptr);
}
for(int id_pic=0;id_pic<32*32;id_pic++){
fread(pData+id_pic*3+1,sizeof(char),1,fptr);
}
for(int id_pic=0;id_pic<32*32;id_pic++){
fread(pData+id_pic*3,sizeof(char),1,fptr);
}
images[tot_img]=torch::from_blob(image.data,{3,32,32},torch::kByte);
targets[tot_img]=labelr;
}
}
return {images.to(torch::kFloat32).div_(255), targets.to(torch::kInt64)};
}
As far as I am concerned, I will appreciate it if you could check these code, and leave me some hint about what causes the noise in the image exported.
Thank you!
Thank you for sharing the project! It is a good practice to walk-through the process. I followed your instruction and finished the training.My understanding is the AlexNet intends to do the object detection and classification. One question is How to use your program to do inference for a given image?
The main() does not take any argument, so I do not know what to do next. It would be great if you can shed some light on it.
This is not an issue, rather it is a question for advice.
how can i training my own dataset?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.