Created
April 18, 2017 04:30
-
-
Save yujuwon/d111e9d60b00b9a014dda15819aa227a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pickle | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| %matplotlib inline | |
| def unpickle(file): | |
| fo = open(file, 'rb') | |
| dict = pickle.load(fo) | |
| fo.close() | |
| return dict | |
| def grayscale(a): | |
| return a.reshape(a.shape[0], 3, 32, 32).mean(1).reshape(a.shape[0], -1) | |
| def data_show(data, data_len): | |
| plt.rcParams['figure.figsize'] = (2, 2) | |
| for i in range(data_len): | |
| img = np.reshape(data[i,:], (32, 32)) | |
| plt.subplot(1, data_len, i+1) | |
| plt.imshow(img, cmap='gray') | |
| names = unpickle('./cifar-10-batches-py/batches.meta')['label_names'] | |
| data, labels = [], [] | |
| for i in range(1, 6): | |
| filename = './cifar-10-batches-py/data_batch_' + str(i) | |
| batch_data = unpickle(filename) | |
| if len(data) > 0: | |
| data = np.vstack((data, batch_data['data'])) | |
| labels = np.hstack((labels, batch_data['labels'])) | |
| else: | |
| data = batch_data['data'] | |
| labels = batch_data['labels'] | |
| data = grayscale(data) | |
| x = np.matrix(data) | |
| y = np.array(labels) | |
| horse_indices = np.where(y == 7)[0] | |
| horse_x = x[horse_indices] | |
| input_dim = np.shape(horse_x)[1] | |
| hidden_dim = 100 | |
| ae = Autoencoder(input_dim, hidden_dim, 10000) | |
| ae.train(horse_x) | |
| test_data = unpickle('./cifar-10-batches-py/test_batch') | |
| test_x = grayscale(test_data['data']) | |
| test_labels = np.array(test_data['labels']) | |
| test_1 = test_x[13].reshape(1, np.shape(test_x[13])[0]) | |
| print(np.shape(test_1)) | |
| data_show(test_1, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment