简体   繁体   English

如何使用预训练的权重在 tensorflow 中训练卷积神经网络?

[英]How to use pre-trained weight for training convolutional NN in tensorflow?

In my experiment, I want to train convolutional NN (CNN) with cifar10 on imagenet, and I used ResNet50 .在我的实验中,我想在 imagenet 上使用 cifar10 训练卷积神经网络(CNN),我使用ResNet50 Since cifar10 is 32x32x3 set of images while ResNet50 uses 224x224x3.由于 cifar10 是 32x32x3 的图像集,而ResNet50使用 224x224x3。 To do so, I need to resize input image in order to train CNN on imagenet .为此,我需要调整输入图像的大小以便在imagenet上训练 CNN。 However, I came up following up attempt to train simple CNN on imagenet:但是,我尝试在 imagenet 上训练简单的CNN

my current attempt :我目前的尝试

Please see my whole implementation in this gist :在此要点中查看我的整个实现

base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = Conv2D(32, (3, 3))(base_model.output)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Flatten()(x)
x = Dense(256)(x)
x = Dense(10)(x)
x = Activation('softmax')(x)
outputs = x
model = models.Model(base_model.input, outputs)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=50, epochs=3, verbose=1, validation_data=(X_test, y_test))

but this attempt gave me ResourceExhaustedError ;但这次尝试给了我ResourceExhaustedError I occurred this error before and changing batch_size removed the error.我之前发生过这个错误,改变batch_size消除了这个错误。 But now even I changed batch_size as small as possible, and still end up with error.但是现在即使我将batch_size更改得尽可能小,但仍然会出错。 I am wondering the way of training CNN on imagenet on above may not be correct or something wrong in my attempt.我想知道在上面的 imagenet 上训练 CNN 的方式在我的尝试中可能不正确或有问题。

update :更新

I want to understand how about using pre-trained weights (ie, ResNet50 on imagenet) to train convolutional NN;我想了解如何使用预训练的权重(即 imagenet 上的 ResNet50)来训练卷积神经网络; I am not sure how to get this done in tensorflow.我不确定如何在 tensorflow 中完成这项工作。 Can anyone provide possible feasible approach to get this right?任何人都可以提供可能的可行方法来解决这个问题吗? Thanks谢谢

Can anyone point me out what went wrong with my attempt?谁能指出我的尝试出了什么问题? What would be correct way of training state-of-art CNN model with cifar10 on imagenet?在 imagenet 上使用 cifar10 训练最先进的CNN model 的正确方法是什么? Can anyone share possible thoughts or efficient way of doing this in tensorflow?任何人都可以在 tensorflow 中分享可能的想法或有效的方法吗? Any idea?任何想法? Thanks!谢谢!

You might be getting this error because you are trying to allocate the memory (RAM) to the whole data at once.您可能会收到此错误,因为您试图一次将 memory (RAM) 分配给整个数据。 For starters, you might be using numpy arrat to store the images, then those images are being converted to tensors .对于初学者,您可能正在使用numpy arrat 存储图像,然后将这些图像转换为tensors So you have 2X the memory already even before creating anything.因此,在创建任何东西之前,您就已经拥有 2 倍的 memory。 On top of that, resnet is very heavy model so you are trying to pass the whole data at once.最重要的是, resnet非常重 model 所以你试图一次传递整个数据。 That is why the models work with batches .这就是模型使用batches的原因。 Try to create a generator by using tf.data.Dataset documentation or use the very easy keras.preprocessing.Image.ImageDataGenerator class.尝试使用tf.data.Dataset文档或使用非常简单的keras.preprocessing.Image.ImageDataGenerator class 创建生成器。 It is very easy to use.这是非常容易使用。 You can save address of your image files in the Datarame column with another column representing the class and use .flow_from_directory .您可以将图像文件的地址保存在Datarame列中,另一列代表 class 并使用.flow_from_directory Or you can use flow_from_directory if you have your images saved in the directory.或者,如果您将图像保存在目录中,则可以使用flow_from_directory

Checkout the documentation 查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM