A problem in multi-label classification with hdf5 dataset

104 views
Skip to first unread message

包青平

unread,
Nov 17, 2015, 6:43:57 AM11/17/15
to Caffe Users
I tried to do a multi-label classifiction with hdf5 dataset .there are 2 labels.  one label could be labeld 0 or 1 , the other could be labeled 0,1,2,3,4.   But during training iterations, accuracy kept the same, namely accuracy_1 was kept 0.5 without changing and accuracy_2 was kept 0.2. I have checked the code for many times , but still don't know why. Would someone give me some advice.

this is the code to create hdf5 dataset
image_dir = 'test/'
HDF5_FILE_TRAIN = 'hdf5_test.h5'
LABELS = dict(
    kinds_0 = (0, 0),
    kinds_1 = (0, 1),
    kinds_2 = (0, 2),
    kinds_3 = (0, 3),
    kinds_4 = (0, 4),
    kinds_5 = (1, 0),
    kinds_6 = (1, 1),
    kinds_7 = (1, 2),
    kinds_8 = (1, 3),
    kinds_9 = (1, 4),
)

file_list = os.listdir(image_dir)
random.shuffle(file_list)


datas = np.zeros((len(file_list), 3, 256, 256))
labels = np.zeros((len(file_list), 2)).astype(np.int)

with open('test.txt', 'r') as T:
lines = T.readlines()

labelDict = dict()
for i,l in enumerate(lines):
sp = l.strip().split(' ')
fileName = sp[0]
label = sp[1]
labelDict[fileName] = label


for j,_file in enumerate(file_list):
image = np.array(Image.open(image_dir + _file))
image = np.transpose(image, (2,0,1))
datas[j, :, :, :] = np.array(image).astype(np.float32) / 255
labels[j,:] = np.array(LABELS[labelDict[_file]]).astype(np.int)

with h5py.File(HDF5_FILE_TRAIN, 'w') as f:
    f['data'] = datas
    f['labels'] = labels
    f.close()

in text.txt are  lines as follows:
long_plaid_001.jpg kinds_0
long_plaid_002.jpg kinds_0
long_plaid_003.jpg kinds_0
......

hdf5 dataset for training is created the same way.

the definition of network:
name: "CaffeNet"
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "labels"
  include {
    phase: TRAIN
  }
  hdf5_data_param {
    source: "/home/percy/caffe/models/multilabel/list_train.txt"
    batch_size: 50
  }
}
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "labels"
  include {
    phase: TEST
  }
  hdf5_data_param {
    source: "/home/percy/caffe/models/multilabel/list_test.txt"
    batch_size: 50
  }
}
layer {
  name: "slicers"
  type: "Slice"
  bottom: "labels"
  top: "label_1"
  top: "label_2"
  slice_param {
    axis: 1
    slice_point: 1
  }
}
.......
Conv Pool ...
.......
layer {
  name: "fc6"
  type: "InnerProduct"
  bottom: "pool5"
  top: "fc6"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.005
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layer {
  name: "relu6"
  type: "ReLU"
  bottom: "fc6"
  top: "fc6"
}
layer {
  name: "drop6"
  type: "Dropout"
  bottom: "fc6"
  top: "fc6"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  name: "fc7"
  type: "InnerProduct"
  bottom: "fc6"
  top: "fc7"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.005
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layer {
  name: "relu7"
  type: "ReLU"
  bottom: "fc7"
  top: "fc7"
}
layer {
  name: "drop7"
  type: "Dropout"
  bottom: "fc7"
  top: "fc7"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  name: "fc8"
  type: "InnerProduct"
  bottom: "fc7"
  top: "fc8"
  param {
    lr_mult: 10
    decay_mult: 1
  }
  param {
    lr_mult: 20
    decay_mult: 0
  }
  inner_product_param {
    num_output: 2
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "accuracy_1"
  type: "Accuracy"
  bottom: "fc8"
  bottom: "label_1"
  top: "accuracy_1"
  include {
    phase: TEST
  }
}
layer {
  name: "loss_1"
  type: "SoftmaxWithLoss"
  bottom: "fc8"
  bottom: "label_1"
  top: "loss_1"
}
layer {
  name: "fc9"
  type: "InnerProduct"
  bottom: "pool5"
  top: "fc9"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.005
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layer {
  name: "relu9"
  type: "ReLU"
  bottom: "fc9"
  top: "fc9"
}
layer {
  name: "drop9"
  type: "Dropout"
  bottom: "fc9"
  top: "fc9"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  name: "fc10"
  type: "InnerProduct"
  bottom: "fc9"
  top: "fc10"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.005
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layer {
  name: "relu10"
  type: "ReLU"
  bottom: "fc10"
  top: "fc10"
}
layer {
  name: "drop10"
  type: "Dropout"
  bottom: "fc10"
  top: "fc10"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  name: "fc11"
  type: "InnerProduct"
  bottom: "fc10"
  top: "fc11"
  param {
    lr_mult: 10
    decay_mult: 1
  }
  param {
    lr_mult: 20
    decay_mult: 0
  }
  inner_product_param {
    num_output: 5
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layer {
  name: "accuracy_2"
  type: "Accuracy"
  bottom: "fc11"
  bottom: "label_2"
  top: "aaccuracy_2"
  include {
    phase: TEST
  }
}
layer {
  name: "loss_2"
  type: "SoftmaxWithLoss"
  bottom: "fc11"
  bottom: "label_2"
  top: "loss_2"
}

solver.prototxt:
net: "/home/percy/caffe/models/multilabel/train_val.prototxt"
test_iter: 1000  
test_interval: 1000
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 10000
display: 50
max_iter: 40000
momentum: 0.9
weight_decay: 0.0005
snapshot: 4000
snapshot_prefix: "caffenet_train"
solver_mode: GPU
Reply all
Reply to author
Forward
0 new messages