Example #1
0
def build_model(args: argparse.Namespace, weights_path: str) -> Model:
    K.clear_session()
    model = ssd_300(image_size=(args.img_height, args.img_width,
                                args.img_channels),
                    n_classes=args.n_classes,
                    mode='training',
                    l2_regularization=0.0005,
                    scales=args.scales,
                    aspect_ratios_per_layer=args.aspect_ratios,
                    two_boxes_for_ar1=args.two_boxes_for_ar1,
                    steps=args.steps,
                    offsets=args.offsets,
                    clip_boxes=args.clip_boxes,
                    variances=args.variances,
                    normalize_coords=args.normalize_coords,
                    subtract_mean=args.mean_color,
                    swap_channels=args.swap_channels)

    model.load_weights(weights_path, by_name=True)

    sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=sgd, loss=ssd_loss.compute_loss)

    return model
    def __init__(self):
        self.reseted = None
        self.vis_obs = Pose()
        self.bridge = CvBridge()
        self.dunet_obs_pub = rospy.Publisher("/dunet/obs", Pose, queue_size=10)
        self.dunet_img_pub = rospy.Publisher("/dunet/image_raw",
                                             Image,
                                             queue_size=10)
        self.img_height = 320
        self.img_width = 320
        self.frame = None
        self.model = dronenet(
            image_size=(self.img_height, self.img_width, 3),
            n_classes=20,
            mode='inference',
            l2_regularization=0.0005,
            scales=[0.05, 0.15, 0.3, 0.6, 0.8],
            aspect_ratios_per_layer=[[1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]],
            two_boxes_for_ar1=True,
            steps=None,
            offsets=[0.5, 0.5, 0.5, 0.5],
            clip_boxes=True,
            variances=[0.1, 0.1, 0.2, 0.2],
            normalize_coords=True,
            subtract_mean=[123, 117, 104],
            swap_channels=[2, 1, 0],
            confidence_thresh=0.01,  # check
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400)

        # Set the weights path
        rospack = rospkg.RosPack()
        pkg_path = rospack.get_path('reinforced_selfiestickdrone')
        weights_path = pkg_path + '/script/dunet/weights/dunet_pascal_epoch-75_loss-2.9597_val_loss-3.1265.h5'

        self.model.load_weights(weights_path, by_name=True)

        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)
        sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

        self.model.compile(optimizer=sgd, loss=ssd_loss.compute_loss)

        self.graph = tf.get_default_graph()
        self.color = list(np.random.choice(range(0, 256, 50), size=3))

        self.classes = [
            'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
            'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
            'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
            'tvmonitor'
        ]
Example #3
0
def num_detection(in_img):
    model_path = 'num_ssd.h5'
    # We need to create an SSDLoss object in order to pass that to the model loader.
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

    K.clear_session()  # Clear previous models from memory.

    model = load_model(model_path, custom_objects={'AnchorBoxes': AnchorBoxes,
                                                   'compute_loss': ssd_loss.compute_loss})

    img_height = 192
    img_width = 192

    test_img,test_single_channel = ssd2_resize(in_img,img_height,img_width)
    input_images=[]
    input_images.append(test_img)
    input_images = np.array(input_images)

    y_pred = model.predict(input_images)
    # 4: Decode the raw predictions in `y_pred`.
    confidence_threshold = 0.5

    # y_pred_thresh = [y_pred[k][y_pred[k, :, 1] > confidence_threshold] for k in range(y_pred.shape[0])]

    y_pred_decoded = decode_detections(y_pred,
                                       confidence_thresh=0.5,
                                       iou_threshold=0.1,
                                       top_k=200,
                                       normalize_coords=True,
                                       img_height=img_height,
                                       img_width=img_width)

    np.set_printoptions(precision=2, suppress=True, linewidth=90)
    print("Predicted boxes:\n")
    print('   class   conf xmin   ymin   xmax   ymax')
    print(y_pred_decoded[0])


    new_list = []
    t_img = test_img.copy()
    for box in y_pred_decoded[0]:
        if float(box[1])>0.7:
            i_class = int(box[0])-1 if int(box[0])!=11 else '-'
            xmin = int(box[2])
            ymin = int(box[3])
            xmax = int(box[4] )
            ymax = int(box[5]  )
            cv2.rectangle(t_img, (xmin, ymin), (xmax, ymax), (0,0,255), 1)
            new_list.append([i_class,box[1],xmin,ymin,xmax,ymax])
            # mask_sets.append([xmin,ymin,xmax,ymax])
            # cv2.rectangle(input_images[0], (xmin, ymin), (xmax, ymax), 255, 2)

    input_images[0] = cv2.cvtColor(input_images[0], cv2.COLOR_RGB2BGR)

    # imwrite(dir_path + '/0_black_img.jpg', black_img)
    # mask = cv2.dilate(mask, kernel4)
    sort_arr =  parse_box(new_list)
    imwrite(dir_path+'/0_ssd_detect.jpg', t_img)

    return sort_arr,test_img,test_single_channel
