heuritech / convnets-keras

MIT License
594 stars 185 forks source link

Using AlexNet on a different dataset #5

Closed jerpint closed 8 years ago

jerpint commented 8 years ago

Hi,

I'm wondering if and how it would be possible to modify this AlexNet convnet to output to different results (i.e. how to change the last layer output from 1000 to an arbitrary number, and train it from this starting point to a new training data set)

Thanks,

J

ouceduxzk commented 8 years ago

Since the alexnet is non Sequential model, but a general Model, which does not have support for the add method. Currently, once you load the model with weights, as far as I know, there is no way to add new modules. There might be a choice with Graph,

graph_m = Graph()
graph_m.add_input(name = 'main_input', input_shape=(3, 227, 227))
graph_m.add_node( model, name = 'alex',  input = 'main_input')
graph_m.add_node(Dense(10, activation='softmax'), name='Final', input='')

but since the model is a predefined model with inputs = Input((3,227,227)), there is not way to connect the input of the graph to the model.

leonardblier commented 8 years ago

Hi, In fact, you can modify this model and keep the pre-trained weights, even with a sequential model.

from keras.optimizers import SGD
from keras.models import Model
from keras.layers import Dense, Input, Activation
from convnetskeras. convnets import convnet

vgg16 = convnet('vgg_16', weights_path='vgg16_weights.h5')

input = vgg16.input
img_representation = vgg16.get_layer("dense_2").output

classifier = Dense(2,name='classifier')(img_representation)
classifier = Activation("softmax", name="softmax")(classifier)
model = Model(input=input,output=classifier)
sgd = SGD(lr=.1, decay=1.e-6, momentum=0.9, nesterov=False)
model.compile(optimizer=sgd, loss='categorical_crossentropy',metrics=["accuracy"])

Then, you can train the new model with your own data.

ouceduxzk commented 8 years ago

I am talking about alexnet, which is non sequential in keras way. I understand how to do finetune with vgg, which is completely sequential

leonardblier commented 8 years ago

I think the same method will work for AlexNet :

from keras.optimizers import SGD
from keras.models import Model
from keras.layers import Dense, Input, Activation
from convnetskeras. convnets import convnet

alexnet = convnet('alexnet', weights_path='alexnet_weights.h5')

input = alexnet.input
img_representation = alexnet.get_layer("dense_2").output

classifier = Dense(2,name='classifier')(img_representation)
classifier = Activation("softmax", name="softmax")(classifier)
model = Model(input=input,output=classifier)
sgd = SGD(lr=.1, decay=1.e-6, momentum=0.9, nesterov=False)
model.compile(optimizer=sgd, loss='categorical_crossentropy',metrics=["accuracy"])

This should work. With the new functional API, you can easily do this kind of operations.

ouceduxzk commented 8 years ago

I see. That helps me a lot. Thanks

leonardblier commented 8 years ago

You are welcome. I close this issue.