<p>I have built one CNN model and applied it to chest-xray Covid 19 pneumonia dataset. I am getting the classification report as follows:</p>
<p><a href="https://i.sstatic.net/ZpuGo.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZpuGo.png" alt="enter image description here" /></a></p>
<p>I am surprised to see that it is giving an excellent result on minority class but relatively poor on <strong>class 1</strong>. Can anybody help me tune my model so that I can build a more robust model?</p>
<p>CNN code</p>
<pre><code>def architecture (_img_dims ):
    
    inputs = Input(shape=(_img_dims, _img_dims, 3))

    x = SeparableConv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = SeparableConv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = MaxPool2D(pool_size=(2, 2))(x)

    
    x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = MaxPool2D(pool_size=(2, 2))(x)
    x = Dropout(rate=0.2)(x)

    
    x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = MaxPool2D(pool_size=(2, 2))(x)
    x = Dropout(rate=0.2)(x)

    
    x = Flatten()(x)
    x = Dense(units=512, activation='relu')(x)
    x = Dropout(rate=0.7)(x)
    x = Dense(units=128, activation='relu')(x)
    x = Dropout(rate=0.5)(x)
    x = Dense(units=64, activation='relu')(x)
    x = Dropout(rate=0.3)(x)

    # Output layer
    output = Dense(units=3, activation='softmax')(x)
    
    return inputs, output
</code></pre>
<p><strong>INFO ABOUT CLASSES</strong></p>
<ul>
<li>Class 0:  COVID</li>
<li>Class 1:  Normal</li>
<li>Class 2:  Pneumonia</li>
</ul>
<p>Image SAMPLE</p>
<p><a href="https://i.sstatic.net/cwHbv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cwHbv.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/d9shp.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/d9shp.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/irCOl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/irCOl.png" alt="enter image description here" /></a></p>
<p>I have also used Data-Augmentation. Still the result is same</p>
<pre><code>train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=10,
        zoom_range=0.4,
        horizontal_flip=True,
        validation_split=0.01
        )
</code></pre>
<p><strong>Edit</strong></p>
<p>I am trying to increase, the number of class-level 1 images i.e Normal Images by using AC-GAN. Code is taken from <a href="https://machinelearningmastery.com/how-to-develop-an-auxiliary-classifier-gan-ac-gan-from-scratch-with-keras/" rel="nofollow noreferrer">here</a></p>
<pre><code>from math import sqrt
from numpy import asarray
from numpy.random import randn
from keras.models import load_model
from matplotlib import pyplot
 
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples, n_class):
    # generate points in the latent space
    x_input = randn(latent_dim * n_samples)
    # reshape into a batch of inputs for the network
    z_input = x_input.reshape(n_samples, latent_dim)
    # generate labels
    labels = asarray([n_class for _ in range(n_samples)])
    return [z_input, labels]
 
# create and save a plot of generated images
def save_plot(examples, n_examples):
    # plot images
    for i in range(n_examples):
        # define subplot
        pyplot.subplot(sqrt(n_examples), sqrt(n_examples), 1 + i)
        # turn off axis
        pyplot.axis('off')
        # plot raw pixel data
        pyplot.imshow(examples[i, :, :, 0], cmap='gray_r')
    pyplot.show()
 
# load model
model = load_model('../input/model111/Xception.h5')
latent_dim = 224
n_examples = 100 # must be a square
n_class = 1 # Normal
# generate images
latent_points, labels = generate_latent_points(latent_dim, n_examples, n_class)
# generate images
X  = model.predict([latent_points, labels])
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the result
save_plot(X, n_examples)
</code></pre>
<p>But I am getting error as</p>
<pre><code>ValueError: Layer model expects 1 input(s), but it received 2 input tensors. Inputs received: [&lt;tf.Tensor 'IteratorGetNext:0' shape=(None, 224) dtype=float32&gt;, &lt;tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=int64&gt;]
</code></pre>