Example #4
0
 def __init__(self):
     self.img_height = 270  # Height of the input images
     self.img_width = 270  # Width of the input images
     self.img_channels = 3  # Number of color channels of the input images
     self.intensity_mean = 127.5  # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`.
     self.intensity_range = 127.5  # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`.
     self.n_classes = 12  # Number of positive classes
     self.scales = [
         0.1, 0.16, 0.32, 0.64, 0.96
     ]  # An explicit list of anchor box scaling factors. If this is passed, it will override `min_scale` and `max_scale`.
     self.aspect_ratios = [
         0.5, 1.0, 1.5
     ]  # The list of aspect ratios for the anchor boxes
     self.two_boxes_for_ar1 = False  # Whether or not you want to generate two anchor boxes for aspect ratio 1
     self.steps = None  # In case you'd like to set the step sizes for the anchor box grids manually; not recommended
     self.offsets = None  # In case you'd like to set the offsets for the anchor box grids manually; not recommended
     self.clip_boxes = False  # Whether or not to clip the anchor boxes to lie entirely within the image boundaries
     self.variances = [
         1.0, 1.0, 1.0, 1.0
     ]  # The list of variances by which the encoded target coordinates are scaled
     self.normalize_coords = True  # Whether or not the model is supposed to use coordinates relative to the image size
     K.clear_session()  #清除原有模型
     self.model_path = '0821.h5'
     self.ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
     self.model = load_model(self.model_path,
                             custom_objects={
                                 'AnchorBoxes': AnchorBoxes,
                                 'compute_loss': self.ssd_loss.compute_loss
                             })
     self.objtuple = []
Example #5
0
    def build_ssd7_model(self):
        intensity_mean = 127.5  # 像素减去
        intensity_range = 127.5  # 像素除以

        K.clear_session()  # Clear previous models from memory.

        model = build_model(image_size=(self.img_height, self.img_width,
                                        self.img_channels),
                            n_classes=self.n_classes,
                            mode='training',
                            l2_regularization=0.0005,
                            scales=self.scales,
                            aspect_ratios_global=self.aspect_ratios,
                            aspect_ratios_per_layer=None,
                            variances=self.variances,
                            normalize_coords=self.normalize_coords,
                            subtract_mean=intensity_mean,
                            divide_by_stddev=intensity_range)

        # model.load_weights('./ssd7_weights.h5', by_name=True)  # 迁移学习,微调参数

        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)

        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

        model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

        return model
Example #6
0
def get_model(weights_path):
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    convert_to_3_channels = ConvertTo3Channels()
    resize = Resize(height=img_height, width=img_width)

    K.clear_session()
    model = ssd_300(image_size=(img_height, img_width, img_channels),
                    n_classes=n_classes,
                    mode='training',
                    l2_regularization=0.0005,
                    scales=scales,
                    aspect_ratios_per_layer=aspect_ratios,
                    two_boxes_for_ar1=two_boxes_for_ar1,
                    steps=steps,
                    offsets=offsets,
                    clip_boxes=clip_boxes,
                    variances=variances,
                    normalize_coords=normalize_coords,
                    subtract_mean=mean_color,
                    swap_channels=swap_channels)

    # 2: Load some weights into the model.
    model.load_weights(weights_path, by_name=True)

    sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)

    model.compile(optimizer=sgd, loss=ssd_loss.compute_loss)

    return model
Example #7
0
def build_ssd7_wider_face(weights_path=WEIGHTS_PATH):
    # 0. Clear the previous models from memory
    K.clear_session()

    # 1. Build the model
    model = build_model(image_size=(img_height, img_width, img_channels),
                        n_classes=n_classes,
                        mode='training',
                        l2_regularization=0.0005,
                        scales=scales,
                        aspect_ratios_global=aspect_ratios,
                        aspect_ratios_per_layer=None,
                        two_boxes_for_ar1=two_boxes_for_ar1,
                        steps=steps,
                        offsets=offsets,
                        clip_boxes=clip_boxes,
                        variances=variances,
                        normalize_coords=normalize_coords,
                        subtract_mean=intensity_mean,
                        divide_by_stddev=intensity_range)

    # 2. Load weights in case of pre-trained
    if weights_path is not None:
        model.load_weights(weights_path)

    # 3. Compile the model
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    #4. Return the model
    return model
Example #8
0
    def __init__(self, net_model):
        SystemExit('Keras not supported yet (for now...)')
        self.framework = "Keras"
        # Parse the dataset to get which labels to yield
        labels_file = LABELS_DICT[net_model['Dataset'].lower()]
        label_map = label_map_util.load_labelmap(
            labels_file)  # loads the labels map.
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=100000)
        category_index = label_map_util.create_category_index(categories)
        self.classes = {}
        # We build is as a dict because of gaps on the labels definitions
        for cat in category_index:
            self.classes[cat] = str(category_index[cat]['name'])

        MODEL_FILE = 'Net/Keras/' + net_model['Model']

        file = h5py.File(MODEL_FILE, 'r')

        ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)

        K.clear_session()

        if str(file.items()[0][0]) == 'model_weights':
            print("Full model detected. Loading it...")
            try:
                self.model = load_model(MODEL_FILE,
                                        custom_objects={
                                            'AnchorBoxes': AnchorBoxes,
                                            'L2Normalization': L2Normalization,
                                            'DecodeDetections':
                                            DecodeDetections,
                                            'compute_loss':
                                            ssd_loss.compute_loss
                                        })
            except Exception as e:
                SystemExit(e)
        else:
            print(
                "Weights file detected. Creating a model and loading the weights into it..."
            )
            print "Model file: ", MODEL_FILE
            self.model = create_model_from_weights.create_model(
                MODEL_FILE, ssd_loss, len(self.classes))

        # the Keras network works on 300x300 images. Reference sizes:
        input_size = self.model.input.shape.as_list()
        self.img_height = input_size[1]
        self.img_width = input_size[2]

        # Output preallocation
        self.predictions = np.asarray([])
        self.boxes = np.asarray([])
        self.scores = np.asarray([])

        dummy = np.zeros([1, self.img_height, self.img_width, 3])
        self.model.predict(dummy)

        print("Network ready!")
