Exemplo n.º 1
0
  def __init__(self, params, result):
    self.result = result
    
    #load data
    if params['source'][-4:] == '.mat':
      self.img_data, self.img_labels = mat2py_imdb(params['source'])
      self.img_data = np.rollaxis(self.img_data,2)  #swap dims to have Num x height x width
    elif os.path.isdir(params['source']):  #assume it is lmdb
      lmdb_ = lmdbpy()
      self.img_data, self.img_labels = lmdb_.read(params['source']) #use read_encoded if the img is encoded
      self.img_data = self.img_data.squeeze()
    else:
      assert 0, 'Invalid format for source {}\n\
      need either lmdb or .mat data'.format(params['source'])
    
    if params['mean_file']==0:
      self.img_mean = 0
    elif params['mean_file'][-4:] == '.mat':
      self.img_mean = mat2py_mean(params['mean_file'])
    elif params['mean_file'][-12:] == '.binaryproto':
      self.img_mean = biproto2py(params['mean_file']).squeeze()
    else:
      assert 0, 'Invalid format for mean_file {}'.format(params['mean_file'])
    
    
    self.indexlist = range(len(self.img_labels))
    shuffle(self.indexlist)
    self._cur = 0  # current image
    
    # this class does some simple data-manipulations
    self.img_augment = SimpleAugment(mean=self.img_mean,shape=params['shape'],
                                     scale = params['scale'], rot = params['rot'])

    print "\nBatchLoader initialized with {} images".format(
        len(self.img_labels))
Exemplo n.º 2
0
    def __init__(self, params):

        self.batch_size = params['batch_size']
        self.outshape = params['shape']
        self.classes_per_batch = params['classes_per_batch']

        self.img_lmdb = lmdbs(params['img_source'])
        if params['skt_source'].endswith('.pkl'):
            self.skt_lmdb = svgs(params['skt_source'])
        else:
            self.skt_lmdb = lmdbs(params['skt_source'])
        self.img_labels = self.img_lmdb.get_label_list()
        self.skt_labels = self.skt_lmdb.get_label_list()
        self.img_mean = biproto2py(params['mean_file']).squeeze()

        self.num_classes = len(set(self.skt_labels))
        assert len(self.skt_labels)%self.num_classes==0, \
          'Unequal sketch training samples for each class'
        self.skt_per_class = len(self.skt_labels) / self.num_classes

        if 'hard_pos' in params:
            self.hard_sel = 'hard_pos'
            self.hard_sel_file = params['hard_pos']
        elif 'hard_neg' in params:
            self.hard_sel = 'hard_neg'
            self.hard_sel_file = params['hard_neg']
        elif 'hard_pn' in params:
            self.hard_sel = 'hard_pn'
            self.hard_sel_file = params['hard_pn']
        else:
            assert False, 'Hard selection must be on'

        self.watchChange = WatchDog(
            self.hard_sel_file)  #check if file has been updated
        self.hardsel_tab = np.load(self.hard_sel_file)
        self.hard_pos = self.hardsel_tab['pos']
        self.hard_neg = self.hardsel_tab['neg']

        self.img_labels_dict, self.classes = vec2dic(self.img_labels)
        self.NSKTS = len(self.skt_labels)
        self.indexlist = range(self.NSKTS)
        self.shuffle_keeping_min_classes_per_batch()
        self._cur = 0  # current image

        # this class does some simple data-manipulations
        self.img_augment = SimpleAugment(mean=self.img_mean,
                                         shape=params['shape'],
                                         scale=params['scale'],
                                         rot=params['rot'])

        if 'verbose' not in params:
            print "BatchLoader initialized with {} sketches, {} images of {} classes".format(
                len(self.skt_labels), len(self.img_labels), self.num_classes)
            print('Hard selection: {}'.format(self.hard_sel))
        #create threadpools for parallel augmentation
        self.pool = ThreadPool()  #4
