tensorflow Nvidia 4060 TI 8GB比CPU慢

q9rjltbz  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(126)

我正在使用这个教程:https://www.tensorflow.org/tutorials/images/classification
在测试中,CPU运行时间约为50秒,GPU运行时间约为7-8分钟。我想我做错了什么。
我的CPU是英特尔i5第10代与96 RAM。我希望GPU运行速度至少快2倍
我启用了混合精度,以确保它使用的是tensorcores
从tensorflow.keras导入mixed_precision mixed_precision.set_global_policy('mixed_float16')
我错过了什么……RTX 4060 TI 8 GBVRAM在使用分类算法时是不是很慢?
我有大约1000个类,但这是不相关的,因为cpu是快得多...
我使用的批次512,vram是在6/8和cpu的大部分时间是约50%
我也在做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:我是AI新手
我尝试使用不同的批量大小
我尝试禁用gpu和运行只是在cpu上作出的测试
我检查了内存,磁盘和CPU的瓶颈(没有一个是100%)。当我运行在cpu的使用率是100%和gpu的1%或更少
这些是我做过的批量测试

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
_________________________________________________________________


这就是我如何加载数据:

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)


这是自动调谐部分

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))

相关问题