2017年4月28日 星期五

Kaggle Competition - The Nature Conservancy Fisheries Monitoring


The Nature Conservancy Fisheries Monitoring
這是我之前參加的一個Kaggle舉辦的比賽,為期大概四五個月,但實際上我參加時間不到一個月,來筆記下我學到的東西。
Problem Statement,簡單來說就是有六類魚,一個other類,再一個No fish的種類一共有八類

  • Albacore Tuna
  • Bigeye Tuna
  • Dolphinfish
  • Moonfish
  • Shark
  • Yellowfin Tuna
  • Others
  • No Fish 
要做分類的競賽,但注意是每一張圖片只會有一類。如下圖(reference Felix Yu, will be mentioned later):




很多人以為這應該是一個分類問題,但實際上它是一個object detection的問題,因為魚相對於背景的占比差異實在大,背景過大造成無法有效的判斷出分類。

這個比賽看似簡單,其實是一個困難的問題,原因如下:
  • 要用少量資料(<3000)去預測大量資料(>1w)。
  • 類分布相當不平均,ALB的魚種幾乎占了全部的1/2。
  • 這似乎是用camera連續攝影的截圖,有很多張是一樣的圖片,很容易造成overfitting。
  • 下面這個種魚實在太像,連肉眼都很難分辨。
Ambiguous Labels


以下為當時我採用的方法:
  • Faster RCNN => 採用了此物體偵測方法,Paper 
  • Data augmentation (only HUE) =>只做了HUE的data augmentation,由於照片裡面會有晚上夜釣的情形,所以會有透過夜視鏡所呈現的照片(night fish)
  • Clips (0.04 and 0.96) => 由於評分標準是採用 log loss:
  • 我們求出來的機率分布範圍是0~1,在極值的情況(0.0001)會產生很大的懲罰,所以clips 0.02~0.98是一個很好的手法。
  • Run in 60000 inter, batch size 16
結果是0.90 in public leaderboard (254/2293)。


以上都不是重點,重點是由第八名的Felix Fu所撰寫的參賽完整心得分享 (Thanks again):

我們缺少了:
  • 強化的Data augmentation
  • 更深的classification model
  • Model ensemble (因為是offline 所以盡情用吧)
  • Establishing a GOOD Local Validation (這個意思是Felix發現,我們自己在做local validation的時候,需要用不一樣的船隻做測試,這樣才能避免魚跟船過度關聯,因為同一艘船有可能捕的魚種都比較相同,但如果測試資料集給的是沒出現過的魚種,就會造成很大的影響)
    從這個競賽可以發現,少量資料(這個比賽是3000筆),去預測大量資料的侷限性(測試資料有一萬筆),必須去避免目標物分背景過度相關而造成與不相干的物體overfitting。

2017年4月27日 星期四

Deep learning fine tuning


最近要開始整理deeep learning學到的東西了,不然學了又忘,忘了又學。
    Deep learning fine tuning, 通常指的是fine tuning + pretrain model. 使用時機在於像是VGG16 or Inception 這種前人大神發明的net, 我們如果想要拿來train自己的model時, 因為類數不同, 需要做修改, 另外, 如果直接train, 需要很多的時間才能達到我們想要的結果, 不然就是結果太於overfitting我們的樣本. 一個方法就是將經過像是imageNet等等大規模學習後的model參數, 拿來做pre train, 而我們只需要重寫top的output layers.

    實作fine tuning有一點需要注意, 就是需要將不是top level的weight frozen, 在這篇paper "How transferable are features in deep neuralnetworks?" 指出:
    The choice of whether or not to fine-tune the first n layers of the target network depends on the size of the target dataset and the number of parameters in the first n layers. If the target dataset is small and the number of parameters is large, fine-tuning may result in overfitting, so the features are often left frozen. On the other hand, if the target dataset is large or the number of parameters is small, so that overfitting is not a problem.

    所以結論就是如果資料量夠大(>1w筆)是可以不用作frozen的. 這邊以keras使用inception v3為例:

#不包含top 的layer
base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
# 將原本的GAP layer加回
x = GlobalAveragePooling2D()(x)
predictions = Dense(nb_classes, activation='softmax')(x)
# 將原本的GAP layer加回
# this is the model we will train
model = Model(input=base_model.input, output=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
layer.trainable = False

但在VGG16中
base_model = VGG16(weights='imagenet', include_top=False)
input = Input(shape=(224,224,3),name = 'image_input')
output_vgg16_conv = base_model(input)

#Add the fully-connected layers
x = Flatten(name='flatten')(output_vgg16_conv)
#原本的下面這兩個layer必須拿掉, 這人理解是因為在fineturning時, 如果資料量太小, 如果增加太多ReLu output layer會讓資料太過於失去原本的特性而無法收斂?這個會在查明原因
#x = Dense(4096, activation='relu', name='fc1')(x)
#x = Dense(4096, activation='relu', name='fc2')(x)

x = Dense(nb_classes, activation='softmax', name='predict')(x)


以上是我的理解, 有錯歡迎指正




Deep learning Class Activation Map

學如不及,猶恐失之啊! 沒時間不是藉口,還是得趁有空時把研究的東西記下來,以免之後忘記。 用deep learning做classification時,結果總是每一類的機率,但是到底NN是從圖像的哪個部分判斷出他是屬於哪一類的機率呢? 這時候就可以用classificai...