python中将手写数据数组转换为图片输入出来

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Input, Activation, add, Dense, Flatten, Dropout, Multiply, Embedding, Lambda
from keras.layers import Conv2D, MaxPooling2D,PReLU
from keras import backend as K
import numpy as np
import sys
import scipy.misc
from keras.optimizers import SGD, Adam


batch_size = 128
num_classes = 10
epochs = 50

#isCenterloss = True
isCenterloss = False



# input image dimensions
img_rows, img_cols = 28, 28

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

for i in range(10):
    x=x_train[y_train==i]
    for j in range(np.max(x.shape)):
        scipy.misc.imsave('G:\\毕业论文\\train\\'+str(i)+'_'+str(j)+'.jpg', x[j])

猜你喜欢

转载自blog.csdn.net/qq_25964837/article/details/79810328