def close(self, *args): # Populate observed category IDs, if necessary if self.classes is None: classes = sorted(self._classes) labels_map_rev = _to_labels_map_rev(classes) for anno in self._annotations: anno.category_id = labels_map_rev[anno.category_id] else: classes = self.classes info = { "year": "", "version": "", "description": "Exported from FiftyOne", "contributor": "", "url": "https://voxel51.com/fiftyone", "date_created": datetime.now().replace(microsecond=0).isoformat(), } categories = [ {"id": i, "name": l, "supercategory": "none"} for i, l in enumerate(classes) ] labels = { "info": info, "licenses": [], "categories": categories, "images": self._images, "annotations": self._annotations, } etas.write_json(labels, self._labels_path)
def close(self, *args): labels = { "classes": self.classes, "labels": self._labels_dict, } etas.write_json(labels, self._labels_path, pretty_print=self.pretty_print)
def close(self, *args): samples = {"samples": self._samples} etas.write_json(self._metadata, self._metadata_path, pretty_print=self.pretty_print) etas.write_json(samples, self._samples_path, pretty_print=self.pretty_print)
def _export_frame_labels(self, sample, uuid): frames = {} for frame_number, frame in sample.frames.items(): frames[str(frame_number)] = frame.to_dict() frames_dict = {"frames": frames} outpath = os.path.join(self._frame_labels_dir, uuid + ".json") etas.write_json(frames_dict, outpath, pretty_print=self.pretty_print) return outpath
def run(config_path, pipeline_config_path=None): '''Run the SVHN Classification Module. Args: config_path: path to a ConvolutionConfig file pipeline_config_path: optional path to a PipelineConfig file ''' svhn_config = SVHNClassificationConfig.from_json(config_path) etam.setup(svhn_config, pipeline_config_path=pipeline_config_path) for data in svhn_config.data: # Read all the MNIST data as numpy arrays mnist_train_images = read_idx(data.mnist_train_images_path) mnist_train_labels = read_idx(data.mnist_train_labels_path) mnist_test_images = read_idx(data.mnist_test_images_path) mnist_test_labels = read_idx(data.mnist_test_labels_path) # Read the digitStruct.mat from the SVHN test folder base_svhn_path = data.svhn_test_path dsf = DigitStructFile(base_svhn_path + "/digitStruct.mat") # training PCA_dir_train, PCA_socre_train, mean_train = Get_PCA( mnist_train_images, svhn_config.parameters.num_PCA) # testing svhn_error_rate, _, _ = test_SVHN(PCA_dir_train, mean_train, PCA_socre_train, mnist_train_labels, dsf, base_svhn_path, svhn_config.parameters.num_neighbor, 300) mnist_error_rate, _, _ = test_MNIST( PCA_dir_train, mean_train, PCA_socre_train, mnist_train_labels, mnist_test_images, mnist_test_labels, svhn_config.parameters.num_neighbor, -1) ''' # visualize PCA_vis, _, _ = Get_PCA(mnist_train_images, 10) visualize_PCA(PCA_vis) # get example test_MNIST_sample(PCA_dir_train, mean_train, PCA_socre_train, mnist_train_labels, mnist_test_images, mnist_test_labels, svhn_config.parameters.num_neighbor) test_SVHN_sample(PCA_dir_train, mean_train, PCA_socre_train, mnist_train_labels, dsf, base_svhn_path, svhn_config.parameters.num_neighbor) ''' error_rate_dic = defaultdict(lambda: defaultdict()) error_rate_dic["error_rates"]["mnist_error_rate"] = mnist_error_rate error_rate_dic["error_rates"]["svhn_error_rate"] = svhn_error_rate etas.write_json(error_rate_dic, data.error_rate_file)
def run(config_path, pipeline_config_path=None): '''Run the Pott's Energy module. Args: config_path: path to a ConvolutionConfig file pipeline_config_path: optional path to a PipelineConfig file ''' potts_config = PottsEnergyConfig.from_json(config_path) etam.setup(potts_config, pipeline_config_path=pipeline_config_path) for data in potts_config.data: energy = _calculate_potts_energy(data) potts_energy = defaultdict(lambda: defaultdict()) potts_energy["result"]["energy"] = energy etas.write_json(potts_energy, data.potts_energy_out)
def _find_line_segments(find_segments_config): for data in find_segments_config.data: in_img = etai.read(data.input_image) gradient_orientation = np.load("out/gradient_orientation.npy") full_segment = _hough_line_seg(in_img, gradient_orientation) #print(data) temp_dict_list = [] temp = defaultdict() for i in range(full_segment.shape[0]): temp = defaultdict() temp["No"] = i + 1 temp["coordinates"] = [(full_segment[i, 0], full_segment[i, 1]), (full_segment[i, 2], full_segment[i, 3])] temp_dict_list.append(temp) segment_out = defaultdict(lambda: defaultdict()) segment_out["Line_segments"] = temp_dict_list etas.write_json(segment_out, data.line_segments)
def write_json(self, json_path, rel_dir=None, pretty_print=False): """Writes the colllection to disk in JSON format. Args: json_path: the path to write the JSON rel_dir (None): a relative directory to remove from the ``filepath`` of each sample, if possible. The path is converted to an absolute path (if necessary) via ``os.path.abspath(os.path.expanduser(rel_dir))``. The typical use case for this argument is that your source data lives in a single directory and you wish to serialize relative, rather than absolute, paths to the data within that directory pretty_print (False): whether to render the JSON in human readable format with newlines and indentations """ etas.write_json( self.to_dict(rel_dir=rel_dir), json_path, pretty_print=pretty_print, )
def download_coco_dataset_split(dataset_dir, split, year="2017", cleanup=True): """Downloads and extracts the given split of the COCO dataset to the specified directory. Any existing files are not re-downloaded. Args: dataset_dir: the directory to download the dataset split: the split to download. Supported values are ``("train", "validation", "test")`` year ("2017"): the dataset year to download. Supported values are ``("2014", "2017")`` cleanup (True): whether to cleanup the zip files after extraction Returns: a tuple of - images_dir: the path to the directory containing the extracted images - anno_path: the path to the annotations JSON file """ if year not in _IMAGE_DOWNLOAD_LINKS: raise ValueError( "Unsupported year '%s'; supported values are %s" % (year, tuple(_IMAGE_DOWNLOAD_LINKS.keys())) ) if split not in _IMAGE_DOWNLOAD_LINKS[year]: raise ValueError( "Unsupported split '%s'; supported values are %s" % (year, tuple(_IMAGE_DOWNLOAD_LINKS[year].keys())) ) # # Download images # images_src_path = _IMAGE_DOWNLOAD_LINKS[year][split] images_zip_path = os.path.join( dataset_dir, os.path.basename(images_src_path) ) images_dir = os.path.join( dataset_dir, os.path.splitext(os.path.basename(images_src_path))[0] ) if not os.path.isdir(images_dir): logger.info("Downloading images zip to '%s'", images_zip_path) etaw.download_file(images_src_path, path=images_zip_path) logger.info("Extracting images to '%s'", images_dir) etau.extract_zip(images_zip_path, delete_zip=cleanup) else: logger.info("Image folder '%s' already exists", images_dir) # # Download annotations # anno_path = os.path.join(dataset_dir, _ANNOTATION_PATHS[year][split]) if split == "test": # Test split has no annotations, so we must populate the labels file # manually images = _make_images_list(images_dir) labels = { "info": {}, "licenses": [], "categories": [], "images": images, "annotations": [], } etas.write_json(labels, anno_path) else: anno_src_path = _ANNOTATION_DOWNLOAD_LINKS[year] anno_zip_path = os.path.join( dataset_dir, os.path.basename(anno_src_path) ) if not os.path.isfile(anno_path): logger.info("Downloading annotations zip to '%s'", anno_zip_path) etaw.download_file(anno_src_path, path=anno_zip_path) logger.info("Extracting annotations to '%s'", anno_path) etau.extract_zip(anno_zip_path, delete_zip=cleanup) else: logger.info("Annotations file '%s' already exists", anno_path) return images_dir, anno_path
def close(self, *args): etas.write_json(self._annotations, self._labels_path)
def run(config_path, pipeline_config_path=None): '''Run the SVHN Classification Module. Args: config_path: path to a ConvolutionConfig file pipeline_config_path: optional path to a PipelineConfig file ''' svhn_config = SVHNClassificationConfig.from_json(config_path) etam.setup(svhn_config, pipeline_config_path=pipeline_config_path) for data in svhn_config.data: # Read all the MNIST data as numpy arrays mnist_train_images = read_idx(data.mnist_train_images_path) mnist_train_labels = read_idx(data.mnist_train_labels_path) mnist_test_images = read_idx(data.mnist_test_images_path) mnist_test_labels = read_idx(data.mnist_test_labels_path) # Read the digitStruct.mat from the SVHN test folder base_svhn_path = data.svhn_test_path dsf = DigitStructFile(base_svhn_path + "/digitStruct.mat") #reshape mnust_train_data train_images = np.reshape(mnist_train_images, (mnist_train_images.shape[0], 784)) test_images = np.reshape(mnist_test_images, (mnist_test_images.shape[0], 784)) eigen_basis = compute_eigen_basis(mnist_train_images) mnist_total_num = 0 mnist_wrong = 0 # Apply PCA to the training and test images train = np.dot(train_images, eigen_basis.T) test = np.dot(test_images, eigen_basis.T) ''' plt.imshow(testarray.reshape(28, 28)) plt.show() pca = PCA(n_components=2) principal_components = pca.fit_transform(train_images) ''' # for i in range(500): # mnist_total_num += 1 # neighbors = getNeighbors(train, test[i], 3, mnist_train_labels) # res = getResponse(neighbors) # if res != mnist_test_labels[i]: # print("wrong", mnist_test_labels[i], res) # # plt.imshow(mnist_test_images[i]) # # plt.show() # mnist_wrong += 1 # else: # print("correct") # plt.imshow(mnist_test_images[i]) # plt.show() ''' Format of the bounding box { 'height': [16.0], 'label': [6.0], 'left': [61.0], 'top': [6.0], 'width': [11.0] } ''' svhn_total_num = 0 svhn_correct = 0 for i in range(300): print(i) for j in range(len(dsf.getBbox(i)['label'])): svhn_total_num += 1 img_original = crop_img(base_svhn_path, i + 1, int(dsf.getBbox(i)['top'][j]), int(dsf.getBbox(i)['height'][j]), int(dsf.getBbox(i)['left'][j]), int(dsf.getBbox(i)['width'][j])) img_gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY).flatten('F') img = np.dot(img_gray, eigen_basis.T) # Apply knn neighbors = getNeighbors(train, img, 1, mnist_train_labels) res = getResponse(neighbors) if res == int(dsf.getBbox(i)['label'][j]): svhn_correct += 1 # else: # print("wrong") # plt.imshow(img_original) # plt.show() print("correct rate:", svhn_correct, float(svhn_correct) / float(svhn_total_num)) '''CALL YOUR FUNCTIONS HERE. Please call of your functions here. For this problem, we ask you to visualize several things. You need to do this yourself (in any way you wish). For the MNIST and SVHN error rates, please store these two error rates in the variables called "mnist_error_rate" and "svhn_error_rate", for the MNIST error rate and SVHN error rate, respectively. These two variables will be used to write the numbers in a JSON file. ''' # Make sure you assign values to these two variables mnist_error_rate = 0.074 svhn_error_rate = (svhn_total_num - svhn_correct) / svhn_total_num error_rate_dic = defaultdict(lambda: defaultdict()) error_rate_dic["error_rates"]["mnist_error_rate"] = mnist_error_rate error_rate_dic["error_rates"]["svhn_error_rate"] = svhn_error_rate etas.write_json(error_rate_dic, data.error_rate_file)