def bbox_transform_inv(boxes, deltas):
    if boxes.shape[0] == 0:
        return xp.zeros((0, deltas.shape[1]), dtype=deltas.dtype)

    boxes = boxes.astype(deltas.dtype, copy=False)

    widths = boxes[:, 2] - boxes[:, 0] + 1.0
    heights = boxes[:, 3] - boxes[:, 1] + 1.0
    ctr_x = boxes[:, 0] + 0.5 * widths
    ctr_y = boxes[:, 1] + 0.5 * heights

    dx = deltas[:, 0::4]
    dy = deltas[:, 1::4]
    dw = deltas[:, 2::4]
    dh = deltas[:, 3::4]

    pred_ctr_x = dx * widths[:, xp.newaxis] + ctr_x[:, xp.newaxis]
    pred_ctr_y = dy * heights[:, xp.newaxis] + ctr_y[:, xp.newaxis]
    pred_w = xp.exp(dw) * widths[:, xp.newaxis]
    pred_h = xp.exp(dh) * heights[:, xp.newaxis]

    pred_boxes = xp.zeros(deltas.shape, dtype=deltas.dtype)
    # x1
    pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * pred_w
    # y1
    pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * pred_h
    # x2
    pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * pred_w
    # y2
    pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * pred_h

    return pred_boxes
Beispiel #2
0
 def test_gpu(self):
     x = Variable(
         cp.zeros((1, 512, self.feat_h, self.feat_w), dtype=cp.float32))
     self.rpn.train = False
     self.rpn.to_gpu(0)
     st = time.time()
     self.rpn(x, self.img_info)
     print(time.time() - st, 'sec')
Beispiel #3
0
 def concat(self, z, y=None, ncls=10):
     bs = z.shape[0]
     if y is None:
         if self.device:
             y_ = cuda.to_gpu(
                 cp.zeros((bs, ncls)).astype(cp.float32), self.device)
             y = Variable(y_)
         else:
             y = np.zeros((bs, ncls)).astype(np.float32)
     return F.concat((z, y), axis=1)
Beispiel #4
0
def img_tuple(state,action,Reward,state_dash,episode_end):
    _state=""
    _action=cp.zeros(1, dtype=cp.uint8)
    _Reward=np.zeros((1, 1), dtype=np.float32)
    _state_dash=""
    _episode_end=np.zeros((1, 1), dtype=np.bool)
    _state=state
    _action[0]=cp.asarray(action)
    _Reward[0][0]=Reward
    _state_dash=state_dash
    _episode_end[0][0]=episode_end
    return (_state,_action,_Reward,_state_dash,_episode_end)
Beispiel #5
0
def ious_gpu_1(boxes, query_boxes):
    """Kernel function IOU computation."""
    # TODO: Fix, does not work. Not using ElementwiseKernel correct.
    n_boxes = boxes.shape[0]
    n_query_boxes = query_boxes.shape[0]

    print(n_boxes)
    print(n_query_boxes)
    print(boxes)
    print(query_boxes)

    ious = cp.zeros((n_query_boxes, n_boxes), dtype=cp.float32)

    print(ious)

    cp.ElementwiseKernel(
        '''raw float32 boxes, float32 query_boxes, raw int32 num_boxes,
    raw int32 num_query_boxes
    ''', 'raw float32 ious', '''
        for (int q = 0; q < num_query_boxes; ++q) {
            float box_area = (query_boxes[q, 2] - query_boxes[q, 0] + 1.0) *
                (query_boxes[q, 3] - query_boxes[q, 1] + 1.0);
            ious[q, 0] = q;
            for (int b = 0; b < num_boxes; ++b) {
                float iw = min(boxes[b, 2], query_boxes[q, 2]) -
                    max(boxes[b, 0], query_boxes[q, 0]) + 1.0;
                if (iw > 0.0) {
                    float ih = min(boxes[b, 3], query_boxes[q, 3]) -
                        max(boxes[b, 1], query_boxes[q, 1]) + 1.0;
                    if (ih > 0.0) {
                        float ua = (boxes[b, 2] - boxes[b, 0] + 1.0) *
                            (boxes[b, 3] - boxes[b, 1] + 1.0) +
                            box_area - (iw * ih);
                         // ious[q, b] = q;
                      //ious[q, b] = (iw * ih) / ua;
                    }
                } else {
                    ious[q, b] = -1.1;
                }
            }
        }
    ''', 'intersecion_over_unions')(boxes,
                                    query_boxes,
                                    n_boxes,
                                    n_query_boxes,
                                    ious,
                                    size=1)
    return ious
