Caffe - How to imbalance Cifar10 data - machine-learning

I'm doing a research on the impact of imbalanced data with caffe framework. Now I am trying to make a new cifar10 distribution by trying to remove some of the data from specified class. I read the document of cifar10. It said that the .bin file has a data structure like
1*8 bit label data | 3*1024 for RGB pixel
So I write down a script to filter out the data for those class. And make a new .bin file.
Now I run the script on caffe and try to make a LMDB dataset
#!/usr/bin/env sh
# This script converts the cifar data into leveldb format.
EXAMPLE=examples/cifar10
DATA=data/cifar10
DBTYPE=lmdb
echo "Creating $DBTYPE..."
rm -rf $EXAMPLE/cifar10_train_$DBTYPE $EXAMPLE/cifar10_test_$DBTYPE
./build/examples/cifar10/convert_cifar_data.bin $DATA $EXAMPLE $DBTYPE
echo "Computing image mean..."
./build/tools/compute_image_mean -backend=$DBTYPE \
$EXAMPLE/cifar10_train_$DBTYPE $EXAMPLE/mean.binaryproto
echo "Done."
However After I filter out those data. It seems that the IMDB still has the same size and doesn't look any different than the one without filtered. Can somebody tell me what should I do to make the data imbalanced?

Related

Yolo training yolo with own dataset

I want to build a database with Yolo and this is my first time working with deep learning
how can I build a database for Yolo and train it?
How do I get the weights of the classifications?
Is it too difficult for someone new to Deep Learning?
Yes you can do it with ease!! and welcome to the Deep learning Community. You are welcome.
First download the darknet folder from Link
Go inside the folder and type make in command prompt
git clone https://github.com/pjreddie/darknet
cd darknet
make
Define these files -
data/custom.names
data/images
data/train.txt
data/test.txt
Now its time to label the images using LabelImg and save it in YOLO format which will generate corresponding label .txt files for the images dataset.
Labels of our objects should be saved in data/custom.names.
Using the script you can split the dataset into train and test-
import glob, os
dataset_path = '/media/subham/Data1/deep_learning/usecase/yolov3/images'
# Percentage of images to be used for the test set
percentage_test = 20
# Create and/or truncate train.txt and test.txt
file_train = open('train.txt', 'w')
file_test = open('test.txt', 'w')
# Populate train.txt and test.txt
counter = 1
index_test = round(100 / percentage_test)
for pathAndFilename in glob.iglob(os.path.join(dataset_path, "*.jpg")):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
if counter == index_test+1:
counter = 1
file_test.write(dataset_path + "/" + title + '.jpg' + "\n")
else:
file_train.write(dataset_path + "/" + title + '.jpg' + "\n")
counter = counter + 1
For train our object detector we can use the existing pre trained weights that are already trained on huge data sets. From here we can download the pre trained weights to the root directory.
Create a yolo-custom.data file in the custom_data directory which should contain information regarding the train and test data sets
classes=2
train=custom_data/train.txt
valid=custom_data/test.txt
names=custom_data/custom.names
backup=backup/
Now we have to make changes in our yolov3.cfg for training our model. For two classes. Based on the required performance we can select the YOLOv3 configuration file. For this example we will be using yolov3.cfg. We can duplicate the file from cfg/yolov3.cfg to custom_data/cfg/yolov3-custom.cfg
The maximum number of iterations for which our network should be trained is set with the param max_batches=4000. Also update steps=3200,3600 which is 80%, 90% of max_batches.
We will need to update the classes and filters params of [yolo] and [convolutional] layers that are just before the [yolo] layers.
In this example since we have a single class (tesla) we will update the classes param in the [yolo] layers to 1 at line numbers: 610, 696, 783
Similarly we will need to update the filters param based on the classes count filters=(classes + 5) * 3. For two classes we should set filters=21 at line numbers: 603, 689, 776
All the configuration changes are made to custom_data/cfg/yolov3-custom.cfg
Now, we have defined all the necessary items for training the YOLOv3 model. To train-
./darknet detector train custom_data/detector.data custom_data/cfg/yolov3-custom.cfg darknet53.conv.74
Also you can mark bounded boxes of objects in images for training Yolo right in your web browser, just open url. This tool is deployed to GitHub Pages.
Use this popular forked darknet repository https://github.com/AlexeyAB/darknet. The author describes many steps that will help you to build and use your own Yolo detector model.
How to build your own custom dataset and train it? Follow this step . He suggests to use Yolo Mark labeling tool to build your dataset, but you can also try another tool as described in here and here.
How to get the weights? The weights will be stored in darknet/backup/ directory after every 1000 iterations (you can adjust this value later). The link above explains everything about how to make and use the weights file.
I don't think it will be so difficult if you already know math, statistic and programming. Learning the basic neural network like perceptron, MLP then move to modern Machine Learning is a good start. Then you might want to expand your knowledge to Computer Vision related or NLP related area
Depending on what kind of OS you have. You can either hit up https://github.com/AlexeyAB/darknet [especially for Windows] or stick to https://github.com/pjreddie/darknet.
Steps to do so:
1) Setup darknet as detailed in the posts.
2) I used LabelIMG to label my images. make sure that the format
you save the images is in YOLO. If you save using the PascalVOC format or others you can write scripts to change it to the format that darknet expects.[YOLO]. Also, make sure that you do not change your labels file. If you want to add new labels, at it at the end of the file, not in between. YOLO format is quite different, so your previously labelled images may get messed up if you make changes in between the classes.
3)The weights will be generated as you train your model in a specific folder in darknet.[If you need more details I am happy to help answer that]. You can download the .74 file in YOLO and start training. The input to train needs a built darknet.exe a cfg file a .74 file and your training data location/access.
The setup is draconian, the process itself is not.
To build your own dataset, you should use LabelImg. It's a free and very easy software which will produce for you all the files you need to build a dataset. In fact, because you are working with yolo, you need a txt file for each of your image which will contain important information like bbox coordinates, label name. All these txt files are automatically produced by LabelImg so all you have to do is open the directory which contains all your images with LabelImg, and start the labelisation. Then, well you will have all your txt files, you will also need to create some other files in order to start training (see https://blog.francium.tech/custom-object-training-and-detection-with-yolov3-darknet-and-opencv-41542f2ff44e).