Exemplo n.º 3
0
    def __init__(self, params):
        #load data
        self.batch_size = params['batch_size']
        self.outshape = params['shape']

        if params['source'][-4:] == '.mat':
            self.img_data, self.img_labels = mat2py_imdb(params['source'])
            self.img_data = self.img_data.transpose(
                2, 0, 1)  #swap dims to have Num x height x width
        elif os.path.isdir(params['source']):  #assume it is lmdb
            lmdb_ = lmdbpy()
            self.img_data, self.img_labels = lmdb_.read(
                params['source'])  #use read_encoded if the img is encoded
            self.img_data = self.img_data.squeeze()
        else:
            assert 0, 'Invalid format for source {}\n\
      need either lmdb or .mat data'.format(params['source'])

        label_ids = list(set(self.img_labels))
        NCATS = len(label_ids)
        if label_ids[0] != 0 or label_ids[-1] != NCATS - 1:
            print 'Your data labels are not [0:{}]. Converting label ...'.format(
                NCATS - 1)
            self.img_labels = [
                label_ids.index(label) for label in self.img_labels
            ]

        if params['mean_file'] == 0:
            self.img_mean = 0
        elif params['mean_file'][-4:] == '.mat':
            self.img_mean = mat2py_mean(params['mean_file'])
        elif params['mean_file'][-12:] == '.binaryproto':
            self.img_mean = biproto2py(params['mean_file']).squeeze()
        else:
            assert 0, 'Invalid format for mean_file {}'.format(
                params['mean_file'])

        self.indexlist = range(len(self.img_labels))
        shuffle(self.indexlist)
        self._cur = 0  # current image

        # this class does some simple data-manipulations
        self.img_augment = SimpleAugment(mean=self.img_mean,
                                         shape=params['shape'],
                                         scale=params['scale'],
                                         rot=params['rot'])

        print "\nBatchLoader initialized with {} images".format(
            len(self.img_labels))
        self.pool = ThreadPool()  #4
Exemplo n.º 4
0
 def __init__(self, params):
   
   self.batch_size = params['batch_size']
   self.outshape = params['shape']
   
   self.lmdb = lmdbs(params['source'])
   self.labels = self.lmdb.get_label_list()
   self.img_mean = biproto2py(params['mean_file']).squeeze()
   
   self.NIMGS = len(self.labels)
   
   self.num_batches = int(np.ceil(self.NIMGS/float(self.batch_size)))
   self._cur = 0  # current batch
   
   # this class does some simple data-manipulations
   self.img_augment = SimpleAugment(mean=self.img_mean,shape=params['shape'],
                                    scale = params['scale'])
Exemplo n.º 5
0
 def __init__(self, params):
   
   self.batch_size = params['batch_size']
   self.outshape = params['shape']
   
   self.lmdb = lmdbs(params['source'])
   self.labels = self.lmdb.get_label_list()
   self.img_mean = biproto2py(params['mean_file']).squeeze()
   
   self.NIMGS = len(self.labels)
   assert self.NIMGS%self.batch_size==0,'NIMGS {} not dividible by batchsize {}'.format(
          self.NIMGS,self.batch_size)
   
   self.num_batches = self.NIMGS/self.batch_size
   self._cur = 0  # current batch
   self.labels_tab = self.labels.reshape((self.num_batches,self.batch_size))
   
   # this class does some simple data-manipulations
   self.img_augment = SimpleAugment(mean=self.img_mean,shape=params['shape'],
                                    scale = params['scale'])
Exemplo n.º 6
0
    def __init__(self, params):

        self.batch_size = params['batch_size']
        self.outshape = params['shape']
        self.classes_per_batch = params['classes_per_batch']

        self.img_lmdb = lmdbs(params['img_source'])
        if params['skt_source'].endswith('.pkl'):
            self.skt_lmdb = svgs(params['skt_source'])
        else:
            self.skt_lmdb = lmdbs(params['skt_source'])
        self.img_labels = self.img_lmdb.get_label_list()
        self.skt_labels = self.skt_lmdb.get_label_list()
        self.img_mean = biproto2py(params['mean_file']).squeeze()

        self.num_classes = len(set(self.skt_labels))
        assert len(self.skt_labels)%self.num_classes==0, \
          'Unequal sketch training samples for each class'
        self.skt_per_class = len(self.skt_labels) / self.num_classes

        self.hard_sel = 0

        self.img_labels_dict, self.classes = vec2dic(self.img_labels)

        self.indexlist = range(len(self.skt_labels))
        self.shuffle_keeping_min_classes_per_batch()
        self._cur = 0  # current image

        # this class does some simple data-manipulations
        self.img_augment = SimpleAugment(mean=self.img_mean,
                                         shape=params['shape'],
                                         scale=params['scale'],
                                         rot=params['rot'])

        if 'verbose' not in params:
            print "BatchLoader initialized with {} sketches, {} images of {} classes".format(
                len(self.skt_labels), len(self.img_labels), self.num_classes)
        #create threadpools for parallel augmentation
        self.pool = ThreadPool()  #4
