Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fatal error: elements.count must be greater than or equal to shape.volume: elements.count = 1024, shape.volume = 3136: #3

Closed
LinShiwei opened this issue Jul 13, 2016 · 7 comments

Comments

@LinShiwei
Copy link

LinShiwei commented Jul 13, 2016

I have used my own images to trained by Tensorflow's DeepMnist example code.
And I used the way you give in the other issue to output the Models.
But when I replace your Models with mine and run, I meet the error:

fatal error: elements.count must be greater than or equal to shape.volume: elements.count = 1024, shape.volume = 3136:

I think maybe there is something wrong with my models.
But the DeepMnist training is successful:
2016-07-13 10 26 21
And the output of models is successful, too.

What can I do to solve this problem? In order to replace the models successfully, should I do something with the Classifier Struct?

@koher
Copy link
Member

koher commented Jul 13, 2016

I guess you have some mismatch of shapes between in Python and in Swift. Probably 3136 means 7 * 7 * 64. It is the input size of FC1 in "Deep MNIST for Experts". But your tensor seems to have only 1024 elements.

@LinShiwei
Copy link
Author

Do the models all convert in this way:

import struct

ndarray = W_conv1.eval(session=sess)
list = ndarray.reshape([-1]).tolist()
f = open("W_conv1", "wb")
f.write(struct.pack("%df" % len(list), *list))
f.close()

Only change the name 'W_conv1' to another?

@koher
Copy link
Member

koher commented Jul 13, 2016

I guess you have something wrong about the shapes of the tensors. Could you show me both of your codes, in Python and in Swift?

@LinShiwei
Copy link
Author

Deep mnist code I use is here.
I only train and test 36 images.
And my swift classifier: function:

    private func classify(plate plate:UIImage,withNSValueArray array:NSMutableArray)->String{
        var result = ""
        for rectValue in array.reverse(){
            if rectValue is NSValue {
                let image = OpenCVWrapper.cutOutCharFrom(plate, withRectValue: rectValue as! NSValue)
                let grayImage = OpenCVWrapper.convertBGR2GRAY(image)
                let cgImage = grayImage.CGImage
                var pixels = [UInt8](count: inputSize * inputSize, repeatedValue: 0)

                let context  = CGBitmapContextCreate(&pixels, inputSize, inputSize, 8, inputSize, CGColorSpaceCreateDeviceGray()!, CGBitmapInfo.ByteOrderDefault.rawValue)!
                CGContextClearRect(context, CGRect(x: 0.0, y: 0.0, width: CGFloat(inputSize), height: CGFloat(inputSize)))

                let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(inputSize), height: CGFloat(inputSize))
                CGContextDrawImage(context, rect, cgImage)
                let input : Tensor

                input = Tensor(shape: [Dimension(inputSize), Dimension(inputSize), 1], elements: pixels.map { (Float($0) / 255.0 )})

                let resultInt = classifier.classify(input)
                result += String(resultInt)
            }

        }
        print("result is\(result)")
        return result
    }

Before I replace the models , it runs successfully , but the accuracy is not good enough. So I try to use my models but it doesn't work.

@koher
Copy link
Member

koher commented Jul 13, 2016

Is the classifier identical to this? Then the inputSize must be 28.

I guess the inputSize is 16 because you got the following error.

fatal error: elements.count must be greater than or equal to shape.volume: elements.count = 1024, shape.volume = 3136:

16x16x1 -> 8x8x32 -> 4x4x64 = 1024

@LinShiwei
Copy link
Author

I got it. How careless I am !

I changed the inputSize to 14 a week ago and forgot to change it back.

Thank you very much!

By the way, I find that no matter the inputSize is 14 or 16, the error message is the same as following.

fatal error: elements.count must be greater than or equal to shape.volume: elements.count = 1024, shape.volume = 3136:

@koher
Copy link
Member

koher commented Jul 14, 2016

👍

By the way, I find that no matter the inputSize is 14 or 16, the error message is the same as following.

Because of 14x14x1 -> 7x7x32 -> 4x4x64 = 1024.

@koher koher closed this as completed Jul 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants