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

        ret, frame = cap.read()
        if not ret:
            print 'Not Found Devices.'
        print 'Video capture successed.'

        frame = frame[32:448, 112:528, :]
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.GaussianBlur(frame, (3, 3), 0)
        kernel = np.array(([0, -1, 0], [-1, 5, -1], [0, -1, 0]), dtype="int")
        frame = cv2.filter2D(frame, -1, kernel)

        frame = np.divide(frame, 255.)
        frame = np.expand_dims(frame, 0)

        t2 = time.time()
        print 'Video capture and preprocess takes {0} seconds.'.format(t2 - t1)

        t1 = time.time()

        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model.input: frame,
                input_image_shape: [416, 416],
                K.learning_phase(): 0
            })

        t2 = time.time()

        print 'Detection takes {0} seconds.'.format(t2 - t1)
        return out_boxes, out_scores, out_classes
Esempio n. 2
0
 def output(self, image_data, input_image_shape):
     feed_dict = {
         self.yolo_model.input: image_data,
         self.input_image_shape: input_image_shape,
         K.learning_phase(): 0
     }
     out_boxes, out_scores, out_classes = self.sess.run(
         [self.boxes, self.scores, self.classes], feed_dict=feed_dict)
     return out_boxes, out_scores, out_classes
Esempio n. 3
0
 def _get_nth_layer_output(self, model, n_layer, X, train=1):
     '''
     Returns output of nth layer in a given model.
     :param model: keras model to get an intermediate value out of
     :param n_layer: the layer number to get the value of
     :param X: input data for which layer value should be computed and returned.
     :param train: (1/0): 1 to use the same setting as training (for example, with Dropout, etc.), 0 to use the same setting as testing phase for the model.
     :return the value of n_layer in the given model, input, and setting
     '''
     get_nth_layer_output = K.function(
         [model.layers[0].input, K.learning_phase()],
         [model.layers[n_layer].output])
     return get_nth_layer_output([X, train])[0]
Esempio n. 4
0
    def detect_image(self, image):
        if self.is_fixed_size:
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                KTF.learning_phase(): 0
            })
        return_boxs = []
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            if predicted_class != 'person':
                continue
            box = out_boxes[i]
            # score = out_scores[i]
            x = int(box[1])
            y = int(box[0])
            w = int(box[3] - box[1])
            h = int(box[2] - box[0])
            if x < 0:
                w = w + x
                x = 0
            if y < 0:
                h = h + y
                y = 0
            return_boxs.append([x, y, w, h])

        return return_boxs
Esempio n. 5
0
def get_activations(model,
                    data,
                    layer,
                    batch_size=256,
                    flatten=True,
                    cropsize=0,
                    verbose=0):
    #    get_layer_output = K.function([model.layers[0].input, K.learning_phase()],
    #                                      [model.layers[layername].output if type(layername) is type(int) else model.get_layer(layername).output])
    #

    if type(layer) is str:
        layerindex = None
        layername = layer
    else:
        layerindex = layer
        layername = None


#    print (layername, layerindex)

    get_layer_output = K.function(
        [model.layers[0].input, K.learning_phase()],
        [model.get_layer(name=layername, index=layerindex).output])

    activations = []
    batch_size = int(batch_size)
    for i in tqdm(range(int(np.ceil(len(data) / batch_size))),
                  desc='Feature extraction') if verbose == 1 else range(
                      int(np.ceil(len(data) / batch_size))):
        batch = np.array(data[i * batch_size:(i + 1) * batch_size],
                         dtype=np.float32)
        if cropsize != 0:
            diff = (batch.shape[1] - cropsize) // 2
            batch = batch[:, diff:-diff, :]
        act = get_layer_output([batch, 0])[0]
        activations.extend(act)
    activations = np.array(activations, dtype=np.float32)
    if flatten: activations = activations.reshape([len(activations), -1])
    return activations
Esempio n. 6
0
                def __init__(self):
                        import keras.backend.tensorflow_backend as K
                        config = tf.ConfigProto()
                        config.gpu_options.allow_growth = True
                        self.session = tf.Session(config=config)
                        K.set_session(self.session)
                        K.manual_variable_initialization(True)
                        self.lp = K.learning_phase()
                        
                        self.model = build_model()
                        self.model._make_predict_function()
                        self.model.summary()

                        self.predict_model = build_model()
                        self.predict_model._make_predict_function()
        
                        self.graph = self._build_graph(self.model)
                        self.popart = self.popart_ops()
                        self.session.run(tf.global_variables_initializer())
                        self.default_graph = tf.get_default_graph()
                        self.sync_weight()
                        self.tensorboard_setting()
                        self.default_graph.finalize()
                        self.taskqueue = queue.Queue(maxsize=16)
