Exemplo n.º 1
0
def plot_residuals(pred,
                   data,
                   range=None,
                   variable_names=['pT', 'eta', 'phi', 'E'],
                   bins=1000,
                   save=None,
                   title=None):
    alph = 0.8
    residuals = (pred.numpy() - data.numpy()) / data.numpy()
    for kk in np.arange(4):
        plt.figure()
        n_hist_pred, bin_edges, _ = plt.hist(residuals[:, kk],
                                             label='Residuals',
                                             alpha=alph,
                                             bins=bins,
                                             range=range)
        if title is None:
            plt.suptitle('Residuals of %s' % variable_names[kk])
        else:
            plt.suptitle(title)
        plt.xlabel(
            r'$(%s_{recon} - %s_{true}) / %s_{true}$' %
            (variable_names[kk], variable_names[kk], variable_names[kk]))
        plt.ylabel('Number of events')
        sciy()
        if save is not None:
            plt.savefig(save + '_%s' % variable_names[kk])
Exemplo n.º 2
0
    def visualize_sample(self, path, number, shape, class_=None):

        data, target = self.get_sample(number, shape)

        # get sample in order from 0 to 9
        target, order = target.sort()
        data = data[order]

        image_frame_dim = int(np.floor(np.sqrt(number)))

        if shape[2] == 1:
            data_np = data.numpy().reshape(number, shape[0], shape[1],
                                           shape[2])
            save_images(data_np[:image_frame_dim * image_frame_dim, :, :, :],
                        [image_frame_dim, image_frame_dim], path)
        elif shape[2] == 3:
            # data = data.numpy().reshape(number, shape[0], shape[1], shape[2])
            # if self.dataset_name == 'cifar10':
            data = data.numpy().reshape(number, shape[2], shape[1], shape[0])
            # data = data.numpy().reshape(number, shape[0], shape[1], shape[2])

            # remap between 0 and 1
            # data = data - data.min()
            # data = data / data.max()

            data = data / 2 + 0.5  # unnormalize
            make_samples_batche(data[:number], number, path)
        else:
            save_images(data[:image_frame_dim * image_frame_dim, :, :, :],
                        [image_frame_dim, image_frame_dim], path)

        return data
Exemplo n.º 3
0
 def matplotlib_imshow(data, is_image):
     if is_image:  #for images
         data = data / 4 + 0.5  # unnormalize
         npimg = data.numpy()
         plt.imshow(npimg, cmap="gray")
     else:  # for labels
         nplbl = data.numpy()
         plt.imshow(t_loader.decode_segmap(nplbl))
Exemplo n.º 4
0
def train(epoch, prior):
    prior = BayesianGaussianMixture(n_components=50,
                                    covariance_type='diag',
                                    n_init=5,
                                    max_iter=1000)
    tmp = []
    for (data, _) in train_loader:
        #print(data.numpy().shape)
        tmp.append(data.numpy().reshape(data.numpy().shape[0], -1))
    prior.fit(np.vstack(tmp))
    return prior
Exemplo n.º 5
0
def calculate_fvd_from_inference_result(gen_path,
                                        ref_path='./Evaluation/ref',
                                        num_of_video=16,
                                        video_length=10):

    VIDEO_LENGTH = video_length
    print('{}'.format(video_length))
    base_ref = VideoGenerateDataset(ref_path, min_len=VIDEO_LENGTH)
    base_tar = VideoGenerateDataset(gen_path, min_len=VIDEO_LENGTH)

    bs = num_of_video
    assert bs % 16 == 0

    videoloader_ref = torch.utils.data.DataLoader(
        base_ref,
        batch_size=bs,  #len(videodataset),
        drop_last=True,
        shuffle=False)
    videoloader_tar = torch.utils.data.DataLoader(
        base_tar,
        batch_size=bs,  #len(videodataset),
        drop_last=True,
        shuffle=False)

    with tqdm(total=len(videoloader_ref), dynamic_ncols=True) as pbar:
        for i, data in enumerate(videoloader_ref):
            images_ref = data.numpy()
            break
        for i, data in enumerate(videoloader_tar):
            images_tar = data.numpy()
            break

    with tf.Graph().as_default():
        ref_tf = tf.convert_to_tensor(images_ref, dtype=tf.uint8)
        tar_tf = tf.convert_to_tensor(images_tar, dtype=tf.uint8)

        first_set_of_videos = ref_tf  #14592
        second_set_of_videos = tar_tf

        result = calculate_fvd(
            create_id3_embedding(preprocess(first_set_of_videos, (224, 224)),
                                 bs),
            create_id3_embedding(preprocess(second_set_of_videos, (224, 224)),
                                 bs))

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.tables_initializer())
            return sess.run(result)
 def double_check_size(self, data, gt_boxes, im_info):
     check = np.array(data.size(), dtype=np.int)
     scale_list = list(cfg.TRAIN.SCALES)
     scale_list.append(cfg.TRAIN.MAX_SIZE)
     scale_list = np.unique(np.array(scale_list))
     if not np.in1d(check, scale_list).any():
         import cv2
         data = data.numpy()
         gt_boxes = gt_boxes.numpy()
         im_info = im_info.numpy()
         data = data.astype(np.float32, copy=False)
         im_shape = data.shape
         im_size_min = np.min(im_shape[0:2])
         im_size_max = np.max(im_shape[0:2])
         target_size = min(cfg.TRAIN.SCALES)
         max_size = cfg.TRAIN.MAX_SIZE
         im_scale = float(target_size) / float(im_size_min)
         # Prevent the biggest axis from being more than MAX_SIZE
         if im_scale * im_size_max > max_size:
             im_scale = float(max_size) / float(im_size_max)
         data = cv2.resize(data,
                           None,
                           None,
                           fx=im_scale,
                           fy=im_scale,
                           interpolation=cv2.INTER_LINEAR)
         gt_boxes[:, 0:4] = gt_boxes[:, 0:4] * im_scale
         im_info = np.array(
             [[data.shape[0], data.shape[1], im_info[0][2] * im_scale]],
             dtype=np.float32)
         data = torch.from_numpy(data)
         gt_boxes = torch.from_numpy(gt_boxes)
         im_info = torch.from_numpy(im_info)
     return data, gt_boxes, im_info
Exemplo n.º 7
0
def get_mean_std(data_path, input_size, rgb):
    tform = []
    tform.append(transforms.Resize(size=input_size))
    if not rgb:
        tform.append(transforms.Grayscale())
    tform.append(transforms.ToTensor())
    tform = transforms.Compose(tform)
    dset = datasets.ImageFolder(root=data_path, transform=tform)
    train_loader = DataLoader(dataset=dset, batch_size=50)
    scaler = StandardScaler(with_mean=True, with_std=True)
    print('Computing pixel mean and stdev...')
    for idx, (data, labels) in enumerate(train_loader):
        if idx % 20 == 0:
            print("Batch {:d} / {:d}".format(idx, len(train_loader)))
        data = data.numpy()
        n_channels = data.shape[1]
        # reshape into [n_pixels x 3]
        data = data.transpose((0, 2, 3, 1)).reshape((-1, n_channels))
        # pass batch to incremental mean and stdev calculator
        scaler.partial_fit(data)
    print('Done, mean = ')
    pixel_mean = scaler.mean_
    pixel_std = scaler.scale_
    print(pixel_mean)
    print('std = ')
    print(pixel_std)
    return pixel_mean, pixel_std
Exemplo n.º 8
0
def loadWaymoTestFrames(PATH):
    #train_folders = ["training_0005","training_0004","training_0003","training_0002","training_0001","training_0000"]#["training_0001"]# ["training_0000", "training_0001"]
    test_folders = [
        "testing_0007", "testing_0006", "testing_0005", "testing_0004",
        "testing_0003", "testing_0002", "testing_0001", "testing_0000"
    ]
    data_files = [
        path for x in test_folders
        for path in glob(os.path.join(PATH, x, "*.tfrecord"))
    ]
    print(data_files)  #all TFRecord file list
    print(len(data_files))
    dataset = [
        tf.data.TFRecordDataset(FILENAME, compression_type='')
        for FILENAME in data_files
    ]  #create a list of dataset for each TFRecord file
    frames = [
    ]  #store all frames = total number of TFrecord files * 40 frame(each TFrecord)
    for i, data_file in enumerate(dataset):
        print("Datafile: ", data_files[i])  #Each TFrecord file
        for idx, data in enumerate(
                data_file
        ):  #Create frame based on Waymo API, 199 frames per TFrecord (20s, 10Hz)
            #             if idx % 5 != 0: #Downsample every 5 images, reduce to 2Hz, total around 40 frames
            #                 continue
            frame = open_dataset.Frame()
            frame.ParseFromString(bytearray(data.numpy()))
            frames.append(frame)
    return frames
Exemplo n.º 9
0
def train(epoch):
    model.train()
    train_loss = 0
    for batch_idx, data in enumerate(train_loader):
        noise = np.random.poisson(np.random.uniform(1, 5, 1),
                                  data.numpy().shape)
        noise = torch.from_numpy(noise.astype(float))
        noise = noise.float()
        noisy_data = data + noise
        noisy_data = Variable(noisy_data)
        noisy_data = noisy_data.cuda()
        data = Variable(data)
        data = data.cuda()
        optimizer.zero_grad()
        recon_batch = model(noisy_data)
        loss = loss_function(recon_batch, data)
        loss.backward()
        train_loss += loss.data[0]
        optimizer.step()
        if batch_idx % log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader),
                loss.data[0] / len(data)))
            train_loss_log.append(train_loss)
    print('====> Epoch: {} Average loss: {:.4f}'.format(
        epoch, train_loss / len(train_loader.dataset)))
    avg_train_loss_log.append(train_loss / len(train_loader.dataset))
Exemplo n.º 10
0
def load_wav_to_torch(full_path, target_sr=None):
    if full_path.endswith('wav') and sf is not None:
        sampling_rate, data = read(
            full_path)  # scipy only supports .wav but reads faster...
    else:
        data, sampling_rate = sf.read(full_path,
                                      always_2d=True)[:, 0]  # than soundfile.

    if np.issubdtype(data.dtype, np.integer):  # if audio data is type int
        max_mag = -np.iinfo(
            data.dtype).min  # maximum magnitude = min possible value of intXX
    else:  # if audio data is type fp32
        max_mag = max(np.amax(data), -np.amin(data))
        max_mag = (2**31) + 1 if max_mag > (2**15) else (
            (2**15) + 1 if max_mag > 1.01 else 1.0
        )  # data should be either 16-bit INT, 32-bit INT or [-1 to 1] float32

    data = torch.FloatTensor(data.astype(np.float32)) / max_mag

    if target_sr is not None and sampling_rate != target_sr:
        data = torch.from_numpy(
            librosa.core.resample(data.numpy(), sampling_rate, target_sr))
        sampling_rate = target_sr

    data = data.clamp(min=-0.9999, max=0.9999)

    return data, sampling_rate
Exemplo n.º 11
0
def test(epoch, prior):
    ans = np.zeros((50, 10))
    for data, lab in test_loader:
        C = prior.predict(data.numpy().reshape(data.numpy().shape[0], -1))
        for i in xrange(len(lab)):
            ans[C[i], lab[i]] += 1
    print(ans)
    s = np.sum(ans)
    v = 0
    for i in xrange(ans.shape[0]):
        for j in xrange(ans.shape[1]):
            if ans[i, j] > 0:
                v += ans[i, j] / s * np.log(ans[i, j] / s /
                                            (np.sum(ans[i, :]) / s) /
                                            (np.sum(ans[:, j]) / s))
    print("Mutual information: " + str(v))
Exemplo n.º 12
0
def save_embedding_process(model, save_loader, feed_data, is_cuda):
    fts1 = feed_data['fts1']
    fts2 = feed_data['fts2']

    user_embedding1_list = []
    user_embedding2_list = []
    model.eval()
    for batch_idx, data in enumerate(save_loader):
        data = data.reshape([-1])
        val_user_arr = data.numpy()
        v_item1 = fts1[val_user_arr]
        v_item2 = fts2[val_user_arr]
        if is_cuda:
            v_user = torch.LongTensor(val_user_arr).cuda()
            v_item1 = torch.FloatTensor(v_item1).cuda()
            v_item2 = torch.FloatTensor(v_item2).cuda()
        else:
            v_user = torch.LongTensor(val_user_arr)
            v_item1 = torch.FloatTensor(v_item1)
            v_item2 = torch.FloatTensor(v_item2)

        res = model.get_user_embedding(v_item1, v_item2)
        user_embedding1 = res[0]
        user_embedding2 = res[1]
        if is_cuda:
            user_embedding1 = user_embedding1.detach().cpu().numpy()
            user_embedding2 = user_embedding2.detach().cpu().numpy()
        else:
            user_embedding1 = user_embedding1.detach().numpy()
            user_embedding2 = user_embedding2.detach().numpy()

        user_embedding1_list.append(user_embedding1)
        user_embedding2_list.append(user_embedding2)

    return np.concatenate(user_embedding1_list, axis=0), np.concatenate(user_embedding2_list, axis=0)
Exemplo n.º 13
0
 def do_transforms(self, data):
     toPIL = vision.transforms.ToPILImage()
     toTensor = vision.transforms.ToTensor()
     data = toTensor(
         self.transform(toPIL(data.astype('uint8').transpose(
             (1, 2, 0))))) * 255
     return data.numpy()
Exemplo n.º 14
0
 def idx2str(self, data):
     '''translate a list of int back to string'''
     if not hasattr(self, 'rev_vocab'):
         self.rev_vocab = {v: k for k, v in self._vocab.items()}
     if data.__class__ == torch.Tensor:
         data = data.numpy()
     return [self.rev_vocab[d] for d in data]
Exemplo n.º 15
0
def test(epoch):
    model.eval()
    test_loss = 0
    correct = 0
    total = 0
    for batch_idx, (data, z_class) in enumerate(test_loader):
        y_class = np.eye(10)[z_class.numpy()]

        data = Variable(data, volatile=True)
        recon_batch, mu, logvar, y_pred = model(data)

        test_loss += loss_function([recon_batch], data, mu, logvar, y_pred,
                                   y_class, epoch)
        z = y_pred.data.cpu().numpy()
        for i, row in enumerate(z):
            pred = np.argmax(row)
            if pred == z_class[i]:
                correct += 1
        total += len(z_class)

    test_loss /= len(test_loader.dataset)
    print('Correct: ' + str(correct))
    print('Total: ' + str(total))
    print('====> Test set loss: ' + str(test_loss.data.cpu().numpy()[0]))

    if epoch % args.eval_interval == 0:
        imgs = []
        # for z in zs:
        #     # for c in cs:
        #     model.eval()
        #     x = model.decode(z)

        #     imgFile = np.swapaxes((x.data).cpu().numpy()[0],0,2)
        #     print(imgFile.shape)
        #     imgs.append(imgFile)

        for batch_idx, (data, y_class) in enumerate(test_loader2):
            if batch_idx % 200 != 0:
                continue
            img = np.swapaxes(np.swapaxes(data.numpy()[0], 0, 1), 1, 2)
            imgs.append(img)

            data = Variable(data, volatile=True)
            mu, _, _ = model.encode(data)

            # print(batch_idx)
            a = torch.from_numpy(np.eye(10)[y_class.numpy()]).type(
                torch.FloatTensor)
            img = model.decode(mu, Variable(a))
            img = np.swapaxes(np.swapaxes(img.data.numpy()[0], 0, 1), 1, 2)
            imgs.append(img)

        imgFile = stack(imgs)
        imgFile = imgFile * 255  #/ np.max(imgFile)
        imgFileName = args.save_image + "_" + str(epoch) + ".png"

        cv.imwrite(imgFileName, imgFile)
