def __init__(self, class_map=[], imsize=(224, 224), load_pretrained_weight=False, train_whole_network=False): if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) self.imsize = imsize self.num_class = len(class_map) self.class_map = [c.encode("ascii", "ignore") for c in class_map] self._model = CNN_VGG11(self.num_class) self._train_whole_network = train_whole_network self._opt = rm.Sgd(0.01, 0.9) self.decay_rate = 0.0005 if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self._model.load(load_pretrained_weight) self._model.fc1.params = {} self._model.fc2.params = {} self._model.fc3.params = {} assert not load_pretrained_weight, "Currently pretrained weight of %s is not prepared. Please set False to `load_pretrained_weight` flag." % self.__class__.___name__
def __init__(self, class_map=[], imsize=(224, 224), load_pretrained_weight=False, train_whole_network=False): assert not load_pretrained_weight, "In ReNomIMG version {}, pretained weight of {} is not prepared.".format( __version__, self.__class__.__name__) if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) layer_per_block = [6, 12, 48, 32] growth_rate = 32 self.imsize = imsize self.num_class = len(class_map) self.class_map = [c.encode("ascii", "ignore") for c in class_map] self._train_whole_network = train_whole_network self._model = CNN_DenseNet(self.num_class, layer_per_block, growth_rate, train_whole_network) self._opt = rm.Sgd(0.01, 0.9) self.decay_rate = 0.0005 if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self._model.load(load_pretrained_weight) for layer in self._model._network.iter_models(): layer.params = {} if self.num_class != 1000: self._model.params = {} self._freeze()
def fetch_classification_dataset_caltech101(split_validation=True, test_size=0.2): """ Args: split_validation (boolean): Whether or not split validation data. test_size(float): proportion of the test dataset in total datasets. Returns: (list): This returns list of image path. (list): This returns list of label. """ caltech101_url = "http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz" image_caltech101 = "101_ObjectCategories" caltech_101_tar = "101_ObjectCategories.tar.gz" if not os.path.exists(image_caltech101): download(caltech101_url) with tarfile.open(caltech_101_tar) as tar: tar.extractall() class_map = sorted(os.listdir(image_caltech101))[1:] image_path_list = [] label_list = [] for i, c in enumerate(class_map): root_path = os.path.join(image_caltech101, c) img_files = os.listdir(root_path) image_path_list.extend( [os.path.join(root_path, path) for path in img_files]) label_list += [i] * len(img_files) if split_validation == True: N = len(image_path_list) perm = np.random.permutation(N) test_N = int(N * test_size) train_image_path_list = [image_path_list[p] for p in perm[test_N:]] train_label_list = [label_list[p] for p in perm[test_N:]] valid_image_path_list = [image_path_list[p] for p in perm[:test_N]] valid_label_list = [label_list[p] for p in perm[:test_N]] return train_image_path_list, train_label_list, valid_image_path_list, valid_label_list else: return image_path_list, label_list
def pull(self): """Pull trained weight from ReNomIMG server. Trained weight will be downloaded into current directory. Example: >>> from renom_img.api.inference.detector import Detector >>> detector = Detector() >>> detector.pull() """ url = self._url + ':' + self._port download_weight_api = "/api/renom_img/v1/projects/{}/deployed_model".format( self._pj_id) download_param_api = "/api/renom_img/v1/projects/{}/deployed_model_info".format( self._pj_id) download_weight_api = url + download_weight_api download_param_api = url + download_param_api ret = requests.get(download_param_api).json() filename = ret["filename"] if ret["algorithm"] == ALG_YOLOV1: self._alg_name = "Yolov1" img_w = ret["hyper_parameters"]["image_width"] img_h = ret["hyper_parameters"]["image_height"] self._model = Yolov1(imsize=(img_h, img_w)) elif ret["algorithm"] == ALG_YOLOV2: self._alg_name = "Yolov2" img_w = ret["hyper_parameters"]["image_width"] img_h = ret["hyper_parameters"]["image_height"] self._model = Yolov2(imsize=(img_h, img_w)) if not os.path.exists(filename): download(download_weight_api, filename) self._model.load(filename) self._model_info = { "Algorithm": self._alg_name, "Image size": "{}x{}".format(img_w, img_h), "Num class": "{}".format(self._model.num_class) }
def pull(self): """Pull trained weight from ReNomIMG server. Trained weight will be downloaded into current directory. Example: >>> from renom_img.api.inference.detector import Detector >>> detector = Detector() >>> detector.pull() """ url = self._url + ':' + self._port download_weight_api = "/api/renom_img/v2/deployed_model/task/1" download_param_api = "/api/renom_img/v2/deployed_model_info/task/1" download_weight_api = url + download_weight_api download_param_api = url + download_param_api ret = self.error_handler( lambda: requests.get(download_param_api).json()) if ret.get('error_msg', False): raise Exception(ret.get('error_msg')) filename = os.path.basename(ret["filename"]) if not os.path.exists(filename): self.error_handler(lambda: download(download_weight_api, filename)) if ret["algorithm_id"] == Algorithm.YOLOV1.value: self._alg_name = "Yolov1" img_w = ret["hyper_parameters"]["imsize_w"] img_h = ret["hyper_parameters"]["imsize_h"] self._model = Yolov1(imsize=(img_h, img_w)) self._model.load(filename) elif ret["algorithm_id"] == Algorithm.YOLOV2.value: self._alg_name = "Yolov2" img_w = ret["hyper_parameters"]["imsize_w"] img_h = ret["hyper_parameters"]["imsize_h"] self._model = Yolov2(imsize=(img_h, img_w)) self._model.load(filename) elif ret["algorithm_id"] == Algorithm.SSD.value: self._alg_name = "SSD" img_w = ret["hyper_parameters"]["imsize_w"] img_h = ret["hyper_parameters"]["imsize_h"] self._model = SSD(imsize=(img_h, img_w)) self._model.load(filename) self._model._network.num_class = self._model.num_class self._model_info = { "Algorithm": self._alg_name, "Image size": "{}x{}".format(img_w, img_h), "Num class": "{}".format(self._model.num_class) }
def __init__(self, class_map=[], imsize=(299, 299), load_pretrained_weight=False, train_whole_network=True): if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) self.imsize = imsize self.num_class = len(class_map) self.class_map = [c.encode("ascii", "ignore") for c in class_map] self._train_whole_network = train_whole_network self._model = CNN_InceptionV4(self.num_class) self._opt = rm.Sgd(0.045, 0.9) self.decay_rate = 0.0005 if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self._model.load(load_pretrained_weight) for layer in self._model._network.iter_models(): layer.params = {}
def __init__(self, num_class=1000, load_pretrained_weight=False): self._num_class = num_class self._base = Darknet19Base() self._last = rm.Conv2d(num_class, filter=1) self._last.params = { "w": rm.Variable(self._last._initializer((num_class, 1024, 1, 1)), auto_update=True), "b": rm.Variable(self._last._initializer((1, num_class, 1, 1)), auto_update=False), } super(Darknet19, self).__init__() if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self.load(load_pretrained_weight)
def __init__(self, class_map=None, imsize=(224, 224), load_pretrained_weight=False, train_whole_network=False, load_target=None): # 1. Cast class_map to list and encodes the class names to ascii. if class_map is None: self.class_map = [] else: if isinstance(class_map, list): self.class_map = [c.encode("ascii", "ignore") for i, c in enumerate(class_map)] elif isinstance(class_map, dict): self.class_map = [k.encode("ascii", "ignore") for k, v in class_map.items()] # Determines last layer's unit size according to the class number. self.num_class = len(self.class_map) self.set_last_layer_unit(self.num_class) # 2. Accepts imsize both tuple and int. if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) self.imsize = imsize # Train whole or not. self.train_whole_network = train_whole_network # 4. Load pretrained weight. self.load_pretrained_weight = load_pretrained_weight if load_pretrained_weight and load_target is not None: assert self.WEIGHT_URL, \ "The class '{}' has no pretrained weight.".format(self.__class__.__name__) if isinstance(load_pretrained_weight, bool): weight_path = self.__class__.__name__ + '.h5' elif isinstance(load_pretrained_weight, str): weight_path = load_pretrained_weight if not os.path.exists(weight_path): download(self.WEIGHT_URL, weight_path) load_target.load(weight_path)
def __init__(self, class_map=[], cells=7, bbox=2, imsize=(224, 224), load_pretrained_weight=False, train_whole_network=False): num_class = len(class_map) if not hasattr(cells, "__getitem__"): cells = (cells, cells) if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) self.num_class = num_class self.class_map = [c.encode("ascii", "ignore") for c in class_map] self._cells = cells self._bbox = bbox self._last_dense_size = (num_class + 5 * bbox) * cells[0] * cells[1] model = Darknet(self._last_dense_size) self._train_whole_network = train_whole_network self.imsize = imsize self._freezed_network = rm.Sequential(model[:-7]) self._network = rm.Sequential(model[-7:]) self._opt = rm.Sgd(0.01, 0.9) if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self.load(load_pretrained_weight) for layer in self._network.iter_models(): layer.params = {}
def __init__(self, class_map=[], imsize=(299, 299), load_pretrained_weight=False, train_whole_network=True): assert not load_pretrained_weight, "In ReNomIMG version {}, pretained weight of {} is not prepared.".format( __version__, self.__class__.__name__) if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) self.imsize = imsize self.num_class = len(class_map) self.class_map = [c.encode("ascii", "ignore") for c in class_map] self._train_whole_network = train_whole_network self._model = CNN_InceptionV2(self.num_class) self._opt = rm.Sgd(0.045, 0.9) self.decay_rate = 0.0005 if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self._model.load(load_pretrained_weight) layer._model.aux1.params = {} layer._model.aux2.params = {}
def __init__(self, class_map=[], imsize=(224, 224), cardinality=32, plateau=False, load_pretrained_weight=False, train_whole_network=False): if not hasattr(imsize, "__getitem__"): imsize = (imsize, imsize) self.num_class = len(class_map) self.class_map = [c.encode("ascii", "ignore") for c in class_map] self.imsize = imsize self._train_whole_network = train_whole_network self.decay_rate = 0.0001 self.cardinality = cardinality self._model = ResNeXt(self.num_class, Bottleneck, [3, 4, 23, 3], self.cardinality) self._opt = rm.Sgd(0.1, 0.9) # for error plateau self.plateau = plateau self._patience = 15 self._counter = 0 self._min_lr = 1e-6 self._factor = np.sqrt(0.1) if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self._model.load(load_pretrained_weight) self._model.fc.params = {}
def __init__(self, num_class=1000, load_pretrained_weight=False): super(Darknet, self).__init__([ # 1st Block rm.Conv2d(channel=64, filter=7, stride=2, padding=3, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.MaxPool2d(stride=2, filter=2), # 2nd Block rm.Conv2d(channel=192, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.MaxPool2d(stride=2, filter=2), # 3rd Block rm.Conv2d(channel=128, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=256, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=256, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.MaxPool2d(stride=2, filter=2), # 4th Block rm.Conv2d(channel=256, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=256, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=256, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=256, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=1024, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.MaxPool2d(stride=2, filter=2), # 5th Block rm.Conv2d(channel=512, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=1024, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=512, filter=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), rm.Conv2d(channel=1024, filter=3, padding=1, ignore_bias=True), rm.BatchNormalize(mode='feature'), rm.LeakyRelu(slope=0.1), # layers for ImageNet rm.Conv2d(channel=num_class, filter=1), rm.LeakyRelu(slope=0.1), rm.AveragePool2d(filter=7), rm.Softmax() ]) if load_pretrained_weight: if isinstance(load_pretrained_weight, bool): load_pretrained_weight = self.__class__.__name__ + '.h5' if not os.path.exists(load_pretrained_weight): download(self.WEIGHT_URL, load_pretrained_weight) self.load(load_pretrained_weight)
def setup_example(): """ """ print("### Setup a example dataset ###") create_directories() print("1/7: Downloading voc dataset.") voc2012 = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar" if not os.path.exists("voc2012.tar"): download(voc2012, "voc2012.tar") print("2/7: Extracting tar file.") if not os.path.exists("voc"): with tarfile.open("voc2012.tar", "r:*") as tar: tar.extractall('voc') print("3/7: Moving image data to datasrc/img...") voc_img_dir = Path("voc/VOCdevkit/VOC2012/JPEGImages") for image in os.listdir(str(voc_img_dir)): shutil.copy(str(voc_img_dir / image), str(DATASET_IMG_DIR / image)) print("4/7: Moving image data to datasrc/prediction_set/img...") for image in list(os.listdir(str(voc_img_dir)))[::50]: shutil.copy(str(voc_img_dir / image), str(DATASET_PREDICTION_IMG_DIR / image)) # Setting Detection print("5/7: Moving xml data to datasrc/label/detection...") voc_detection_label_dir = Path("voc/VOCdevkit/VOC2012/Annotations") for xml in os.listdir(str(voc_detection_label_dir)): shutil.copy(str(voc_detection_label_dir / xml), str(DATASET_LABEL_DETECTION_DIR / xml)) # Setting Segmentation print( "6/7: Moving segmentation target data to datasrc/label/segmentation..." ) classes = list( sorted([ "aeroplane", "bicycle", "boat", "bus", "car", "motorbike", "train", "person", "bird", "cat", "cow", "dog", "horse", "sheep", "bottle", "chair", "dining_table", "potted_plant", "sofa", "tv/monitor" ])) voc_segmentation_label_dir = Path( "voc/VOCdevkit/VOC2012/SegmentationClass") for xml in os.listdir(str(voc_segmentation_label_dir)): shutil.copy(str(voc_segmentation_label_dir / xml), str(DATASET_LABEL_SEGMENTATION_DIR / xml)) with open(str(DATASET_LABEL_SEGMENTATION_DIR / "class_map.txt"), "w") as writer: for i, c in enumerate(["background"] + classes): writer.write("{} {}\n".format(c, i)) # Setting Classification print( "7/7: Creating classification target data to datasrc/label/classification..." ) with open(str(DATASET_LABEL_CLASSIFICATION_DIR / "target.txt"), "w") as writer: for path in sorted(voc_detection_label_dir.iterdir()): tree = ElementTree.parse(str(path)) root = tree.getroot() obj = list(root.findall('object'))[0] class_name = obj.find('name').text.strip() writer.write("{} {}\n".format( path.with_suffix(".jpg").name, class_name)) print("Setup done.")
def fetch_detection_dataset_voc_2007(split_validation=True): """ Args: split_validation (boolean): Whether or not split validation data. Returns: (list): This ret1urns list of image path. (list): This returns list of annotations. Each annotation has a list of dictionary which includes keys 'box' and 'name'. The structure is bellow. [ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'name': class_name(string), 'size': (x, y)(list of float), 'class': id(int)}, {'box': [x(float), y, w, h], 'name': class_name(string), 'size': (x, y)(list of float), 'class': id(int)}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'name': class_name(string), 'size': (x, y)(list of float), 'class': id(int)}, {'box': [x(float), y, w, h], 'name': class_name(string), 'size': (x, y)(list of float), 'class': id(int)}, ... ] ], """ voc_2007_url = "http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar" voc_2007_tar = "VOCtrainval_06-Nov-2007.tar" image_voc_2007 = "VOCdevkit/VOC2007/JPEGImages/" label_voc_2007 = "VOCdevkit/VOC2007/Annotations/" if not os.path.exists("VOCdevkit/VOC2007"): if not os.path.exists(voc_2007_tar): download(voc_2007_url) with tarfile.open(voc_2007_tar) as tar: tar.extractall() train_voc_2007 = [ line.strip() for line in open( "VOCdevkit/VOC2007/ImageSets/Main/train.txt").readlines() ] valid_voc_2007 = [ line.strip() for line in open( "VOCdevkit/VOC2007/ImageSets/Main/val.txt").readlines() ] train_image_path_list = [] train_label_path_list = [] valid_image_path_list = [] valid_label_path_list = [] # Use training dataset of VOC2007 as training data. for path in train_voc_2007: train_image_path_list.append( os.path.join(image_voc_2007, path + '.jpg')) train_label_path_list.append( os.path.join(label_voc_2007, path + '.xml')) # Use validation dataset of VOC2007 as validation data. for path in valid_voc_2007: valid_image_path_list.append( os.path.join(image_voc_2007, path + '.jpg')) valid_label_path_list.append( os.path.join(label_voc_2007, path + '.xml')) if split_validation == True: train_annotation_list, _ = parse_xml_detection(train_label_path_list) valid_annotation_list, _ = parse_xml_detection(valid_label_path_list) return train_annotation_list, train_image_path_list, valid_annotation_list, valid_image_path_list else: train_label_path_list.extend(valid_label_path_list) label_path_list = train_label_path_list train_image_path_list.extend(valid_image_path_list) image_path_list = train_image_path_list annotation_list, _ = parse_xml_detection(label_path_list) return annotation_list, image_path_list
def fetch_detection_dataset_pets(split_validation=True, test_size=0.2): """ Args: split_validation (boolean): Whether or not split validation data. test_size(float): propotion of the test dataset in total datasets. Returns: (list): This ret1urns list of image path. (list): This returns list of annotations. Each annotation has a list of dictionary which includes keys 'box' and 'name'. The structure is bellow. [ [ # Objects of 1st image. {'box': [x(float), y, w, h], 'name': class_name(string), 'size': (x, y)(list of float), 'class': id(int)}, {'box': [x(float), y, w, h], 'name': class_name(string), 'size': (x, y)(list of float), 'class': id(int)}, ... ], [ # Objects of 2nd image. {'box': [x(float), y, w, h], 'name': class_name(string), 'size': image_size(float), 'class': id(int)}, {'box': [x(float), y, w, h], 'name': class_name(string), 'size': image_size(float), 'class': id(int)}, ... ] ] """ pets_image_url = "http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz" pets_label_url = "http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz" pets_image_tar = "images.tar.gz" pets_label_tar = "annotations.tar.gz" setting = { "image": [pets_image_url, pets_image_tar], "label": [pets_label_url, pets_label_tar] } for url, path in setting.values(): if not os.path.exists(path): download(url) with tarfile.open(path) as tar: tar.extractall(path="pets") def exist_xml_list(path, xml_name_list): name, _ = os.path.splitext(path) return name in xml_name_list xml_list = os.listdir("pets/annotations/xmls") xml_name_list = [os.path.splitext(name)[0] for name in xml_list] image_path_list = os.listdir("pets/images") image_name_list = [os.path.splitext(name)[0] for name in image_path_list] name_list = list(set(image_name_list) & set(xml_name_list)) xml_list = [ os.path.join("pets/annotations/xmls", name + ".xml") for name in name_list ] annotation_list, _ = parse_xml_detection(xml_list) image_path_list = [ os.path.join("pets/images", name + ".jpg") for name in name_list ] if split_validation == False: return image_path_list, annotation_list else: image_path_list, annotation_list = np.array(image_path_list), np.array( annotation_list) indices = np.random.permutation(image_path_list.shape[0] - 1) threshold = int(np.round(test_size * image_path_list.shape[0])) train_index, test_index = indices[threshold:], indices[:threshold] train_annotation_list, valid_annotation_list = annotation_list[ train_index], annotation_list[test_index] train_image_path_list, valid_image_path_list = image_path_list[ train_index], image_path_list[test_index] return list(train_image_path_list), list(train_annotation_list), list( valid_image_path_list), list(valid_annotation_list)