Example #9
0
    def train(self, epochs, optimizer, callbacks=None):
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
        self.model.compile(optimizer=optimizer, loss=ssd_loss.compute_loss)

        self.model.fit_generator(generator=self.generator,
                                 steps_per_epoch=self.steps_per_epoch,
                                 epochs=epochs,
                                 callbacks=callbacks,
                                 validation_data=self.validation_data,
                                 validation_steps=self.validation_steps)
Example #10
0
    def __init__(self):
        self.reseted = None
        self.vis_obs = Pose()
        self.bridge = CvBridge()
        self.img_height = 320
        self.img_width = 320
        self.frame = None
        self.model = architecture(
            image_size=(self.img_height, self.img_width, 3),
            n_classes=11,
            mode='inference',
            l2_regularization=0.0005,
            scales=[0.05, 0.15, 0.3, 0.6, 0.8],
            aspect_ratios_per_layer=[[1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]],
            two_boxes_for_ar1=True,
            steps=None,
            offsets=[0.5, 0.5, 0.5, 0.5],
            clip_boxes=True,
            variances=[0.1, 0.1, 0.2, 0.2],
            normalize_coords=True,
            subtract_mean=[123, 117, 104],
            swap_channels=[2, 1, 0],
            confidence_thresh=0.01,  # check
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400)

        # Set the weights path
        rospack = rospkg.RosPack()
        pkg_path = rospack.get_path('reinforced_selfiestickdrone')
        weights_path = pkg_path + '/script/face_human_detection_tracking_agent/dunet/weights/dunet_epoch-50_loss-2.4019_val_loss-2.6379.h5'

        self.model.load_weights(weights_path, by_name=True)

        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)
        sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

        self.model.compile(optimizer=sgd, loss=ssd_loss.compute_loss)

        self.graph = tf.get_default_graph()
        self.color = list(np.random.choice(range(0, 256, 50), size=3))

        self.classes = [
            'background', 'Christmas toy', 'coffee machine', 'potted plant',
            'tissue box', 'robot', 'soccer ball', 'turtle bot', 'uav',
            'fire alarm', 'tennis racket', 'person'
        ]
Example #11
0
def get_head_model():
    """A method for loading the Head Detection model from AVAuco/ssd_head_keras github repo

    Returns:
        tf modle: Head Detection model
    """
    # Set the image size.
    img_height = 512
    img_width = 512
    # Set the model's inference mode
    model_mode = 'inference'


    # Set the desired confidence threshold
    conf_thresh = 0.01

    # 1: Build the Keras model
    K.clear_session() # Clear previous models from memory.
    model = ssd_512(image_size=(img_height, img_width, 3),
                    n_classes=1,
                    mode=model_mode,
                    l2_regularization=0.0005,
                    scales=[0.07, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1.05], # PASCAL VOC
                    aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                            [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                            [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                            [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                            [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                            [1.0, 2.0, 0.5],
                                            [1.0, 2.0, 0.5]],
                two_boxes_for_ar1=True,
                steps=[8, 16, 32, 64, 128, 256, 512],
                offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
                clip_boxes=False,
                variances=[0.1, 0.1, 0.2, 0.2],
                normalize_coords=True,
                subtract_mean=[123, 117, 104],
                swap_channels=[2, 1, 0],
                confidence_thresh=conf_thresh,
                iou_threshold=0.45,
                top_k=200,
                nms_max_output_size=400)

    # 2: Load the trained weights into the model.
    weights_path = './detection_models/head_detection_ssd512-hollywood-trainval.h5'
    model.load_weights(weights_path, by_name=True)

    # 3: Compile the model so that Keras won't complain the next time you load it.
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    # model.save('./detection_models/head_detection')
    return model
Example #12
0
def load_model(input_model_path, input_json_path):
    if not Path(input_model_path).exists():
        raise FileNotFoundError(
            'Model file `{}` does not exist.'.format(input_model_path))
    try:
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
        with CustomObjectScope({
                'relu6':
                keras.applications.mobilenet.relu6,
                'DepthwiseConv2D':
                keras.applications.mobilenet.DepthwiseConv2D
        }):
            model = keras.models.load_model(input_model_path,
                                            custom_objects={
                                                'AnchorBoxes':
                                                AnchorBoxes,
                                                'L2Normalization':
                                                L2Normalization,
                                                'DecodeDetections':
                                                DecodeDetections,
                                                'compute_loss':
                                                ssd_loss.compute_loss
                                            })
            return model
    except FileNotFoundError as err:
        logging.error('Input mode file (%s) does not exist.',
                      FLAGS.input_model)
        raise err
    except ValueError as wrong_file_err:
        if input_json_path:
            if not Path(input_json_path).exists():
                raise FileNotFoundError(
                    'Model description json file `{}` does not exist.'.format(
                        input_json_path))
            try:
                model = model_from_json(open(str(input_json_path)).read())
                model.load_weights(input_model_path)
                return model
            except Exception as err:
                logging.error("Couldn't load model from json.")
                raise err
        else:
            logging.error(
                'Input file specified only holds the weights, and not '
                'the model definition. Save the model using '
                'model.save(filename.h5) which will contain the network '
                'architecture as well as its weights. If the model is '
                'saved using model.save_weights(filename), the flag '
                'input_model_json should also be set to the '
                'architecture which is exported separately in a '
                'json format. Check the keras documentation for more details '
                '(https://keras.io/getting-started/faq/)')
            raise wrong_file_err
    def __init__(self):
        self.img_height = 320
        self.img_width = 320
        self.frame = None
        self.model = dronenet(
            image_size=(self.img_height, self.img_width, 3),
            n_classes=10,
            mode='inference',
            l2_regularization=0.0005,
            #scales=[0.035, 0.07, 0.15, 0.33, 0.62], # The scales for MS COCO are [0.07, 0.15, 0.33, 0.51, 0.69, 0.87, 1.05]
            #scales=[0.05, 0.2, 0.4, 0.6, 0.8],
            scales=[0.035, 0.075, 0.22, 0.45, 0.75],  # 128
            aspect_ratios_per_layer=[[1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]],
            two_boxes_for_ar1=True,
            steps=[4, 8, 16, 32],
            offsets=[0.5, 0.5, 0.5, 0.5],
            clip_boxes=True,
            variances=[0.1, 0.1, 0.2, 0.2],
            normalize_coords=True,
            subtract_mean=[123, 117, 104],
            swap_channels=[2, 1, 0],
            confidence_thresh=0.5,  # check
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400)

        #weights_path = os.path.join('/home/saif/Documents/datasets/droneSet/weights/myTrain/dronenet_fpn_scale/' + 'dronenet_epoch-160_loss-3.2399_val_loss-3.0114.h5')
        weights_path = os.path.join(
            '/home/saif/Documents/datasets/droneSet/weights/myTrain/dronenet_fpn_256_original_augment/'
            + 'dronenet_epoch-185_loss-3.7147_val_loss-3.5914.h5')
        self.model.load_weights(weights_path, by_name=True)

        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)
        sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

        self.model.compile(optimizer=sgd, loss=ssd_loss.compute_loss)

        self.graph = tf.get_default_graph()
        self.color = list(np.random.choice(range(0, 256, 50), size=3))

        self.classes = [
            'background', 'Christmas toy', 'coffee machine', 'potted plant',
            'tissue box', 'robot', 'soccer ball', 'turtle bot', 'uav',
            'fire alarm', 'tennis racket'
        ]
Example #14
0
    def get_model(self,
                  mode='inference',
                  weights_path='',
                  n_classes='',
                  id2digit=''):
        #
        # n_classes, id2digit: for inference
        config = self.config
        if n_classes:  # inference setting
            self.n_classes = n_classes

        if id2digit:
            self.id2digit = id2digit

        self.model = ssd_300(
            image_size=(config.img_height, config.img_width,
                        config.img_channels),
            n_classes=self.n_classes,
            mode=mode,
            l2_regularization=0.0005,
            scales=config.scales,
            aspect_ratios_per_layer=config.aspect_ratios,
            two_boxes_for_ar1=config.two_boxes_for_ar1,
            steps=config.steps,
            offsets=config.offsets,
            clip_boxes=config.clip_boxes,
            variances=config.variances,
            normalize_coords=config.normalize_coords,
            subtract_mean=config.subtract_mean,  #
            divide_by_stddev=None,  #
            swap_channels=config.swap_channels,
            confidence_thresh=0.5,  #
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400,
            return_predictor_sizes=False)

        if weights_path:
            print(f'Loading weights from {weights_path}')
            self.model.load_weights(weights_path, by_name=True)
            self.weights_path = weights_path

        #adam = Adam(lr=0.005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        #sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)
        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)

        ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)

        self.model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
