def forward_once(self, x, profile=False, visualize=False):
        y, dt = [], []  # outputs
        for m in self.model:
            if m.f != -1:  # if not from previous layer
                x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]  # from earlier layers

            if profile:
                o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 if thop else 0  # FLOPs
                t = time_sync()
                for _ in range(10):
                    _ = m(x)
                dt.append((time_sync() - t) * 100)
                if m == self.model[0]:
                    LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s}  {'module'}")
                LOGGER.info(f'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f}  {m.type}')

            x = m(x)  # run
            y.append(x if m.i in self.save else None)  # save output

            if visualize:
                feature_visualization(x, m.type, m.i, save_dir=visualize)

        if profile:
            LOGGER.info('%.1fms total' % sum(dt))
        return x
Exemple #2
0
 def _forward_once(self, x, profile=False, visualize=False, gamma=0., validation=False, domain=None, epoch=3):
     y, dt, dis_out = [], [], [] # outputs
     for m in self.model:
         if m.f != -1:  # if not from previous layer
             x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]  # from earlier layers
         if profile:
             self._profile_one_layer(m, x, dt)
         if any([module in m.type for module in ['Discriminator', 'DiscriminatorConv']]):
             if not validation:
                 num_channels = torch.tensor(x.shape[1])
                 # NxHxW to Nx1xHxW
                 obj_map = torch.unsqueeze(obj_map, 1)
                 if obj_map.get_device() != -1:
                     num_channels = num_channels.to(obj_map.get_device()) 
                 # Nx1xHxW to Nx3xHxW
                 obj_map = torch.repeat_interleave(obj_map, num_channels, dim=1)
                 weigh_feat_map = (1-gamma)*x + gamma*x*obj_map
                 dis_out.append(m(weigh_feat_map, epoch))
         elif domain is not None and any([module in m.type for module in ['C3TR', 'C3DETRTR', 'C3SwinTR']]):
             x, obj_map = m(x, domain)  # run
         elif domain is not None and any([module in m.type for module in ['Conv', 'C3', 'SPPF']]):
             x = m(x, domain)  # run
         else:
             x = m(x)  # run
         y.append(x if m.i in self.save else None)  # save output
         if visualize:
             feature_visualization(x, m.type, m.i, save_dir=visualize)
     return x if not dis_out else (x, dis_out)
Exemple #3
0
 def _forward_once(self, x, profile=False, visualize=False):
     y, dt = [], []  # outputs
     for m in self.model:
         if m.f != -1:  # if not from previous layer
             x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]  # from earlier layers
         if profile:
             self._profile_one_layer(m, x, dt)
         x = m(x)  # run
         y.append(x if m.i in self.save else None)  # save output
         if visualize:
             feature_visualization(x, m.type, m.i, save_dir=visualize)
     return x