예제 #1
0
def validate(model, test_data, loss_weight=None):

    total_loss = 0.
    n_correct1 = 0.
    n_correct2 = 0.
    count = 0.
    model.eval()

    pbar = tqdm(total=len(test_data), position=0, leave=False, file=sys.stdout)

    for images, labels in test_data:
        images = images.to(model.device)
        labels = labels.to(model.device)
        output = model(images)
        total_loss += F.cross_entropy(
            output, labels, weight=loss_weight).item() * labels.size(0)
        predict = torch.argmax(output, -1)
        n_correct1 += (predict == labels).sum().item()
        n_correct2 += (~(data.isrecyclable(predict)
                         ^ data.isrecyclable(labels))).sum().item()
        count += labels.size(0)
        pbar.update(1)

    tqdm.close(pbar)

    avg_loss = total_loss / float(count)
    accuracy1 = n_correct1 / float(count)
    accuracy2 = n_correct2 / float(count)

    return avg_loss, accuracy1, accuracy2
예제 #2
0
def main():
    # load haar cascades model
    faces = cv2.CascadeClassifier(FACE_MODEL_FILE)
    eyes = cv2.CascadeClassifier(EYES_MODEL_FILE)
    plates = [cv2.CascadeClassifier(p) for p in PLATE_FILES]

    # connect to camera
    camera = cv2.VideoCapture(0)
    while not camera.isOpened():
        time.sleep(0.5)

    # read and show frames
    progress = tqdm()
    while True:

        ret, frame = camera.read()
        frame = process(frame, [
            (faces, (255, 255, 0), dict(scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)), face_name),
            (eyes, (0, 0, 255), dict(scaleFactor=1.1, minNeighbors=5, minSize=(20, 20)), eye_name),
        ])
        #        frame = process(frame, [
        #            (model, (0, 255, 0), dict(scaleFactor=1.1, minNeighbors=5, minSize=(20, 20)))
        #            for model in plates
        #            ])
        cv2.imshow('Objects', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        progress.update()

    # gracefully close
    camera.release()
    cv2.destroyWindows()
    tqdm.close()
예제 #3
0
 def preprocess(self):
     bgremover = model.U2NET()
     device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
     bgremover.load_state_dict(
         torch.load('model/u2net.pth', map_location=device))
     bgremover.eval()
     b = torch.ones(1, 3, 320, 320, device=device)
     b = transforms.Normalize([0.485, 0.456, 0.406],
                              [0.229, 0.224, 0.225])(b)
     pbar = tqdm(total=len(self.images),
                 position=0,
                 leave=False,
                 file=sys.stdout)
     for i in range(len(self.images)):
         image = self.images[i]
         x = image.resize([320, 320])
         x = transforms.ToTensor()(img)
         x = transforms.Normalize([0.485, 0.456, 0.406],
                                  [0.229, 0.224, 0.225])(x)
         x = x.view(1, *x.size())
         with torch.no_grad():
             mask, _, _, _, _, _, _ = bgremover(x)
         x = x * mask + b * (1 - mask)
         x = x.squeeze(0)
         x = x.detach().cpu().numpy()
         x[0, :, :] = x[0, :, :] * 0.229 + 0.485
         x[1, :, :] = x[1, :, :] * 0.224 + 0.456
         x[2, :, :] = x[2, :, :] * 0.225 + 0.406
         x = x.transpose([1, 2, 0])
         self.images[i] = Image.fromarray(
             (x * 255).astype(np.uint8)).convert('RGB')
         pbar.update(1)
     tqdm.close(pbar)
예제 #4
0
    def close(self):
        _xclosed = True
        _tqdm.close(self)

        if self._xloghandler:
            self._xrootlog.removeHandler(self._xloghandler)
            self._xrootlog.addHandler(self._xoldhandler)
예제 #5
0
    def __init__(self, root_dirs, mode='none', exclude='google'):
        super().__init__()
        self.images = list()
        self.obj_types = list()
        self.store = 'RAM'
        self.transform = get_transform(mode)
        self.add_idx = list()

        if type(root_dirs) != list:
            root_dirs = list(root_dirs)

        if type(exclude) != list:
            exclude = list(exclude)

        file_paths = list()
        for root_dir in root_dirs:
            subdirs = glob(os.path.join(root_dir, '*'))
            for subdir in subdirs:
                ignore = False
                for exc in exclude:
                    if subdir.endswith(exc):
                        ignore = True
                        break
                if not ignore:
                    for ext in ['png', 'jpg']:
                        file_paths += glob(os.path.join(subdir, '*.' + ext))
        n_files = len(file_paths)

        pbar = tqdm(total=n_files, position=0, leave=False, file=sys.stdout)

        for file_path in file_paths:
            self.obj_types.append(file_path.split('/')[-2])
            if self.store == 'RAM':
                fptr = Image.open(file_path).convert('RGB')
                file_copy = fptr.copy()
                fptr.close()
                self.images.append(file_copy)
            elif self.store == 'DISK':
                self.images.append(file_path)
            pbar.update(1)

        tqdm.close(pbar)
예제 #6
0
def train_single_epoch(model,
                       optimizer,
                       train_data,
                       scheduler=None,
                       loss_weight=None):

    total_loss = 0.
    n_correct1 = 0.
    n_correct2 = 0.
    count = 0.
    model.train()

    pbar = tqdm(total=len(train_data),
                position=0,
                leave=False,
                file=sys.stdout)

    for images, labels in train_data:
        optimizer.zero_grad()
        images = images.to(model.device)
        labels = labels.to(model.device)
        output = model(images)
        loss = F.cross_entropy(output, labels, weight=loss_weight)
        loss.backward()
        optimizer.step()
        if scheduler != None:
            scheduler.step()
        total_loss += loss.item() * labels.size(0)
        predict = torch.argmax(output, -1)
        n_correct1 += (predict == labels).sum().item()
        n_correct2 += (~(data.isrecyclable(predict)
                         ^ data.isrecyclable(labels))).sum().item()
        count += labels.size(0)
        pbar.update(1)

    tqdm.close(pbar)

    avg_loss = total_loss / float(count)
    accuracy1 = n_correct1 / float(count)
    accuracy2 = n_correct2 / float(count)

    return avg_loss, accuracy1, accuracy2

for message in app.iter_history(chat_id=channel_name,
                                offset_id=offset_id,
                                reverse=reverse):
    try:
        if message.document.file_name in os.listdir(save_path):
            pass
        else:
            if message.document.file_name.endswith('.7z'):  #过滤指定类型文件。
                continue
            #elif message.document.file_name.endswith('.zip'):
            #    continue
            #elif message.document.file_name.endswith('.rar'):
            #    continue
            else:
                tqdm = TqdmUpTo(
                    total=message.document.file_size,
                    desc=f'{message.message_id} - {message.document.file_name}',
                    unit='B',
                    unit_scale=True)
                message.download(file_name=os.path.join(
                    save_path, message.document.file_name),
                                 progress=tqdm.my_update,
                                 block=True)
                tqdm.close()
                time.sleep(0.01)

    except Exception as e:
        continue
예제 #8
0
    def train(self, epochs, steps_per_epoch, batch_size, epoch_save_period=10):
        """ Train the generator network """
        loss_dict = collections.OrderedDict()

        for epoch in range(1, epochs + 1):
            print("\nEpoch %d" % epoch)
            with tqdm(total=steps_per_epoch) as pbar:
                for i in range(steps_per_epoch):
                    # Generate random noise as an input to initialize the generator
                    noise = np.random.randn(batch_size, 100)

                    # Generate fake SMPL parameters from noisy input
                    generated_params = self.generator.predict(noise)

                    # Get a random set of real SMPL parameters and corrupt these with additive noise
                    param_batch = self.X[np.random.randint(
                        low=0, high=self.X.shape[0], size=batch_size)]
                    param_batch += np.random.uniform(low=-0.02,
                                                     high=0.02,
                                                     size=param_batch.shape)

                    # Construct different batches of real and fake data
                    X = np.concatenate([param_batch, generated_params])

                    # Labels for real and generated data - soft labels help in training
                    y_dis = np.zeros(2 * batch_size)
                    y_dis[:batch_size] = np.random.uniform(0.9,
                                                           1.0,
                                                           size=batch_size)
                    y_dis[batch_size:] = np.random.uniform(0.0,
                                                           0.1,
                                                           size=batch_size)

                    # Pre-train discriminator on fake and real data before starting the GAN
                    self.discriminator.trainable = True
                    disc_outputs = self.discriminator.train_on_batch(X, y_dis)
                    loss_dict["disc_loss"] = "{:.03f}".format(disc_outputs[0])
                    loss_dict["disc_acc"] = "{:.03f}".format(disc_outputs[1])

                    # Treat the generated data as real
                    noise = np.random.randn(batch_size, 100)
                    y_gen = np.ones(batch_size)

                    # During the training of the GAN the weights of discriminator are fixed
                    self.discriminator.trainable = False

                    # Train the GAN by alternating the training of the Discriminator
                    # and training the chained GAN model with the Discriminator’s weights frozen
                    gan_outputs = self.gan.train_on_batch(noise, y_gen)
                    loss_dict["gan_loss"] = "{:.03f}".format(gan_outputs[0])
                    loss_dict["gan_acc"] = "{:.03f}".format(gan_outputs[1])

                    pbar.set_postfix(loss_dict, refresh=True)
                    pbar.update()

            tqdm.close(pbar)

            # Evaluate the model on an example batch
            fake_output = self.discriminator.predict(
                self.generator.predict(np.random.randn(batch_size, 100)))
            if self.X_val is not None:
                real_output = self.discriminator.predict(
                    self.X_val[np.random.randint(low=0,
                                                 high=self.X_val.shape[0],
                                                 size=batch_size)])
                print("Discriminator output on real data: {:04f}".format(
                    np.mean(real_output)))
            print("Discriminator output on fake data: {:04f}".format(
                np.mean(fake_output)))

            if epoch == 1 or epoch % epoch_save_period == 0:
                self.save_generated_smpl(epoch, render=True)
예제 #9
0
파일: utils.py 프로젝트: victim10wq3/buzz-1
def _tqdm_close(tqdm):
    """
    Try to close tqdm, or do nothing
    """
    if tqdm is not None:
        tqdm.close()