def __init__(self, config): """Constructor. """ self.config = config idxs = [] with open(self.config['path'] + '/seq_ref_clean.txt', 'r', encoding="latin-1") as f: for l, line in enumerate(f): if l >= 4: idxs.append([int(j) for j in line.split(' ')]) idxs[-1][1] = 4371 X = self.config['pad_value'] * np.ones( (len(idxs), np.max(np.diff(np.array(idxs), axis=1)) + 1, self.config['size'], self.config['size'], 3), dtype=self.config['dtype']) y = np.zeros((len(idxs), ), dtype='int32') for i, idx in enumerate(idxs): for j, q in enumerate(range(idx[0], idx[1] + 1)): img = imread(self.config['path'] + '/img_clean/img' + '{:04d}'.format(q) + '.jpg') img = imresize(img, (self.config['size'], self.config['size'])) X[i, j, :, :, :] = img y[i] = i X /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X = self.ZCA.fit_transform(X) if self.config['imagenet_preprocessing'] is True: X = np.pad(X, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X)): for t in range(len(X[0])): X[i, t] = imresize(X[i, t], (32, 32)) X = preprocess_input(X, data_format='channels_last') Y = to_categorical(y, len(X)) self.X = X self.Y = Y print(self.X.shape, self.Y.shape)
class COIL100Dataset(MNISTRotatedDataset): def __init__(self, config): """Constructor. """ self.config = config X = np.zeros((100, 72, self.config['size'], self.config['size'], 3), dtype=self.config['dtype']) y = np.zeros((100, ), dtype='int32') for i in range(len(X)): for j, t in enumerate(range(0, 360, 5)): img = imread(self.config['path'] + '/obj' + str(i + 1) + '__' + str(t) + '.png') img = imresize(img, (self.config['size'], self.config['size'])) X[i, j, :, :, :] = img y[i] = i X /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X = self.ZCA.fit_transform(X) if self.config['imagenet_preprocessing'] is True: X = np.pad(X, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X)): for t in range(len(X[0])): X[i, t] = imresize(X[i, t], (32, 32)) X = preprocess_input(X, data_format='channels_last') Y = to_categorical(y, len(X)) self.X = X self.Y = Y print(self.X.shape, self.Y.shape) def generate_labeled_data(self, num_labeled): if num_labeled is None: num_labeled = self.X_train[1] ind_train = list( range(0, 64 - np.mod(64, 64 // num_labeled), 64 // num_labeled)) ind_val = list( set( range((64 // num_labeled) // 2, 64 - np.mod(64, 64 // num_labeled), 64 // num_labeled)) - set(ind_train)) ind_test = list( set(range(self.X.shape[1])) - set(ind_train) - set(ind_val)) self.X_train_labeled = self.X[:, ind_train] self.Y_train_labeled = self.Y self.X_val_labeled = self.X[:, ind_val] self.Y_val_labeled = self.Y self.X_test = self.X[:, ind_test] self.Y_test = self.Y print(self.X_train_labeled.shape, self.Y_train_labeled.shape, self.X_val_labeled.shape, self.Y_val_labeled.shape, self.X_test.shape, self.Y_test.shape)
def __init__(self, config): """Constructor. """ self.config = config X = np.zeros((100, 72, self.config['size'], self.config['size'], 3), dtype=self.config['dtype']) y = np.zeros((100, ), dtype='int32') for i in range(len(X)): for j, t in enumerate(range(0, 360, 5)): img = imread(self.config['path'] + '/obj' + str(i + 1) + '__' + str(t) + '.png') img = imresize(img, (self.config['size'], self.config['size'])) X[i, j, :, :, :] = img y[i] = i X /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X = self.ZCA.fit_transform(X) if self.config['imagenet_preprocessing'] is True: X = np.pad(X, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X)): for t in range(len(X[0])): X[i, t] = imresize(X[i, t], (32, 32)) X = preprocess_input(X, data_format='channels_last') Y = to_categorical(y, len(X)) self.X = X self.Y = Y print(self.X.shape, self.Y.shape)
class MNISTRotatedDataset(): def __init__(self, config): """Constructor. """ self.config = config (X_train, y_train), (X_test, y_test) = mnist.load_data() X_train = X_train.reshape(-1, 28, 28, 1) X_test = X_test.reshape(-1, 28, 28, 1) X_train = X_train.astype(self.config['dtype']) X_test = X_test.astype(self.config['dtype']) X_train_rot = np.zeros((X_train.shape[0], self.config['rotations'], self.config['size'], self.config['size'], 1)) X_test_rot = np.zeros((X_test.shape[0], self.config['rotations'], self.config['size'], self.config['size'], 1)) for i in range(len(X_train)): img = Image.fromarray(np.reshape(X_train[i, :, :, :], (28, 28))) img = img.convert('L') phase = 360. * np.random.rand() for t in range(self.config['rotations']): _img = img.rotate(phase + t * 360. / self.config['rotations'], Image.BILINEAR).resize((28, 28), Image.BILINEAR).getdata() _img = np.reshape(np.array(_img, dtype=np.uint8), (28, 28, 1))[:self.config['size'], :self.config['size'], :] X_train_rot[i, t, :, :, :] = _img.astype(dtype=self.config['dtype']) for i in range(len(X_test)): img = Image.fromarray(np.reshape(X_test[i, :, :, :], (28, 28))) img = img.convert('L') phase = 360. * np.random.rand() for t in range(self.config['rotations']): _img = img.rotate(phase + t * 360. / self.config['rotations'], Image.BILINEAR).resize((28, 28), Image.BILINEAR).getdata() _img = np.reshape(np.array(_img, dtype=np.uint8), (28, 28, 1))[:self.config['size'], :self.config['size'], :] X_test_rot[i, t, :, :, :] = _img.astype(dtype=self.config['dtype']) X_train_rot /= 255. X_test_rot /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X_train_rot = self.ZCA.fit_transform(X_train_rot) X_test_rot = self.ZCA.transform(X_test_rot) if self.config['imagenet_preprocessing'] is True: X_train_rot = np.pad(X_train_rot, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') X_test_rot = np.pad(X_test_rot, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') X_train_rot = np.repeat(X_train_rot, 3, axis=-1) X_test_rot = np.repeat(X_test_rot, 3, axis=-1) for i in range(len(X_train_rot)): for t in range(len(X_train_rot[0])): X_train_rot[i, t] = imresize(X_train_rot[i, t], (32, 32)) for i in range(len(X_test_rot)): for t in range(len(X_test_rot[0])): X_test_rot[i, t] = imresize(X_test_rot[i, t], (32, 32)) X_train_rot = preprocess_input(X_train_rot, data_format='channels_last') X_test_rot = preprocess_input(X_test_rot, data_format='channels_last') nb_classes = 10 Y_train = to_categorical(y_train, nb_classes) Y_test = to_categorical(y_test, nb_classes) self.X_train = X_train_rot self.Y_train = Y_train self.X_test = X_test_rot self.Y_test = Y_test print(self.X_train.shape, self.Y_train.shape, self.X_test.shape, self.Y_test.shape) def generate_labeled_data(self, num_labeled=None): if num_labeled is None: num_labeled = round(0.9 * len(self.X_train)) while True: perm = np.random.permutation(len(self.X_train)) self.X_train_labeled = self.X_train[perm[:num_labeled]] self.Y_train_labeled = self.Y_train[perm[:num_labeled]] self.X_val_labeled = self.X_train[perm[num_labeled:round(1.1111 * num_labeled)]] self.Y_val_labeled = self.Y_train[perm[num_labeled:round(1.1111 * num_labeled)]] if np.all(np.any(self.Y_train_labeled, axis=0)) and np.all(np.any(self.Y_val_labeled, axis=0)): break print(self.X_train_labeled.shape, self.Y_train_labeled.shape, self.X_val_labeled.shape, self.Y_val_labeled.shape, self.X_test.shape, self.Y_test.shape)
class CIFAR10ScannedDataset(MNISTRotatedDataset): def __init__(self, config): """Constructor. """ self.config = config (X_train, y_train), (X_test, y_test) = cifar10.load_data() X_train = X_train.reshape(-1, 32, 32, 3) X_test = X_test.reshape(-1, 32, 32, 3) X_train = X_train.astype(self.config['dtype']) X_test = X_test.astype(self.config['dtype']) N = ((32 - self.config['size']) // self.config['stride']) + 1 X_train_scan = np.zeros((X_train.shape[0], N**2, self.config['size'], self.config['size'], 3)) X_test_scan = np.zeros((X_test.shape[0], N**2, self.config['size'], self.config['size'], 3)) for i in range(len(X_train)): for t1 in range(N): for t2 in (range(N) if (np.mod(t1, 2) == 0) else range(N - 1, -1, -1)): X_train_scan[i, t1 * N + (t2 if (np.mod(t1, 2) == 0) else (N - 1 - t2)), :, :, :] = X_train[i, ( t2 * self.config['stride']):( t2 * self.config['stride'] + self.config['size']), ( t1 * self.config['stride']):( t1 * self.config['stride'] + self.config['size']), :] for i in range(len(X_test)): for t1 in range(N): for t2 in (range(N) if (np.mod(t1, 2) == 0) else range(N - 1, -1, -1)): X_test_scan[i, t1 * N + (t2 if (np.mod(t1, 2) == 0) else (N - 1 - t2)), :, :, :] = X_test[ i, (t2 * self.config['stride']):( t2 * self.config['stride'] + self.config['size']), (t1 * self.config['stride']):( t1 * self.config['stride'] + self.config['size']), :] X_train_scan /= 255. X_test_scan /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X_train_scan = self.ZCA.fit_transform(X_train_scan) X_test_scan = self.ZCA.transform(X_test_scan) if self.config['imagenet_preprocessing'] is True: X_train_scan = np.pad(X_train_scan, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') X_test_scan = np.pad(X_test_scan, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X_train_scan)): for t in range(len(X_train_scan[0])): X_train_scan[i, t] = imresize(X_train_scan[i, t], (32, 32)) for i in range(len(X_test_scan)): for t in range(len(X_test_scan[0])): X_test_scan[i, t] = imresize(X_test_scan[i, t], (32, 32)) X_train_scan = preprocess_input(X_train_scan, data_format='channels_last') X_test_scan = preprocess_input(X_test_scan, data_format='channels_last') nb_classes = 10 Y_train = to_categorical(y_train, nb_classes) Y_test = to_categorical(y_test, nb_classes) self.X_train = X_train_scan self.Y_train = Y_train self.X_test = X_test_scan self.Y_test = Y_test print(self.X_train.shape, self.Y_train.shape, self.X_test.shape, self.Y_test.shape)