# How can I use CNN to make a cumulative count of the number of occurrences of each of the different objects in all the images in the test set?

Let's say there are three images in the test set, the first with three triangles, the second with two triangles and two circles, the third with four circles and two squares, and the final tally is a total of 5 triangles, 6 circles, 2 squares and 0 pentagrams (if the "pentagram " is also included in the labels in the training set) How should I design the layers of this neural network and do I need to use more than one sort of kernel and filter? How could I label the images in my training set (for instance, the training set I mentioned above)?

• Is a cumulative count really necessary, if you have separate input images? Adding up the number of predicted items / image is tricky for the loss function, since 3 + 2 + 0 triangles = 5, but so is 0 + 0 + 5. Commented Feb 22, 2023 at 6:24

Well, if you are using high-level libraries, you don't have to find filters by yourself. Kernel types might change throughout the process, if you are trying to find different patterns, there will be different type of filters too.

Here is a Keras functional api code that might help you in this context. Also notice that there is a stride of 2 which means your image would shrink into half by each Conv2D layers, this also helps your filters to find bigger patterns since they are covering more space on the image.

i = Input(shape=x_train[0].shape)
x = Conv2D(32, (3,3), strides=2, activation='relu')(i)
x = Conv2D(64, (3,3), strides=2, activation='relu')(x)
x = Conv2D(128, (3,3), strides=2, activation='relu')(x)
x = Flatten()(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(4, activation='softmax')(x)
model = Model(i, x)


How you find the most fitting layers with filters is mostly intuition. These types of variables are called hyper-parameters. There are conventions about these too.

How you label your data is basically training your neural network with a vector of targets, which would correspond to your training data's indexes.

• This network's structure doesn't really correspond to the question's case. They have n input images, looking to identify and count m different classes and outputting their total numbers. Commented Feb 22, 2023 at 6:27

You can probably look at object detection models. Then count the number of boxes for each category that is created by the network. See for example the following image from the linked blog post: