Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caffe: Regression with CNN #2818

Closed
shubham-pachori opened this issue Jul 25, 2015 · 2 comments
Closed

Caffe: Regression with CNN #2818

shubham-pachori opened this issue Jul 25, 2015 · 2 comments

Comments

@shubham-pachori
Copy link

Hi!

I am working on some problem statement in Computer vision. I am new to Caffe. Is there anyway in which I could perform regression with CNN such that the size of my input and label (output) is height_width_no. of channels ? And is there any way to import training data and training labels separately in lmdb format in Caffe?

@adnan15110
Copy link

Hi!

First, create two separate lmdb files (data_lmdb and label_lmdb). Here is an example for regression problem for image:

import numpy as np
import lmdb,os,sys,math

caffe_root = 'path to caffe'
sys.path.insert(0, caffe_root + 'python')

import caffe

class CreateLmdb:
    def __init__(self):
        # configure below variables
        # location to the folder where the raw image file located
        self.dataset_path = os.path.join("---path to stored image")
        # location to the folder where the train.txt, test.txt and val.txt located
        self.txt_file_path = os.path.join("--path to train.txt and others--")

        dataset_types = ['train','val','test']

        for d_type in dataset_types:
            self.create_lmdb_file(d_type)

    def create_images_labels_list(self, dataset_type):
        images = []
        labels = []
        f = open(os.path.join(self.txt_file_path, dataset_type+'.txt'))
        for line in f.readlines():
            image,label = line.strip().split(' ')
            image_location = os.path.join(self.dataset_path,dataset_type,image)
            images.append(image_location)
            labels.append(float(label))
        return images, labels

    def create_lmdb_file(self, dataset_type):
        print 'Writing LMDB data ...'

        lmdb_data_name = dataset_type + '_data_lmdb'
        lmdb_label_name = dataset_type + '_score_lmdb'

        images,labels = self.create_images_labels_list(dataset_type)
        print 'Writing labels ...'

        # Size of buffer: 1000 elements to reduce memory consumption
        for idx in range(int(math.ceil(len(labels)/1000.0))):
            in_db_label = lmdb.open(lmdb_label_name, map_size=int(1e12))
            with in_db_label.begin(write=True) as in_txn:
                for label_idx, label_ in enumerate(labels[(1000*idx):(1000*(idx+1))]):
                    im_dat = caffe.io.array_to_datum(np.array(label_).astype(float).reshape(1,1,1))
                    in_txn.put('{:0>10d}'.format(1000*idx + label_idx), im_dat.SerializeToString())
                    string_ = str(1000*idx+label_idx+1) + ' / ' + str(len(labels))
                    sys.stdout.write("\r%s" % string_)
                    sys.stdout.flush()
            in_db_label.close()

        print '\nfinished'

        print 'Writing image data'
        for idx in range(int(math.ceil(len(images)/1000.0))):
            in_db_data = lmdb.open(lmdb_data_name, map_size=int(1e12))
            with in_db_data.begin(write=True) as in_txn:
                for in_idx, in_ in enumerate(images[(1000*idx):(1000*(idx+1))]):
                    im = caffe.io.load_image(in_)
                    im_dat = caffe.io.array_to_datum(im.astype(float).transpose((2, 0, 1)))
                    in_txn.put('{:0>10d}'.format(1000*idx + in_idx), im_dat.SerializeToString())

                    string_ = str(1000*idx+in_idx+1) + ' / ' + str(len(images))
                    sys.stdout.write("\r%s" % string_)
                    sys.stdout.flush()
            in_db_data.close()
        print '\nfinished'


if __name__ == '__main__':
    create_lmdb = CreateLmdb() 

Then define you .prototxt file as follows:

layer {
  name: "image"
  type: "Data"
  top: "image"
  include {
    phase: TRAIN
  }
  data_param {
    source: "data_lmdb"
    backend: LMDB
  }
}

layer {
  name: "label"
  type: "Data"
  top: "label"
  data_param {
    source: "labels_lmdb"
    batch_size:1
    backend: LMDB
  }
}

# attach the image layer as bottom to next layer

attach the label to the euclidean loss or hingeloss layer as follows:

layer {
  name: "loss"
  type: "EuclideanLoss"
  bottom:  --last layer that calculates the output--
  bottom: "label"
  top: "loss"
}

** most of the codes are taken from several issues in caffe.

@longjon
Copy link
Contributor

longjon commented Aug 2, 2015

Closing as this looks like a usage issue/request for help.

This tracker is reserved for specific Caffe development issues and bugs; please ask usage questions on the caffe-users list.

For more information, see our contributing guide.

Thanks!

@longjon longjon closed this as completed Aug 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants