torrvision / crfasrnn

This repository contains the source code for the semantic image segmentation method described in the ICCV 2015 paper: Conditional Random Fields as Recurrent Neural Networks. http://crfasrnn.torr.vision/
Other
1.34k stars 462 forks source link

Images in lmdb format #113

Closed AlexTS1980 closed 7 years ago

AlexTS1980 commented 7 years ago

How did you convert Pascal data set to lmdb format? Specifically, labels for each image. I made something like an array with datum.data set to four values, (x,y,h,w) of he bounding box.

sadeepj commented 7 years ago

Here is an example that might be useful:

import sys
caffe_root = "../caffe/"
sys.path.insert(0, caffe_root + "/python")
import caffe
import lmdb
from PIL import Image
import numpy as np

dataset_home = "/home/sadeep/data/"

label_home = dataset_home + "/labels/"
list_input = dataset_home + "/train_image_list.txt"
lmdb_out = dataset_home + "/train_labels_lmdb"

label_db = lmdb.open(lmdb_out, map_size=int(1e12))

fp = open(list_input)

with label_db.begin(write=True) as in_txn:
    for in_idx, line in enumerate(fp):

        file_title = line.rstrip("\n")
        label_im_path = label_home + file_title + ".png"

        im = np.array(Image.open(label_im_path))
        cur_height, cur_width = im.shape
        im = im.reshape(cur_height, cur_width, 1)

        cur_h, cur_w, cur_c = im.shape
        pad_h = 500 - cur_h
        pad_w = 500 - cur_w
        im = np.pad(im, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode='constant', constant_values=255)
        im = im.transpose(2, 0, 1)
        im_dat = caffe.io.array_to_datum(im)
        in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
        if in_idx % 100 == 0:
            print("Processed {}: {}".format(in_idx, file_title))

label_db.close()
fp.close()