Example #15
0
    def __init__(self, confidence_threshold=0.5):

        self.confidence_th = confidence_threshold

        # 0: Set the image size.
        img_height = 300
        img_width = 300

        # 1: Build the Keras model
        self.loaded_model = ssd_300(
            image_size=(img_height, img_width, 3),
            n_classes=20,
            mode='inference',
            l2_regularization=0.0005,
            scales=[
                0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 1.05
            ],  # The scales for MS COCO are [0.07, 0.15, 0.33, 0.51, 0.69, 0.87, 1.05]
            aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]],
            two_boxes_for_ar1=True,
            steps=[8, 16, 32, 64, 100, 300],
            offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
            clip_boxes=False,
            variances=[0.1, 0.1, 0.2, 0.2],
            normalize_coords=True,
            subtract_mean=[123, 117, 104],
            swap_channels=[2, 1, 0],
            confidence_thresh=0.5,
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400)

        # 2: Load the trained weights into the model.
        weights_path = 'models/VGG_VOC0712_SSD_300x300_iter_240000.h5'
        self.loaded_model.load_weights(weights_path, by_name=True)

        # 3: Compile the model so that Keras won't complain the next time you load it.
        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
        self.loaded_model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

        # 4: make prediction (graph)
        self.loaded_model._make_predict_function()
Example #16
0
 def __init__(self,name,weightsPath):
     self.name = name
     self.ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
     self.weightsPath = weightsPath
     self.model = load_model(
         self.weightsPath,
         custom_objects={
             'AnchorBoxes': AnchorBoxes,
             'compute_loss': self.ssd_loss.compute_loss
         }
     )
     #warmup
     print("WARMING UP .." + self.name)
     self.model._make_predict_function()
Example #17
0
    def build_model_300(self):
        # 1: Build the Keras model

        K.clear_session()  # Clear previous models from memory.

        self.model = ssd_300(
            image_size=(self.img_height, self.img_width, 3),
            n_classes=20,
            mode='inference',
            l2_regularization=0.0005,
            scales=[0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 1.05],
            # The scales for MS COCO are [0.07, 0.15, 0.33, 0.51, 0.69, 0.87, 1.05]
            aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                     [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]],
            two_boxes_for_ar1=True,
            steps=[8, 16, 32, 64, 100, 300],
            offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
            clip_boxes=False,
            variances=[0.1, 0.1, 0.2, 0.2],
            normalize_coords=True,
            subtract_mean=[123, 117, 104],
            swap_channels=[2, 1, 0],
            confidence_thresh=0.5,
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400)

        # 2: Load the trained weights into the model.

        # TODO: Set the path of the trained weights.
        weights_path = self.weights_path

        self.model.load_weights(weights_path, by_name=True)

        # 3: Compile the model so that Keras won't complain the next time you load it.

        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)

        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

        self.model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
Example #18
0
    def __init__(self, weights_path='VGG_VOC0712_SSD_512x512_iter_120000.h5'): # assume weights are in the master folder.

        K.clear_session()  # Clear previous models from memory.

        # load parameters
        self.model = ssd_512(image_size=(512, 512, 3), # 512x512
                        n_classes=20,
                        mode='inference',
                        l2_regularization=0.0005,
                        scales=[0.07, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1.05],
                        # The scales for MS COCO are [0.04, 0.1, 0.26, 0.42, 0.58, 0.74, 0.9, 1.06]
                        aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                                 [1.0, 2.0, 0.5],
                                                 [1.0, 2.0, 0.5]],
                        two_boxes_for_ar1=True,
                        steps=[8, 16, 32, 64, 128, 256, 512],
                        offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
                        clip_boxes=False,
                        variances=[0.1, 0.1, 0.2, 0.2],
                        normalize_coords=True,
                        subtract_mean=[123, 117, 104],
                        swap_channels=[2, 1, 0],
                        confidence_thresh=0.5,
                        iou_threshold=0.45,
                        top_k=200,
                        nms_max_output_size=400)

        # load weights
        self.model.load_weights(weights_path, by_name=True)
        #self.model.load_weights(weights_path)

        # compile model
        adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
        self.model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

        # classses (for reference)
        self.classes = ['background',
           'aeroplane', 'bicycle', 'bird', 'boat',
           'bottle', 'bus', 'car', 'cat',
           'chair', 'cow', 'diningtable', 'dog',
           'horse', 'motorbike', 'person', 'pottedplant',
           'sheep', 'sofa', 'train', 'tvmonitor']
def model_load(model_path):
    model_path = model_path

    # We need to create an SSDLoss object in order to pass that to the model
    #  loader.
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=0.8)

    K.clear_session()  # Clear previous models from memory.

    model = load_model(os.path.join(model_path, 'ssd300_all.h5'),
                       custom_objects={'AnchorBoxes': AnchorBoxes,
                                       'L2Normalization': L2Normalization,
                                       'compute_loss': ssd_loss.compute_loss})

    model2 = unet()
    model2.load_weights(os.path.join(model_path, "unet_8.hdf5"))
    return model, model2
Example #20
0
def init_model(weights_path='./ssdweights/rovio_v2.h5'):
    img_height = 300
    img_width = 300

    dirname = os.path.dirname(os.path.abspath(__file__))

    assert os.path.exists(weights_path), '%s not found...' % dirname

    K.clear_session()  # to clear all memory in the RAM

    model = ssd_300(image_size=(img_height, img_width, 3),
                    n_classes=2,
                    mode='inference_fast',
                    l2_regularization=0.0005,
                    scales=[0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 1.05],
                    aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5],
                                             [1.0, 2.0, 0.5]],
                    two_boxes_for_ar1=True,
                    steps=[8, 16, 32, 64, 100, 300],
                    offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
                    limit_boxes=False,
                    variances=[0.1, 0.1, 0.2, 0.2],
                    coords='centroids',
                    normalize_coords=True,
                    subtract_mean=[123, 117, 104],
                    swap_channels=True,
                    confidence_thresh=0.5,
                    iou_threshold=0.45,
                    top_k=200,
                    nms_max_output_size=400)

    model.load_weights(weights_path, by_name=True)

    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=5e-04)

    ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)

    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    return model, ['background', 'rovio', 'rovio']
Example #21
0
def build_SSD(weight_path, class_list, img_dimension):
    '''
    --input--
    weight_path: path of the pre-trained weights,
    class_list: list of pre-trained classes,
    img_dimension: tupule of (img_height,img_width)
    --output--
    model: compiled SSD model
    classes: dict(class:index)
    classes_rev: dict(index:class)
    '''
    model = ssd_512(image_size=(img_dimension[0], img_dimension[1], 3),
                    n_classes=20,
                    mode='inference',
                    l2_regularization=0.0005,
                    scales=[0.07, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1.05],
                    aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                             [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]],
                    two_boxes_for_ar1=True,
                    steps=[8, 16, 32, 64, 128, 256, 512],
                    offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
                    clip_boxes=False,
                    variances=[0.1, 0.1, 0.2, 0.2],
                    normalize_coords=True,
                    subtract_mean=[123, 117, 104],
                    swap_channels=[2, 1, 0],
                    confidence_thresh=0.5,
                    iou_threshold=0.45,
                    top_k=200,
                    nms_max_output_size=400)
    classes = {class_list[i]: i for i in range(0, len(class_list))}
    classes_rev = {i: class_list[i] for i in range(0, len(class_list))}

    model.load_weights(weight_path, by_name=True)

    adam = Adam(lr=0.001, epsilon=1e-08)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
    return model, classes, classes_rev
Example #22
0
def build_prediction_model():
    global model

    # 1: Build the Keras model

    K.clear_session()  # Clear previous models from memory.

    # Loss function
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    #model_path = '/home/kara9147/ML/ssd_keras_caltech/ssd7_epoch-03_loss-2.4693_val_loss-2.4097.h5'
    model_path = '/home/kara9147/ML/ssd_keras_caltech/conf2_ssd7_epoch-05_loss-2.4001_val_loss-2.3803.h5'

    # 2: Load the saved model

    model = load_model(model_path,
                       custom_objects={
                           'AnchorBoxes': AnchorBoxes,
                           'compute_loss': ssd_loss.compute_loss
                       })
Example #23
0
def get_model(p_size, mc_dropout=True):
    K.clear_session()  # Clear previous models from memory.

    model = ssd_512(
        image_size=(img_height, img_width, 3),
        n_classes=20,
        mode='inference',
        l2_regularization=0.0005,
        scales=[
            0.07, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1.05
        ],  # The scales for MS COCO are [0.04, 0.1, 0.26, 0.42, 0.58, 0.74, 0.9, 1.06]
        aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]],
        two_boxes_for_ar1=True,
        steps=[8, 16, 32, 64, 128, 256, 512],
        offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
        clip_boxes=False,
        variances=[0.1, 0.1, 0.2, 0.2],
        normalize_coords=True,
        subtract_mean=[123, 117, 104],
        swap_channels=[2, 1, 0],
        confidence_thresh=0.5,
        iou_threshold=0.45,
        top_k=200,
        nms_max_output_size=400,
        mc_dropout=mc_dropout,
        dropout_size=p_size)

    # 2: Load the trained weights into the model.
    model.load_weights(weights_path, by_name=True)
    # 3: Compile the model so that Keras won't complain the next time you load it.
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
    return model
Example #24
0
    def load_model(self):

        self.model_path = './ssd_keras_crack.h5'

        # We need to create an SSDLoss object in order to pass that to the model loader.
        ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)

        K.clear_session()  # Clear previous models from memory.

        self.session = tf.Session()
        self.graph = tf.get_default_graph()

        with self.graph.as_default():
            with self.session.as_default():
                self.model = load_model(self.model_path,
                                        custom_objects={
                                            'AnchorBoxes': AnchorBoxes,
                                            'L2Normalization': L2Normalization,
                                            'DecodeDetections':
                                            DecodeDetections,
                                            'compute_loss':
                                            ssd_loss.compute_loss
                                        })
Example #25
0
    def __init__(self, listener):

        # label of traffic sign detected by this traffic sign detector
        self.sign = -1

        # image lister object which can provide current frame
        self.listener = listener

        # build model
        self.model = build_model(image_size=(img_height, img_width,
                                             img_channels),
                                 n_classes=n_classes,
                                 mode='training',
                                 l2_regularization=0.0005,
                                 scales=scales,
                                 aspect_ratios_global=aspect_ratios,
                                 aspect_ratios_per_layer=None,
                                 two_boxes_for_ar1=two_boxes_for_ar1,
                                 steps=steps,
                                 offsets=offsets,
                                 clip_boxes=clip_boxes,
                                 variances=variances,
                                 normalize_coords=normalize_coords,
                                 subtract_mean=intensity_mean,
                                 divide_by_stddev=intensity_range)

        # 2: Optional: Load some weights
        self.model.load_weights(
            'models/ssd7_epoch-04_loss-0.2610_val_loss-0.6570.h5',
            by_name=True)

        # 3: Instantiate an Adam optimizer and the SSD loss function and compile the model
        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)
        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
        self.model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

        img = cv2.imread(
            "/home/namntse05438/Cuoc_Dua_So/Ros_python/traffic_sign_data/1545920226.49_50_1.54542034912e-05.jpg"
        )
        img = self.preprocessing(img)
        rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        y_pred_decoded = self.get_ypred_decoded(
            rgb_img.reshape(1, img_height, img_width, 3))
        print(y_pred_decoded[0])
        for box in y_pred_decoded[0]:
            xmin = int(box[-4])
            ymin = int(box[-3])
            xmax = int(box[-2])
            ymax = int(box[-1])
            label = '{}: {:.2f}'.format(classes[int(box[0])], box[1])
            print(label)
            #draw bounding box
            cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)
            cv2.putText(img, str(label), (xmin, ymin + 20),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 2,
                        cv2.LINE_AA)

        # cv2.imshow('traffic_sign_test', img)
        cv2.waitKey(1)
Example #26
0
def predict_num_area(src):
    '''
    :param src:opencv读取的图
    :return: scale:缩放倍率,mask:数字区域的全白图,all_blocks:矩形坐标
    '''

    model_path = 'ssd7_pascal_07_epoch-17_loss-0.8387_val_loss-0.8608.h5'
    # We need to create an SSDLoss object in order to pass that to the model loader.
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

    K.clear_session()  # Clear previous models from memory.

    model = load_model(model_path,
                       custom_objects={
                           'AnchorBoxes': AnchorBoxes,
                           'compute_loss': ssd_loss.compute_loss
                       })
    img_height = 341
    img_width = 256
    scale_y = src.shape[0] / 341
    scale_x = src.shape[1] / 256
    normalize_coords = True
    orig_images = []  # Store the images here.
    input_images = []  # Store resized versions of the images here.

    # We'll only load one image in this example.
    filename = '../NumInstrument/img/im3.JPG'
    # filename='../ssd_trains/JPEGImages/image1024.JPG'

    # img = cv2.imread(filename)
    img = cv2.resize(src, (img_width, img_height))
    orig_images.append(img)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    input_images.append(img)
    input_images = np.array(input_images)

    y_pred = model.predict(input_images)
    # 4: Decode the raw predictions in `y_pred`.
    confidence_threshold = 0.5

    y_pred_thresh = [
        y_pred[k][y_pred[k, :, 1] > confidence_threshold]
        for k in range(y_pred.shape[0])
    ]

    y_pred_decoded = decode_detections(y_pred,
                                       confidence_thresh=0.5,
                                       iou_threshold=0.45,
                                       top_k=200,
                                       normalize_coords=True,
                                       img_height=img_height,
                                       img_width=img_width)

    np.set_printoptions(precision=2, suppress=True, linewidth=90)
    # print("Predicted boxes:\n")
    # print('   class   conf xmin   ymin   xmax   ymax')
    # print(y_pred_decoded[0])
    mask = np.zeros((341, 256), np.uint8)
    for box in y_pred_decoded[0]:
        xmin = int(box[2])
        ymin = int(box[3])
        xmax = int(box[4] - 2)
        ymax = int(box[5] + 5)
        cv2.rectangle(mask, (xmin, ymin), (xmax, ymax), 255, -1)
        cv2.rectangle(input_images[0], (xmin, ymin), (xmax, ymax), 255, 2)

    input_images[0] = cv2.cvtColor(input_images[0], cv2.COLOR_RGB2BGR)

    mask = cv2.dilate(mask, kernel4)

    cv2.imwrite('./pre_model/4_pre_mask' + str(0) + '.jpg', mask)
    cv2.imwrite('./pre_model/4_pre_res' + str(0) + '.jpg', input_images[0])

    all_blocks = cutImage(mask, input_images[0], -1)
    # print(all_blocks,scale)
    tmp = []
    for abox in all_blocks:
        aa = [
            int(abox[0] * scale_x),
            int(abox[1] * scale_y),
            int(abox[2] * scale_x),
            int(abox[3] * scale_y)
        ]
        tmp.append(aa)
    all_blocks = tmp
    K.clear_session()
    return mask, all_blocks
                                     normalize_coords=normalize_coords,
                                     subtract_mean=mean_color,
                                     confidence_thresh=0.5,
                                     iou_threshold=0.45)

    # 3: Instantiate an optimizer and the SSD loss function and compile the model.
    #    If you want to follow the original Caffe implementation, use the preset SGD
    #    optimizer, otherwise I'd recommend the commented-out Adam optimizer.
    print(model.summary())
    weights_path = 'VGG_ILSVRC_16_layers_fc_reduced.h5'

    model.load_weights(weights_path, by_name=True)
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=5e-04)
    #sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False)

    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0, n_neg_min=0)

    model.compile(optimizer=adam,
                  loss=ssd_loss.compute_loss,
                  metrics=['mae', 'accuracy'])
    # Optional: If you have enough memory, consider loading the images into memory for the reasons explained above.
    print('output shape', model.output_shape)
    train_dataset = DataGenerator(load_images_into_memory=False,
                                  hdf5_dataset_path=None)
    val_dataset = DataGenerator(load_images_into_memory=False,
                                hdf5_dataset_path=None)
    # The directories that contain the images.
    images_dir = '/home/docker/Jessi/smart-traffic-sensor-lab/vehicles-dataset/images/'

    # The directories that contain the annotations.
    annotations_dir = '/home/docker/Jessi/smart-traffic-sensor-lab/vehicles-dataset/annotations/'
Example #28
0
from ssd_encoder_decoder.ssd_input_encoder import SSDInputEncoder
from ssd_encoder_decoder.ssd_output_decoder import decode_detections, decode_detections_fast

from data_generator.object_detection_2d_data_generator import DataGenerator
from data_generator.object_detection_2d_misc_utils import apply_inverse_transforms
from data_generator.data_augmentation_chain_variable_input_size import DataAugmentationVariableInputSize
from data_generator.data_augmentation_chain_constant_input_size import DataAugmentationConstantInputSize
from data_generator.data_augmentation_chain_original_ssd import SSDDataAugmentation

img_height = 224  # Height of the input images
img_width = 224  # Width of the input images
img_channels = 3  # Number of color channels of the input images
n_classes = 20  # Number of positive classes
n_predictor_layers = 2
normalize_coords = True

model_path = 'model_train_test.h5'

K.clear_session()  # Clear previous models from memory.

# We need to create an SSDLoss object in order to pass that to the model loader.
ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

model = load_model(model_path,
                   custom_objects={
                       'AnchorBoxes': AnchorBoxes,
                       'compute_loss': ssd_loss.compute_loss
                   })

model.summary()
Example #29
0
def init_Model():
    ##################################################################################################################
    img_height = 300  # Height of the input images
    img_width = 480  # Width of the input images
    img_channels = 3  # Number of color channels of the input images
    intensity_mean = 127.5  # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`.
    intensity_range = 127.5  # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`.
    n_classes = 1  # Number of positive classes
    #scales = [0.08, 0.16, 0.32, 0.64, 0.96, 1] # An explicit list of anchor box scaling factors. If this is passed, it will override `min_scale` and `max_scale`.
    scales = [0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 1.05]
    aspect_ratios = [0.5, 1.0,
                     2.0]  # The list of aspect ratios for the anchor boxes
    two_boxes_for_ar1 = True  # Whether or not you want to generate two anchor boxes for aspect ratio 1
    steps = None  # In case you'd like to set the step sizes for the anchor box grids manually; not recommended
    offsets = None  # In case you'd like to set the offsets for the anchor box grids manually; not recommended
    clip_boxes = False  # Whether or not to clip the anchor boxes to lie entirely within the image boundaries
    variances = [
        1.0, 1.0, 1.0, 1.0
    ]  # The list of variances by which the encoded target coordinates are scaled
    normalize_coords = True  # Whether or not the model is supposed to use coordinates relative to the image size
    ###################################################################################################################

    model = build_model(image_size=(img_height, img_width, img_channels),
                        n_classes=n_classes,
                        mode='training',
                        l2_regularization=0.0005,
                        scales=scales,
                        aspect_ratios_global=aspect_ratios,
                        aspect_ratios_per_layer=None,
                        two_boxes_for_ar1=two_boxes_for_ar1,
                        steps=steps,
                        offsets=offsets,
                        clip_boxes=clip_boxes,
                        variances=variances,
                        normalize_coords=normalize_coords,
                        subtract_mean=intensity_mean,
                        divide_by_stddev=intensity_range)

    # 2: Optional: Load some weights

    #model.load_weights(r'C:/code/python/ssd_keras-master/weight/ssd7_epoch-00_loss-2.1119_val_loss-1.7316.h5', by_name=True)
    model.load_weights(
        r'C:/code/python/ssd_keras-master-copy/weight_base_line/ssd7_epoch-03_loss-2.3741_val_loss-2.3561.h5',
        by_name=True)

    # add: freeze some layer
    freeze_layers = [
        'identity_layer', 'input_mean_normalization',
        'input_stddev_normalization', 'conv1_1', 'conv1_2', 'pool1', 'conv2_1',
        'conv2_2', 'pool2', 'conv3_1', 'conv3_2', 'conv3_3'
    ]

    for layer in model.layers:
        if layer.name in freeze_layers:
            layer.trainable = False

    # 3: Instantiate an Adam optimizer and the SSD loss function and compile the model

    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

    #model.compile(optimizer=adam, loss='binary_crossentropy')
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
    #model.compile(optimizer=adam, loss=[ssd_loss.compute_loss, losses.binary_crossentropy],
    #loss_weights = [1,  0.2])

    return model