Exemplo n.º 16
0
def MyPlotFuc(data):
    import matplotlib.pyplot as plt
    import numpy as np
    data = data.cpu()
    data = data.numpy()
    data = np.where(data > 0)
    plt.xlim(0, 128)
    plt.ylim(0, 128)
    plt.scatter(data[0], data[1], s=5)
    plt.show()
    def visualize_sample(self, path, number, shape):

        data, target = self.get_sample(number, shape)

        # get sample in order from 0 to 9
        target, order = target.sort()
        data = data[order]
        #
        # if self.transform is not None:
        #     tf_bacth = None
        #     for i in range(number):
        #          tf_data = TF.to_pil_image(data[i])
        #          tf_data = self.transform(tf_data)
        #          if i == 0:
        #             tf_bacth = torch.FloatTensor(number, tf_data.shape[0], tf_data.shape[1], tf_data.shape[2])
        #          tf_bacth[i] = tf_data
        #     data = tf_bacth

        image_frame_dim = int(np.floor(np.sqrt(number)))

        if shape[2] == 1:
            data_np = data.numpy().reshape(number, shape[0], shape[1],
                                           shape[2])
            save_images(data_np[:image_frame_dim * image_frame_dim, :, :, :],
                        [image_frame_dim, image_frame_dim], path)
        elif shape[2] == 3:
            # data = data.numpy().reshape(number, shape[0], shape[1], shape[2])
            # if self.dataset_name == 'cifar10':
            data = data.numpy().reshape(number, shape[2], shape[1], shape[0])
            # data = data.numpy().reshape(number, shape[0], shape[1], shape[2])

            # remap between 0 and 1
            # data = data - data.min()
            # data = data / data.max()

            data = data / 2 + 0.5  # unnormalize
            make_samples_batche(data[:number], number, path)
        else:
            save_images(data[:image_frame_dim * image_frame_dim, :, :, :],
                        [image_frame_dim, image_frame_dim], path)

        return data
Exemplo n.º 18
0
    def get_batch(self):
        data = next(self.dataset_iter)
        data = data.numpy().astype(self.input_data_type) / 255.0
        num_files, num_data, height, width, channel = data.shape
        num_batch_per_file = num_data // (self.seq_len + self.horizon)
        batch = data[:, :num_batch_per_file *
                     (self.seq_len + self.horizon)].reshape(
                         num_files * num_batch_per_file,
                         (self.seq_len + self.horizon), height, width, -1)

        return batch
def stack_examples(data):
    data = data.numpy().squeeze()
    sample = data[0]
    n = len(data)
    w, h = sample.shape
    out = numpy.ones((w, h * n))
    space = 1
    for i in range(n):
        out[space:-space,
            i * h + space:(i + 1) * h - space] = data[i, space:-space,
                                                      space:-space]
    scipy.misc.imsave("samples.png", out)
Exemplo n.º 20
0
    def generate_variant_dataset(self, variant):
        """Generate a dataset corresponding to the given MNIST variant.

        The modified MNIST data will be saved in a similar fashion to
        that of the original MNIST dataset. Also, presumably some randomness will be
        involved, meaning the dataset will change every time this function is called.
        """
        # process and save as torch files
        print('Generating...')

        if not os.path.exists(self.variant_folder):
            os.makedirs(self.variant_folder)

        def _rot(image_data):
            """Destructive rotation."""
            for i in range(image_data.shape[0]):
                rand_deg = np.random.random() * 360.0
                image_data[i] = rotate(image_data[i], rand_deg, reshape=False)

        def _bg_rand(image_data):
            """Destructive random background."""
            noise = np.random.randint(0,
                                      256,
                                      image_data.shape,
                                      dtype=image_data.dtype)
            image_data[image_data == 0] = noise[image_data == 0]

        for data_file in (self.training_file, self.test_file):
            # load original MNIST data
            data, targets = torch.load(
                os.path.join(self.processed_folder, data_file))

            modified_data = data.numpy()  # shape: (n, 28, 28)
            if variant == 'rot':
                _rot(modified_data)
            elif variant == 'bg_rand':
                _bg_rand(modified_data)
            elif variant == 'bg_rand_rot':
                _rot(modified_data)
                _bg_rand(modified_data)

            with open(os.path.join(self.variant_folder, data_file), 'wb') as f:
                torch.save((torch.from_numpy(modified_data), targets), f)

        print('Done!')
        print('Saved dataset to %s.' % self.variant_folder)
Exemplo n.º 21
0
def testTorchKtop():
    batch_size = 20
    train_dataset = downloadData(True)
    train_loader = loadData(train_dataset, batch_size, True)
    images,labels = next(iter(train_loader))
    data = images.view(images.shape[0],-1)
    D = data.numpy()
    W = cosine_similarity(D,D)
    print(W)
    W = torch.FloatTensor(W)
    nn = torch.topk(W,batch_size)
    qq = torch.randn(batch_size,batch_size)
    nnInd = nn[1]
    qq[nnInd] = 0
    # nn = getKneibor(X=data,n_nbrs=batch_size)
    print(nnInd)
    print(qq)
    show_img(images,batch_size)
Exemplo n.º 22
0
def load_wav_to_torch(full_path,
                      target_sr=None,
                      return_empty_on_exception=False):
    sampling_rate = None
    try:
        data, sampling_rate = sf.read(full_path,
                                      always_2d=True)  # than soundfile.
    except Exception as ex:
        print(f"'{full_path}' failed to load.\nException:")
        print(ex)
        if return_empty_on_exception:
            return [], sampling_rate or target_sr or 48000
        else:
            raise Exception(ex)

    if len(data.shape) > 1:
        data = data[:, 0]
        assert len(
            data
        ) > 2  # check duration of audio file is > 2 samples (because otherwise the slice operation was on the wrong dimension)

    if np.issubdtype(data.dtype, np.integer):  # if audio data is type int
        max_mag = -np.iinfo(
            data.dtype).min  # maximum magnitude = min possible value of intXX
    else:  # if audio data is type fp32
        max_mag = max(np.amax(data), -np.amin(data))
        max_mag = (2**31) + 1 if max_mag > (2**15) else (
            (2**15) + 1 if max_mag > 1.01 else 1.0
        )  # data should be either 16-bit INT, 32-bit INT or [-1 to 1] float32

    data = torch.FloatTensor(data.astype(np.float32)) / max_mag

    if (torch.isinf(data) | torch.isnan(data)).any(
    ) and return_empty_on_exception:  # resample will crash with inf/NaN inputs. return_empty_on_exception will return empty arr instead of except
        return [], sampling_rate or target_sr or 48000
    if target_sr is not None and sampling_rate != target_sr:
        data = torch.from_numpy(
            librosa.core.resample(data.numpy(), sampling_rate, target_sr))
        sampling_rate = target_sr

    return data, sampling_rate
Exemplo n.º 23
0
 def visualize_data(self):
     n = len(self)
     for i in range(n):
         image, impulse, response, bbox, one_hot = [
             data.numpy() for data in self[i]
         ]
         image = np.moveaxis(image, 0, -1)
         image *= self.config.STD_PIXEL
         image += self.config.MEAN_PIXEL
         image *= 255
         impulse = impulse * 255
         image[:, :, 0] = np.where(impulse[0] == 255, 255, image[:, :, 0])
         response = np.squeeze(response) * 255
         bbox = np.squeeze(bbox) * 255
         Image.fromarray(image.astype(np.uint8), "RGB").show()
         # for i in range(impulse.shape[0]):
         #     Image.fromarray(impulse[i].astype(np.uint8), "L").show()
         Image.fromarray(response.astype(np.uint8), "L").show()
         Image.fromarray(bbox.astype(np.uint8), "L").show()
         print(self.config.CLASS_NAMES[np.argmax(one_hot)])
         input()
Exemplo n.º 24
0
def datasets_initialization_kcenter(data,
                                    labels,
                                    init_size,
                                    init_weight,
                                    pho_p=0,
                                    pho_n=0):
    if torch.is_tensor(data):
        data = data.numpy()
    if torch.is_tensor(labels):
        labels = labels.numpy()
    kcenter = KCenter(data, labels)
    for i in range(init_size - 1):
        kcenter.select_one()
    unlabeled_set = torch.utils.data.TensorDataset(
        torch.from_numpy(kcenter.pool).float(),
        torch.from_numpy(kcenter.pool_y).float())
    labeled_set = WeightedTensorDataset(
        torch.from_numpy(kcenter.selected).float(),
        torch.from_numpy(kcenter.selected_y).float(),
        init_weight * torch.ones(init_size, 1), pho_p, pho_n)
    return unlabeled_set, labeled_set
Exemplo n.º 25
0
def plot_log_dist(data,
                  logger,
                  name="undefined",
                  save_path="./",
                  cumulative=False,
                  hist=True,
                  kde=False,
                  bins=60):
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if (isinstance(data, torch.Tensor)):
        data = data.numpy()
    data = data.flatten()
    data_log = []
    count_0 = 0
    count = data.shape[0]
    for dat in data:
        if (dat > 0.0):
            data_log.append(math.log(dat, 10))
        elif (dat < 0.0):
            print("%s is not a positive data" % (name))
            return False
        else:
            count_0 += 1
    data_log = np.array(data_log)
    note = "non-zero rate: %.4f(%d/%d)" % (
        (count - count_0) / count, count - count_0, count)
    if count_0 == count:
        logger.write("%s is all-zero." % (name))
        return True
    plot_dist(data=data_log,
              logger=logger,
              name=name + "(log)",
              save_dir=save_dir,
              notes=note,
              cumulative=cumulative,
              hist=hist,
              kde=kde,
              bins=bins)
    return True
Exemplo n.º 26
0
    def __getitem__(self, index):
        data = self.data[index]
        lbl = self.labels[index]

        data = Image.fromarray(data.numpy(), mode='L')

        if self.is_noisy:
            opencvImage = np.expand_dims(np.array(data), 2)
            opencvImage = addNoise(opencvImage, var=.00001)
            opencvImage = np.squeeze(opencvImage, 2)
            imgNoisy = Image.fromarray(opencvImage, mode='L')

            if self.transform is not None:
                imgNoisy = self.transform(imgNoisy)

        if self.transform is not None:
            data = self.transform(data)

        if not self.is_noisy:
            return data, lbl
        else:
            return data, lbl, imgNoisy

        pass
Exemplo n.º 27
0
    def __init__(self,
                 label_file,
                 file_dir,
                 frame_qty,
                 keyframe_skip,
                 keyframe_interval,
                 cache_file_prefix,
                 file_ext='mp4'):

        VideoFramesBase.__init__(self)

        for line_num, file_name_prefix, label_str in get_labels(label_file):

            lab = LABEL_DICT[label_str]

            cache_file_data = VideoFramesTest.cache_file_name(
                cache_file_prefix, file_name_prefix)
            if os.path.exists(cache_file_data):
                print(f'File {cache_file_data} is already generated!')
                continue

            gc.collect()
            file_name = os.path.join(file_dir,
                                     file_name_prefix + '.' + file_ext)
            tensor_list = []

            statinfo = os.stat(file_name)
            if False and statinfo.st_size == 0:
                print(f'Empty file {file_name}, generating zero tensor')
                tensor_list.append(
                    torch.zeros(3, MIN_WIDTH_HEIGHT, MIN_WIDTH_HEIGHT))
            else:

                frame_iter = GetFrames(file_name)

                while frame_iter.read_next():

                    frame = frame_iter.frame
                    if frame >= keyframe_skip:

                        if (frame - keyframe_skip) % keyframe_interval != 0:
                            continue

                        tran_img_tensor = self.get_image_tensor(
                            frame_iter.img, frame_iter.width,
                            frame_iter.height)
                        tensor_list.append(tran_img_tensor)

                        if len(tensor_list) >= frame_qty:
                            break

                frame_iter.release()

            if not tensor_list:
                raise Exception(
                    f'Faulty video {file_name}, remove it from the list!')

            data = torch.stack(tensor_list)
            frame_qty = len(tensor_list)

            print(f'Saving {frame_qty} frames {cache_file_data}')
            np.save(cache_file_data, data.numpy())
def save_image(filename, data):
    img = data.numpy()
    img = np.clip(img, 0, 255)
    img = img.transpose(1, 2, 0).astype("uint8")

    cv2.imwrite(filename, img)
Exemplo n.º 29
0
def extract_features(dtypes, opts, start_time=time.time()):
    cnn = cnns.__dict__[opts.cnn](pretrained=True)
    if opts.gpu:
        cnn = torch.nn.DataParallel(cnn).cuda()
        torch.backends.cudnn.benchmark = True
    cnn.eval()

    for dtype in dtypes:
        path = 'datasets/{dataset}'.format(dataset=opts.dataset)
        data_path = '{path}/{cnn}_{dtype}.h5'.format(path=path,
                                                     cnn=opts.cnn,
                                                     dtype=dtype)
        if not os.path.isfile(data_path):
            # dataset
            dataset = folder.StaticImageFolder(
                dataset=opts.dataset,
                dtype=dtype,
                transform=transforms.Compose([
                    transforms.Resize(256),
                    transforms.CenterCrop(224),
                    transforms.ToTensor(),
                    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225]),
                ]))
            num_data = len(dataset)
            labels_ = np.array([dataset.imgs[k][1] for k in range(num_data)])

            # data loader
            data_loader = torch.utils.data.DataLoader(
                dataset,
                batch_size=opts.batch_size,
                shuffle=False,
                num_workers=opts.workers,
                pin_memory=True)
            print('{dtype}; data loader; '.format(dtype=dtype), end='')
            print('{time:8.3f} s'.format(time=time.time() - start_time))

            # feature extraction
            data_dim = 2048  # feature dimension before softmax
            data = torch.zeros(num_data, data_dim)
            labels = torch.zeros(num_data, out=torch.LongTensor())
            for i, (inputs, targets) in enumerate(data_loader):
                pos = i * opts.batch_size
                inputs = Variable(inputs.cuda(),
                                  volatile=True) if opts.gpu else Variable(
                                      inputs, volatile=True)
                data[pos:pos + inputs.size(0)] = cnn(inputs).data.cpu()
                labels[pos:pos + targets.size(0)] = targets
                print('{dtype}; '.format(dtype=dtype), end='')
                print('{pos:7d}/{num_data:7d} i; '.format(pos=pos,
                                                          num_data=num_data),
                      end='')
                print('{time:8.3f} s'.format(time=time.time() - start_time))
            data = data.numpy()
            labels = labels.numpy()

            # sanity check
            print('{dtype}; '.format(dtype=dtype), end='')
            print('order of image path and data {consistency}consistent; ' \
                  .format(consistency='' if (labels_ == labels).all() else 'not '), end='')
            print('{time:8.3f} s'.format(time=time.time() - start_time))

            # save
            with h5py.File(data_path, 'w') as f:
                f.create_dataset('data',
                                 data=data,
                                 compression='gzip',
                                 compression_opts=9)
                f.create_dataset('labels',
                                 data=labels,
                                 compression='gzip',
                                 compression_opts=9)

        print('{dtype}; {time:8.3f} s'.format(dtype=dtype,
                                              time=time.time() - start_time))
Exemplo n.º 30
0
    def show(self, samples=8, plot=True, rand=False):
        if self.num > 0 and plot:
            if self.samples['score'].size()[0] < samples:
                samples = self.samples['score'].size()[0]

            image_num = 0
            scalar_list = []
            for key, data in self.samples.items():
                if len(data.size()) == 1:
                    scalar_list.append(key)
                else:
                    image_num += 1
            scalar_num = np.size(scalar_list)

            pindex = np.arange(samples)
            if rand:
                pindex = random.sample(
                    np.arange(self.samples['score'].size()[0]).tolist(),
                    samples)
            plt.figure(figsize=(15., 20. * samples / 8))
            k = 1
            col = max(2, image_num)
            for p in pindex:
                for key, data in self.samples.items():
                    data = data.numpy()
                    if data.ndim > 1:
                        plt.subplot(samples, col, k)
                        k += 1
                        if data.shape[1] == 1:
                            plt.imshow(data[p, 0, :, :],
                                       cm.Greys_r,
                                       interpolation='nearest',
                                       vmin=0.,
                                       vmax=1.)
                        else:
                            plt.imshow(np.array(
                                np.clip(data[p, :3, :, :].transpose([1, 2, 0]),
                                        0., 1.) * 255.,
                                dtype=np.uint8),
                                       interpolation='nearest',
                                       vmin=0.,
                                       vmax=1.)
                        title = ''
                        if image_num > 1:
                            title = str(key) + ' - '
                        for scalar in scalar_list:
                            title += str(scalar) + '=' + \
                                     str(self.samples[scalar][p])[:4] + ' - '
                        plt.title(title[:-3])

            plt.subplots_adjust(hspace=0.3)
            plt.show()

            k = 1
            plt.figure(figsize=(10., 2.))
            for scalar in scalar_list:
                plt.subplot(1, scalar_num, k)
                k += 1
                hist, bins = np.histogram(self.samples[scalar].numpy(),
                                          bins=50)
                width = 0.7 * (bins[1] - bins[0])
                center = (bins[:-1] + bins[1:]) / 2
                plt.bar(center, hist, align='center', width=width)
                plt.title("Histogram " + scalar)
            plt.show()