Esempio n. 7
0
 def __enter__(self):
     self.learning_phase_placeholder = K.learning_phase()
     K.set_learning_phase(self.value)
    def detect_image(self, image):
        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = image_preporcess(
                np.copy(image), tuple(reversed(self.model_image_size)))
            image_data = boxed_image

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape:
                [image.shape[0],
                 image.shape[1]],  #[image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        #print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        thickness = (image.shape[0] + image.shape[1]) // 600
        fontScale = 1
        ObjectsList = []

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            #label = '{}'.format(predicted_class)
            scores = '{:.2f}'.format(score)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.shape[0],
                         np.floor(bottom + 0.5).astype('int32'))
            right = min(image.shape[1], np.floor(right + 0.5).astype('int32'))

            mid_h = (bottom - top) / 2 + top
            mid_v = (right - left) / 2 + left

            # put object rectangle
            cv2.rectangle(image, (left, top), (right, bottom), self.colors[c],
                          thickness)

            # get text size
            (test_width, text_height), baseline = cv2.getTextSize(
                label, cv2.FONT_HERSHEY_SIMPLEX, thickness / self.text_size, 1)

            # put text rectangle
            cv2.rectangle(image, (left, top),
                          (left + test_width, top - text_height - baseline),
                          self.colors[c],
                          thickness=cv2.FILLED)

            # put text above rectangle
            cv2.putText(image, label, (left, top - 2),
                        cv2.FONT_HERSHEY_SIMPLEX, thickness / self.text_size,
                        (0, 0, 0), 1)

            # add everything to list
            ObjectsList.append(
                [top, left, bottom, right, mid_v, mid_h, label, scores])

        return image, ObjectsList
Esempio n. 9
0
    def gan(self):
    # initialize a GAN trainer

    # this is the fastest way to train a GAN in Keras
    # two models are updated simutaneously in one pass

        noise = Input(shape=self.generator.input_shape[1:])
        real_data = Input(shape=self.discriminator.input_shape[1:])

        generated = self.generator(noise)
        gscore = self.discriminator(generated)
        rscore = self.discriminator(real_data)

        def log_eps(i):
            return K.log(i+1e-11)

        # single side label smoothing: replace 1.0 with 0.9
        dloss = - K.mean(log_eps(1-gscore) + .1 * log_eps(1-rscore) + .9 * log_eps(rscore))
        gloss = - K.mean(log_eps(gscore))

        Adam = tf.train.AdamOptimizer

        lr,b1 = 1e-4,.2 # otherwise won't converge.
        optimizer = Adam(lr)

        grad_loss_wd = optimizer.compute_gradients(dloss, self.discriminator.trainable_weights)
        update_wd = optimizer.apply_gradients(grad_loss_wd)

        grad_loss_wg = optimizer.compute_gradients(gloss, self.generator.trainable_weights)
        update_wg = optimizer.apply_gradients(grad_loss_wg)

        def get_internal_updates(model):
            # get all internal update ops (like moving averages) of a model
            inbound_nodes = model.inbound_nodes
            input_tensors = []
            for ibn in inbound_nodes:
                input_tensors+= ibn.input_tensors
            updates = [model.get_updates_for(i) for i in input_tensors]
            return updates

        other_parameter_updates = [get_internal_updates(m) for m in [self.discriminator,self.generator]]
        # those updates includes batch norm.

        print('other_parameter_updates for the models(mainly for batch norm):')
        print(other_parameter_updates)

        train_step = [update_wd, update_wg, other_parameter_updates]
        losses = [dloss,gloss]

        learning_phase = K.learning_phase()

        def gan_feed(sess,batch_image,z_input):
            # actual GAN trainer
            nonlocal train_step,losses,noise,real_data,learning_phase

            res = sess.run([train_step,losses],feed_dict={
            noise:z_input,
            real_data:batch_image,
            learning_phase:True,
            # Keras layers needs to know whether
            # this run is training or testring (you know, batch norm and dropout)
            })

            loss_values = res[1]
            return loss_values #[dloss,gloss]

        return gan_feed