Example #30
0
    def __init__(self):

        K.clear_session()  # Clear previous models from memory.

        self.nga_tu = 0

        self.flag = 0

        self.steering_bool = False

        self.count_for_traffic = 0

        self.count = 0

        self.pre_count = 0

        self.old_count = 0

        self.flag_car = False

        self.position_count_left = 0

        self.position_count_right = 0

        self.vi_tri_vat_can = 0

        self.vi_tri_vat_can_hough = 0

        config = tf.ConfigProto()

        config.gpu_options.allow_growth = True

        config.intra_op_parallelism_threads = 4

        config.inter_op_parallelism_threads = 4

        self.session = tf.InteractiveSession(config=config)

        self.graph = tf.get_default_graph()

        self.model = G2_Unet()

        self.model.summary()

        model_path = rospy.get_param('~model_dl')

        self.model.load_weights(model_path)

        self.subscriber1 = rospy.Subscriber(
            "g2_never_die/camera/rgb/compressed",
            CompressedImage,
            self.callback,
            queue_size=10)

        self.speed = rospy.Publisher("g2_never_die/set_speed",
                                     Float32,
                                     queue_size=1)

        self.angle_car = rospy.Publisher("g2_never_die/set_angle",
                                         Float32,
                                         queue_size=1)

        # self.traffic_sign = rospy.Subscriber("g2_never_die/traffic_sign",
        #                                     Int8,
        #                                     self.callback_sign,
        #                                     queue_size=1)

        # self.depth_x = rospy.Subscriber("g2_never_die/depth_x",Float32,self.depth_x_callback,queue_size=1)

        # self.depth_y = rospy.Subscriber("g2_never_die/depth_y",Float32,self.depth_y_callback,queue_size=1)

        self.flag_sign = 0

        self.balance_road = 0

        self.den_diem_re = False

        self.stop_here = False

        self.depth_x_coor = 0

        self.depth_y_coor = 0

        self.check_point = 0

        self.count_for_car = 0

        self.check_car = False

        self.last_angle = 0

        self.check_balance = False

        self.flag_for_re = False

        self.nga_tu_real = 0

        self.check_nga_tu = False

        self.nga_tu_fake = 0

        self.dem_nga_tu = 0

        self.count_for_bungbinh = 0

        self.count_for_car_real = 0

        self.flag_car_for_real = False

        self.count_for_sign = 0

        self.drive_for_my_way = False

        self.da_den_bung_binh = False

        self.cX_center = 0

        self.cY_center = 0

        ## SSD ##

        img_height = 240  # Height of the input images
        img_width = 320  # Width of the input images
        img_channels = 3  # Number of color channels of the input images
        intensity_mean = 127.5  # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`.
        intensity_range = 127.5  # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`.
        n_classes = 3  # Number of positive classes
        scales = [
            0.08, 0.16, 0.32, 0.64, 0.96
        ]  # An explicit list of anchor box scaling factors. If this is passed, it will override `min_scale` and `max_scale`.
        aspect_ratios = [0.5, 1.0,
                         2.0]  # The list of aspect ratios for the anchor boxes
        two_boxes_for_ar1 = True  # Whether or not you want to generate two anchor boxes for aspect ratio 1
        steps = None  # In case you'd like to set the step sizes for the anchor box grids manually; not recommended
        offsets = None  # In case you'd like to set the offsets for the anchor box grids manually; not recommended
        clip_boxes = False  # Whether or not to clip the anchor boxes to lie entirely within the image boundaries
        variances = [
            1.0, 1.0, 1.0, 1.0
        ]  # The list of variances by which the encoded target coordinates are scaled
        self.normalize_coords = True  # Whether or not the model is supposed to use coordinates relative to the image size
        # weight_path = 'trained_models/digit_detect_pretrained.h5'
        weight_path = rospy.get_param('~ssd_model')

        self.ssd_model = build_model(image_size=(img_height, img_width,
                                                 img_channels),
                                     n_classes=n_classes,
                                     mode='training',
                                     l2_regularization=0.0005,
                                     scales=scales,
                                     aspect_ratios_global=aspect_ratios,
                                     aspect_ratios_per_layer=None,
                                     two_boxes_for_ar1=two_boxes_for_ar1,
                                     steps=steps,
                                     offsets=offsets,
                                     clip_boxes=clip_boxes,
                                     variances=variances,
                                     normalize_coords=self.normalize_coords,
                                     subtract_mean=intensity_mean,
                                     divide_by_stddev=intensity_range)

        if (weight_path is not None):
            self.ssd_model.load_weights(weight_path, by_name=True)

        adam = Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=1e-08,
                    decay=0.0)

        ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

        self.ssd_model.compile(optimizer=adam, loss=ssd_loss.compute_loss)