It may be better to have a title that generates an image from a numpy array in Python3 (dare to be a niche (^^;)
cifar is an image set of airplanes and cats, and cucumber is an image set of cucumbers (as the name implies). It is distributed in numpy array format so that it is easy to use for machine learning, but the motivation is to take a look at the actual contents.
https://www.cs.toronto.edu/~kriz/cifar.html https://github.com/workpiles/CUCUMBER-9
It seems that cucmber has the same format as cifer, but in reality it was different and required separate processing. Is the cucumber format more convenient for Tensorflow? (I don't know the reason)
data.shape = (10000, 3072) # cifar-10
data.shape = (1485, 1024) # CUCUMBER-9
The sample program is as follows. For Python2, use cPickle. Requires numpy and PIL.
import numpy as np
import _pickle,random
from PIL import Image
def unpickle(file):
fo = open(file, 'rb')
dict = _pickle.load(fo, encoding='latin-1')
fo.close()
return dict
def image_from_cifar(index,dic):
name = dic['filenames'][index]
data = dic['data'][index].reshape(3, 32, 32).transpose(1, 2, 0) # shape=(32, 32, 3)
img = Image.fromarray(data, 'RGB')
print(name)
img.save(name)
def image_from_cucumber(index,dic):
name = dic['filenames'][index]
r = dic['data'][index*3]
g = dic['data'][index*3+1]
b = dic['data'][index*3+2]
data = np.array([r,g,b]).T.reshape(32,32,3)
img = Image.fromarray(data, 'RGB')
print(name)
img.save(name)
if __name__ == "__main__":
dic = unpickle('cifar-10-batches-py/data_batch_1')
image_from_cifar(random.randint(0,len(dic['filenames'])),dic)
dic = unpickle('CUCUMBER-9-master/prototype_1/cucumber-9-python/data_batch_1')
image_from_cucumber(random.randint(0,len(dic['filenames'])),dic)
If you execute each data set in the current directory, one will be randomly taken out and saved as an image.
Recommended Posts