-
Notifications
You must be signed in to change notification settings - Fork 977
/
Copy pathsampler-cifar10-2.1.0.py
executable file
·70 lines (59 loc) · 1.72 KB
/
sampler-cifar10-2.1.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''Demonstrates how to sample and plot CIFAR10 images
using Keras API
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# numpy package
import numpy as np
import math
# keras mnist module
from keras.datasets import cifar10
# for plotting
import matplotlib.pyplot as plt
# load dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
class_id = 0
class_count = 0
images = None
for i in range(100):
while True:
index = np.random.randint(0, x_train.shape[0], size=1)
image = x_train[index]
if y_train[index] == class_id:
break
if images is None:
images = image
else:
images = np.concatenate([images, image], axis=0)
class_count += 1
if class_count == 10:
class_id += 1
class_count = 0
print(images.shape)
plt.figure(figsize=(10, 10))
num_images = images.shape[0]
image_size = images.shape[1]
rows = int(math.sqrt(num_images))
row_names = ['{}'.format(row) for row in ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']]
index = 0
for i in range(num_images):
ax = plt.subplot(rows, rows, i + 1)
image = images[i, :, :, :]
image = np.reshape(image, [image_size, image_size, 3])
plt.imshow(image)
# plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.grid(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
if (i % rows) == 0:
ax.set_ylabel(row_names[index], rotation=45, size='large')
ax.yaxis.labelpad = 20
print(row_names[index])
index += 1
# plt.tight_layout()
plt.savefig("cifar10-samples.png")
plt.show()
plt.close('all')