text: # -*- coding: utf-8 -*-
  """
  Created on Sun Jun 24 09:43:25 2018
  
  @author: new
  """
  import numpy as np
  import os
  import sys
  import keras.backend as K
  from six.moves import cPickle
  import cv2
  import numpy as np
  import tensorflow as tf
  import keras
  from keras.models import Sequential
  from keras.layers import Conv2D, MaxPooling2D
  from keras.layers.core import Dense, Dropout, Activation, Flatten
  from keras.layers.convolutional import Convolution2D, MaxPooling2D
  from keras.preprocessing.image import ImageDataGenerator
  from keras.optimizers import SGD, Adadelta, Adagrad
  from keras.utils import np_utils, generic_utils
  from keras.utils import plot_model
  import matplotlib.pyplot as plt
  import numpy as np
  import scipy.io as sio
  
  os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  gpu_options = tf.GPUOptions(allow_growth=True)
  sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
  
  #【0】设置超参
  batch_size = 32
  num_classes = 10
  epochs = 5
  data_augmentation = True
  
  
  def load_batch(fpath, label_key='labels'):
      f = open(fpath, 'rb')
      if sys.version_info < (3,):
          d = cPickle.load(f)
      else:
          d = cPickle.load(f, encoding='bytes')
          # decode utf8
          d_decoded = {}
          for k, v in d.items():
              d_decoded[k.decode('utf8')] = v
          d = d_decoded
      f.close()
      data = d['data']
      labels = d[label_key]
  
      data = data.reshape(data.shape[0], 3, 32, 32)
      return data, labels
  def load_data():
      dirname = 'C:/Users/18301/Desktop/cifar-10-batches-py'
      num_train_samples = 50000
  
      x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8')
      y_train = np.empty((num_train_samples,), dtype='uint8')
      for i in range(1, 6):
          fpath = os.path.join(dirname, 'data_batch_' + str(i))
          (x_train[(i - 1) * 10000: i * 10000, :, :, :],
           y_train[(i - 1) * 10000: i * 10000]) = load_batch(fpath)
      fpath = os.path.join(dirname, 'test_batch')
      x_test, y_test = load_batch(fpath)
  
      y_train = np.reshape(y_train, (len(y_train), 1))
      y_test = np.reshape(y_test, (len(y_test), 1))
  
      if K.image_data_format() == 'channels_last':
          x_train = x_train.transpose(0, 2, 3, 1)
          x_test = x_test.transpose(0, 2, 3, 1)
  
      return (x_train, y_train), (x_test, y_test)
  
  
  (x_train,  y_train), (x_test, y_test)=load_data()
  print('x_train shape:', x_train.shape)
  print('y_train shape:', y_train.shape)
  print('x_test shape:', x_test.shape)
  print('y_test shape:', y_test.shape)
  
  plt.figure(1)
  plt.imshow(x_train[0]) # 显示第一张训练图片
  plt.figure(2)
  plt.imshow(x_test[0])  # 显示第一张测试图片
  
   # 【3】将标签转化成 one-hot 编码
  y_train = keras.utils.to_categorical(y_train, num_classes)
  y_test = keras.utils.to_categorical(y_test, num_classes)
  
  # 【4】构建深度CNN序贯模型
  model = Sequential()
  model.add(Conv2D(32, (3, 3), padding='same',
                   input_shape=x_train.shape[1:]))
  model.add(Activation('relu'))
  model.add(Conv2D(32, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  
  model.add(Conv2D(64, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(Conv2D(64, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  
  model.add(Flatten())
  model.add(Dense(512))
  model.add(Activation('relu'))
  model.add(Dropout(0.5))
  model.add(Dense(num_classes))
  model.add(Activation('softmax'))
  
  print(model.summary())                              # 打印模型概况
  
  # 【5】编译模型
  opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)#初始化一个 RMSprop 优化器
  model.compile(loss='categorical_crossentropy',
                optimizer=opt,
                metrics=['accuracy'])
  
  # 【6】数据预处理/增强+模型训练
  x_train = x_train.astype('float32')
  x_test = x_test.astype('float32')
  x_train /= 255
  x_test /= 255
  
  if not data_augmentation:
      print('Not using data augmentation.')
      model.fit(x_train, y_train,
                batch_size=batch_size,
                epochs=epochs,
                validation_data=(x_test, y_test),
                )
  else:
      print('Using real-time data augmentation.')
  
      # ImageDataGenerator:图片生成器，用以生成一个batch的图像数据，训练时该函数会无限生成数据
      # 直到达到规定的epoch次数。图片生成(CPU)和训练（GPU）并行执行。
  
      datagen = ImageDataGenerator(
          featurewise_center=False,  
          samplewise_center=False,  
          featurewise_std_normalization=False,  
          samplewise_std_normalization=False,  
          zca_whitening=False, 
          rotation_range=0,        # 随机旋转的角度范围
          width_shift_range=0.1,   # 随机水平偏移的幅度范围
          height_shift_range=0.1,  
          horizontal_flip=True,    # 随机水平翻转
          vertical_flip=False)     
  
  
      datagen.fit(x_train)         # 计算样本的统计信息，进行数据预处理（如去中心化，标准化）
  
      model.fit_generator(datagen.flow(x_train, y_train,           # datagen.flow()不断生成一个batch的数据用于模型训练
                                       batch_size=batch_size),
                          epochs=epochs,
                          validation_data=(x_test, y_test),
                          workers=4)
  
  # 【7】保存模型以及权重
  save_dir = os.path.join(os.getcwd(), 'saved_models')
  model_name = 'keras_cifar10_trained_model.h5'
  if not os.path.isdir(save_dir):
      os.makedirs(save_dir)
  model_path = os.path.join(save_dir, model_name)
  model.save(model_path)
  print('Saved trained model at %s ' % model_path)
  
  # 【8】测试集评估模型
  scores = model.evaluate(x_test, y_test, verbose=1)
  print('Test loss:', scores[0])
  print('Test accuracy:', scores[1])