dedhiaparth98 / capsule-network

A TensorFlow implementation of Capsule Network as described in the paper Dynamic Routing Between Capsules
https://towardsdatascience.com/implementing-capsule-network-in-tensorflow-11e4cca5ecae
MIT License
20 stars 15 forks source link

train on my own dataset #2

Open fatemeh1291374 opened 3 years ago

fatemeh1291374 commented 3 years ago

Hello. Thank you very much for your publishing the code.

I want to use your model for my own dataset, but I get this error. My data is exactly similar to mnist in shape, type, dtype.

ValueError: Negative dimension size caused by subtracting 9 from 1 for '{{node ConvolutionLayer/Conv2D/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](ConvolutionLayer/Conv2D/Reshape, ConvolutionLayer/Conv2D/Conv2D/ReadVariableOp)' with input shapes: [896,28,1,1], [9,9,1,256].

Thanks a lot. Best regards

fatemeh1291374 commented 3 years ago

I solved this problem but I have another question. My accuracy in 50 epochs is constant on 0.5008892841262783. Why is it so low?

dedhiaparth98 commented 3 years ago

What is the size of the dataset, both image size as well as # of sample ? And how many training parameters do you have in your model ?

fatemeh1291374 commented 3 years ago

Size of the my dataset is (28,28,1) with 2 classes. 2245 samples for each of the classes in training and 250 samples (from each classes) for validation. training parameters are 6,970,384.

shaikhsharukh commented 2 years ago

Hi @dedhiaparth98 , My data attribute is as below image I get the below error could you help, please?

ValueError: One of the dimensions in the output is <= 0 due to downsampling in PrimaryCapsule. Consider increasing the input size.

Thanks!

Luke115 commented 2 years ago

I have image size of (4184,220,110) dataset, than what parameters to use, getting error from below,

ValueError: Exception encountered when calling layer "capsule_network_6" (type CapsuleNetwork).

in user code:

File "<ipython-input-38-1bf0f25ce173>", line 41, in call  *
    u_hat = tf.matmul(self.w, u) # u_hat.shape: (None, 1152, 10, 16, 1)

ValueError: Dimensions must be equal, but are 1152 and 137088 for '{{node CapsuleFormation/MatMul}} = BatchMatMulV2[T=DT_FLOAT, adj_x=false, adj_y=false](CapsuleFormation/MatMul/ReadVariableOp, CapsuleFormation/ExpandDims_1)' with input shapes: [1,1152,10,16,8], [32,137088,1,8,1].

Call arguments received: • inputs=['tf.Tensor(shape=(32, 220, 100, 1), dtype=float32)', 'tf.Tensor(shape=(32, 10), dtype=float32)']

dedhiaparth98 commented 2 years ago

@Luke115 The issues in your case are because of that number 1152 in the line u_hat = tf.matmul(self.w, u) # u_hat.shape: (None, 1152, 10, 16, 1) and any occurrences after that. That number is fixed based on the input and the number of secondary capsule. I would request you to go through the blog link which explains the calculation of the same.

Let me know if this helps you with the problem :smile:

dedhiaparth98 commented 2 years ago

@shaikhsharukh please can you paste the entire error here.

dedhiaparth98 commented 2 years ago

@fatemeh1291374 Sorry for late reply.

Size of the my dataset is (28,28,1) with 2 classes. 2245 samples for each of the classes in training and 250 samples (from each classes) for validation.
training parameters are 6,970,384.

If you see here, then the number of samples for each class is very less. Capsule network uses MNIST which has around 5K images per class ( on an average ) after separating the validation set. Since your dataset size is comparably less, I would recommend increasing the dataset size. If that seems to be infeasible then I would recommend using some augmentation function and then passing the same to the model.

Hope this help!!

Luke115 commented 2 years ago

Hi @dedhiaparth98 Your suggestion are clear to me, suppose I have dataset with image size of (100,100,1) and have 24 classes than what should I have to change, I have already gone through the blog post, but rarely helps, Thanks in advance.

shaikhsharukh commented 2 years ago

Hi @dedhiaparth98 thanks for your reply, I have resolved my issue. My issue was similar to @Luke115 's issue. Because my dataset was not in a shape that is favorable to the parameters of Capsulenet ( original paper) it was not able train. I was training with audio signals. I did not have enough time to understand capsule net thoroughly and change the parameters which is why I did a workaround by converting the audio (wav) to MFCC (png) and then reading those images with target size as (28,28,1) which solved my issue. @Luke115 try resizing the images to (28,28,1) and the number of classes won't much matter I guess as I also used only 2 classes and it worked for me