Keras model convert into pytorch - python

def network():
inputs = Input(name='inputs', shape=[51, ])
layer1 = Dense(2048)(inputs)
layer1 = BatchNormalization(name='BC11')(layer1)
layer1 = Activation('relu', name='Act11')(layer1)
layer2 = Dense(1024, name='FC1')(layer1)
layer2 = BatchNormalization(name='BC1')(layer2)
layer2 = Activation('relu', name='Act1')(layer2)
layer_shortcut1 = Dense(1024, activation='relu')(inputs)
layer_shortcut1 = BatchNormalization(name='BCshortcut1')(layer_shortcut1)
layer2 = Add()([layer2, layer_shortcut1])
merge1 = concatenate([layer1, layer2])
layer3 = Dense(512, name='FC3')(merge1)
layer3 = BatchNormalization(name='BC3')(layer3)
layer3 = Activation('relu', name='Act3')(layer3)
layer4 = Dense(256, name='FC5')(layer3)
layer4 = BatchNormalization(name='BC5')(layer4)
layer4= Activation('relu', name='Act5')(layer4)
layer_shortcut2 = Dense(256, activation='relu')(layer2)
layer_shortcut2 = BatchNormalization(name='BCshortcut2')(layer_shortcut2)
layer4 = Add()([layer4, layer_shortcut2])
merge2 = concatenate([layer3, layer4])
layer5 = Dense(128, name='FC7')(merge2)
layer5 = BatchNormalization(name='BC7')(layer5)
layer5 = Activation('relu', name='Act7')(layer5)
layer6 = Dense(64, name='FC8')(layer5)
layer6 = BatchNormalization(name='BC8')(layer6)
layer6 = Activation('relu', name='Act8')(layer6)
layer_shortcut3 = Dense(64, activation='relu')(layer4)
layer_shortcut3 = BatchNormalization(name='BCshortcut')(layer_shortcut3)
layer6 = Add()([layer6, layer_shortcut3])
merge3 = concatenate([layer5, layer6])
layer7 = Dense(32, name='FC9')(merge3)
layer7 = BatchNormalization(name='BC9')(layer7)
layer7 = Activation('relu', name='Act9')(layer7)
layer8 = Dense(16, name='FC0')(layer7)
layer8 = BatchNormalization(name='BC0')(layer8)
layer8 = Activation('relu', name='Act0')(layer8)
out0 = Dense(12, activation='sigmoid', name='Out0')(merge2)
out1 = Dense(12, activation='sigmoid', name='Out1')(merge3)
out2 = Dense(12, activation='sigmoid', name='Out2')(layer8)
model = Model(inputs=inputs, outputs=[out0,out1,out2])
return model
I am new in pytorch. Could someone help me to convert this into pytorch? I have seen How can I convert this keras cnn model to pytorch version but meet the dimension issues. Many thanks!

From this stack overflow answer. Forward your upvotes to the OG.
You can save keras weight and reload them in pytorch. the steps are
Step 0: Train a Model in Keras. ...
Step 1: Recreate & Initialize Your Model Architecture in PyTorch. ...
Step 2: Import Your Keras Model and Copy the Weights. ...
Step 3: Load Those Weights onto Your PyTorch Model. ...
Step 4: Test and Save Your Pytorch Model.
You Can follow example here https://gereshes.com/2019/06/24/how-to-transfer-a-simple-keras-model-to-pytorch-the-hard-way/

Related

Remove n last layers from ResNet50 network

I have the following network that will be used for binary classification on medical image data. However, I would like to use only the 80 first layers of this model as I currently don't have a lot of data and my model is overfitting. I would like to delete all layers from block 4 or 5, and only keep blocks 1, 2 and 3. I have tried using layer.pop() but it does not work.
from keras.applications.resnet50 import ResNet50
resnet = ResNet50(include_top=False, weights='imagenet', input_shape=(im_size,im_size,3))
headModel = AveragePooling2D(pool_size=(7, 7))(resnet.output)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(256, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(1, activation="sigmoid")(headModel)
Solution 1 : You could use model.summary() to quickly check the names of the layers (80th).
layer_name = 'name_of_the_80th_layer'
intermediate_model = Model(inputs=resnet.input,
outputs=resnet.get_layer(layer_name).output)
final_model = AveragePooling2D(pool_size=(7, 7))(intermediate_model.output)
final_model = Flatten(name="flatten")(final_model)
final_model = Dense(256, activation="relu")(final_model)
final_model = Dropout(0.5)(final_model)
final_model = Dense(1, activation="sigmoid")(final_model)
Solution 2:
You could directly get it like:
intermediate_model = Model(inputs=resnet.input,
outputs=resnet.layers[80].output)
...

Keras ValueError : the Dens' input dimension should be defined

I'm trying to use VGG16 with some modification on it. I followed this blog post from keras.io
Here the code I'm using to create the model:
def create_model():
vgg16_model = vgg16.VGG16(weights='imagenet', include_top=False)
print('[INFO] Model loaded.')
x = vgg16_model.output
x = Flatten()(x)
x = Dense(256, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1, activation='linear')(x)
model = Model(inputs=vgg16_model.inputs, outputs=x)
return model
Calling the model = create_model() gives an error:
ValueError: The last dimension of the inputs to Dense should be defined. Found None.
What could be the problem?
try to pass an input_shape when you use the vgg16.VGG16
def create_model():
vgg16_model = vgg16.VGG16(input_shape=(224,224,3), weights='imagenet', include_top=False)
print('[INFO] Model loaded.')
x = vgg16_model.output
x = Flatten()(x)
x = Dense(256, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1, activation='linear')(x)
model = Model(inputs=vgg16_model.inputs, outputs=x)
return model

how to localize object with CNN?

model = keras.Sequential([
keras.layers.Conv2D(32,(3,3),padding = 'same' ,activation = 'relu', input_shape= (image_width,image_height,image_channels)),
keras.layers.MaxPooling2D(pool_size = (2,2)),
keras.layers.Dropout(0.25),
keras.layers.Conv2D(64,(3,3),padding = 'same',activation = 'relu'),
keras.layers.MaxPooling2D(pool_size = (2,2)),
keras.layers.Dropout(0.25),
keras.layers.Conv2D(64,(3,3),padding = "same",activation = 'relu'),
keras.layers.MaxPooling2D(pool_size = (2,2)),
keras.layers.Dropout(0.25),
keras.layers.Flatten(),
keras.layers.Dense(256, activation = 'relu'),
keras.layers.Dropout(0.25),
keras.layers.Dense(5,activation = 'softmax')
])
This is my model that have been trained and get above 85% of accuracy for classifying 5 classess of flower. For the classification it works well and successfully classify most of the images. But for the object localization what layers should I add up in order to localize and detect the flower object within the test images?

Convert from keras to pytorch

I am training a multilabel classifier in keras ,i want to convert that to pytorch , i am mostly confused about how to handle the loss ,this is what the code looks like
model = keras.applications.densenet.DenseNet121(include_top=False, input_shape=(224, 224, 3))
x = model.output
x = Flatten()(x)
x = Dense(512)(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
output1 = Dense(1, activation = 'sigmoid')(x)
output2 = Dense(1, activation = 'sigmoid')(x)
output3 = Dense(1, activation = 'sigmoid')(x)
output4 = Dense(1, activation = 'sigmoid')(x)
output5 = Dense(1, activation = 'sigmoid')(x)
output6 = Dense(1, activation = 'sigmoid')(x)
output7 = Dense(1, activation = 'sigmoid')(x)
output8 = Dense(1, activation = 'sigmoid')(x)
model = Model(model.inputs,[output1,output2,output3,output4,output5, output6, output7, output8])
# print(model.summary())
model.compile(optimizers.rmsprop(lr = 0.0001, decay = 1e-6),
loss = ["binary_crossentropy","binary_crossentropy","binary_crossentropy","binary_crossentropy", "binary_crossentropy","binary_crossentropy","binary_crossentropy","binary_crossentropy"],metrics = ["accuracy"])
How can i do this in pytorch ,this is what i have till now
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(2304, 256)
self.fc2 = nn.Linear(256, 8)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(x.size(0), -1) # Flatten layer
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.sigmoid(x)
model = Net().cuda()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
def train(epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
# data, target = data.cuda(async=True), target.cuda(async=True) # On GPU
data, target = Variable(data).cuda(), Variable(target).float().cuda()
optimizer.zero_grad()
output = model(data)
loss = F.binary_cross_entropy(output, target)
loss.backward()
optimizer.step()
if batch_idx % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
for epoch in range(1, 10):
train(epoch)
Thanks in advance, any suggestions will be very helpful

How to add svm on top of cnn as final classifier?

I work on sentiment analysis task and i want to add SVM layer on top CNN as a final classifier, how can i do that without using hing-loss?
tweet_input = Input(shape=(seq_len,), dtype='int32')
tweet_encoder = Embedding(vocabulary_size, EMBEDDING_DIM,
input_length=seq_len, trainable=True)(tweet_input)
bigram_branch = Conv1D(filters=64, kernel_size=2, padding='same',
activation='relu', strides=1)(tweet_encoder)
bigram_branch = GlobalMaxPooling1D()(bigram_branch)
trigram_branch = Conv1D(filters=32, kernel_size=3, padding='same',
activation='relu', strides=1)(tweet_encoder)
trigram_branch = GlobalMaxPooling1D()(trigram_branch)
fourgram_branch = Conv1D(filters=16, kernel_size=4, padding='same',
activation='relu', strides=1)(tweet_encoder)
fourgram_branch = GlobalMaxPooling1D()(fourgram_branch)
merged = concatenate([bigram_branch, trigram_branch, fourgram_branch], axis=1)
merged = Dense(512, activation='softmax')(merged)
merged = Dropout(0.8)(merged)
merged = Dense(2)(merged)
output = Activation('sigmoid')(merged)
model = Model(inputs=[tweet_input], outputs=[output])
adam=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(loss='hinge',
optimizer= adam,
metrics=['accuracy'])
model.summary()

Categories

Resources