2
$\begingroup$

I am trying to create a model using resnet50 to classify ct scan images as covid or not. However when using model.predict with a given image its giving the exact same value for all images that is:

array([[1.,0.]], dtype=float32)

The model summary:

Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 resnet50 (Functional)       (None, 2048)              23587712  
                                                                 
 flatten_2 (Flatten)         (None, 2048)              0         
                                                                 
 dropout_2 (Dropout)         (None, 2048)              0         
                                                                 
 dense_2 (Dense)             (None, 2)                 4098      
                                                                 
=================================================================
Total params: 23,591,810
Trainable params: 1,059,842
Non-trainable params: 22,531,968

My highest accuracy was 0.754.

Here's the code :

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
import os
import pandas as pd
import numpy as np
import cv2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen= ImageDataGenerator(rescale=1./255,validation_split=0.2)
from tensorflow.keras.applications.resnet import ResNet50
from tensorflow.keras.applications.vgg19 import VGG19, preprocess_input
#from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 
from tensorflow.keras.layers import Flatten,Dense,Dropout,BatchNormalization
from tensorflow.keras.models import Model,Sequential
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.layers import PReLU
from tensorflow.keras.optimizers import RMSprop

from tensorflow.keras.callbacks import ReduceLROnPlateau

base_dir = r'\filepath for dataset'
img_h,img_w= (164,164)
batch_size=32
epochs=10


base_model= ResNet50(include_top=False, weights='imagenet',
                                       input_tensor=None, input_shape= 
                                       (img_h,img_w,3),pooling='avg')
 
for layer in base_model.layers[:-7]:
    layer.trainable=False

 
model=Sequential()
model.add(base_model)
model.add(Flatten())
model.add(Dropout(0.4))


model.add(Dense(2,activation='softmax'))
model.summary()

for layer in base_model.layers:
    print(layer,"-->",layer.trainable)

from tensorflow.keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop
from tensorflow.keras.callbacks import ReduceLROnPlateau


reduce_learning_rate = ReduceLROnPlateau(monitor='loss',
                                         factor=0.1,
                                         patience=3,
                                         cooldown=2,
                                         min_lr=1e-10,
                                         verbose=1)

checkpoint =tf.keras.callbacks.ModelCheckpoint(filepath="reswtk.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')


import time
class TimeHistory(tf.keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.times = []

    def on_epoch_begin(self, batch, logs={}):
        self.epoch_time_start = time.time()

    def on_epoch_end(self, batch, logs={}):
        self.times.append(time.time() - self.epoch_time_start)

time_callback = TimeHistory()

callbacks = [reduce_learning_rate, checkpoint, time_callback]
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile( loss='categorical_crossentropy',optimizer= optimizer, metrics=['accuracy'])

train_generator = datagen.flow_from_directory(
        base_dir,  # This is the source directory for training images
        target_size=(img_h, img_w),  
        batch_size=batch_size,
        class_mode='categorical',
        subset='training',
        #color_mode="rgb",
        shuffle=True)


validation_generator = datagen.flow_from_directory(
         base_dir,     
        target_size=(img_h, img_w),
        batch_size=batch_size,
        class_mode='categorical',
        #color_mode="rgb",
        subset='validation',
        shuffle=False)

history = model.fit(
      train_generator,
      steps_per_epoch=train_generator.samples//batch_size, 
      epochs=epochs,
      validation_data=validation_generator,
      validation_steps=validation_generator.samples//batch_size,  
      callbacks=callbacks,
      verbose=1)

model.evaluate(validation_generator,verbose=1)
accu= history.history['val_accuracy']

img = cv2.imread("\filepath to png file") 
image_resized= cv2.resize(img, (img_h,img_w))
img=np.expand_dims(image_resized,axis=0)
y_proba = model.predict(img)
y_classes = y_proba.argmax(axis=-1)
y_proba

the output for this comes as

array([[1.,0.]], dtype=float32)

and the output for y_classes is

array([0], dtype=int64)

both stay same for all images.

This code was working initially. However i tried to install np_utils via conda and the problems started afterwards. I am not sure if i deleted some code or not.

$\endgroup$
4
  • $\begingroup$ You may use a generator to preprocess images in the same fashion as done with training images stackoverflow.com/a/55991598/9524424 $\endgroup$
    – Peter
    Jan 13, 2022 at 12:55
  • $\begingroup$ @Peter its still giving the same output for all images. $\endgroup$
    – acnt1
    Jan 13, 2022 at 13:31
  • $\begingroup$ what is the class balance of training images? Did you look at a confusion matrix to check prediction performance by class? $\endgroup$
    – Peter
    Jan 13, 2022 at 14:09
  • $\begingroup$ @Peter covid dataset has 4 more images than non covid. confusion matrix is array([[200, 50], [ 52, 193]], dtype=int64) $\endgroup$
    – acnt1
    Jan 13, 2022 at 14:44

1 Answer 1

0
$\begingroup$

Changing the batch size value seemed to be work. I put

batch_size=10

and now the model is predicting properly. Not super accurate but atleast now its giving separate predictions

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.