def create_set_files(n_sets, input_set_index, input_set_name, output_set_index, output_set_name, exclusion_files=None, include_full=False, set_range=100): class_images = load_imagelist(set_index=input_set_index, set_name=input_set_name, exclusion_files=exclusion_files) all_classes = np.unique(class_images.values()) n_images = len(class_images) revalations = [str(r) for r in xrange(0, set_range, 10)] if include_full: revalations += ['full'] n_revalations = len(revalations) image_to_index = {} idx = 0 for c in all_classes: image_list = [k for k in class_images.keys() if class_images[k] == c] for image in np.random.permutation(image_list): image_to_index[image] = idx idx += 1 for i_set in xrange(n_sets): set_fn = get_imageset_filename(set_index=output_set_index + i_set, set_name=output_set_name) print 'Writing %s' % set_fn with open(set_fn, 'wt') as fid: image_list = np.random.permutation( [k for k in class_images.keys()]) for image in image_list: rev = revalations[(image_to_index[image] + i_set) % n_revalations] fid.write('%s/%s\t%d\n' % (rev, image, class_images[image]))
def load_im2path(self, experiment_ids): self.im2path = {} for experiment_id in experiment_ids: set_index, set_name = util.get_experiment_imageset(experiment_id) imageset_filename = util.get_imageset_filename(set_index, set_name) im_names = label_results(imageset_filename) im_root = util.get_input_image_root(set_index, set_name) for k in im_names.iterkeys(): self.im2path[k] = im_root
def convert_raw_imageset(set_index, set_name): fileid_filename = get_imageset_filename(set_index=set_index, set_name=set_name) raw_filename = get_imageset_filename_raw(set_index=set_index, set_name=set_name) if os.path.isfile(fileid_filename): raise RuntimeError('File exists: %s' % fileid_filename) convert_raw_to_fileid(raw_filename, fileid_filename) print 'Written to %s' % fileid_filename
def write_set_file(set_index, set_name, dst_filename): # Load set and convert to CSV src_fn = get_imageset_filename(set_index=set_index, set_name=set_name) data = open(src_fn, 'rt').read().splitlines() class_identifiers = ['nonanimal', 'animal' ] # in src_fn, 0 is the distractor; 1 is the tagetg with open(dst_filename, 'wt') as fid: fid.write('img,cat\n') for line in data: ll = line.split('\t') fn = ll[0] class_identifier = class_identifiers[int(ll[1])] fid.write('%s,%s\n' % (fn, class_identifier))
def eval_by_classes(self, class_idcs, class_names): self.model_comp_by_class = {} for class_idx,class_name in zip(class_idcs, class_names): imageset_filename = util.get_imageset_filename(class_idx, 'set') class_images = label_results(imageset_filename) comp = {'animal': [], 'nonanimal': []} rts = {'animal': [], 'nonanimal': []} for im,lbl in class_images.iteritems(): if im in self.im2key: comp[lbl] += [self.model_comp['animal']['human_acc'][0, self.im2key[im]]] rts[lbl] += [self.data_by_img[im[:-4]]['rt']] self.model_comp_by_class[class_idx] = comp print 'Class %s: Found %d animal, %d nonanimal trials' % (class_name, len(comp['animal']), len(comp['nonanimal'])) print 'Class %s percent correct: %.1f (animal=%.1f, nonanimal=%.1f)' % (class_name, np.mean(comp['animal']+comp['nonanimal']), np.mean(comp['animal']), np.mean(comp['nonanimal'])) print 'Class %s RT: %.1fms (animal=%.1fms, nonanimal=%.1fms)' % (class_name, np.mean(rts['animal'] + rts['nonanimal']), np.mean(rts['animal']),np.mean(rts['nonanimal']))
def load_ground_truth(self, set_index, set_name): imageset_filename = util.get_imageset_filename(set_index, set_name) # Generate ground truth key self.im2lab.update(label_results(imageset_filename)) self.loaded_sets += [(set_index, set_name)]