![](/img/trans.png)
[英]Pretrained CNN(tensorflow/darknet/caffe) weights for human/vehicle detection only
[英]Pass pretrained weights in CNN Pytorch to a CNN in Tensorflow
我已經在 Pytorch 中為 224x224 大小的圖像和 4 個類訓練了這個網絡。
class CustomConvNet(nn.Module):
def __init__(self, num_classes):
super(CustomConvNet, self).__init__()
self.layer1 = self.conv_module(3, 64)
self.layer2 = self.conv_module(64, 128)
self.layer3 = self.conv_module(128, 256)
self.layer4 = self.conv_module(256, 256)
self.layer5 = self.conv_module(256, 512)
self.gap = self.global_avg_pool(512, num_classes)
#self.linear = nn.Linear(512, num_classes)
#self.relu = nn.ReLU()
#self.softmax = nn.Softmax()
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.layer5(out)
out = self.gap(out)
out = out.view(-1, 4)
#out = self.linear(out)
return out
def conv_module(self, in_num, out_num):
return nn.Sequential(
nn.Conv2d(in_num, out_num, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=(2, 2), stride=None))
def global_avg_pool(self, in_num, out_num):
return nn.Sequential(
nn.Conv2d(in_num, out_num, kernel_size=3, stride=1, padding=1),
#nn.BatchNorm2d(out_num),
#nn.LeakyReLU(),
nn.ReLU(),
nn.Softmax(),
nn.AdaptiveAvgPool2d((1, 1)))
我從第一個 Conv2D 得到權重,它的大小torch.Size([64, 3, 3, 3])
我已將其保存為:
weightsCNN = net.layer1[0].weight.data
np.save('CNNweights.npy', weightsCNN)
這是我在 Tensorflow 中構建的 model。 我想將從 Pytorch model 中保存的權重傳遞給這個 Tensorflow CNN。
model = models.Sequential()
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(224, 224, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(512, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(512, (3, 3), activation='relu'))
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dense(4, activation='softmax'))
print(model.summary())
adam = optimizers.Adam(learning_rate=0.0001, amsgrad=False)
model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=['accuracy'])
nb_train_samples = 6596
nb_validation_samples = 1290
epochs = 10
batch_size = 256
history = model.fit_generator(
train_generator,
steps_per_epoch=np.ceil(nb_train_samples/batch_size),
epochs=epochs,
validation_data=validation_generator,
validation_steps=np.ceil(nb_validation_samples / batch_size)
)
我應該怎么做? Tensorflow 需要什么形狀的砝碼? 謝謝!
您可以非常簡單地檢查所有keras
層的所有權重的形狀:
for layer in model.layers:
print([tensor.shape for tensor in layer.get_weights()])
這將為您提供所有權重的形狀(包括偏差),因此您可以相應地准備加載numpy
權重。
要設置它們,請執行類似的操作:
for torch_weight, layer in zip(model.layers, torch_weights):
layer.set_weights(torch_weight)
其中torch_weights
應該是一個列表,其中包含您必須加載的np.array
列表。
通常, torch_weights
的每個元素將包含一個用於權重的np.array
和一個用於偏差的。
請記住,從 print 收到的形狀必須與您放入set_weights
的形狀完全相同。
有關更多信息,請參閱文檔。
順便提一句。 確切的形狀取決於 model 執行的層和操作,有時您可能必須轉置一些 arrays 以“適應它們”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.