Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Image Classification using CNN #38

Open
@malhosary

Description

@malhosary

Hi,
Thank you for the great work you did. I have the following simple CNN for image classification:
`
featureShape = new Shape(new int[] { imageWidth, imageHeight, 3 });
var inputShape = new Shape(new int[] { imageWidth, imageHeight, 3 });
frameX = new DataFrame(featureShape);

            labelShape = new Shape(new int[] { 3 });
            frameY = new DataFrame(labelShape);

            model = new Sequential(inputShape);
            model.Add(new Conv2D(16, Tuple.Create(3, 3), Tuple.Create(2, 2), activation: new ReLU(), weightInitializer: new SiaNet.Model.Initializers.Xavier(), useBias: true, biasInitializer: new Model.Initializers.Ones()));

            model.Add(new MaxPool2D(Tuple.Create(3, 3)));
            model.Add(new Conv2D(32, Tuple.Create(3, 3), Tuple.Create(2, 2), activation: new ReLU(), weightInitializer: new SiaNet.Model.Initializers.Xavier(), useBias: true, biasInitializer: new Model.Initializers.Ones()));


            model.Add(new MaxPool2D(Tuple.Create(3, 3)));


            model.Add(new Flatten());
            model.Add(new Dense(200, activation: new ReLU()));
           // model.Add(new Dropout(0.5));
            model.Add(new Dense(3, activation: new Softmax()));

var compiledModel = model.Compile();
compiledModel.EpochEnd += CompiledModel_EpochEnd;
compiledModel.BatchStart += CompiledModel_BatchStart;
compiledModel.BatchEnd += CompiledModel_BatchEnd;
compiledModel.TrainingStart += CompiledModel_TrainingStart1;
compiledModel.TrainingEnd += CompiledModel_TrainingEnd1;

            // prepare dataset
            trainingData = new DataFrameList(featureShape, labelShape);
            PrepareDataSet2(@"C:\Authorized Model\Keras Dataset\Fire\", new float[] { 1,0,0 }, true);
            PrepareDataSet2(@"C:\Authorized Model\Keras Dataset\Person\", new float[] { 0, 1, 0 }, true);
            PrepareDataSet2(@"C:\Authorized Model\Keras Dataset\Police\", new float[] { 0,0,1 }, true);

            compiledModel.Fit(trainingData, 40, 150, new Adam(), new CrossEntropy());

and the PrepareDataSet2 method:

    static void PrepareDataSet2(string folderName, float[] classLabel, bool train = true)
    {
        int width = int.Parse(ConfigurationManager.AppSettings["ImageWidth"]);
        int height = int.Parse(ConfigurationManager.AppSettings["ImageHeight"]);
        Console.WriteLine("Loading dataset for class: " + classLabel);
        var files = Directory.EnumerateFiles(folderName, "*.*", SearchOption.AllDirectories)
                    .Where(s => s.ToLower().EndsWith(".jpeg") || s.ToLower().EndsWith(".jpg") || s.ToLower().EndsWith(".bmp") || s.ToLower().EndsWith(".png") || s.ToLower().EndsWith(".gif"));

        foreach (var file in files)
        {
            using (Bitmap full = (Bitmap)Bitmap.FromFile(file))
            {
                using (Bitmap bt = full.Resize(width, height, true))
                {
                    float[] imgMatrix = bt.ParallelExtractCHW().ToArray();
                    //float[] imgMatrix = bt.ToByteArray().ToArray().Select(d => (float)d).ToArray();
                    if (imgMatrix.All(d => d == 0))
                    {
                        throw new Exception("All values in the image re 0!!");
                    }
                    //frameX.Add(imgMatrix);

                   
                    if (train)
                    {
                        trainingData.AddFrame(imgMatrix, classLabel);
                    }
                    else
                    {
                        frameX.Add(imgMatrix);
                    }
                }
            }
        }
        Console.WriteLine("Finished loading dataset for class: " + classLabel);
    }

`
The issue is: i do training for this CNN and i use Predict method to do classification for new images but i always get the same result for prediction (0,0,1).
Appreciate your support?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions