コード例 #1
0
    def test_function(self):
        val = np.random.random((4, 2))
        input_val = np.random.random((4, 2))

        xth = KTH.variable(val)
        xtf = KTF.variable(val)
        yth = KTH.placeholder(ndim=2)
        ytf = KTF.placeholder(ndim=2)

        exp_th = KTH.square(xth) + yth
        exp_tf = KTF.square(xtf) + ytf

        update_th = xth * 2
        update_tf = xtf * 2
        fth = KTH.function([yth], [exp_th], updates=[(xth, update_th)])
        ftf = KTF.function([ytf], [exp_tf], updates=[(xtf, update_tf)])

        function_outputs_th = fth([input_val])[0]
        function_outputs_tf = ftf([input_val])[0]
        assert function_outputs_th.shape == function_outputs_tf.shape
        assert_allclose(function_outputs_th, function_outputs_tf, atol=1e-05)

        new_val_th = KTH.get_value(xth)
        new_val_tf = KTF.get_value(xtf)
        assert new_val_th.shape == new_val_tf.shape
        assert_allclose(new_val_th, new_val_tf, atol=1e-05)
コード例 #2
0
ファイル: test_backends.py プロジェクト: CheRaissi/keras
    def test_function(self):
        val = np.random.random((4, 2))
        input_val = np.random.random((4, 2))

        xth = KTH.variable(val)
        xtf = KTF.variable(val)
        yth = KTH.placeholder(ndim=2)
        ytf = KTF.placeholder(ndim=2)

        exp_th = KTH.square(xth) + yth
        exp_tf = KTF.square(xtf) + ytf

        update_th = xth * 2
        update_tf = xtf * 2
        fth = KTH.function([yth], [exp_th], updates=[(xth, update_th)])
        ftf = KTF.function([ytf], [exp_tf], updates=[(xtf, update_tf)])

        function_outputs_th = fth([input_val])[0]
        function_outputs_tf = ftf([input_val])[0]
        assert function_outputs_th.shape == function_outputs_tf.shape
        assert_allclose(function_outputs_th, function_outputs_tf, atol=1e-05)

        new_val_th = KTH.get_value(xth)
        new_val_tf = KTF.get_value(xtf)
        assert new_val_th.shape == new_val_tf.shape
        assert_allclose(new_val_th, new_val_tf, atol=1e-05)
コード例 #3
0
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(
            self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = KTF.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                           self.anchors,
                                           len(self.class_names),
                                           self.input_image_shape,
                                           score_threshold=self.score,
                                           iou_threshold=self.iou)
        return boxes, scores, classes
コード例 #4
0
    def __init__(self, yolo, **cfgs):
        self.yolo = yolo
        self.model_path = cfgs['model_path']
        self.anchors_path = cfgs['anchors_path']
        self.classes_path = cfgs['classes_path']

        self.score_threshold = cfgs['score_threshold']
        self.iou_threshold = cfgs['iou_threshold']

        self.class_names = self._get_class()
        self.anchors = self._get_anchors()
        self.num_anchors = len(self.anchors)
        self.num_classes = len(self.class_names)
        #self.colors = self._colors()

        if cfgs['cpu']:
            config = tf.ConfigProto(device_count={"GPU": 0})
            K.set_session(tf.Session(config=config))

        self.sess = K.get_session()

        self.model_image_size = (416, 416)  # fixed size or (None, None), hw
        self.input_image_shape = K.placeholder(shape=(2, ))

        self.yolo_model = self._load_weights()
        self.boxes, self.scores, self.classes = self.yolo.yolo_eval(
            self.yolo_model.output,
            self.anchors,
            self.num_classes,
            self.input_image_shape,
            score_threshold=self.score_threshold,
            iou_threshold=self.iou_threshold)
コード例 #5
0
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors == 6  # default setting
        try:
            self.yolo_model = load_model(model_path, compile=False)
        except:
            self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
            self.yolo_model.load_weights(
                self.model_path)  # make sure model, anchors and classes match
        else:
            assert self.yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'

        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))

        np.random.shuffle(
            self.colors)  # Shuffle colors to decorrelate adjacent classes.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                           self.anchors,
                                           len(self.class_names),
                                           self.input_image_shape,
                                           score_threshold=self.score,
                                           iou_threshold=self.iou)
        return boxes, scores, classes
コード例 #6
0
    if pooltype == 1:
        return AveragePooling2D((2, 2), strides=(2, 2))(x)
    else:
        return MaxPooling2D((2, 2), strides=(2, 2))(x)


# get tensor representation of base image
base_image = K.variable(
    preprocess_image(base_image_path, True, read_mode=read_mode))
style_reference_images = []
for style_path in style_image_paths:
    style_reference_images.append(K.variable(preprocess_image(style_path)))