Exemplo n.º 7
0
    def __init__(self, params):

        self.batch_size = params['batch_size']
        self.img_shape = params['shape']
        self.classes_per_batch = params['classes_per_batch']

        self.img_lmdb = lmdbs(params['img_source'])
        if params['skt_source'].endswith('.pkl'):
            self.skt_lmdb = svgs(params['skt_source'])
        else:
            self.skt_lmdb = lmdbs(params['skt_source'])
        self.img_labels = self.img_lmdb.get_label_list()
        self.skt_labels = self.skt_lmdb.get_label_list()
        label_ids = list(set(self.img_labels))
        NCATS = len(label_ids)
        if label_ids[0] != 0 or label_ids[-1] != NCATS - 1:
            if 'verbose' not in params:
                print 'Your data labels are not [0:{}]. Converting label ...'.format(
                    NCATS - 1)
            self.img_labels = [
                label_ids.index(label) for label in self.img_labels
            ]
            self.skt_labels = [
                label_ids.index(label) for label in self.skt_labels
            ]

        self.img_mean = biproto2py(params['mean_file']).squeeze()
        #self.skt_mean = biproto2py(params['skt_mean']).squeeze()

        self.num_classes = len(set(self.skt_labels))
        assert self.num_classes == NCATS, 'XX!!Sketch & image datasets unequal #categories'
        assert len(self.skt_labels)%self.num_classes==0, \
          'Unequal sketch training samples for each class'
        self.skt_per_class = len(self.skt_labels) / self.num_classes

        if 'hard_pos' in params:
            self.hard_sel = 1
            self.hard_pos = np.load(params['hard_pos'])['pos']
        elif 'hard_neg' in params:
            self.hard_sel = 2
            self.hard_neg = np.load(params['hard_neg'])['neg']
        elif 'hard_pn' in params:
            self.hard_sel = 3
            tmp = np.load(params['hard_pn'])
            self.hard_pos = tmp['pos']
            self.hard_neg = tmp['neg']
        else:  #hard selection turn off
            self.hard_sel = 0

        #self.img_labels_dict, self.classes = vec2dic(self.img_labels)

        self.indexlist = range(len(self.skt_labels))
        self.indexlist_img = range(len(self.img_labels))
        #self.shuffle_keeping_min_classes_per_batch()
        shuffle(self.indexlist)
        shuffle(self.indexlist_img)
        self._cur = 0  # current image
        self._cur_img = 0

        # this class does some simple data-manipulations
        self.img_augment = SimpleAugment(mean=self.img_mean,
                                         shape=self.img_shape,
                                         scale=params['scale'],
                                         rot=params['rot'])

        print "BatchLoader initialized with {} sketches, {} images of {} classes".format(
            len(self.skt_labels), len(self.img_labels), self.num_classes)
        #create threadpools for parallel augmentation
        self.pool = ThreadPool()  #4
def extractitem(net, image):
    """
  extract feature
  """

    inblob = net.inputs[0]
    outblob = net.blobs.keys()[layer - 1]
    net.blobs[inblob].data[...] = image
    _ = net.forward()
    prediction = net.blobs[outblob].data.squeeze()
    return prediction


if __name__ == '__main__':
    if domain == 'image':
        DEPLOY = 'models/deploy_images.prototxt'
        scale_factor = 1.0
    else:
        DEPLOY = 'models/deploy_sketch.prototxt'
        scale_factor = 2.0

    net = get_net(WEIGHTS, DEPLOY)
    mean_image = biproto2py(MEAN_DB).squeeze()
    """pre-processing"""
    img = preprocess(INPUT, mean_image, domain)
    """feat extraction"""
    net.blobs[net.inputs[0]].reshape(1, 1, 225, 225)
    feat = extractitem(net, img) * scale_factor

    print 'Done.'