Example #1
0
    def __init__(self):
        NUM_CLASSES = 3 + 1
        input_shape = (300, 300, 3)

        #config_string = rospy.get_param("/traffic_light_config")
        #self.config = yaml.load(config_string)
        #self.stop_line_positions = self.config['stop_line_positions']

        # get path to resources
        #path_to_resources = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', '..', 'tlc')
        # "prior boxes" in the paper
        #priors = pickle.load(open(os.path.join(path_to_resources, 'prior_boxes_ssd300.pkl'), 'rb'))
        priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
        self.bbox_util = BBoxUtility(NUM_CLASSES, priors)

        # Traffic Light Classifier model and its weights
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        #self.model.load_weights(os.path.join(path_to_resources, self.config['classifier_weights_file']), by_name=True)
        #self.model.load_weights('weights.180314.hdf5', by_name=True)
        self.model.load_weights('checkpoints/weights.07-0.70.hdf5',
                                by_name=True)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        self.is_in_progress = False
        self.last_result = TrafficLight.UNKNOWN
Example #2
0
    def __init__(self):

        self.dictindex = []

        with open('./label.txt') as f:
            content = f.readlines()
            for symbol in content:
                symbol = symbol.replace('\n', '')

                split = symbol.split(' ')

                self.dictindex.append(split[0])

        # Load model
        self.net = SSD300()
        checkpoint = torch.load('./ssdtrain0511_11.pth')
        checkpoint['net']
        self.net.load_state_dict(checkpoint['net'])
        self.net.eval()

        self.data_encoder = DataEncoder()

        self.transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=(0.485, 0.456, 0.406),
                                 std=(0.229, 0.224, 0.225))
        ])
Example #3
0
def predict_img(numpy_array, orig_numpy_array):
    # Save the original image for attachment
    scipy.misc.imsave('temp_cat_motion.jpg', np.uint8(orig_numpy_array))
    
    # Number of voc_classes + 1
    NUM_CLASSES = 3
    input_shape=(300, 300, 3)
    # SSD model
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('./model/weights.18-0.09.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)
    
    # Inception v3 transfer learning model
    model_cnn = load_model(filepath='./model/model_v2.03-0.40.hdf5')
    ssd_img_size=300
    img_size=299

    inputs = []
    images = []

    images.append(orig_numpy_array)
    
    inputs.append(numpy_array.copy())
    inputs = preprocess_input(np.array(inputs))
    preds = model.predict(inputs, batch_size=1, verbose=0)
    results = bbox_util.detection_out(preds)

    cat_inside_image = False
    # If the SSD model does not find a cat, return False
    for i, img in enumerate(images):
        cat_inside_image = ssd_image(img, results, i)
    
    return cat_inside_image
Example #4
0
    def __init__(self,
                 modelfile,
                 shape=(300, 300, 3),
                 num_classes=21,
                 conf_thresh=0.6):

        self.input_shape = shape
        self.num_classes = num_classes
        self.conf_thresh = conf_thresh

        # モデル作成
        model = SSD(shape, num_classes=num_classes)
        model.load_weights(modelfile)
        self.model = model

        # バウンディングボックス作成ユーティリティ
        self.bbox_util = BBoxUtility(self.num_classes)
Example #5
0
 def __init__(self):
     #顔検出モデルと年齢・性別検出モデルを復元
     self.age_detector = load_model("transfer_Xception_29.h5")
     NUM_CLASSES = 2
     input_shape = (300, 300, 3)
     priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
     self.bbox_util = BBoxUtility(NUM_CLASSES, priors)
     self.face_detector = SSD300(input_shape, num_classes=NUM_CLASSES)
     self.face_detector.load_weights('weights.05-3.15.hdf5', by_name=True)
    def __init__(self):
        self.node_name = "ssd_keras"
        rospy.init_node(self.node_name)
        self.class_names = [
            "background", "aeroplane", "bicycle", "bird", "boat", "bottle",
            "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
            "motorbike", "person", "pottedplant", "sheep", "sofa", "train",
            "tvmonitor"
        ]
        self.num_classes = len(self.class_names)
        self.input_shape = (300, 300, 3)
        self.model = SSD(self.input_shape, num_classes=self.num_classes)
        self.model.load_weights(pkg_path +
                                '/resources/ssd_keras/weights_SSD300.hdf5')

        self.bbox_util = BBoxUtility(self.num_classes)
        self.conf_thresh = 0.25

        self.model._make_predict_function()
        self.graph = tf.get_default_graph()

        self.detection_index = DL_msgs_boxes()

        # Create unique and somewhat visually distinguishable bright
        # colors for the different classes.
        self.class_colors = []
        for i in range(0, self.num_classes):
            # This can probably be written in a more elegant manner
            hue = 255 * i / self.num_classes
            col = np.zeros((1, 1, 3)).astype("uint8")
            col[0][0][0] = hue
            col[0][0][1] = 128  # Saturation
            col[0][0][2] = 255  # Value
            cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
            col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]),
                   int(cvcol[0][0][2]))
            self.class_colors.append(col)

        self.bridge = CvBridge()  # Create the cv_bridge object

        self.Image_Status = "Not_Ready"
        self.StartImage = cv2.imread(pkg_path + '/resources/start.jpg')
        self.to_draw = cv2.resize(self.StartImage, (640, 480))

        self.image_sub = rospy.Subscriber(
            "/floating_sensor/camera/rgb/image_raw",
            Image,
            self.detect_image,
            queue_size=1)  # the appropriate callbacks

        self.box_coordinate_pub = rospy.Publisher(
            "/ssd_detction/box", DL_msgs_boxes,
            queue_size=5)  # the appropriate callbacks
        self.SSD_Serv = rospy.Service('SSD_Detection', DL_box,
                                      self.SSD_Detection_Server)
Example #7
0
    def __init__(self):
        #TODO load classifier
        NUM_CLASSES = 3 + 1
        input_shape = (300, 300, 3)

        # "prior boxes" in the paper
        priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
        self.bbox_util = BBoxUtility(NUM_CLASSES, priors)

        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        self.model.load_weights('weights.180314.hdf5', by_name=True)
Example #8
0
    def __init__(self):
        self.image_width = 300
        self.image_height = 300

        self.voc_classes = [
            'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car',
            'Cat', 'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike',
            'Person', 'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
        ]
        self.NUM_CLASSES = len(self.voc_classes) + 1

        self.model = SSD300((self.image_height, self.image_width, 3),
                            num_classes=self.NUM_CLASSES)
        self.model.load_weights('weights_SSD300.hdf5', by_name=True)
        self.bbox_util = BBoxUtility(self.NUM_CLASSES)
def init_model(weight_file='ssd_keras/SSD/weights_SSD300.hdf5'):

    np.set_printoptions(suppress=True)

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.45
    set_session(tf.Session(config=config))

    NUM_CLASSES = len(voc_classes) + 1

    input_shape = (300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights(weight_file, by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)
    return model, bbox_util
Example #10
0
    def __init__(self, conf_limit=0.6):
        self.conf_limit = conf_limit
        np.set_printoptions(suppress=True)
        config = tf.ConfigProto()
        #config.gpu_options.per_process_gpu_memory_fraction = 0.45
        set_session(tf.Session(config=config))

        self.voc_classes = [
            'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
            'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
            'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
        ]
        NUM_CLASSES = len(self.voc_classes) + 1
        self.bbox_util = BBoxUtility(NUM_CLASSES)

        input_shape = (300, 300, 3)
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        self.model.load_weights('weights_SSD300.hdf5', by_name=True)
Example #11
0
def get_model(name, experiment, input_shape, num_classes=6, verbose=True):
    """ Gets an SSD model, with trained weights
    
        Arguments:
        name        -- name of the dataset
        experiment  -- name of this training run
        input_shape -- size of images fed to SSD as a tuple like (640,480,3)
        num_classes -- the number of different object classes (including background)
    """
    model = SSD300((input_shape[1], input_shape[0], input_shape[2]),
                   num_classes=num_classes)
    weights_files = list((runs_path / "{}_{}".format(name, experiment) /
                          "checkpoints").glob('*.hdf5'))
    weights_files_loss = np.array(
        [float(wf.stem.split('-')[-1]) for wf in weights_files])
    weights_file = weights_files[np.argmin(weights_files_loss)]
    model.load_weights(weights_file, by_name=True)
    if verbose:
        print_flush('Model loaded from {}'.format(weights_file))
    return model
Example #12
0
    def __init__(
            self,
            path_weights="/home/francisco/git/ssd_keras/weights_SSD300.hdf5"):
        config = tf.ConfigProto()
        config.gpu_options.per_process_gpu_memory_fraction = 0.45
        set_session(tf.Session(config=config))

        self.labels = [
            'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car',
            'Cat', 'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike',
            'Person', 'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
        ]
        NUM_CLASSES = len(self.labels) + 1

        input_shape = (300, 300, 3)
        # Get detections with confidence higher than 0.6.
        self.detection_confidence = 0.6
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        self.model.load_weights(path_weights, by_name=True)
        self.bbox_util = BBoxUtility(NUM_CLASSES)
        self.detections = []
Example #13
0
def predict_img(numpy_array, orig_numpy_array):
    # Save the original image for attachment
    scipy.misc.imsave('temp_cat_water.jpg', np.uint8(orig_numpy_array))

    # Number of voc_classes + 1
    NUM_CLASSES = 3
    input_shape = (300, 300, 3)
    # SSD model
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('./model/weights.18-0.09.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)

    # Inception v3 transfer learning model
    model_cnn = load_model(filepath='./model/model_v2.03-0.40.hdf5')
    ssd_img_size = 300
    img_size = 299

    inputs = []
    images = []

    images.append(orig_numpy_array)

    inputs.append(numpy_array.copy())
    inputs = preprocess_input(np.array(inputs))
    preds = model.predict(inputs, batch_size=1, verbose=0)
    results = bbox_util.detection_out(preds)

    # If the SSD model does not find an appropriate object, automatically return 0.00
    for i, img in enumerate(images):
        if type(results[i]) is not list:
            ssd_img = ssd_image(img, results, i)
            resize_img = imresize(ssd_img, (img_size, img_size))

            x = np.expand_dims(resize_img, axis=0)
            y_pred = model_cnn.predict(x)
            prediction = round(y_pred[0][0], 3)
        else:
            prediction = 0.00

    return prediction
Example #14
0
def convertToModel(path):

    vgg = torch.load('./raw_pretrain/pretrain_selectedtotrain.pth')

    vgg = vgg['net']

    #print(vgg.keys())

    ssd_net = SSD300()

    layer_indices = [
        0, 1, 3, 4, 7, 8, 10, 11, 14, 15, 17, 18, 20, 21, 24, 25, 27, 28, 30,
        31
    ]  #,24,25,27,28,30,31]

    prefix = 'module.'

    for layer_idx in layer_indices:
        ssd_net.base[layer_idx].weight.data = vgg[prefix +
                                                  'features.%d.weight' %
                                                  layer_idx]
        ssd_net.base[layer_idx].bias.data = vgg[prefix +
                                                'features.%d.bias' % layer_idx]

    # [24,26,28]
    ssd_net.conv5_1.weight.data = vgg[prefix + 'features.34.weight']
    ssd_net.conv5_1.bias.data = vgg[prefix + 'features.34.bias']
    ssd_net.conv5_2.weight.data = vgg[prefix + 'features.37.weight']
    ssd_net.conv5_2.bias.data = vgg[prefix + 'features.37.bias']
    ssd_net.conv5_3.weight.data = vgg[prefix + 'features.40.weight']
    ssd_net.conv5_3.bias.data = vgg[prefix + 'features.40.bias']

    ssd_net.norm5_1.weight.data = vgg[prefix + 'features.35.weight']
    ssd_net.norm5_1.bias.data = vgg[prefix + 'features.35.bias']
    ssd_net.norm5_2.weight.data = vgg[prefix + 'features.38.weight']
    ssd_net.norm5_2.bias.data = vgg[prefix + 'features.38.bias']
    ssd_net.norm5_3.weight.data = vgg[prefix + 'features.41.weight']
    ssd_net.norm5_3.bias.data = vgg[prefix + 'features.41.bias']

    torch.save(ssd_net.state_dict(), path)
Example #15
0
def main(img_paths):
    """
    Detect objects in images.

    Parameters
    ----------
    img_paths : list of strings
    """
    # Load the model
    voc_classes = [
        'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car', 'Cat',
        'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike', 'Person',
        'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
    ]
    NUM_CLASSES = len(voc_classes) + 1
    input_shape = (300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('weights_SSD300.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)

    # Load the inputs
    inputs = []
    images = []
    for img_path in img_paths:
        img = image.load_img(img_path, target_size=(300, 300))
        img = image.img_to_array(img)
        images.append(imread(img_path))
        inputs.append(img.copy())
    inputs = preprocess_input(np.array(inputs))

    # Predict
    preds = model.predict(inputs, batch_size=1, verbose=1)
    results = bbox_util.detection_out(preds)

    # Visualize
    for i, img in enumerate(images):
        create_overlay(img, results[i], voc_classes,
                       "{}-det.png".format(img_paths[i]))
Example #16
0
    def __init__(self):
        NUM_CLASSES = 3 + 1
        input_shape = (300, 300, 3)

        config_string = rospy.get_param("/traffic_light_config")
        self.config = yaml.load(config_string)
        self.stop_line_positions = self.config['stop_line_positions']

        # get path to resources
        path_to_resources = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', '..',
            'tlc')
        # "prior boxes" in the paper
        priors = pickle.load(
            open(os.path.join(path_to_resources, 'prior_boxes_ssd300.pkl'),
                 'rb'))
        self.bbox_util = BBoxUtility(NUM_CLASSES, priors)

        # Traffic Light Classifier model and its weights
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        print(self.model.summary())
        self.model.load_weights(os.path.join(
            path_to_resources, self.config['classifier_weights_file']),
                                by_name=True)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        self.capture_images = False
        self.image_counts = {0: 0, 1: 0, 2: 0, 4: 0}

        self.last_classification = None
Example #17
0
    print("save " + plt_fname)


if __name__ == "__main__":
    import glob
    imagesList = glob.glob("images/*.jpg")

    # Load the model
    voc_classes = [
        'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car', 'Cat',
        'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike', 'Person',
        'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
    ]
    NUM_CLASSES = len(voc_classes) + 1
    input_shape = (300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('weights_SSD300.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)

    # Load the inputs
    inputs = []
    images = []
    for img_path in imagesList:
        print("process " + img_path)
        img = image.load_img(img_path, target_size=(300, 300))
        img = image.img_to_array(img)
        images.append(imread(img_path))
        inputs.append(img.copy())
    # 前置處理
    print("前置處理...")
    inputs = preprocess_input(np.array(inputs))
from ssd import SSD300, SSD512
from utils.caffe2keras import add_missing_layers

model = SSD300((300, 300, 3), num_classes=21)
add_missing_layers(model, 'ssd300_voc_weights.hdf5',
                   'ssd300_voc_weights_fixed.hdf5')

model = SSD512((512, 512, 3), num_classes=21)
add_missing_layers(model, 'ssd512_voc_weights.hdf5',
                   'ssd512_voc_weights_fixed.hdf5')
Example #19
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('model')
    parser.add_argument('image')
    args = parser.parse_args()

    size = 300

    multibox_encoder = MultiBoxEncoder(
        n_scale=6,
        variance=(0.1, 0.2),
        grids=(38, 19, 10, 5, 3, 1),
        aspect_ratios=((2,), (2, 3), (2, 3), (2, 3), (2,), (2,)))

    model = SSD300(
        n_class=20,
        n_anchors=multibox_encoder.n_anchors)
    serializers.load_npz(args.model, model)

    src = cv2.imread(args.image, cv2.IMREAD_COLOR)

    x = cv2.resize(src, (size, size)).astype(np.float32)
    x -= (103.939, 116.779, 123.68)
    x = x.transpose(2, 0, 1)
    x = x[np.newaxis]

    loc, conf = model(x)
    boxes, conf = multibox_encoder.decode(loc.data[0], conf.data[0])
    conf = conf[:, 1:]

    img = src.copy()
Example #20
0
'''Convert pretrained VGG model to SSD.

VGG model download from PyTorch model zoo: https://download.pytorch.org/models/vgg16-397923af.pth
'''
import torch

from ssd import SSD300

vgg = torch.load('./model/vgg16-397923af.pth')

ssd = SSD300()
layer_indices = [0, 2, 5, 7, 10, 12, 14, 17, 19, 21]

for layer_idx in layer_indices:
    ssd.base[layer_idx].weight.data = vgg['features.%d.weight' % layer_idx]
    ssd.base[layer_idx].bias.data = vgg['features.%d.bias' % layer_idx]

# [24,26,28]
ssd.conv5_1.weight.data = vgg['features.24.weight']
ssd.conv5_1.bias.data = vgg['features.24.bias']
ssd.conv5_2.weight.data = vgg['features.26.weight']
ssd.conv5_2.bias.data = vgg['features.26.bias']
ssd.conv5_3.weight.data = vgg['features.28.weight']
ssd.conv5_3.bias.data = vgg['features.28.bias']
for k in ssd.state_dict():
    print(k)
torch.save(ssd.state_dict(), 'model/ssd.pth')
Example #21
0
from chainer import serializers
from chainer.links.caffe import CaffeFunction

import config
from ssd import SSD300

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('source')
    parser.add_argument('target')
    parser.add_argument('--baseonly', action='store_true')
    parser.set_defaults(baseonly=False)
    args = parser.parse_args()

    caffe_model = CaffeFunction(args.source)
    model = SSD300(n_class=20, aspect_ratios=config.aspect_ratios)

    model.base.conv1_1.copyparams(caffe_model.conv1_1)
    model.base.conv1_2.copyparams(caffe_model.conv1_2)

    model.base.conv2_1.copyparams(caffe_model.conv2_1)
    model.base.conv2_2.copyparams(caffe_model.conv2_2)

    model.base.conv3_1.copyparams(caffe_model.conv3_1)
    model.base.conv3_2.copyparams(caffe_model.conv3_2)
    model.base.conv3_3.copyparams(caffe_model.conv3_3)

    model.base.conv4_1.copyparams(caffe_model.conv4_1)
    model.base.conv4_2.copyparams(caffe_model.conv4_2)
    model.base.conv4_3.copyparams(caffe_model.conv4_3)
Example #22
0
def train(NUM_CLASSES, nb_epoch, base_lr=3e-4, path_prefix='data/train/', dev="cpu"):
    import keras
    import tensorflow as tf
    from keras.backend.tensorflow_backend import set_session
    import os
    import matplotlib.pyplot as plt
    import numpy as np
    import pickle
    from sklearn.model_selection import train_test_split
    from ssd import SSD300
    from ssd_training import MultiboxLoss
    from ssd_training import Generator
    from ssd_utils import BBoxUtility
    from keras.callbacks import TensorBoard
    plt.rcParams['figure.figsize'] = (8, 8)
    plt.rcParams['image.interpolation'] = 'nearest'
    np.set_printoptions(suppress=True)
    if dev == "cpu":
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'  # for multi GPUs
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = '0'
        config = tf.ConfigProto()
        config.gpu_options.per_process_gpu_memory_fraction = 0.9
        set_session(tf.Session(config=config))

    # some constants
    NUM_CLASSES = NUM_CLASSES + 1  # 1 means mask
    input_shape = (300, 300, 3)
    # nb_epoch = 5
    # base_lr = 3e-4
    # path_prefix = 'data/train/'  # path to your data

    priors = pickle.load(open('data/prior_boxes_ssd300.pkl', 'rb'))
    bbox_util = BBoxUtility(NUM_CLASSES, priors)

    gt = pickle.load(open('data/train.pkl', 'rb'), encoding='iso-8859-1')  # for python3.x
    lable = pickle.load(open('data/label.pkl', 'rb'), encoding='iso-8859-1')  # for python3.x
    # gt = pickle.load(open('data_convert/train.pkl', 'rb'))
    # keys = sorted(gt.keys())
    # num_train = int(round(0.85 * len(keys)))
    # train_keys = keys[:num_train]
    # val_keys = keys[num_train:]
    # num_val = len(val_keys)
    train_keys, val_keys, train_label, val_label = train_test_split(sorted(lable.keys()), sorted(lable.values()),
                                                                    test_size=0.1, random_state=0)

    num_train = len(train_keys)
    num_val = len(val_keys)
    print(train_keys)
    print(val_keys)

    gen = Generator(gt, bbox_util, 1, path_prefix,
                    train_keys, val_keys,
                    (input_shape[0], input_shape[1]), do_crop=False)

    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('data/weights_SSD300.hdf5', by_name=True)

    freeze = ['input_1', 'conv1_1', 'conv1_2', 'pool1',
              'conv2_1', 'conv2_2', 'pool2',
              'conv3_1', 'conv3_2', 'conv3_3', 'pool3',]
              # 'conv4_1', 'conv4_2', 'conv4_3', 'pool4']

    # freeze = ['input_1', 'conv1_1', 'conv1_2', 'pool1',
    #           'conv2_1', 'conv2_2', 'pool2',
    #           'conv3_1', 'conv3_2', 'conv3_3', 'pool3',
    #           'conv4_1', 'conv4_2', 'conv4_3', 'pool4', 'conv4_3_norm',
    #           'conv5_1', 'conv5_2', 'conv5_3', 'pool5', 'fc6', 'fc7',
    #           'conv6_1', 'conv6_2',
    #           'conv7_1', 'conv7_1z', 'conv7_2',
    #           'conv8_1', 'conv8_2',
    #           'pool6'
    #           ]
    for L in model.layers:
        if L.name in freeze:
            L.trainable = False

    def schedule(epoch, decay=0.9):
        return base_lr * decay ** (epoch)

    # checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5
    callbacks = [keras.callbacks.ModelCheckpoint('checkpoints/weights.hdf5',
                                                 verbose=1,
                                                 save_weights_only=True, save_best_only=True, mode='auto', period=1),
                 keras.callbacks.LearningRateScheduler(schedule),TensorBoard(log_dir='logs')]

    optim = keras.optimizers.Adam(lr=base_lr)
    # optim = keras.optimizers.RMSprop(lr=base_lr)
    # optim = keras.optimizers.SGD(lr=base_lr, momentum=0.9, decay=decay, nesterov=True)
    model.compile(optimizer=optim,
                  loss=MultiboxLoss(NUM_CLASSES, neg_pos_ratio=2.0).compute_loss)
    a=0

    history = model.fit_generator(gen.generate(True), steps_per_epoch=num_train,
                                  epochs=nb_epoch, verbose=1,
                                  callbacks=callbacks,
                                  validation_data=gen.generate(False),
                                  validation_steps=num_val,
                                  nb_worker=1)
        cv2.imshow("SSD result", orig_image)
        if cv2.waitKey(5) & 0xFF == ord('s'):
            if len(image_stack) == frame_number:
                if not os.path.exists(save_path + str(sample_count + 1)):
                    os.mkdir(save_path + str(sample_count + 1))
                for pic in range(frame_number):
                    cv2.imwrite(
                        save_path + str(sample_count + 1) + '/' +
                        str(1000 + pic) + '.jpg', image_stack[pic])
                    print('saving ' + save_path + str(sample_count + 1) + '/' +
                          str(1000 + pic) + '.jpg')
                image_stack = []
                empty_count = 0
                sample_count += 1


if __name__ == '__main__':
    action_class = 'stand/'
    root_path = 'images/'
    save_path = root_path + action_class
    if not os.path.exists(root_path):
        os.mkdir(root_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    save_frames = 16
    input_shape = (300, 300, 3)
    ssd_model = SSD(input_shape, num_classes=21)
    ssd_model.load_weights('weights_SSD300.hdf5')
    run_camera(input_shape, ssd_model, save_path, save_frames)
Example #24
0
class ssdKeras():
    def __init__(self):
        #self.node_name = "ssd_keras"
        #rospy.init_node(self.node_name)
        self.class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
        self.num_classes = len(self.class_names)
        self.input_shape = (300,300,3)
        self.model = SSD(self.input_shape,num_classes=self.num_classes)
        self.model.load_weights('/home/abdulrahman/catkin_ws/src/victim_localization/resources/ssd_keras/weights_SSD300.hdf5')

        self.bbox_util = BBoxUtility(self.num_classes)
        self.conf_thresh = 0.7

        self.model._make_predict_function()
        self.graph = tf.get_default_graph()

        self.detection_index=DL_msgs_boxes()

        # Create unique and somewhat visually distinguishable bright
        # colors for the different classes.
        self.class_colors = []
        for i in range(0, self.num_classes):
            # This can probably be written in a more elegant manner
            hue = 255*i/self.num_classes
            col = np.zeros((1,1,3)).astype("uint8")
            col[0][0][0] = hue
            col[0][0][1] = 128 # Saturation
            col[0][0][2] = 255 # Value
            cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
            col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]), int(cvcol[0][0][2]))
            self.class_colors.append(col)

        self.bridge = CvBridge() # Create the cv_bridge object

        self.image_sub = rospy.Subscriber("/image_raw_converted2", Image, self.detect_image,queue_size=1)  # the appropriate callbacks

        self.box_coordinate_pub = rospy.Publisher("/ssd_detction/box", DL_msgs_boxes ,queue_size=5)  # the appropriate callbacks
    def detect_image(self, ros_image):
        """ Runs the test on a video (or webcam)

        # Arguments

        conf_thresh: Threshold of confidence. Any boxes with lower confidence
                     are not visualized.

        """


        #### Use cv_bridge() to convert the ROS image to OpenCV format  ####
        try:
            image_orig = self.bridge.imgmsg_to_cv2(ros_image, "bgr8")
        except CvBridgeError as e:
            print(e)
        ##########

        vidw = 1280.0 # change from cv2.cv.CV_CAP_PROP_FRAME_WIDTH
        vidh = 720.0 # change from cv2.cv.CV_CAP_PROP_FRAME_HEIGHT
        vidar = vidw/vidh

        #print(type(image_orig))
        im_size = (self.input_shape[0], self.input_shape[1])
        resized = cv2.resize(image_orig, im_size)
        rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)

        # Reshape to original aspect ratio for later visualization
        # The resized version is used, to visualize what kind of resolution
        # the network has to work with.
        to_draw = cv2.resize(resized, (1280, 720))

        # Use model to predict
        inputs = [image.img_to_array(rgb)]
        tmp_inp = np.array(inputs)
        x = preprocess_input(tmp_inp)

        start_time = time.time() #debuggin

        with self.graph.as_default():
            y = self.model.predict(x)
        #print("--- %s seconds_for_one_image ---" % (time.time() - start_time))

        # This line creates a new TensorFlow device every time. Is there a
        # way to avoid that?
        results = self.bbox_util.detection_out(y)

        if len(results) > 0 and len(results[0]) > 0:
        # Interpret output, only one frame is used
            det_label = results[0][:, 0]
            det_conf = results[0][:, 1]
            det_xmin = results[0][:, 2]
            det_ymin = results[0][:, 3]
            det_xmax = results[0][:, 4]
            det_ymax = results[0][:, 5]

            top_indices = [i for i, conf in enumerate(det_conf) if conf >= self.conf_thresh]

            top_conf = det_conf[top_indices]

            top_label_indices = det_label[top_indices].tolist()
            top_xmin = det_xmin[top_indices]
            top_ymin = det_ymin[top_indices]
            top_xmax = det_xmax[top_indices]
            top_ymax = det_ymax[top_indices]

            #initiaze the detection msgs
            box_msg = DL_msgs_box()
            box_msg.xmin=0
            box_msg.ymin=0
            box_msg.xmax=0
            box_msg.ymax=0
            box_msg.Class="Non" # 100 reflect a non-class value
            self.detection_index.boxes.append(box_msg)


            print (top_xmin)
            for i in range(top_conf.shape[0]):
                    self.detection_index.boxes[:]=[]
                    xmin = int(round(top_xmin[i] * to_draw.shape[1]))
                    ymin = int(round(top_ymin[i] * to_draw.shape[0]))
                    xmax = int(round(top_xmax[i] * to_draw.shape[1]))
                    ymax = int(round(top_ymax[i] * to_draw.shape[0]))

                    #include the corner to be published
                    box_msg = DL_msgs_box()
                    box_msg.xmin=xmin
                    box_msg.ymin=ymin
                    box_msg.xmax=xmax
                    box_msg.ymax=ymax
                    box_msg.Class=self.class_names[int(top_label_indices[i])]
                    self.detection_index.boxes.append(box_msg)

                    # Draw the box on top of the to_draw image

                    class_num = int(top_label_indices[i])
                    if (self.class_names[class_num]=="person"):
                        cv2.rectangle(to_draw, (xmin, ymin), (xmax, ymax),
                                      self.class_colors[class_num], 2)
                        text = self.class_names[class_num] + " " + ('%.2f' % top_conf[i])

                        text_top = (xmin, ymin-10)
                        text_bot = (xmin + 80, ymin + 5)
                        text_pos = (xmin + 5, ymin)
                        cv2.rectangle(to_draw, text_top, text_bot, self.class_colors[class_num], -1)
                        cv2.putText(to_draw, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0,0,0), 1)
                        #cv2.circle(to_draw, (xmax, ymax),1,self.class_colors[class_num],30);

            self.detection_index.header = std_msgs.msg.Header()
            self.detection_index.header.stamp=rospy.Time.now()
            print (self.detection_index)
            self.box_coordinate_pub.publish(self.detection_index)
            self.detection_index.boxes[:]=[]
            #self.detection_index.boxes.clear()
        cv2.imshow("SSD result", to_draw)
        cv2.waitKey(1)

    def main(self):
        rospy.spin()
import torch
import torchvision
import torch.nn.functional as F
import torchvision.transforms as transforms
import os
from torch.autograd import Variable
import numpy as np
from ssd import SSD300
from encoder import DataEncoder
from PIL import Image, ImageDraw
import torch.backends.cudnn as cudnn
use_cuda = torch.cuda.is_available() 

# Load model
net = SSD300()
#net = net.cuda() # for gpu
checkpoint = torch.load('/media/biometric/Data1/Ranjeet/NewPytorch/SSD_Ear_RGB_300/Model/Model_Cropped.pth')
net.load_state_dict(checkpoint['net'])
net.eval()



if use_cuda:
	print("Gpu is available")
	net = torch.nn.DataParallel(net, device_ids=[0])
	net.cuda()
	cudnn.benchmark = True

# Load test image
list_image = os.listdir('/media/biometric/Data1/Ranjeet/NewPytorch/Challenge_images')
for image in list_image:
import keras
import pickle
from videotest import VideoTest

import sys
sys.path.append("..")
from ssd import SSD300 as SSD

input_shape = (300, 300, 3)

# Change this if you run with other classes than VOC
class_names = [
    "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
    "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike",
    "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"
]
NUM_CLASSES = len(class_names)
model = SSD(input_shape, num_classes=NUM_CLASSES)

# Change this path if you want to use your own trained weights
model.load_weights('./checkpoints/weights.200-3.67.hdf5')

vid_test = VideoTest(class_names, model, input_shape)

# To test on webcam 0, remove the parameter (or change it to another number
# to test on that webcam)

vid_test.run()
Example #27
0
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    evaluate_detections(all_boxes, output_dir, dataset)


def evaluate_detections(box_list, output_dir, dataset):
    write_voc_results_file(box_list, dataset)
    do_python_eval(output_dir)


if __name__ == '__main__':
    # load net
    num_classes = len(VOC_CLASSES) + 1  # +1 background
    #net = build_ssd('test', 300, num_classes) # initialize SSD
    net = SSD300(num_classes, 'test')
    net.load_state_dict(torch.load(args.trained_model))
    net.eval()
    print('Finished loading model!')
    # load data
    dataset = VOCDetection(args.voc_root, [('2007', set_type)],
                           BaseTransform(300, dataset_mean),
                           AnnotationTransform())
    if args.cuda:
        net = net.cuda()
        cudnn.benchmark = True
    # evaluation
    test_net(args.save_folder,
             net,
             args.cuda,
             dataset,
Example #28
0
import keras
import pickle
from videotest import VideoTest

import sys
sys.path.append("..")
from ssd import SSD300 as SSD

input_shape = (300,300,3)

# Change this if you run with other classes than VOC
class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"];
NUM_CLASSES = len(class_names)

model = SSD(input_shape, num_classes=NUM_CLASSES)

# Change this path if you want to use your own trained weights
model.load_weights('../weights_SSD300.hdf5') 
        
vid_test = VideoTest(class_names, model, input_shape)

# To test on webcam 0, remove the parameter (or change it to another number
# to test on that webcam)
vid_test.run('path/to/your/video.mkv')
Example #29
0
import keras
import pickle
from videotest import VideoTest

import sys
sys.path.append("..")
from ssd import SSD300 as SSD

input_shape = (300, 300, 3)

# Change this if you run with other classes than VOC
class_names = [
    "background", "dog", "bicycle", "bird", "boat", "bottle", "bus", "car",
    "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike",
    "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"
]
NUM_CLASSES = len(class_names)

model = SSD(input_shape, num_classes=NUM_CLASSES)

# Change this path if you want to use your own trained weights
model.load_weights('../weights_SSD300.hdf5')

vid_test = VideoTest(class_names, model, input_shape)

# To test on webcam 0, remove the parameter (or change it to another number
# to test on that webcam)
vid_test.run('path/to/your/video.mkv')
Example #30
0
def get_anchor_boxes():
    model = SSD300(1)
    return model.anchors
Example #31
0
from ssd import SSD300
from ssd_utils import BBoxUtility
from ssd_training import MultiboxLoss
from pycocotools.coco import COCO
import numpy as np
from scipy.misc import imread
from scipy.misc import imresize
import pickle

model = SSD300(num_classes=81)

mbLoss = MultiboxLoss(num_classes=81)
model.compile(loss=mbLoss.compute_loss, optimizer='adam')

ssd300_priors = pickle.load(open('prior_boxes_ssd300.pkl'))
bboxer = BBoxUtility(num_classes=81, priors=ssd300_priors)

cocodata = COCO('/DATA/COCO/annotations/instances_train2017.json')
cocoDir = '/DATA/COCO/train2017/'
catsToIds = {}

for i, catid in enumerate(cocodata.getCatIds()):
    catsToIds[catid] = i


def generator(batch_size=4):
    imgList = cocodata.imgs.keys()
    imgcount = len(imgList)
    n = 0
    while True:
        X = np.zeros((batch_size, 300, 300, 3), dtype=np.float)