tensorflownvidia

Nvidia 4060 TI 8GB slower than CPU in classification


I'm using this tutorial: https://www.tensorflow.org/tutorials/images/classification

In tests, the cpu takes about 50seconds to run and GPU about 7-8 minutes. I'm guessing I'm doing something wrong.

My cpu is a intel i5 10th generation with 96 ram. I would expect gpu to run at least 2x faster

I enabled mixed precision so that I make sure it is using tensorcores

from tensorflow.keras import mixed_precision mixed_precision.set_global_policy('mixed_float16')

What am I missing.... is rtx 4060 ti 8gb vram so slow when using classification algorithm?

I have about 1000 classes,but this is not relevant because cpu is alot faster ...

I'm using batches of 512, vram is at 6/8 and cpu most of the time is about 50%

I'm als doing BatchNormalization

model = Sequential([
  data_augmentation,
  layers.Rescaling(1./255),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  BatchNormalization(),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  BatchNormalization(),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  BatchNormalization(),
  layers.MaxPooling2D(),
  layers.Dropout(0.2),
  BatchNormalization(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes, name="outputs")
])

ps: i'm new to ai

I tried using different batch size

I tried disabling the gpu and running just on cpu to make the test

I check out the ram, disk and cpu for bottleneck(none are 100%). When I run on cpu the usage is 100% and gpu 1% or less

These are the batch test I've done

Batch Time 
4 377s 
8  304s
16  317s 
32 335s
64 446s

And this is the model:

 Layer (type)                Output Shape              Param #   
=================================================================
 sequential_1 (Sequential)   (None, 256, 256, 3)       0         
                                                                 
 rescaling_2 (Rescaling)     (None, 256, 256, 3)       0         
                                                                 
 conv2d_3 (Conv2D)           (None, 256, 256, 16)      448       
                                                                 
 batch_normalization (BatchN  (None, 256, 256, 16)     64        
 ormalization)                                                   
                                                                 
 max_pooling2d_3 (MaxPooling  (None, 128, 128, 16)     0         
 2D)                                                             
                                                                 
 conv2d_4 (Conv2D)           (None, 128, 128, 32)      4640      
                                                                 
 batch_normalization_1 (Batc  (None, 128, 128, 32)     128       
 hNormalization)                                                 
                                                                 
 max_pooling2d_4 (MaxPooling  (None, 64, 64, 32)       0         
 2D)                                                             
                                                                 
 conv2d_5 (Conv2D)           (None, 64, 64, 64)        18496     
                                                                 
 batch_normalization_2 (Batc  (None, 64, 64, 64)       256       
 hNormalization)                                                 
                                                                 
 max_pooling2d_5 (MaxPooling  (None, 32, 32, 64)       0         
 2D)                                                             
                                                                 
 dropout (Dropout)           (None, 32, 32, 64)        0         
                                                                 
 batch_normalization_3 (Batc  (None, 32, 32, 64)       256       
 hNormalization)                                                 
                                                                 
 flatten_1 (Flatten)         (None, 65536)             0         
                                                                 
 dense_2 (Dense)             (None, 128)               8388736   
                                                                 
 outputs (Dense)             (None, 863)               111327    
                                                                 
=================================================================
Total params: 8,524,351
Trainable params: 8,523,999
Non-trainable params: 352
_________________________________________________________________

And this is how I load the data:

folder = "some-folder"
train_ds = tf.keras.utils.image_dataset_from_directory(

    folder,
 validation_split=0.2,
  subset="training",
  seed=1,
  image_size=image_size,
  batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(
    folder,
    validation_split=0.2,
  subset="validation",
  seed=1,
  image_size=image_size,
  batch_size=batch_size)

And this is the autotune part

AUTOTUNE = tf.data.AUTOTUNE

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
normalization_layer = layers.Rescaling(1. / 255)

normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
niter = iter(normalized_ds)
image_batch, labels_batch = next(niter)
first_image = image_batch[0]
# Notice the pixel values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))

Solution

  • The answer is wrong for out-of-date. To now, it is CUDA toolkit compatible. By official website.