内存问题资源extexhustedError OOM在CNN模型中分配张量时 - 如何降低数字参数?

发布于 2025-02-10 08:57:37 字数 1875 浏览 2 评论 0原文

我的二进制分类模型的图像将我调整为224x224分辨率。我的基本CNN模型定义如下:

input_shape
(224, 224, 3)

cnn_model = tf.keras.Sequential([
   Conv2D(32, (5,5), padding = 'same', activation = 'relu', input_shape =(input_shape)),
   Conv2D(128, (3,3), padding = 'same', activation = 'relu', input_shape =(input_shape)),
   MaxPooling2D(2,2),
   Flatten(),
   Dense(128, activation = 'relu'),
   Dense(1, activation = "sigmoid"),                       

])

cnn_model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 224, 32)      2432      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 224, 224, 128)     36992     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 128)     0         
_________________________________________________________________
flatten (Flatten)            (None, 1605632)           0         
_________________________________________________________________
dense (Dense)                (None, 128)               205521024 
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 129       
=================================================================
Total params: 205,560,577
Trainable params: 205,560,577
Non-trainable params: 0

它给我带来了错误错误,(ResourceExhustedError:OOM使用形状分配张量时[1605632,128] [1605632,128],然后键入float/job:localhost/poplica:local -host/poplica:0/task:0/task :gpu:0分配器gpu_0_bfc)当我尝试训练模型时。

因此,我可以看到Flatten()层中有1605632个参数。

如何更改架构以减少这些参数的数量? 我不想更改分辨率,因为我还将使用Visual Transformer验证​​的模型,这将需要224 x 224分辨率。

谢谢你!

my binary classification model has images which I resized to 224x224 resolution. My basic CNN model is defined as follows:

input_shape
(224, 224, 3)

cnn_model = tf.keras.Sequential([
   Conv2D(32, (5,5), padding = 'same', activation = 'relu', input_shape =(input_shape)),
   Conv2D(128, (3,3), padding = 'same', activation = 'relu', input_shape =(input_shape)),
   MaxPooling2D(2,2),
   Flatten(),
   Dense(128, activation = 'relu'),
   Dense(1, activation = "sigmoid"),                       

])

cnn_model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 224, 32)      2432      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 224, 224, 128)     36992     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 128)     0         
_________________________________________________________________
flatten (Flatten)            (None, 1605632)           0         
_________________________________________________________________
dense (Dense)                (None, 128)               205521024 
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 129       
=================================================================
Total params: 205,560,577
Trainable params: 205,560,577
Non-trainable params: 0

It gives me an out of memory error, (ResourceExhaustedError: OOM when allocating tensor with shape[1605632,128] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc) when I try to train the model.

so I can see there are 1605632 parameters in the Flatten() layer.

how can I change my architecture to reduce the number of these parameters?
I do not want to change the resolution because I will also be using visual transformer pretrained models, which will require the 224 x 224 resolution.

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文