def feeder_factory(file_list, reader_type, feeder_type="npyfile", stereo=False): if feeder_type == "npyfile": if stereo and reader_type != "extrinsic": FeederClass = NpyFileFeederStereoLeft else: FeederClass = NpyFileFeeder else: raise WrongInputException("Wrong feeder type: " + feeder_type) if reader_type == "image": reader = ImageReaderStereo() if stereo else ImageReader() elif reader_type == "intrinsic": reader = NpyTxtReaderStereo() if stereo else NpyTxtReader() elif reader_type == "depth": reader = DepthReaderStereo() if stereo else DepthReader() elif reader_type == "pose": reader = PoseReaderStereo() if stereo else PoseReader() elif reader_type == "extrinsic": reader = NpyTxtReader() else: raise WrongInputException("Wrong reader type: " + reader_type) feeder = FeederClass(file_list, reader) return feeder
def optimizer_factory(opt_name, basic_lr, epoch=0): if opt_name == "adam_constant": return tf.optimizers.Adam(learning_rate=basic_lr) if opt_name == "sgd_constant": return tf.optimizers.SGD(learning_rate=basic_lr) else: raise WrongInputException(f"{opt_name} is NOT an available model name")
def list_drive_paths(self): split_path = op.join(self.base_path, self.left_img_dir + self.dir_suffix, self.split) if not op.isdir(split_path): raise WrongInputException( "[list_sequence_paths] path does NOT exist:" + split_path) city_names = os.listdir(split_path) city_names = [ city for city in city_names if op.isdir(op.join(split_path, city)) ] total_sequences = [] for city in city_names: pattern = op.join(split_path, city, "*.png") files = glob(pattern) seq_numbers = [file.split("_")[-3] for file in files] seq_numbers = list(set(seq_numbers)) seq_numbers.sort() seq_paths = [ op.join(self.split, city, f"{city}_{seq}") for seq in seq_numbers ] total_sequences.extend(seq_paths) # total_sequences: list of ["city/city_seqind"] print("[list_drive_paths]", total_sequences) return total_sequences
def flow_net_factory(self, net_name, conv2d_f): if net_name == "PWCNet": flownet = PWCNet(self.input_shape, conv2d_f)() else: raise WrongInputException( "[flow_net_factory] wrong flow net name: " + net_name) return flownet
def pose_net_factory(self, net_name, conv2d_p): if net_name == "PoseNet": posenet = PoseNet(self.input_shape, conv2d_p)() else: raise WrongInputException( "[pose_net_factory] wrong pose net name: " + net_name) return posenet
def __init__(self, method): if method == "L1": self.photometric_loss = photometric_loss_l1 elif method == "SSIM": self.photometric_loss = photometric_loss_ssim else: raise WrongInputException("Wrong photometric loss name: " + method)
def activation_factory(self, activ_name): if activ_name == "InverseSigmoid": return InverseSigmoidActivation() elif activ_name == "Exponential": return ExponentialActivation() else: WrongInputException( "[activation_factory] wrong activation name: " + activ_name)
def set_type_and_shape(self, value): if isinstance(value, np.ndarray): if value.dtype == np.uint8: self.decode_type = "tf.uint8" elif value.dtype == np.float32: self.decode_type = "tf.float32" else: raise WrongInputException( f"[FeederBase] Wrong numpy type: {value.dtype}") self.parse_type = "tf.string" self.shape = list(value.shape) elif isinstance(value, int): self.parse_type = "tf.int64" self.shape = None else: raise WrongInputException( f"[FeederBase] Wrong type: {type(value)}")
def depth_net_factory(self, net_name, conv2d_d, pred_activ, upsample_interp): if net_name == "DepthNetBasic": depth_net = DepthNetBasic(self.input_shape, conv2d_d, pred_activ, upsample_interp)() elif net_name == "DepthNetNoResize": depth_net = DepthNetNoResize(self.input_shape, conv2d_d, pred_activ, upsample_interp)() elif net_name in PRETRAINED_MODELS: depth_net = DepthNetFromPretrained(self.input_shape, conv2d_d, pred_activ, upsample_interp, net_name, self.pretrained_weight)() else: raise WrongInputException( "[depth_net_factory] wrong depth net name: " + net_name) return depth_net
def dataset_reader_factory(raw_data_path, dataset, split, stereo=opts.STEREO): if dataset == "kitti_raw" and split == "train": data_reader = kr.KittiRawTrainReader(raw_data_path, stereo) elif dataset == "kitti_raw" and split == "test": data_reader = kr.KittiRawTestReader(raw_data_path, stereo) elif dataset == "kitti_odom" and split == "train": data_reader = kr.KittiOdomTrainReader(raw_data_path, stereo) elif dataset == "kitti_odom" and split == "test": data_reader = kr.KittiOdomTestReader(raw_data_path, stereo) elif dataset == "cityscapes": data_reader = cr.CityScapesReader(raw_data_path, stereo, split) elif dataset == "cityscapes_seq": data_reader = cr.CityScapesReader(raw_data_path, stereo, split, "_sequence") else: raise WrongInputException( f"Wrong dataset and split: {dataset}, {split}") return data_reader
def encode(self, input_image): """ :param input_image: (batch, height, width, channel) """ input_shape = input_image.get_shape() height = input_shape[1] net_name = self.net_name weights = "imagenet" if self.pretrained_weight else None jsonfile = op.join(opts.PROJECT_ROOT, "model", "build_model", "scaled_layers.json") output_layers = self.read_output_layers(jsonfile) out_layer_names = output_layers[net_name] if net_name == "MobileNetV2": from tensorflow.keras.applications.mobilenet_v2 import preprocess_input pproc_img = layers.Lambda(lambda x: preprocess_input(x), name="preprocess_mobilenet")(input_image) ptmodel = tfapp.MobileNetV2(input_shape=input_shape, include_top=False, weights=weights) elif net_name == "NASNetMobile": from tensorflow.keras.applications.nasnet import preprocess_input assert height == 128 def preprocess_layer(x): x = preprocess_input(x) x = tf.image.resize(x, size=NASNET_SHAPE[:2], method="bilinear") return x pproc_img = layers.Lambda(lambda x: preprocess_layer(x), name="preprocess_nasnet")(input_image) ptmodel = tfapp.NASNetMobile(input_shape=NASNET_SHAPE, include_top=False, weights=weights) elif net_name == "DenseNet121": from tensorflow.keras.applications.densenet import preprocess_input pproc_img = layers.Lambda(lambda x: preprocess_input(x), name="preprocess_densenet")(input_image) ptmodel = tfapp.DenseNet121(input_shape=input_shape, include_top=False, weights=weights) elif net_name == "VGG16": from tensorflow.keras.applications.vgg16 import preprocess_input pproc_img = layers.Lambda(lambda x: preprocess_input(x), name="preprocess_vgg16")(input_image) ptmodel = tfapp.VGG16(input_shape=input_shape, include_top=False, weights=weights) elif net_name == "Xception": from tensorflow.keras.applications.xception import preprocess_input assert height == 128 def preprocess_layer(x): x = preprocess_input(x) x = tf.image.resize(x, size=XCEPTION_SHAPE[:2], method="bilinear") return x pproc_img = layers.Lambda(lambda x: preprocess_layer(x), name="preprocess_xception")(input_image) ptmodel = tfapp.Xception(input_shape=XCEPTION_SHAPE, include_top=False, weights=weights) elif net_name == "ResNet50V2": from tensorflow.keras.applications.resnet import preprocess_input pproc_img = layers.Lambda(lambda x: preprocess_input(x), name="preprocess_resnet")(input_image) ptmodel = tfapp.ResNet50V2(input_shape=input_shape, include_top=False, weights=weights) elif net_name == "NASNetLarge": from tensorflow.keras.applications.nasnet import preprocess_input assert height == 128 def preprocess_layer(x): x = preprocess_input(x) x = tf.image.resize(x, size=NASNET_SHAPE[:2], method="bilinear") return x pproc_img = layers.Lambda(lambda x: preprocess_layer(x), name="preprocess_nasnet")(input_image) ptmodel = tfapp.NASNetLarge(input_shape=NASNET_SHAPE, include_top=False, weights=weights) else: raise WrongInputException("Wrong pretrained model name: " + net_name) # collect multi scale convolutional features layer_outs = [] for layer_name in out_layer_names: layer = ptmodel.get_layer(name=layer_name[1], index=layer_name[0]) # print("extract feature layers:", layer.name, layer.get_input_shape_at(0), layer.get_output_shape_at(0)) layer_outs.append(layer.output) # create model with multi scale features multi_scale_model = tf.keras.Model(ptmodel.input, layer_outs, name=net_name + "_base") features_ms = multi_scale_model(pproc_img) return features_ms