コード例 #1
0
 def _load_datasets(self):
     abspaths = {name: self.get_path(path)
                 for name, path in self.data_files.items()}
     train_mat = loadmat(abspaths['train'])
     train_images = train_mat['X'].transpose((3, 0, 1, 2))
     train_labels = train_mat['y'].squeeze()
     if self.train_on_extra:
         extra_mat = loadmat(abspaths['extra'])
         train_images = np.vstack((train_images,
                                   extra_mat['X'].transpose((3, 0, 1, 2))))
         train_labels = np.concatenate((train_labels,
                                        extra_mat['y'].squeeze()))
     train_labels[train_labels == 10] = 0
     train_images = train_images.astype(np.float32) / 255
     test_mat = loadmat(abspaths['test'])
     test_images = test_mat['X'].transpose((3, 0, 1, 2))
     test_images = test_images.astype(np.float32) / 255
     test_labels = test_mat['y'].squeeze()
     test_labels[test_labels == 10] = 0
     # raise
     self.train = ImageDataset(train_images, train_labels,
                               image_shape=self.image_shape,
                               label_shape=self.label_shape,
                               shuffle=self.shuffle)
     self.test = ImageDataset(test_images, test_labels,
                               image_shape=self.image_shape,
                               label_shape=self.label_shape,
                               shuffle=self.shuffle)
コード例 #2
0
    def _load_datasets(self):
        abspaths = {
            name: os.path.join(self.data_path, path)
            for name, path in self.data_files.items()
        }

        data = shelve.open(abspaths['all'])
        imgs = data['imgs'].astype(np.float32)
        counts = data['counts'].astype(np.float32)

        np.random.seed(self.seed)
        ind = np.random.permutation(176)
        train_img_num = 100

        self.train = ImageDataset(imgs[ind[:train_img_num], ...],
                                  counts[ind[:train_img_num], ...],
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        self.test = ImageDataset(imgs[ind[train_img_num:], ...],
                                 counts[ind[train_img_num:], ...],
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)
コード例 #3
0
    def _load_datasets(self):
        dataset,label=pickle.load(open("./data/officehome/product_256.pkl","rb"))

        train_images =dataset
        train_labels =label

        validation_images, validation_labels = pickle.load(open("./data/officehome/product_256.pkl", "rb"))
        test_images = []
        test_labels = []
        totalfile=dataset.shape[0]
        for i in range(int(totalfile/2)):
            imgi=random.randint(0,totalfile-1)
            test_images.append(dataset[imgi])
            test_labels.append(label[imgi])
        test_images=np.array(test_images)
        test_labels=np.array(test_labels)


        self.train = ImageDataset(train_images, train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        self.testtest = ImageDataset(test_images, test_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        self.validation = ImageDataset(validation_images, validation_labels,
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)
コード例 #4
0
ファイル: mnist_sua.py プロジェクト: Hyunjun-kim-sualab/ADDA
 def _load_datasets(self):
     abspaths = {
         name: self.get_path(path)
         for name, path in self.data_files.items()
     }
     train_images = self._read_images(abspaths['train_images']).reshape(
         -1, 32, 32, 3)
     train_images = train_images.astype(np.float32) / 255
     train_labels = self._read_labels(abspaths['train_labels'])
     test_images = self._read_images(abspaths['test_images']).reshape(
         -1, 32, 32, 3)
     test_images = test_images.astype(np.float32) / 255
     test_labels = self._read_labels(abspaths['test_labels'])
     valid_images = self._read_images(abspaths['valid_images']).reshape(
         -1, 32, 32, 3)
     valid_images = valid_images.astype(np.float32) / 255
     valid_labels = self._read_images(abspaths['valid_labels'])
     self.train = ImageDataset(train_images,
                               train_labels,
                               image_shape=self.image_shape,
                               label_shape=self.label_shape,
                               shuffle=self.shuffle)
     self.test = ImageDataset(test_images,
                              test_labels,
                              image_shape=self.image_shape,
                              label_shape=self.label_shape,
                              shuffle=self.shuffle)
     self.valid = ImageDataset(valid_images,
                               valid_labels,
                               image_shape=self.image_shape,
                               label_shape=self.label_shape,
                               shuffle=self.shuffle)
コード例 #5
0
    def _load_datasets(self):
        abspaths = {
            name: self.get_path(path)
            for name, path in self.data_files.items()
        }
        rand = np.random.RandomState(self.seed)
        train_images = self._read_images(abspaths['train_images'])
        train_labels = self._read_labels(abspaths['train_labels'])
        inds = rand.permutation(len(train_images))[:2000]
        inds.sort()
        train_images = train_images[inds]
        train_labels = train_labels[inds]
        test_images = self._read_images(abspaths['test_images'])
        test_labels = self._read_labels(abspaths['test_labels'])

        self.train = ImageDataset(train_images,
                                  train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)
        self.test = ImageDataset(test_images,
                                 test_labels,
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)
コード例 #6
0
 def _load_datasets(self):
     abspaths = {name: self.get_path(path)
                 for name, path in self.data_files.items()}
     train_images = self._read_images(abspaths['train_images'])
     train_labels = self._read_labels(abspaths['train_labels'])
     test_images = self._read_images(abspaths['test_images'])
     test_labels = self._read_labels(abspaths['test_labels'])
     self.train = ImageDataset(train_images, train_labels,
                               image_shape=self.image_shape,
                               label_shape=self.label_shape,
                               shuffle=self.shuffle)
     self.test = ImageDataset(test_images, test_labels,
                              image_shape=self.image_shape,
                              label_shape=self.label_shape,
                              shuffle=self.shuffle)
コード例 #7
0
ファイル: dagm_d10.py プロジェクト: Hyunjun-kim-sualab/ADDA
 def _load_datasets(self):
     abspaths = {name: self.get_path(path)
                 for name, path in self.data_files.items()}
     train_images = self._read_images(abspaths['train_images']).reshape(-1, 256, 256, 1)
     train_images = train_images.astype(np.float32) / 255
     train_labels = self._read_labels(abspaths['train_labels'])
     print(train_labels.shape)
     test_images = self._read_images(abspaths['test_images']).reshape(-1, 256, 256, 1)
     test_images = test_images.astype(np.float32) / 255
     test_labels = self._read_labels(abspaths['test_labels'])
     print(np.size(train_labels))
     self.train = ImageDataset(train_images, train_labels,
                               image_shape=self.image_shape,
                               label_shape=self.label_shape,
                               shuffle=self.shuffle)
     self.test = ImageDataset(test_images, test_labels,
                              image_shape=self.image_shape,
                              label_shape=self.label_shape,
                              shuffle=self.shuffle)
コード例 #8
0
    def _load_datasets(self):
        dataset, label = pickle.load(open("./data/office31/dslr_256.pkl",
                                          "rb"))
        train_images = dataset
        train_labels = label

        test_images = dataset[0:10]
        test_labels = label[0:10]
        self.train = ImageDataset(train_images,
                                  train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        self.test = ImageDataset(test_images,
                                 test_labels,
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)

        dataset_validation, label_validation = dataset, label  #pickle.load(open("./data/office31/dslr25.pkl", "rb"))

        self.validation = ImageDataset(dataset_validation,
                                       label_validation,
                                       image_shape=self.image_shape,
                                       label_shape=self.label_shape,
                                       shuffle=self.shuffle)

        dataset_validation_10, label_validation_10 = pickle.load(
            open("./data/office31/dslr_10.pkl", "rb"))

        self.validation_10 = ImageDataset(dataset_validation_10,
                                          label_validation_10,
                                          image_shape=self.image_shape,
                                          label_shape=self.label_shape,
                                          shuffle=self.shuffle)
コード例 #9
0
ファイル: S3_distilling.py プロジェクト: xiongpeng78/MDDA
def main(source, target, model, root_dataset, class_num, encoder_exp_name,
         classifer_exp_name, exp_name, result_image_path, adda_encoder,
         ft_classifer, lr, stepsize, snapshot, solver, iterations, batch_size,
         display):

    image_size = 28
    keep_prob_num = 0.9
    adda.util.config_logging()
    if 'CUDA_VISIBLE_DEVICES' in os.environ:
        logging.info('CUDA_VISIBLE_DEVICES specified, ignoring --gpu flag')
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = "0"
    logging.info('Using GPU {}'.format(os.environ['CUDA_VISIBLE_DEVICES']))

    seed = 19
    logging.info('Using random seed {}'.format(seed))
    random.seed(seed)
    np.random.seed(seed + 1)
    tf.set_random_seed(seed + 2)

    output_classifier = "{}/model/{}/{}/newclassifer/{}_NC_{}_{}_{}".format(
        head_path, root_dataset, source, source, target, model, exp_name)

    encoder = adda.models.get_model_fn(model + '_encoder')
    classifier = adda.models.get_model_fn(model + '_classifier')
    is_train_dataset = tf.placeholder(dtype=tf.bool, shape=())
    keep_prob = tf.placeholder(dtype=tf.float32, shape=())

    train_labels_np_path = os.path.join(result_image_path, "wgan_label.npy")
    train_images_np_path = os.path.join(result_image_path, "wgan_image.npy")
    train_images = np.load(train_images_np_path)
    train_labels = np.load(train_labels_np_path)
    train_image_shape = train_images[0].shape
    train_label_shape = train_labels[0].shape
    train_dataset = ImageDataset(train_images,
                                 train_labels,
                                 image_shape=train_image_shape,
                                 label_shape=train_label_shape,
                                 shuffle=False)

    train_im, train_label = train_dataset.tf_ops()
    train_im = adda.models.preprocessing(train_im, encoder)
    train_im_batch, train_label_batch = tf.train.batch([train_im, train_label],
                                                       batch_size=batch_size)

    encoder_scope = model + '_encoder'
    adda_encoder_scope = 'adda_' + encoder_scope
    classifier_scope = model + '_classifier'

    fts, layer = encoder(train_im_batch,
                         keep_prob,
                         scope=encoder_scope,
                         is_training=False)

    classifier_net, layer = classifier(fts,
                                       n_class=class_num,
                                       scope=classifier_scope,
                                       is_training=True)
    class_loss = tf.losses.sparse_softmax_cross_entropy(
        train_label_batch, classifier_net)

    loss = class_loss

    lr_var = tf.Variable(lr, name='learning_rate', trainable=False)
    if solver == 'sgd':
        optimizer = tf.train.MomentumOptimizer(lr_var, 0.99)
    else:
        optimizer = tf.train.AdamOptimizer(lr_var, 0.5)

    update_ops = adda.util.collect_vars(classifier_scope)
    step = optimizer.minimize(loss, var_list=update_ops)

    config = tf.ConfigProto(device_count=dict(GPU=1))
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    sess.run(tf.global_variables_initializer())

    # install model
    var_dict = adda.util.collect_vars(encoder_scope)
    restorer = tf.train.Saver(var_list=var_dict)
    encoder_path = '{}/model/{}/{}/encoder/{}_E_{}_{}_{}'.format(
        head_path, root_dataset, source, source, target, model,
        encoder_exp_name)
    if os.path.isdir(encoder_path):
        encoder_weights = tf.train.latest_checkpoint(encoder_path)
    restorer.restore(sess, encoder_weights)

    classifier_var_dict = adda.util.collect_vars(classifier_scope)
    classifier_restorer = tf.train.Saver(var_list=classifier_var_dict)
    classifier_weights = '{}/model/{}/{}/classifer/{}_C_{}_{}_{}'.format(
        head_path, root_dataset, source, source, source, model,
        classifer_exp_name)
    if os.path.isdir(classifier_weights):
        classifier_weights = tf.train.latest_checkpoint(classifier_weights)
    classifier_restorer.restore(sess, classifier_weights)

    var_dict_classifier = adda.util.collect_vars(classifier_scope)
    if not os.path.exists(output_classifier):
        os.makedirs(output_classifier)
    saver_classifier = tf.train.Saver(var_list=var_dict_classifier)
    saver = tf.train.Saver(var_list=var_dict_classifier)

    losses = deque(maxlen=10)
    bar = range(iterations + 1)

    epoch_num = int(len(train_dataset) / batch_size)

    for i in bar:

        loss_val, _ = sess.run([loss, step],
                               feed_dict={keep_prob: keep_prob_num})
        losses.append(loss_val)

        if i % display == 0:
            logging.info('{:20} {:10.4f}     (avg: {:10.4f})'.format(
                'Iteration {}:'.format(i), loss_val, np.mean(losses)))
        if stepsize is not None and (i + 1) % stepsize == 0:
            lr = sess.run(lr_var.assign(lr * 0.2))
            logging.info('Changed learning rate to {:.0e}'.format(lr))

    classifier_snapshot_path = saver_classifier.save(
        sess,
        os.path.join(output_classifier, "classifier_{}".format(str(i + 1))),
        global_step=i + 1)
    logging.info('Saved snapshot to {}'.format(classifier_snapshot_path))

    coord.request_stop()
    coord.join(threads)
    sess.close()
コード例 #10
0
    def _load_datasets(self):
        abspaths = {name: self.get_path(path)
                    for name, path in self.data_files.items()}
        train_images, train_labels = self._read_datafile(abspaths['train'])
        test_images, test_labels = self._read_datafile(abspaths['test'])
        self.train = ImageDataset(train_images, train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)
        self.test = ImageDataset(test_images, test_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [0, 255, 255], [255, 0, 255]]

        train_total = []
        label_total = []
        train_1_total = []
        train_0_total = []
        label_1_total = []
        label_0_total = []

        for i in range(len(train_labels)):
            if int(train_labels[i]) < 4:
                label_total.append(0)
                img = np.reshape(np.array(train_images[i]), [16, 16])
                # print("np.array(train_images[i]).shape: ", img.shape)
                img = np.uint8(img)
                img = Image.fromarray(img)
                img = np.array(img.convert("RGB").resize((28, 28), Image.BICUBIC))
                if random.random() > 0.1:
                    img = (img + colors[random.randint(0, 5)] )/2 # random.randint(0, 5)
                train_total.append(img)
                train_0_total.append(img)
                label_0_total.append(0)

                img = np.uint8(img)
                savedirr = "usps_colored/"
                savename = savedirr + ("%d.jpg" % i)
                Image.fromarray(img).save(savename)
            else:
                label_total.append(1)
                img = np.reshape(np.array(train_images[i]), [16, 16])
                # print ("np.array(train_images[i]).shape: ",img.shape)
                img = np.uint8(img)
                img = Image.fromarray(img)
                img = np.array(img.convert("RGB").resize((28, 28), Image.BICUBIC))
                if random.random() > 0.1:
                    img = (img +colors[random.randint(0, 5)])/2
                train_total.append(img)
                train_1_total.append(img)
                label_1_total.append(1)

                img = np.uint8(img)
                savedirr="usps_colored/"
                savename=savedirr+("%d.jpg"%i)
                Image.fromarray(img).save(savename)

        train_total = np.array(train_total)
        label_total = np.array(label_total)
        print("-----------train_total.shape:", train_total.shape)
        train_1_total = np.array(train_1_total)
        train_0_total = np.array(train_0_total)
        label_1_total = np.array(label_1_total)
        label_0_total = np.array(label_0_total)

        print("dataset.shape:", train_total.shape, "dataset.shape:", label_total.shape)
        pickle.dump((train_total, label_total, train_1_total, label_1_total, train_0_total, label_0_total),
                    open("usps_colored.pkl", "wb"))
        print("end..")
コード例 #11
0
    def _load_datasets(self):
        abspaths = {name: self.get_path(path)
                    for name, path in self.data_files.items()}
        rand = np.random.RandomState(self.seed)
        train_images, train_labels = self._read_datafile(abspaths['train'])
        inds = rand.permutation(len(train_images))[:7500]
        inds.sort()
        train_images = train_images[inds]
        train_labels = train_labels[inds]
        test_images, test_labels = self._read_datafile(abspaths['test'])
        self.train = ImageDataset(train_images, train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)
        self.test = ImageDataset(test_images, test_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        colors = np.array([ [0, 0, 255], [255, 255, 0], [0, 255, 255], [255, 0, 255]])

        train_total = []
        label_total = []
        train_1_total = []
        train_0_total = []
        label_1_total = []
        label_0_total = []
        label_total_true = []
        noise_p = 0.0 * 100
        color_p = 0.9 * 100



        for i in range(len(train_labels)):
            if int(train_labels[i]) < 5:
                label_total_true.append(0)
                if random.randint(a=0,b=100) < noise_p:
                    label_total.append([1, 0])
                    Y = 1
                else:
                    label_total.append([0, 0])
                    Y = 0
                img = np.reshape(np.array(train_images[i]), [16, 16])
                # print("np.array(train_images[i]).shape: ", img.shape)
                img = np.uint8(img*255)
                img = Image.fromarray(img)
                img = np.array(img.convert("RGB").resize((16, 16), Image.BICUBIC))
                if random.randint(a=0,b=100) < color_p:
                    colori=abs(1-Y)
                else:
                    colori = abs(Y)

                colori = random.randint(a=0, b=len(colors)-1)
                img = (img + colors[colori])/2
                train_total.append(img)
                train_0_total.append(img)
                if random.randint(a=0,b=100) < noise_p:
                    label_0_total.append([1, 0])
                else:
                    label_0_total.append([0, 0])

                img = np.uint8(img)
                savedirr = "usps_colored/"
                savename = savedirr + ("%d.jpg" % i)
                Image.fromarray(img).save(savename)
            else:
                label_total_true.append(1)
                if random.randint(a=0,b=100) < noise_p:
                    label_total.append([0, 1])
                    Y = 0
                else:
                    label_total.append([1, 1])
                    Y = 1
                img = np.reshape(np.array(train_images[i]), [16, 16])
                # print ("np.array(train_images[i]).shape: ",img.shape)
                img = np.uint8(img*255)
                img = Image.fromarray(img)
                img = np.array(img.convert("RGB").resize((16, 16), Image.BICUBIC))
                train_total.append(img)
                if random.randint(a=0,b=100) < color_p:
                    colori = abs(1 - Y)
                else:
                    colori = abs(Y)

                colori=random.randint(a=0,b=len(colors)-1)
                img = (img + colors[colori]) / 2

                train_1_total.append(img)
                if random.randint(a=0,b=100) < noise_p:
                    label_1_total.append([0,1])
                else:
                    label_1_total.append([1,1])

                img = np.uint8(img)
                savedirr = "usps_colored/"
                savename = savedirr + ("%d.jpg" % i)
                Image.fromarray(img).save(savename)

        train_total = np.array(train_total)
        label_total = np.array(label_total)
        label_total_true = np.array(label_total_true)
        print("-----------train_total.shape:", train_total.shape)
        train_1_total = np.array(train_1_total)
        train_0_total = np.array(train_0_total)
        label_1_total = np.array(label_1_total)
        label_0_total = np.array(label_0_total)


        print("dataset.shape:", train_total.shape, "dataset.shape:", label_total.shape)
        pickle.dump((train_total, label_total,label_total_true, train_1_total, label_1_total, train_0_total, label_0_total),
                    open("usps_colored.pkl", "wb"))
        print("end..")
コード例 #12
0
    def _load_datasets(self):
        abspaths = {
            name: self.get_path(path)
            for name, path in self.data_files.items()
        }
        train_images = self._read_images(abspaths['train_images'])
        train_labels = self._read_labels(abspaths['train_labels'])
        test_images = self._read_images(abspaths['test_images'])
        test_labels = self._read_labels(abspaths['test_labels'])
        print("--train_images", train_images.shape)
        print("--train_labels", train_labels)

        colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0],
                  [0, 255, 255], [255, 0, 255]]
        print("--------------------------mnist----------------")
        print("-------------------------------len(train_images: ",
              np.array(train_images).shape)
        print("--------------------------------len(train_labels: ",
              np.array(test_labels).shape)
        train_total = []
        label_total = []
        train_1_total = []
        train_0_total = []
        label_1_total = []
        label_0_total = []

        for i in range(len(train_labels)):
            if int(train_labels[i]) <= 4:
                label_total.append(0)
                img = np.reshape(np.array(train_images[i]), [28, 28])
                # print("np.array(train_images[i]).shape: ", img.shape)
                img = np.uint8(img)
                img = Image.fromarray(img)
                img = np.array(img.convert("RGB"))
                if random.random() > 0.1:
                    img = img + colors[0]
                train_total.append(img)
                train_0_total.append(img)
                label_0_total.append(0)
            else:
                label_total.append(1)
                img = np.reshape(np.array(train_images[i]), [28, 28])
                # print ("np.array(train_images[i]).shape: ",img.shape)
                img = np.uint8(img)
                img = Image.fromarray(img)
                img = np.array(img.convert("RGB"))
                if random.random() > 0.1:
                    img = img + colors[1]
                train_total.append(img)
                train_1_total.append(img)
                label_1_total.append(1)

        train_total = np.array(train_total)
        label_total = np.array(label_total)
        print("-----------train_total.shape:", train_total.shape)
        train_1_total = np.array(train_1_total)
        train_0_total = np.array(train_0_total)
        label_1_total = np.array(label_1_total)
        label_0_total = np.array(label_0_total)

        print("dataset.shape:", train_total.shape, "dataset.shape:",
              label_total.shape)
        pickle.dump((train_total, label_total, train_1_total, label_1_total,
                     train_0_total, label_0_total),
                    open("mnist_colored.pkl", "wb"))
        print("end..")

        self.train = ImageDataset(np.array(train_images),
                                  np.array(train_labels),
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)
        self.test = ImageDataset(test_images,
                                 test_labels,
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)
コード例 #13
0
    def _load_datasets(self):
        abspaths = {
            name: self.get_path(path)
            for name, path in self.data_files.items()
        }
        rand = np.random.RandomState(self.seed)
        train_images, train_labels = self._read_datafile(abspaths['train'])
        inds = rand.permutation(len(train_images))[:1800]
        inds.sort()
        train_images = train_images[inds]
        train_labels = train_labels[inds]
        test_images, test_labels = self._read_datafile(abspaths['test'])

        colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0],
                  [0, 255, 255], [255, 0, 255]]
        print("-------------------------------len(train_images: ",
              np.array(train_images).shape)
        print("--------------------------------len(train_labels: ",
              np.array(test_labels).shape)
        train_total = []
        label_total = []
        train_1_total = []
        train_0_total = []
        label_1_total = []
        label_0_total = []

        for i in range(len(train_labels)):
            if int(train_labels[i]) < 4:
                label_total.append(0)
                img = np.reshape(np.array(train_images[i]), [16, 16])
                #print("np.array(train_images[i]).shape: ", img.shape)
                img = np.uint8(img)
                img = Image.fromarray(img)
                img = np.array(
                    img.convert("RGB").resize((28, 28), Image.BICUBIC))
                if random.random() > 0.1:
                    img = img + colors[random.randint(
                        0, 5)]  #random.randint(0, 5)
                train_total.append(img)
                train_0_total.append(img)
                label_0_total.append(0)
            else:
                label_total.append(1)
                img = np.reshape(np.array(train_images[i]), [16, 16])
                #print ("np.array(train_images[i]).shape: ",img.shape)
                img = np.uint8(img)
                img = Image.fromarray(img)
                img = np.array(
                    img.convert("RGB").resize((28, 28), Image.BICUBIC))
                if random.random() > 0.1:
                    img = img + colors[random.randint(0, 5)]
                train_total.append(img)
                train_1_total.append(img)
                label_1_total.append(1)

        train_total = np.array(train_total)
        label_total = np.array(label_total)
        print("-----------train_total.shape:", train_total.shape)
        train_1_total = np.array(train_1_total)
        train_0_total = np.array(train_0_total)
        label_1_total = np.array(label_1_total)
        label_0_total = np.array(label_0_total)

        print("dataset.shape:", train_total.shape, "dataset.shape:",
              label_total.shape)
        pickle.dump((train_total, label_total, train_1_total, label_1_total,
                     train_0_total, label_0_total),
                    open("usps_colored.pkl", "wb"))
        print("end..")

        self.train = ImageDataset(train_images,
                                  train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)
        self.test = ImageDataset(test_images,
                                 test_labels,
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)
コード例 #14
0
    def _load_datasets(self):
        dataset, label = pickle.load(
            open("./data/office31/amazon_256.pkl", "rb"))
        train_images = dataset
        train_labels = label

        test_images = dataset[0:10]
        test_labels = label[0:10]
        self.train = ImageDataset(train_images,
                                  train_labels,
                                  image_shape=self.image_shape,
                                  label_shape=self.label_shape,
                                  shuffle=self.shuffle)

        self.test = ImageDataset(test_images,
                                 test_labels,
                                 image_shape=self.image_shape,
                                 label_shape=self.label_shape,
                                 shuffle=self.shuffle)

        dataset_validation, label_validation = dataset, label

        self.validation = ImageDataset(dataset_validation,
                                       label_validation,
                                       image_shape=self.image_shape,
                                       label_shape=self.label_shape,
                                       shuffle=self.shuffle)
        ##########################
        colors = np.array([[0, 0, 255], [255, 255, 0], [0, 255, 255],
                           [255, 0, 255]])
        train_total_color = []
        label_total_color = []
        noise_p = 0.0 * 100
        color_p = 0.1 * 100
        for i in range(len(train_labels)):
            if int(train_labels[i]) < 16:
                if random.randint(low=0, high=100) < noise_p:
                    label_total_color.append(1)
                    Y = 1
                else:
                    label_total_color.append(0)
                    Y = 0
                img = np.array(train_images[i])
                # print("np.array(train_images[i]).shape: ", img.shape)

                if random.randint(low=0, high=100) < color_p:
                    colori = abs(1 - Y)
                else:
                    colori = abs(Y)
                colori = random.randint(a=0, b=len(colors) - 1)
                img = (img + colors[colori]) / 2
                train_total_color.append(img)
                img = np.uint8(img)
                savedirr = "amazon_colored/"
                savename = savedirr + ("%d.jpg" % i)
                Image.fromarray(img).save(savename)
            else:
                if random.randint(low=0, high=100) < noise_p:
                    label_total_color.append(0)
                    Y = 0
                else:
                    label_total_color.append(1)
                    Y = 1
                img = np.array(train_images[i])
                # print ("np.array(train_images[i]).shape: ",img.shape)
                train_total_color.append(img)
                if random.randint(low=0, high=100) < color_p:
                    colori = abs(1 - Y)
                else:
                    colori = abs(Y)

                colori = random.randint(a=0, b=len(colors) - 1)
                img = (img + colors[colori]) / 2
                img = np.uint8(img)
                savedirr = "amazon_colored/"
                savename = savedirr + ("%d.jpg" % i)
                Image.fromarray(img).save(savename)
        train_total_color = np.array(train_total_color)
        label_total_color = np.array(label_total_color)
        print("---colored amazon")
        #dataset_validation_10, label_validation_10 = pickle.load(open("./data/office31/amazon_10.pkl", "rb"))
        self.validation_10 = ImageDataset(train_total_color,
                                          label_total_color,
                                          image_shape=self.image_shape,
                                          label_shape=self.label_shape,
                                          shuffle=self.shuffle)