# this will contain our generated image
if K.image_dim_ordering() == 'th':
    combination_image = K.placeholder((1, 3, img_width, img_height))
else:
    combination_image = K.placeholder((1, img_width, img_height, 3))

image_tensor = [base_image]
for style_image_tensor in style_reference_images:
    image_tensor.append(style_image_tensor)
image_tensor.append(combination_image)

nb_tensors = len(image_tensor)
nb_style_images = nb_tensors - 2  # Content andoutput image not considered

# combine the various image into a single Keras tensor
input_tensor = K.concatenate(image_tensor, axis=0)

if K.image_dim_ordering() == 'th':
コード例 #7
0
    num_threads = os.environ.get('OMP_NUM_THREADS')
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)

    if num_threads:
        return tf.Session(config=tf.ConfigProto(
            gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))
    else:
        return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))


import keras.backend.tensorflow_backend as KTF
KTF.set_session(get_session())
target_image = KTF.constant(preprocess_image(target_image_path))
style_reference_image = KTF.constant(
    preprocess_image(style_reference_image_path))
combination_image = KTF.placeholder((1, img_height, img_width, 3))

# In[9]:

input_tensor = KTF.concatenate(
    [target_image, style_reference_image, combination_image], axis=0)

# In[10]:

model = vgg19.VGG19(input_tensor=input_tensor,
                    weights='imagenet',
                    include_top=False)
print('Model loaded.')

# In[11]:
コード例 #8
0
    def _compile_learning(self):
        # ミニバッチの状態で入力できるようにするplaceholder

        # s = K.placeholder(shape=tuple([None] + [self.history_len] + self.state_shape)) # history?
        s = K.placeholder(shape=tuple([None] + self.state_shape))
        a = K.placeholder(ndim=1, dtype='int32')
        r = K.placeholder(ndim=2, dtype='float32')
        # s2 = K.placeholder(shape=tuple([None] + [self.history_len] + self.state_shape))
        s2 = K.placeholder(shape=tuple([None] + self.state_shape))
        t = K.placeholder(ndim=1, dtype='float32')

        updates = []
        costs = 0
        # costs_arr = np.zeros(len(self.networks))
        costs_list = []
        qs = []
        q2s = []

        # 構築したネットワーク分だけ処理
        for i in range(len(self.networks)):
            local_s = s
            local_s2 = s2

            # remove_features → 未実装

            # 推論値 s: Stをinputとして
            qs.append(self.networks[i](local_s))
            # 教師値 s: St+1をinputとして
            q2s.append(self.target_networks[i](local_s2))

            if self.use_hra:
                # cost = lossの計算
                # cost = self._compute_cost(qs[-1], a, r[:, i], t, q2s[-1])
                cost = self._compute_cost(qs[-1], a, r[:, i], t, q2s[-1])

                optimizer = RMSprop(lr=self.learning_rate,
                                    rho=.95,
                                    epsilon=1e-7)

                # 学習設定
                updates += optimizer.get_updates(
                    params=self.networks[i].trainable_weights, loss=cost)
                # self.networks[i].compile(loss=cost, optimizer=optimizer)
                # costの合計
                costs += cost
                # 各costが格納されたリスト
                costs_list.append(cost)
                # costs_arr[i] = cost

        # target_netのweightを更新
        target_updates = []
        for network, target_network in zip(self.networks,
                                           self.target_networks):
            for target_weight, network_weight in zip(
                    target_network.trainable_weights,
                    network.trainable_weights):
                target_updates.append(K.update(target_weight,
                                               network_weight))  # from, to

        # kerasの関数のインスタンスを作成 updates: 更新する命令のリスト.
        # self._train_on_batch = K.function(inputs=[s, a, r, s2, t], outputs=[costs], updates=updates)
        self._train_on_batch = K.function(inputs=[s, a, r, s2, t],
                                          outputs=costs_list,
                                          updates=updates)
        self.predict_network = K.function(inputs=[s], outputs=qs)
        self.predict_target_network = K.function(inputs=[s], outputs=qs)
        self.update_weights = K.function(inputs=[],
                                         outputs=[],
                                         updates=target_updates)
コード例 #9
0
OUR_DET.append(LABELS.index('sports ball'))

our_score = [0, 0, 0]

#os.system('sudo modprobe bcm2835-v4l2')
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

model = tiny_yolo2()
graph = tf.get_default_graph()
model.load_weights(pretrained_weight_path)

with graph.as_default():
    yolo_outputs = yolo_head(graph, model.output, ANCHORS, NUM_CLASS)
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(graph,
                                       yolo_outputs,
                                       input_image_shape,
                                       score_threshold=0.1,
                                       iou_threshold=0.1)

sess = K.get_session()


def detection():
    with graph.as_default():
        t1 = time.time()

        ret, frame = cap.read()
        if not ret: