tflearn / tflearn

Deep learning library featuring a higher-level API for TensorFlow.
http://tflearn.org
Other
9.62k stars 2.41k forks source link

About finetuned #494

Open huyangc opened 7 years ago

huyangc commented 7 years ago

I trained a model of ResNet on ImageNet and get a model file. I use the model file to finetune ResNet for Oxford102, but the network doesn't converge. The loss is between [4.63,4.65]. Here is my code:

resize_pics = (227,227,3)
# Data loading
X, Y = image_preloader('102flowers/train_256.txt',image_shape=resize_pics,mode='file',files_extension=['jpg','jpeg','JPG','JPEG'],categorical_labels=True)
valX,valY = image_preloader('102flowers/val.txt',image_shape=(224,224,3),mode='file',files_extension=['jpg','jpeg','JPG','JPEG'],categorical_labels=True)
testX,testY = image_preloader('102flowers/test.txt',image_shape=(224,224,3),mode='file',files_extension=['jpg','jpeg','JPG','JPEG'],categorical_labels=True)
# Real-time data preprocessing
imageprocess = ImagePreprocessing()
# crop256  0.50876713  0.4061197   0.34013185
imageprocess.add_featurewise_zero_center(per_channel=True)
imageaug = ImageAugmentation()
imageaug.add_random_crop([224,224,3])
imageaug.add_random_flip_leftright()

# Building Residual Network
net = tflearn.input_data(shape=[None, 224, 224, 3],
                         data_preprocessing=imageprocess,
                         data_augmentation=imageaug)
net = tflearn.conv_2d(net, 64, 7,strides=2,regularizer='L2', weight_decay=0.0001)
net = tflearn.max_pool_2d(net,3,2)
net = tflearn.residual_block(net, 3, 64)
net = tflearn.residual_block(net, 1, 128, downsample=True)
net = tflearn.residual_block(net, 3, 128)
net = tflearn.residual_block(net, 1, 256, downsample=True)
net = tflearn.residual_block(net, 5, 256)
net = tflearn.residual_block(net,1,512,downsample=True)
net = tflearn.residual_block(net,2,512)
net = tflearn.batch_normalization(net)
net = tflearn.activation(net, 'relu')
net = tflearn.global_avg_pool(net)
# Regression
net = tflearn.fully_connected(net, 102, activation='softmax',restore=False)
mom = tflearn.Momentum(0.01, lr_decay=0.7, decay_step=2000, staircase=True)
top5 = Top_k(1)
net = tflearn.regression(net, optimizer=mom,
                         loss='categorical_crossentropy',metric=top5,restore=False)
# Training
model = tflearn.DNN(net, checkpoint_path=None,best_checkpoint_path=None,
                    max_checkpoints=10, tensorboard_verbose=0,
                    clip_gradients=0.)
model.load('resnet/resnet-34-147570')
model.fit(X, Y, n_epoch=2000, validation_set=(valX, valY),
          snapshot_epoch=False, snapshot_step=200,
          show_metric=True, batch_size=64, shuffle=True,
          run_id='resnet_finetuned_oxford')
print (model.evaluate(testX,testY,128))
model.save('resnet-34_finetuned_oxford102')

It is just the same as resnet in example except the last fc's output changed to 102. I don't know how this can happen. Because I just use the finetune flow in caffe and get a good result. please help me. thanks a lot.

aymericdamien commented 7 years ago

I am not so sure why it is not converging, it seems like a good setup. Maybe try to adapt the optimizer / lower learning rate. Also you can play with 'clip_gradient' and regularization.