Caffe's way of doing data shuffling

Is shuffling done by setting the flag --shuffle as below as found in create_imagenet.sh ? :
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
I mean I don't need to shuffle it manually afterwards, if the flag does it already. What about the label, is it shuffled automatically in the generated lmdb file?
Using convert_imageset tool creates a copy of your training/validation data in a binary database file (either in lmdb or leveldb format). The data encoded in the dataset includes pairs of example and its corresponding label.
Therefore, when shuffle-ing the dataset the labels are shuffled with the data to maintain the correspondence between data and its ground-truth label.
There is no need to shuffle the data again during training.

Convert retrained data into .pb format for ios camera example?

I retrained image data using the tutorial at ((https://www.tensorflow.org/versions/r0.9/how_tos/image_retraining/index.html)) I did all the steps until
bazel build tensorflow/examples/image_retraining:retrain
. I was wondering how to turn this image training data into a .pb file that I can use in the ios camera example.
Thank you for your help!
I believe it's a two step process.
Export model definition and weights:
a. The graphdef (*.pb) using tf.train.write_graph: https://www.tensorflow.org/versions/r0.11/api_docs/cc/index.html
b. The weights (*.ckpt) using tf.train.Saver: https://www.tensorflow.org/versions/r0.11/api_docs/python/state_ops.html#Saver
Merge the two components above into one protobuf (final *.pb) using the following script: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

Where can I find the label map between trained model like googleNet's output to there real class label?

everyone, I am new to caffe. Currently, I try to use the trained GoogleNet which was downloaded from model zoo to classify some images. However, the network's output seem to be a vector rather than real label(like dog, cat).
Where can I find the label-map between trained model like googleNet's output to their real class label?
Thanks.
If you got caffe from git you should find in data/ilsvrc12 folder a shell script get_ilsvrc_aux.sh.
This script should download several files used for ilsvrc (sub set of imagenet used for the large scale image recognition challenge) training.
The most interesting file (for you) that will be downloaded is synset_words.txt, this file has 1000 lines, one line per class identified by the net.
The format of the line is
nXXXXXXXX description of class

Weka: Multiclass classification for Text documents giving abnormal result

I am new to Weka. I am trying to classify text documents after OCR process. The training corpus contains 286 mortgage documents and 57 note documents. The test dataset contains 1-100 text pages. So each line of the training and test dataset contains few paragraphs of text data. After classification text documents should be classified into mortgage or note properly.
I am doing a StringToWordVector operation combining both Training and Test dataset with missing values from Test dataset i.e. "?".
Steps are as follows:
Create training Arff file using following command line:
java -cp weka.jar weka.core.converters.TextDirectoryLoader -dir <text directory>
This creates a training dataset with known classes i.e. mortgage, note
Create test Arff file with missing classes i.e "?"
Combine both training and test dataset
Run the classifier with following command line:
java -cp weka.jar weka.classifiers.meta.FilteredClassifier -t train.arff -test.arff -F "weka.filters.MultiFilter -F weka.filters.unsupervised.attribute.StringToWordVector -F weka.filters.unsupervised.attribute.Standardize" -d trained.model -p 0
I am running the above example from both Weka GUI and from command line as well. Everything works fine as far as commands are concerned. The results are abnormal. Not at all correct.
I have also tried to run StringToWordVector operation separately and tested through NaiveBayes, NaiveBayesMultiNomial, J48 and other multiclass classifiers on the dataset but classification prediction is not correct. Always giving abnormal results.
Please help me to get the proper prediction result. Let me know if the above steps are correct and if I am doing anything wrong.

Resources