Exemplo n.º 1
0
    def valid_one_epoch(self, sess, data_loader, data_num, epoch, step):
        '''
        Generate once of image which remove instruments.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        total_loss, total_im_warp_loss, total_flow_loss = 0, 0, 0
        total_l1, total_im_l1, total_l2, total_im_l2, total_psnr = 0, 0, 0, 0, 0
        count = 0
        t0 = time.time()

        im_warp_loss, flow_loss = 0, 0

        #True number to loss
        t_count = 0

        for count in range(1, data_num):
            step += 1

            images, images_da, mask, fn, flag, rect_param = data_loader.get_next_sequence_valid(
            )
            if flag == False:
                #print(fn)
                # Can't find prev data.
                continue
            # Origin optical flow
            for frame in range(1, cfgs.seq_frames):
                im, last_im = images[frame], images[frame - 1]
                flow = sess.run(self.flow_tensor,
                                feed_dict={
                                    self.flow_img0: im,
                                    self.flow_img1: last_im
                                })
            # Da optical flow
            for frame in range(1, cfgs.seq_frames):
                im, last_im = images_da[frame], images_da[frame - 1]
                flow_da = sess.run(self.flow_tensor,
                                   feed_dict={
                                       self.flow_img0: im,
                                       self.flow_img1: last_im
                                   })

            normal_flow, normal_max_v = normal_data(flow)
            inpt_input, flag_ = concat_data(normal_flow, mask, cfgs.grid)
            #epd_sd_mask, epd_rect_param = expand_sdm(rect_param, cfgs.gamma, cfgs.epd_ratio)
            sd_mask = sdm(rect_param, cfgs.gamma)
            if flag_ == False:
                print(fn)
                #After grid, area of mask = 0
                continue

            t_count += 1
            loss, im_warp_loss, flow_loss,\
            l_inpt_p, \
            l1_e, l2_e, im_l1_e, im_l2_e, psnr,\
            l_ori_im, l_warped_im,\
            inpt_warped_im, inpt_flow, pred_flow \
            =sess.run([self.loss, self.im_warp_loss, self.flow_loss_,\
                       self.local_patch_inpt_flow, \
                       self.l1_e, self.l2_e, self.w_l1_e, self.w_l2_e, self.w_psnr,\
                       self.local_ori_im, self.local_warped_im,\
                       self.inpt_warped_im, self.inpt_pred_flow, self.pred_complete_flow],\
                                               feed_dict={self.flow_img1: last_im,
                                                          self.flow_img0: im,
                                                          self.ori_cur_im: images[1],
                                                          self.ori_flow: flow,
                                                          self.max_v: normal_max_v,
                                                          self.inpt_data: inpt_input,
                                                          #self.epd_sd_mask: epd_sd_mask,
                                                          #self.epd_rect_param: epd_rect_param,
                                                          self.sd_mask: sd_mask,
                                                          self.rect_param: rect_param})

            #if count % 10 == 0:
            if False:
                self.view(np.expand_dims(fn, 0),
                          inpt_warped_im,
                          images_da[cfgs.seq_frames - 2],
                          images_da[cfgs.seq_frames - 1],
                          step,
                          f_path='valid')

                self.view_patch_one(l_ori_im[0],
                                    fn,
                                    step,
                                    'l_ori',
                                    f_path='valid')
                self.view_patch_one(l_warped_im[0],
                                    fn,
                                    step,
                                    'l_warped',
                                    f_path='valid')
                self.view_flow_patch_one(l_inpt_p[0],
                                         fn,
                                         step,
                                         'l_inpt_p',
                                         f_path='valid')
                self.view_flow_one(flow[0], fn, step, f_path='valid')
                self.view_flow_one(inpt_flow[0], fn, step, '_inpt', 'valid')
                self.view_flow_one(flow_da[0], fn, step, '_da', 'valid')
                self.view_flow_one(pred_flow[0],
                                   fn,
                                   step,
                                   'complete',
                                   f_path='valid')

            #3. calculate loss
            total_loss += loss
            total_im_warp_loss += im_warp_loss
            total_flow_loss += flow_loss
            total_l1 += l1_e
            total_im_l1 += im_l1_e
            total_l2 += l2_e
            total_im_l2 += im_l2_e
            total_psnr += psnr

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            #line = 'Valid epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t m_imW_loss = %.4f\t m_f_loss = %.4f\t time = %.2f' % (epoch, cfgs.inpt_lr, step, count, loss, (total_loss/t_count), (total_im_warp_loss/t_count), (total_flow_loss/t_count), time_per_batch)
            line = 'Valid epoch %2d\t step = %4d\t count = %4d\t m_imW_loss = %.2f\t m_f_loss = %.2f\t l1_e = %.3f\t im_l1_e = %.3f\t l2_e = %.3f\t im_l2_e = %.3f\t psnr = %4f\t' % (
                epoch, step, count, (total_im_warp_loss / t_count),
                (total_flow_loss / t_count), (total_l1 / count),
                (total_im_l1 / count), (total_l2 / count),
                (total_im_l2 / count), (total_psnr / count))

            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t m_imW_loss = %.4f\t m_f_loss = %.4f\t '
            % (epoch, cfgs.inpt_lr, (total_loss / t_count),
               (total_im_warp_loss / t_count), (total_flow_loss / t_count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 2
0
    def valid_once(self, sess, data_loader, data_num, epoch, step):
        '''
        Generate once training or validation.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        sum_acc = 0
        sum_acc_iou = 0
        sum_acc_ellip = 0
        count = 0
        total_loss = 0
        t0 = time.time()
        mean_acc = 0
        mean_acc_iou = 0
        mean_acc_label = 0
        mean_acc_ellip = 0

        for count in range(1, data_num):
            step += 1

            images, images_pad, gt, fn = data_loader.get_next_sequence()

            # Optical flow
            optflow = []
            for frame in range(1, cfgs.seq_frames):
                im, last_im = images[frame], images[frame - 1]
                flow = sess.run(self.flow_tensor,
                                feed_dict={
                                    self.flow_img0: im,
                                    self.flow_img1: last_im
                                })
                optflow.append(flow)

            # Static segmentation(logits)
            static_segm = []
            for frame in range(cfgs.seq_frames):
                im = images_pad[frame]
                x_logits = sess.run(self.static_output,
                                    feed_dict={self.unet_images: im})

                static_segm.append(x_logits)

            # GRFP
            rnn_input = {
                self.gru_lr: cfgs.grfp_lr,
                self.gru_input_images_tensor: np.stack(images),
                self.gru_input_flow_tensor: np.stack(optflow),
                self.gru_input_seg_tensor: np.stack(static_segm),
                self.gru_targets: gt
            }

            loss, pred, pred_pro,\
            self.accu, self.accu_iou \
            = sess.run([self.gru_loss, self.gru_pred, self.gru_pred_pro,
                        self.accu_tensor, self.accu_iou_tensor],
                        feed_dict=rnn_input)

            #self.view(fn, pred, pred_pro, images[cfgs.seq_frames], step)

            #2. calculate accurary
            self.ellip_acc = 0
            #self.calculate_acc(images[cfgs.seq_frames, 2:cfgs.ANNO_IMAGE_SIZE[0]+2, :, :].copy(), fn, pred, pred_pro, gt, ellip_infos_, if_epoch=if_epoch)
            sum_acc += self.accu
            sum_acc_iou += self.accu_iou
            sum_acc_ellip += self.ellip_acc
            mean_acc = sum_acc / count
            mean_acc_iou = sum_acc_iou / count
            mean_acc_ellip = sum_acc_ellip / count
            #3. calculate loss
            total_loss += loss
            pdb.set_trace()
            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            line = 'Valid epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t acc = %.2f%%\t iou_acc = %.2f%%\t ellip_acc = %.2f\t time = %.2f' % (
                epoch, self.learning_rate, step, count, loss,
                (total_loss / count), mean_acc, mean_acc_iou, mean_acc_ellip,
                time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t train_acc = %.2f%%\t train_iou_acc = %.2f%%\t train_ellip_acc = %.2f'
            % (epoch, self.learning_rate, (total_loss / count),
               (sum_acc / count), (sum_acc_iou / count),
               (sum_acc_ellip / count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 3
0
    def valid_one_epoch(self, sess, data_loader, data_num, epoch, step):
        '''
        Generate once of image which remove instruments.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        sum_acc, sum_acc_iou, sum_acc_ellip, total_loss = 0, 0, 0, 0
        count = 0
        t0 = time.time()
        mean_acc, mean_acc_iou, mean_acc_label, mean_acc_ellip = 0, 0, 0, 0
        self.ellip_acc, self.accu, self.accu_iou, loss = 0, 0, 0, 0

        for count in range(1, data_num):
            step += 1

            images, images_da, mask, fn, flag, mask_sum = data_loader.get_next_sequence(
            )
            if flag == False:
                print(fn)
                # Can't find prev data.
                continue
            # Origin optical flow
            for frame in range(1, cfgs.seq_frames):
                im, last_im = images[frame], images[frame - 1]
                flow = sess.run(self.flow_tensor,
                                feed_dict={
                                    self.flow_img0: im,
                                    self.flow_img1: last_im
                                })
            # Da optical flow
            for frame in range(1, cfgs.seq_frames):
                im, last_im = images_da[frame], images_da[frame - 1]
                flow_da = sess.run(self.flow_tensor,
                                   feed_dict={
                                       self.flow_img0: im,
                                       self.flow_img1: last_im
                                   })

            normal_flow, normal_max_v = normal_data(flow)
            inpt_input, flag_ = concat_data(normal_flow, mask, cfgs.grid)
            if flag_ == False:
                print(fn)
                #After grid, area of mask = 0
                continue



            loss, inpt_warped_im, inpt_flow, pred_flow =sess.run([self.loss, \
                                        self.inpt_warped_im, self.inpt_pred_flow, self.pred_complete_flow],\
                                               feed_dict={self.flow_img1: last_im,
                                                          self.flow_img0: im,
                                                          self.ori_flow: flow,
                                                          self.max_v: normal_max_v,
                                                          self.inpt_data: inpt_input})
            #if count % 20 == 0:
            if True:
                self.view(np.expand_dims(fn, 0),
                          inpt_warped_im,
                          images_da[cfgs.seq_frames - 2],
                          images_da[cfgs.seq_frames - 1],
                          step,
                          f_path='valid')

                self.view_flow_one(flow[0], fn, step, f_path='valid')
                self.view_flow_one(inpt_flow[0], fn, step, '_inpt', 'valid')
                self.view_flow_one(flow_da[0], fn, step, '_da', 'valid')
                self.view_flow_one(pred_flow[0],
                                   fn,
                                   step,
                                   'complete',
                                   f_path='valid')

            #2. calculate accurary
            self.ellip_acc = 0
            sum_acc += self.accu
            sum_acc_iou += self.accu_iou
            sum_acc_ellip += self.ellip_acc
            mean_acc = sum_acc / count
            mean_acc_iou = sum_acc_iou / count
            mean_acc_ellip = sum_acc_ellip / count
            #3. calculate loss
            total_loss += loss

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            line = 'Valid epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t  max_v = %.2f\t fn = %s\t time = %.2f' % (
                epoch, cfgs.inpt_lr, step, count, loss,
                (total_loss / count), normal_max_v, fn, time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t train_acc = %.2f%%\t train_iou_acc = %.2f%%\t train_ellip_acc = %.2f'
            % (epoch, cfgs.inpt_lr, (total_loss / count), (sum_acc / count),
               (sum_acc_iou / count), (sum_acc_ellip / count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 4
0
    def train_one_epoch(self, sess, data_loader, data_num, epoch, step):
        '''
        Generate once training or validation.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        sum_acc = 0
        sum_acc_iou = 0
        sum_acc_ellip = 0
        count = 0
        total_loss = 0
        t0 = time.time()
        mean_acc = 0
        mean_acc_iou = 0
        mean_acc_label = 0
        mean_acc_ellip = 0

        self.ellip_acc, self.accu, self.accu_iou, loss = 0, 0, 0, 0

        for count in range(1, data_num):
            step += 1

            images, images_pad, gt, fn = data_loader.get_next_sequence()

            # Optical flow
            optflow = []
            for frame in range(1, cfgs.seq_frames):
                im, last_im = images[frame], images[frame - 1]
                flow = sess.run(self.flow_tensor,
                                feed_dict={
                                    self.flow_img0: im,
                                    self.flow_img1: last_im
                                })

                optflow.append(flow)

            # Static segmentation(logits)
            static_segm = []
            for frame in range(cfgs.seq_frames):
                im = images_pad[frame]
                x_logits, x, x_pro = sess.run(
                    [self.static_output, self.static_anno_pred, self.unet_pro],
                    feed_dict={self.unet_images: im})

                static_segm.append(x_logits)

            # GRFP
            rnn_input = {
                self.gru_lr: cfgs.grfp_lr,
                self.gru_input_images_tensor: np.stack(images),
                self.gru_input_flow_tensor: np.stack(optflow),
                self.gru_input_seg_tensor: np.stack(static_segm),
                self.gru_targets: gt
            }

            prev_warped, I_diff, pro_prev_warped, anno_prev_warped, \
            cur_pro = \
            sess.run([self.gru_prev_warped, self.gru_I_diff, self.gru_pro_prev_warped, self.gru_anno_prev_warped, \
                      self.gru_pred_pro],
                                    feed_dict=rnn_input)

            #pdb.set_trace()
            self.view(np.expand_dims(fn, 0), anno_prev_warped,
                      images[cfgs.seq_frames - 2], images[cfgs.seq_frames - 1],
                      step)
            #self.view(np.expand_dims(fn, 0), prev_warping, images[cfgs.seq_frames-2], images[cfgs.seq_frames-1], step)

            filter_input = {
                self.inst_mask_pro: pro_prev_warped,
                self.corn_mask_pro: cur_pro
            }
            persist_im = sess.run(self.persist_im, feed_dict=filter_input)
            cv2.imwrite(os.path.join('persist_inter1', '%s_persist.bmp' % fn),
                        persist_im[0] * 255)
            #pdb.set_trace()

            #2. calculate accurary
            self.ellip_acc = 0
            #self.acc, self.acc_iou = 0
            #self.calculate_acc(images[cfgs.seq_frames, 2:cfgs.ANNO_IMAGE_SIZE[0]+2, :, :].copy(), fn, pred, pred_pro, gt, ellip_infos_, if_epoch=if_epoch)
            sum_acc += self.accu
            sum_acc_iou += self.accu_iou
            sum_acc_ellip += self.ellip_acc
            mean_acc = sum_acc / count
            mean_acc_iou = sum_acc_iou / count
            mean_acc_ellip = sum_acc_ellip / count
            #3. calculate loss
            total_loss += loss

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            line = 'Train epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t acc = %.2f%%\t iou_acc = %.2f%%\t ellip_acc = %.2f\t time = %.2f' % (
                epoch, self.learning_rate, step, count, loss,
                (total_loss / count), mean_acc, mean_acc_iou, mean_acc_ellip,
                time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t train_acc = %.2f%%\t train_iou_acc = %.2f%%\t train_ellip_acc = %.2f'
            % (epoch, self.learning_rate, (total_loss / count),
               (sum_acc / count), (sum_acc_iou / count),
               (sum_acc_ellip / count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 5
0
    def train_one_epoch_warp_inpt_flow(self, sess, data_loader, data_num,
                                       epoch, step):
        '''
        Generate one epoch of images using inpatinting flow. 
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''

        sum_acc, sum_acc_iou, sum_acc_ellip, count, total_loss = 0, 0, 0, 0, 0
        t0 = time.time()
        mean_acc, mean_acc_iou, mean_acc_label, mean_acc_ellip = 0, 0, 0, 0
        self.ellip_acc, self.accu, self.accu_iou, loss = 0, 0, 0, 0

        for count in range(1, data_num):
            step += 1

            images, images_pad, gt, fn = data_loader.get_next_sequence()

            # Static segmentation(logits)
            static_segm = []
            static_anno = []
            static_corn_mask = []
            static_corn_mask_im = []
            for frame in range(cfgs.seq_frames):
                im = images_pad[frame]
                im_ori = images[frame]
                x_logits, x, x_pro, corn_mask, corn_mask_im, inst_mask = sess.run(
                    [
                        self.static_output, self.static_anno_pred,
                        self.unet_pro, self.corn_mask, self.corn_mask_im,
                        self.no_inst_mask
                    ],
                    feed_dict={
                        self.unet_images: im,
                        self.persist_im: im_ori
                    })
                x = np.expand_dims(x, 2)
                x3 = x
                x3 = np.append(x3, x, axis=2)
                x3 = np.expand_dims(np.append(x3, x, axis=2), axis=0)
                static_segm.append(x_logits)
                static_anno.append(x3)
                static_corn_mask.append(corn_mask)
                static_corn_mask_im.append(corn_mask_im)

            #flow inpainting
            path = os.path.join(cfgs.inpt_flow_pickle, '%s.pickle' % fn)
            if os.path.exists(path):
                flow_inpt = self.load_pickle(path)
                flow_inpt = np.pad(flow_inpt, ((0, 0), (0, 4), (0, 0), (0, 0)),
                                   'constant')
            else:
                continue
            # Optical flow
            optflow = []
            optflow_no_inst = []
            for frame in range(1, cfgs.seq_frames):
                #Origin Image
                im, last_im = images[frame], images[frame - 1]
                #Segmentation
                segm, last_segm = static_segm[frame], static_segm[frame - 1]
                #Anno
                #im, last_im = static_anno[frame], static_anno[frame-1]

                flow, warped, flow_no_inst, flow_comb, flow_insect,\
                warped_no_inst, in_area,\
                warped_inpt, warped_insect_inpt = sess.run(\
                                [self.flow_tensor, self.warped_im, \
                                self.no_inst_flow, self.flow_comb, self.pred_corn_flow, \
                                self.no_inst_warped_segm, self.insect_area,\
                                self.result_warped, self.insect_result_warped],
                                feed_dict={self.flow_img0: im,
                                           self.flow_img1: last_im,
                                           self.flow_inst_mask: inst_mask,
                                           self.flow_corn_mask: corn_mask,
                                           self.flow_prev_corn_mask: static_corn_mask[cfgs.seq_frames-2],
                                           self.flow_segm: last_segm,
                                           self.flow_warp_img1: last_segm,
                                           self.flow_warp_img0: segm,
                                           self.warp_flow: flow_inpt})

                optflow.append(flow_comb)
                optflow_no_inst.append(flow_no_inst)

            #self.view(np.expand_dims(fn, 0), warped_no_inst, images[cfgs.seq_frames-2], images[cfgs.seq_frames-1], step, '_no_inst')
            #self.view(np.expand_dims(fn, 0), warped_no_inst*127, (static_anno[cfgs.seq_frames-2]*127).astype(np.uint8), (static_anno[cfgs.seq_frames-1]*127).astype(np.uint8), step)
            #self.view(np.expand_dims(fn, 0), warped_inpt*127, (static_anno[cfgs.seq_frames-2]*127).astype(np.uint8), (static_anno[cfgs.seq_frames-1]*127).astype(np.uint8), step, '_inpt', if_more=False)
            self.view(
                np.expand_dims(fn, 0),
                warped_insect_inpt * 255,
                (static_anno[cfgs.seq_frames - 2] * 127).astype(np.uint8),
                (static_anno[cfgs.seq_frames - 1] * 127).astype(np.uint8),
                step,
                '_insect_inpt',
                if_more=True)

            self.view_flow_one(flow_inpt[0], fn, step, '_inpt')
            #self.view_flow_one(flow_no_inst[0], fn, step, '_no_inst')
            #self.view_flow_one(flow_insect[0], fn, step, '_insect')
            self.view_flow_one(flow[0], fn, step)
            #self.view_inst_mask_one((255-inst_mask*255).astype(np.uint8), fn, step)

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            line = 'Train epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t acc = %.2f%%\t iou_acc = %.2f%%\t ellip_acc = %.2f\t time = %.2f' % (
                epoch, self.learning_rate, step, count, loss,
                (total_loss / count), mean_acc, mean_acc_iou, mean_acc_ellip,
                time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t train_acc = %.2f%%\t train_iou_acc = %.2f%%\t train_ellip_acc = %.2f'
            % (epoch, self.learning_rate, (total_loss / count),
               (sum_acc / count), (sum_acc_iou / count),
               (sum_acc_ellip / count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 6
0
    def train_one_epoch_remv_occ_segm(self, sess, data_loader, data_num, epoch,
                                      step):
        '''
        Generate once of image which remove instruments.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        sum_acc = 0
        sum_acc_iou = 0
        sum_acc_ellip = 0
        count = 0
        total_loss = 0
        t0 = time.time()
        mean_acc = 0
        mean_acc_iou = 0
        mean_acc_label = 0
        mean_acc_ellip = 0

        self.ellip_acc, self.accu, self.accu_iou, loss = 0, 0, 0, 0

        for count in range(1, data_num):
            step += 1

            images, images_pad, gt, fn = data_loader.get_next_sequence()

            # Static segmentation(logits)
            static_segm = []
            static_anno = []
            static_corn_mask = []
            static_corn_mask_im = []
            for frame in range(cfgs.seq_frames):
                im = images_pad[frame]
                im_ori = images[frame]
                x_logits, x, x_pro, corn_mask, corn_mask_im, inst_mask = sess.run(
                    [
                        self.static_output, self.static_anno_pred,
                        self.unet_pro, self.corn_mask, self.corn_mask_im,
                        self.no_inst_mask
                    ],
                    feed_dict={
                        self.unet_images: im,
                        self.persist_im: im_ori
                    })
                x = np.expand_dims(x, 2)
                x3 = x
                x3 = np.append(x3, x, axis=2)
                x3 = np.expand_dims(np.append(x3, x, axis=2), axis=0)

                if cfgs.if_dilate:
                    inst_mask = self.dilate_(inst_mask)

                static_segm.append(x_logits)
                static_anno.append(x3)
                static_corn_mask.append(corn_mask)
                static_corn_mask_im.append(corn_mask_im)

            # Optical flow
            optflow = []
            optflow_no_inst = []
            for frame in range(1, cfgs.seq_frames):
                #Origin Image
                im, last_im = images[frame], images[frame - 1]
                #Segmentation
                segm, last_segm = static_segm[frame], static_segm[frame - 1]
                #Anno
                #im, last_im = static_anno[frame], static_anno[frame-1]

                flow, warped, flow_no_inst, flow_comb, flow_insect,\
                warped_no_inst, in_area,\
                flow_mean = sess.run(\
                                [self.flow_tensor, self.warped_im, \
                                self.no_inst_flow, self.flow_comb, self.pred_corn_flow, \
                                self.no_inst_warped_segm, self.insect_area,\
                                self.corn_flow_mean],
                                feed_dict={self.flow_img0: im,
                                           self.flow_img1: last_im,
                                           self.flow_inst_mask: inst_mask,
                                           self.flow_corn_mask: corn_mask,
                                           self.flow_prev_corn_mask: static_corn_mask[cfgs.seq_frames-2],
                                           self.flow_segm: last_segm})

                optflow.append(flow_comb)
                optflow_no_inst.append(flow_no_inst)

            #self.view(np.expand_dims(fn, 0), warped_no_inst, images[cfgs.seq_frames-2], images[cfgs.seq_frames-1], step, '_no_inst')
            #self.view(np.expand_dims(fn, 0), warped_no_inst*127, (static_anno[cfgs.seq_frames-2]*127).astype(np.uint8), (static_anno[cfgs.seq_frames-1]*127).astype(np.uint8), step)
            #self.view(np.expand_dims(fn, 0), warped*127, (static_anno[cfgs.seq_frames-2]*127).astype(np.uint8), (static_anno[cfgs.seq_frames-1]*127).astype(np.uint8), step)

            #self.view_flow_one(flow_comb[0], fn, step, 'comb')
            self.view_flow_one(flow_no_inst[0], fn, step, '_no_inst')
            #self.view_flow_one(flow_insect[0], fn, step, '_insect')
            self.view_flow_one(flow[0], fn, step)
            self.view_inst_mask_one((255 - inst_mask * 255).astype(np.uint8),
                                    fn, step)
            self.pickle_f(flow_no_inst[0], fn)

            #2. calculate accurary
            self.ellip_acc = 0
            #self.acc, self.acc_iou = 0
            #self.calculate_acc(images[cfgs.seq_frames, 2:cfgs.ANNO_IMAGE_SIZE[0]+2, :, :].copy(), fn, pred, pred_pro, gt, ellip_infos_, if_epoch=if_epoch)
            sum_acc += self.accu
            sum_acc_iou += self.accu_iou
            sum_acc_ellip += self.ellip_acc
            mean_acc = sum_acc / count
            mean_acc_iou = sum_acc_iou / count
            mean_acc_ellip = sum_acc_ellip / count
            #3. calculate loss
            total_loss += loss

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            line = 'Train epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t acc = %.2f%%\t iou_acc = %.2f%%\t ellip_acc = %.2f\t time = %.2f' % (
                epoch, self.learning_rate, step, count, loss,
                (total_loss / count), mean_acc, mean_acc_iou, mean_acc_ellip,
                time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t train_acc = %.2f%%\t train_iou_acc = %.2f%%\t train_ellip_acc = %.2f'
            % (epoch, self.learning_rate, (total_loss / count),
               (sum_acc / count), (sum_acc_iou / count),
               (sum_acc_ellip / count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 7
0
    def valid_one_epoch_warped_im(self, sess, data_loader, data_num, epoch,
                                  step):
        '''
        Using warped prev image to do new segmentation.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        total_loss, total_im_warp_loss, total_flow_loss = 0, 0, 0
        count = 0
        t0 = time.time()

        im_warp_loss, flow_loss, loss = 0, 0, 0

        #True number to loss
        t_count = 0

        for count in range(1, data_num):
            step += 1

            #1. Load data.
            images_pad, mask, fn, flag = data_loader.get_next_sequence()
            if flag == False:
                print(fn)
                # Can't find prev data.
                continue

            feed_dict = {
                self.prev_img: images_pad[0],
                self.cur_img: images_pad[1],
                self.cur_mask: mask
            }

            inpt_flow, inpt_warped_im,\
            flow,\
            cur_anno, reconst_anno, warped_anno = sess.run(\
                            [self.inpt_pred_flow_re, self.warped_prev_im,\
                            self.flow_tensor_re,\
                            self.cur_static_anno_pred, self.reconst_cur_anno, self.warped_static_anno_pred],\
                                                  feed_dict=feed_dict)

            #reconst_anno = sess.run(self.reconst_cur_anno, feed_dict=feed_dict)
            #cur_anno = sess.run(self.cur_static_anno_pred, feed_dict=feed_dict)

            #
            self.view_flow_one(inpt_flow[0], fn, step, 'flow_inpt', 'valid')
            self.view_flow_one(flow[0], fn, step, 'flow', 'valid')

            #self.view_patch_one(inpt_warped_im[0], fn, step, 'im', 'valid')

            self.view_im_one(cur_anno[0] * 127, fn, step, 'anno_cur', 'valid')
            self.view_im_one(warped_anno[0] * 127, fn, step, 'anno_warped',
                             'valid')
            self.view_im_one(reconst_anno[0] * 255, fn, step, 'anno_reconst',
                             'valid')
            #self.view_im_cvt_one(images_pad[0][0]+inpt_warped_im[0], fn, step, 'im_prev', 'valid')
            #self.view_im_cvt_one(images_pad[1][0]+inpt_warped_im[0], fn, step, 'im_cur', 'valid')
            self.view_im_cvt_one(images_pad[0][0], fn, step, 'im_prev',
                                 'valid')
            self.view_im_cvt_one(images_pad[1][0], fn, step, 'im_cur', 'valid')

            self.view_im_cvt_one(inpt_warped_im[0], fn, step, 'im_warped',
                                 'valid')
            #self.view_im_one(cur_anno[0]*127, fn, step, 'anno_cur', 'valid')

            t_count += 1
            #3. calculate loss
            total_loss += loss
            total_im_warp_loss += im_warp_loss
            total_flow_loss += flow_loss

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            #line = 'none'
            line = 'Train epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t  m_imW_loss = %.4f\t m_f_loss = %.4f\t  time = %.2f' % (
                epoch, cfgs.inpt_lr, step, count, loss, (total_loss / t_count),
                (total_im_warp_loss / t_count),
                (total_flow_loss / t_count), time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t m_imW_loss = %.4f\t m_f_loss = %.4f\t '
            % (epoch, cfgs.inpt_lr, (total_loss / t_count),
               (total_im_warp_loss / t_count), (total_flow_loss / t_count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step
Exemplo n.º 8
0
    def valid_one_epoch_warped_im(self, sess, data_loader, data_num, epoch,
                                  step):
        '''
        Using warped prev image to do new segmentation.
        Args:
            data_loader: training or validation data_loader.
            data_num: number of data.
        '''
        total_loss, total_im_warp_loss, total_flow_loss = 0, 0, 0
        count = 0
        t0 = time.time()

        im_warp_loss, flow_loss, loss = 0, 0, 0

        #True number to loss
        t_count = 0
        if_epoch = cfgs.test_accu

        for count in range(1, data_num):
            step += 1

            #1. Load data.
            cur_im, prev_im, ellip_info, fn = data_loader.get_next_sequence_search_key(
            )

            feed_dict = {
                self.prev_img: prev_im,
                self.cur_img: cur_im,
            }
            #reconst_anno = sess.run(self.reconst_cur_anno, feed_dict=feed_dict)
            #_ = sess.run(self.cur_static_anno_pred, feed_dict=feed_dict)
            reconst_anno, warped_anno, warped_im, inpt_flow, flow = sess.run(
                [
                    self.reconst_cur_anno, self.warped_static_anno_pred,
                    self.warped_prev_im, self.inpt_pred_flow_re,
                    self.inpt_pred_flow
                ],
                feed_dict=feed_dict)
            self.view_im_cvt_one(warped_im[0], fn, step, 'warped_im', 'valid')
            self.view_im_one(reconst_anno[0, :, :, 1] * 127, fn, step,
                             'anno_reconst', 'valid')
            self.view_im_one(warped_anno[0, :, :, 1] * 127, fn, step,
                             'anno_warp', 'valid')
            self.view_flow_one(inpt_flow[0], fn, step, 'flow', 'valid')
            self.view_flow_one(flow[0], fn, step, 'flow_no', 'valid')

            self.calculate_acc(cur_im.copy(), [fn], reconst_anno[:, :, :, 0],
                               [ellip_info], True, if_epoch)

            t_count += 1
            #3. calculate loss
            total_loss += loss
            total_im_warp_loss += im_warp_loss
            total_flow_loss += flow_loss

            #4. time consume
            time_consumed = time.time() - t0
            time_per_batch = time_consumed / count

            #5. print
            #line = 'none'
            line = 'Train epoch %2d\t lr = %g\t step = %4d\t count = %4d\t loss = %.4f\t m_loss=%.4f\t  m_imW_loss = %.4f\t m_f_loss = %.4f\t  time = %.3f' % (
                epoch, cfgs.inpt_lr, step, count, loss, (total_loss / t_count),
                (total_im_warp_loss / t_count),
                (total_flow_loss / t_count), time_per_batch)
            utils.clear_line(len(line))
            print('\r' + line, end='')

        #End one epoch
        #count -= 1
        print(
            '\nepoch %5d\t learning_rate = %g\t mean_loss = %.4f\t m_imW_loss = %.4f\t m_f_loss = %.4f\t '
            % (epoch, cfgs.inpt_lr, (total_loss / t_count),
               (total_im_warp_loss / t_count), (total_flow_loss / t_count)))
        print('Take time %3.1f' % (time.time() - t0))

        return step