Beispiel #6
0
def _change_one_hot_label(X):
    T = xp.zeros((X.size, 10))
    for idx, row in enumerate(T):
        row[X[idx]] = 1

    return T
Beispiel #7
0
def zeros(dim):
    return Variable(xp.zeros(dim, dtype=xp.float32))
Beispiel #8
0
 def _get_zeros(self, batchsize):
     if self._zeros is None or self._zeros.shape[0] != batchsize:
         self._zeros = np.zeros(batchsize, np.int32) if self._cpu else cp.zeros(batchsize, np.int32)
     return self._zeros
Beispiel #9
0
 def handle(self):
     save_img=0
     
     f=open(args.model,'rb')
     model = pickle.load(f)
     f.close()
     cuda.get_device(args.gpu).use()
     model.to_gpu()
     
     if os.path.isfile(args.model2):
         f=open(args.model2,'rb')
         model2 = DQN.DQN_class()
         model2.model = pickle.load(f)
         model2.target_model_update()
         f.close()
        # cuda.get_device(args.gpu2).use()
         model2.model.to_gpu()
         model2.model_target.to_gpu()
     else:
         model2 = DQN.DQN_class()
        # cuda.get_device(args.gpu2).use()
         model2.model.to_gpu()
         model2.model_target.to_gpu()
     
     que_img = []
     que_img_pass = []
     #train = open('train.txt','w')
     episodeCounter=0
     print "episode "+str(episodeCounter+1)
     double_error=0
     linelist=["python compute_mean.py train.txt","python train_imagenet.py -g 0 -B 16 -b 1 -E 20 train.txt test.txt 2>&1 | tee log"]
     eps=1
     base_time=time.time()
     time_bool=True
     try:
         while True:
             self.data = self.request.recv(1024).strip()
             if time_bool:
                 time_bool=False
                 base_time=base_time-(base_time-time.time())
             self.request.send("receive")
             buf=''
             recvlen=0
             while recvlen<int(self.data): 
                 receivedstr=self.request.recv(1024*1024)
                 recvlen+=len(receivedstr)
                 buf +=receivedstr
             img=np.fromstring(buf,dtype='uint8').reshape(740-290,670-284,3)
             img_check=img[464-290:494-290,284-284:382-284,:].astype(np.float32)
             _img_check = img_check.transpose(2,0,1) - mean_image
             #rsvmsg=cv2.imdecode(rsvmsg,1)
             height, width, depth = img.shape
             new_height = size
             new_width = size
             if height > width:
                 new_width = size * width / height
             else:
                 new_height = size * height / width
             crop_height_start = ( size - new_height ) / 2
             crop_height_end = crop_height_start + new_height
             crop_width_start = ( size - new_width) / 2
             crop_width_end = crop_width_start + new_width
             resized_img = cv2.resize(img, (new_width, new_height))
             cropped_img = np.zeros((size,size,3),np.uint8)
             #cropped_img.fill(255) #white ver
             cropped_img[crop_height_start:crop_height_end,crop_width_start:crop_width_end] = resized_img
             _cropped_img = cropped_img.astype(np.float32).transpose(2,0,1)
             _cropped_img -= mean_image2
             _cropped_img /= 255
             state_dash = cp.ndarray((1, 3, size, size), dtype=cp.float32)
             state_check = cp.ndarray((1, 3, 30, 98), dtype=cp.float32)
             state_dash[0]=cp.asarray(_cropped_img)
             state_check[0]=cp.asarray(_img_check)
             action_dash = model2.e_greedy(state_dash, eps)
             _name=np.argmax(model.predict(state_check,train=False).data.get())
             print action_dash
             state_dash_path="save/img"+str(save_img).zfill(7)+".jpg"
             cv2.imwrite(state_dash_path,cropped_img)
             print "saved "+state_dash_path
             save_img+=1
             if len(que_img_pass)>0:
                 state,action=que_img_pass.pop(0)
                 Reward=(time.time()-base_time)*(1.0/10**3)
                 episode_end=False
                 if _name==-1:
                     Reward=-1
                     episode_end=True
                 que_img.append(img_tuple(state,action,Reward,state_dash_path,episode_end))
                 print Reward
             if _name==1:
                 self.request.send(str(-2))
                 self.request.recv(1024)
                 if episodeCounter==19:
                     while len(que_img)>100000:
                         que_img.pop(0)
                     eps=1
                     DQN_batch_size=1
                     _state=cp.zeros((DQN_batch_size, 3, 224, 224),dtype=cp.float32)
                     _action=cp.zeros(DQN_batch_size, dtype=cp.uint8)
                     _Reward=np.zeros((DQN_batch_size, 1), dtype=np.float32)
                     _state_dash=cp.zeros((DQN_batch_size, 3, 224, 224), dtype=cp.float32)
                     _episode_end=np.zeros((DQN_batch_size, 1), dtype=np.bool)
                     for epoch in range(5):
                         print "epoch"+str(epoch+1)
                         counter=0
                         perm = np.random.permutation(len(que_img))
                         for idx in perm:
                             s_replay,a_replay,r_replay,s_dash_replay,episode_end_replay=que_img[idx]
                             _state[counter:counter+1]=load_img(s_replay)
                             _action[counter:counter+1]=a_replay
                             _Reward[counter:counter+1]=r_replay
                             _state_dash[counter:counter+1]=load_img(s_dash_replay)
                             _episode_end[counter:counter+1]=episode_end_replay
                             if counter==DQN_batch_size-1:
                                 counter=0
                                 model2.optimizer.zero_grads()
                                 loss, _ = model2.forward(_state,_action,_Reward,_state_dash,_episode_end)
                                 loss.backward()
                                 model2.optimizer.update()
                                 del loss
                             else:
                                 counter+=1
                             del s_replay,a_replay,r_replay,s_dash_replay,episode_end_replay
                     episodeCounter=0
                     pickle.dump(model2.model, open(args.model2, 'wb'), -1)
                     model2.target_model_update()
                     #model2.model_target.to_gpu()
                 else:
                     episodeCounter+=1
                 base_time=time.time()
                 time_bool=True
                 print "episode "+str(episodeCounter+1)
                 self.request.send("OK")
             else:
                 que_img_pass.append(img_pass_tuple(state_dash_path,action_dash))
                 self.request.send(str(action_dash))
                 eps -= 5.0/10**4
                 print eps
                 if eps < 0.1:
                     eps = 0.1
     except KeyboardInterrupt:
         pass
Beispiel #10
0
def img_pass_tuple(state,action):
    _state=""
    _action=cp.zeros(1, dtype=cp.uint8)
    _state=state
    _action[0]=cp.asarray(action)
    return (_state,_action)
Beispiel #11
0
 def _get_zeros(self, batchsize):
     if self._zeros is None or self._zeros.shape[0] != batchsize:
         self._zeros = np.zeros(batchsize,
                                np.int32) if self._cpu else cp.zeros(
                                    batchsize, np.int32)
     return self._zeros