I am trying to do multi label classification based on the lmdb database. I create two different databases. One for the images itself and one for the labels. My intention is to have 2 different labels for angles in horizontal and vertical direction. That means label1 [0-360] label2 [0-360].
To do so my code is the following:
data_name = "val"
data = data_name + '.txt'
lmdb_data_name = data_name + '_images_lmdb'
lmdb_label_name = data_name + '_labels_lmdb'
images = []
labels = []
for line in fileinput.input(data):
entries = re.split(' ', line.strip())
# append image
images.append(entries[0])
labels.append(entries[1:])
images_db = lmdb.open(lmdb_data_name, map_size=int(1e12))
labels_db = lmdb.open(lmdb_label_name, map_size=int(1e12))
images_txn = images_db.begin(write=True)
labels_txn = labels_db.begin(write=True)
inputs = zip(images, labels)
for in_idx, (image, label) in enumerate(inputs):
im = cv2.imread(image)
im = im[:,:,::-1]
im = im.transpose((2,0,1))
im_dat = caffe.io.array_to_datum(im)
images_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
label = np.array(label).astype(int).reshape(1, 1, len(label))
label_dat = caffe.io.array_to_datum(label)
labels_txn.put('{:0>10d}'.format(in_idx),label_dat.SerializeToString())
images_txn.commit()
labels_txn.commit()
images_db.close()
labels_db.close()
My train.txt
looks like: /path/to/image label1 label2
where label1
and label2
are integers.
My train_val.prototxt
looks like this:
layer {
name: "data"
type: "Data"
top: "images"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 256
mean_file: "/path/mean_train.binaryproto"
}
data_param {
source: "/path/train_images_lmdb"
batch_size: 10
backend: LMDB
}
}
layer {
name: "data_label"
type: "Data"
top: "labels"
include {
phase: TRAIN
}
data_param {
source: "/train_labels_lmdb"
batch_size: 10
backend: LMDB
}
}
The part for the TEST phase is identical
My loss layer looks like this:
layer {
name: "loss"
type: "SigmoidCrossEntropyLoss"
bottom: "fc8"
bottom: "labels"
top: "loss"
}