CC BY-SA 4.0
2022-09-26T08:33:10.207Z
could you describe more in details what the class represent? (maybe add a couple of images as well). There can be many reasons why class 0 is detected better than class 1 even with less examples, e.g. if class 0 were bananas, class 1 oranges and class 2 mandarins, the color would suffice to get that class perfectly with few examples. So the CNN code is kinda useless here, more important is to inspect the data.
Edoardo Guerriero
CC BY-SA 4.0
2022-09-26T09:03:24.743Z
Hello @EdoardoGuerriero , I have edited the question.
XYZ
<convolutional-neural-networks><computer-vision><python>
How to increase accuracy for CNN?
XYZ
# How to increase accuracy for CNN?

(Asked by: username_0 on 2022-09-26)

I have built one CNN model and applied it to chest-xray Covid 19 pneumonia dataset. I am getting the classification report as follows:

I am surprised to see that it is giving an excellent result on minority class but relatively poor on class 1. Can anybody help me tune my model so that I can build a more robust model?
CNN code
def architecture (_img_dims ):
    
    inputs = Input(shape=(_img_dims, _img_dims, 3))

    x = SeparableConv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = SeparableConv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = MaxPool2D(pool_size=(2, 2))(x)

    
    x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = MaxPool2D(pool_size=(2, 2))(x)
    x = Dropout(rate=0.2)(x)

    
    x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)
    x = MaxPool2D(pool_size=(2, 2))(x)
    x = Dropout(rate=0.2)(x)

    
    x = Flatten()(x)
    x = Dense(units=512, activation='relu')(x)
    x = Dropout(rate=0.7)(x)
    x = Dense(units=128, activation='relu')(x)
    x = Dropout(rate=0.5)(x)
    x = Dense(units=64, activation='relu')(x)
    x = Dropout(rate=0.3)(x)

    # Output layer
    output = Dense(units=3, activation='softmax')(x)
    
    return inputs, output

INFO ABOUT CLASSES

Class 0:  COVID
Class 1:  Normal
Class 2:  Pneumonia

Image SAMPLE



I have also used Data-Augmentation. Still the result is same
train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=10,
        zoom_range=0.4,
        horizontal_flip=True,
        validation_split=0.01
        )

Edit
I am trying to increase, the number of class-level 1 images i.e Normal Images by using AC-GAN. Code is taken from here
from math import sqrt
from numpy import asarray
from numpy.random import randn
from keras.models import load_model
from matplotlib import pyplot
 
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples, n_class):
    # generate points in the latent space
    x_input = randn(latent_dim * n_samples)
    # reshape into a batch of inputs for the network
    z_input = x_input.reshape(n_samples, latent_dim)
    # generate labels
    labels = asarray([n_class for _ in range(n_samples)])
    return [z_input, labels]
 
# create and save a plot of generated images
def save_plot(examples, n_examples):
    # plot images
    for i in range(n_examples):
        # define subplot
        pyplot.subplot(sqrt(n_examples), sqrt(n_examples), 1 + i)
        # turn off axis
        pyplot.axis('off')
        # plot raw pixel data
        pyplot.imshow(examples[i, :, :, 0], cmap='gray_r')
    pyplot.show()
 
# load model
model = load_model('../input/model111/Xception.h5')
latent_dim = 224
n_examples = 100 # must be a square
n_class = 1 # Normal
# generate images
latent_points, labels = generate_latent_points(latent_dim, n_examples, n_class)
# generate images
X  = model.predict([latent_points, labels])
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the result
save_plot(X, n_examples)

But I am getting error as
ValueError: Layer model expects 1 input(s), but it received 2 input tensors. Inputs received: [, ]

**Tags:** convolutional-neural-networks, computer-vision, python

> **Comments:**
> **username_1**: could you describe more in details what the class represent? (maybe add a couple of images as well). There can be many reasons why class 0 is detected better than class 1 even with less examples, e.g. if class 0 were bananas, class 1 oranges and class 2 mandarins, the color would suffice to get that class perfectly with few examples. So the CNN code is kinda useless here, more important is to inspect the data.  
> **username_0**: Hello @EdoardoGuerriero , I have edited the question.  

---

## 0 answers

