Keras's Finetune

keras 实现线性模型


In [1]:
from utils import *
from keras.optimizers import SGD, RMSprop, Adam


Using TensorFlow backend.

In [2]:
x = random((30, 2))
x[:3]


Out[2]:
array([[ 0.4272,  0.6899],
       [ 0.9954,  0.164 ],
       [ 0.1836,  0.522 ]])

In [3]:
y = x.dot([2., 3.]) + 1.
y[:3]


Out[3]:
array([ 3.9242,  3.4829,  2.9333])

In [4]:
lm = Sequential([Dense(1, input_shape=(2,))])
lm.compile(optimizer=SGD(lr=.1), loss='mse')

In [5]:
lm.fit(x, y, nb_epoch=10, batch_size=1)


Epoch 1/10
30/30 [==============================] - 0s - loss: 1.8970       
Epoch 2/10
30/30 [==============================] - 0s - loss: 0.1802     
Epoch 3/10
30/30 [==============================] - 0s - loss: 0.0791     
Epoch 4/10
30/30 [==============================] - 0s - loss: 0.0385     
Epoch 5/10
30/30 [==============================] - 0s - loss: 0.0206     
Epoch 6/10
30/30 [==============================] - 0s - loss: 0.0108     
Epoch 7/10
30/30 [==============================] - 0s - loss: 0.0070     
Epoch 8/10
30/30 [==============================] - 0s - loss: 0.0033         
Epoch 9/10
30/30 [==============================] - 0s - loss: 0.0019     
Epoch 10/10
30/30 [==============================] - 0s - loss: 8.4870e-04 
Out[5]:
<keras.callbacks.History at 0x7f0b8162e208>

In [6]:
lm.get_weights()


Out[6]:
[array([[ 1.9327],
        [ 2.9479]], dtype=float32), array([ 1.0599], dtype=float32)]

VGG change


In [7]:
import vgg16
from vgg16 import Vgg16

In [ ]:
vgg = Vgg16()

In [ ]:
path = 'data/redux/'
# path = 'data/redux/sample/'
model_path = path + 'models/'

In [ ]:


In [ ]:
BATCH_SIZE = 8

In [ ]:
val_batches = get_batches(path + 'valid', shuffle=False, batch_size=BATCH_SIZE)
batches = get_batches(path + 'train', shuffle=False, batch_size=BATCH_SIZE)

In [ ]:
#batches.nb_sample
#batches.next()

In [ ]:
import bcolz
def save_array(fname, arr): c=bcolz.carray(arr, rootdir=fname, mode='w'); c.flush()
def load_array(fname): return bcolz.open(fname)[:]
def onehot(x): return np.array(OneHotEncoder().fit_transform(x.reshape(-1,1)).todense())

In [ ]:
# val_batches

In [ ]:
# val_data = get_data(path + 'valid')

In [ ]:
# trn_data = get_data(path + 'train')

In [ ]:
# save_array(model_path + 'train_data.bc', trn_data)
# save_array(model_path + 'valid_data.bc', val_data)

In [ ]:
# trn_data = load_array(model_path + 'train_data.bc')
# val_data = load_array(model_path + 'valid_data.bc')

In [ ]:
# trn_data.shape

In [ ]:
val_classes = val_batches.classes
trn_classes = batches.classes
val_labels = onehot(val_classes)
trn_labels = onehot(trn_classes)

In [ ]:
trn_labels.shape

In [ ]:
# trn_features = model.predict(trn_data, batch_size=BATCH_SIZE)
# trn_features

In [ ]:
# val_features = model.predict(val_data, batch_size=BATCH_SIZE)
# val_features

In [ ]:
val_features = model.predict_generator(val_batches, val_batches.nb_sample)

In [ ]:
val_features.shape

In [ ]:
trn_features = model.predict_generator(batches, batches.nb_sample)

In [ ]:
trn_features

In [ ]:
# vgg.compile()
# vgg.fit(batches, val_batches, nb_epoch=1)

In [ ]:
save_array(model_path + 'train_lastlayer_features.bc', trn_features)
save_array(model_path + 'valid_lastlayer_features.bc', val_features)

In [ ]:
trn_features = load_array(model_path + 'train_lastlayer_features.bc')
val_features = load_array(model_path + 'valid_lastlayer_features.bc')

In [ ]:
lm = Sequential([Dense(2, activation='softmax', input_shape=(1000, ))])
lm.compile(optimizer=RMSprop(lr=.1), loss='categorical_crossentropy', metrics=['accuracy'])

In [25]:
lm.fit(trn_features, trn_labels, nb_epoch=3, batch_size=BATCH_SIZE, validation_data=(val_features, val_labels))


Train on 23000 samples, validate on 2000 samples
Epoch 1/3
23000/23000 [==============================] - 5s - loss: 0.4311 - acc: 0.8556 - val_loss: 0.4137 - val_acc: 0.8705
Epoch 2/3
23000/23000 [==============================] - 5s - loss: 0.5165 - acc: 0.8646 - val_loss: 0.4735 - val_acc: 0.8725
Epoch 3/3
23000/23000 [==============================] - 5s - loss: 0.5653 - acc: 0.8667 - val_loss: 0.5086 - val_acc: 0.8765
Out[25]:
<keras.callbacks.History at 0x7f5cdf969ef0>

In [26]:
lm.fit(trn_features, trn_labels, nb_epoch=6, batch_size=BATCH_SIZE, validation_data=(val_features, val_labels))


Train on 23000 samples, validate on 2000 samples
Epoch 1/6
23000/23000 [==============================] - 5s - loss: 0.6064 - acc: 0.8662 - val_loss: 0.5452 - val_acc: 0.8745
Epoch 2/6
23000/23000 [==============================] - 5s - loss: 0.6332 - acc: 0.8666 - val_loss: 0.6011 - val_acc: 0.8725
Epoch 3/6
23000/23000 [==============================] - 5s - loss: 0.6785 - acc: 0.8680 - val_loss: 0.6338 - val_acc: 0.8730
Epoch 4/6
23000/23000 [==============================] - 5s - loss: 0.7074 - acc: 0.8678 - val_loss: 0.6630 - val_acc: 0.8730
Epoch 5/6
23000/23000 [==============================] - 5s - loss: 0.7407 - acc: 0.8671 - val_loss: 0.6879 - val_acc: 0.8735
Epoch 6/6
23000/23000 [==============================] - 5s - loss: 0.7630 - acc: 0.8679 - val_loss: 0.7412 - val_acc: 0.8685
Out[26]:
<keras.callbacks.History at 0x7f5cdf90c780>

In [1]:
lm.summary()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-9f204520c84e> in <module>()
----> 1 lm.summary()

NameError: name 'lm' is not defined

In [ ]: