import plotting import numpy as np from data_reader import load from settings import DATA_DIR, DATA_SEED trainx, trainy = load(DATA_DIR, subset='train') rng_data = np.random.RandomState(DATA_SEED) inds = rng_data.permutation(trainx.shape[0]) trainx = trainx[inds] trainy = trainy[inds] txs = [] for j in range(10): txs.append(trainx[trainy == j][:10]) txs = np.concatenate(txs, axis=0) img_bhwc = np.transpose(txs, (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='CIFAR10 samples') plotting.plt.savefig("cifar_sample.png")
from data_reader import load from settings import DATA_DIR import numpy as np trainx, trainy = load(DATA_DIR, subset='train') print('The training data is composed of: ' + str(np.shape(trainx)[0]) + ' examples.') testx, testy = load(DATA_DIR, subset='test') print('The testing data is composed of: ' + str(np.shape(testx)[0]) + ' examples.')
tx_resized = [] for n in range(batch_size): tx_resized.append( preprocess(np.transpose(train_temp[n], (1, 2, 0)))) tx_resized = np.concatenate(tx_resized, axis=0) output_features.append(extract_features(tx_resized)) return np.concatenate(output_features, axis=0) with open('inception_v3.pkl', 'rb') as f: params = pickle.load(f) net = build_network() lasagne.layers.set_all_param_values(net['softmax'], params['param values']) trainx, _ = load(DATA_DIR, subset='train') testx, _ = load(DATA_DIR, subset='test') minibatch_size = 10 feature_layer = net['pool3'] print("Extracting features from train data...") train_features = extract(trainx, feature_layer, minibatch_size) print("Extracting features from test data...") test_features = extract(testx, feature_layer, minibatch_size) print(train_features.shape) print(test_features.shape) np.savez_compressed('cifar_train_x', train_features) np.savez_compressed('cifar_test_x', test_features)
import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA from data_reader import load, get_data from settings import DATA_DIR, DATA_SEED samples_per_class = 500 data_type = 'train' # 'test' or 'train' data_name = 'cifar_train_triplet_100_x.npz' datax = get_data(data_name) _, datay = load(DATA_DIR, subset=data_type) pca = PCA(n_components=2) X_new = pca.fit_transform(datax) print(datax.shape) rng_data = np.random.RandomState(DATA_SEED) inds = rng_data.permutation(X_new.shape[0]) X_new = X_new[inds] datay = datay[inds] plt.rcParams["figure.figsize"] = (15, 12) for j in range(10): txs = X_new[datay == j][:samples_per_class] plt.scatter(txs[:, 0], txs[:, 1]) plt.title('PCA 2D transform on ' + data_name, fontsize=20) plt.xlabel('PC1', fontsize=18) plt.ylabel('PC2', fontsize=18) plt.savefig('pca_' + data_name + '.png')