1 from tensorflow.contrib.keras.api.keras.preprocessing.image import ImageDataGenerator,img_to_array 2 from tensorflow.contrib.keras.api.keras.models import Sequential 3 from tensorflow.contrib.keras.api.keras.layers import Dense, Dropout, Activation, Flatten 4 from tensorflow.contrib.keras.api.keras.layers import Conv2D, MaxPooling2D 5 IMAGE_SIZE = 224 6 img_rows= 224 7 img_cols = 224 8 # 训练图片大小 9 epochs = 50#原来是50 10 # 遍历次数 11 batch_size = 32 12 # 批量大小 13 nb_train_samples = 256*2 14 # 训练样本总数 15 nb_validation_samples = 64*2 16 # 测试样本总数 17 train_data_dir = ‘D:\\pycode\\learn\\data\\train_data\\‘ 18 validation_data_dir = ‘D:\\pycode\\learn\\data\\test_data\\‘ 19 # 样本图片所在路径 20 FILE_PATH = ‘age.h5‘ 21 22 train_datagen = ImageDataGenerator( 23 rescale=1. / 255, 24 horizontal_flip=True) 25 26 test_datagen = ImageDataGenerator(rescale=1. / 255) 27 28 train_generator = train_datagen.flow_from_directory( 29 train_data_dir, 30 target_size=(img_rows, img_cols), 31 batch_size=batch_size, 32 class_mode=‘categorical‘) 33 34 validation_generator = test_datagen.flow_from_directory( 35 validation_data_dir, 36 target_size=(img_rows, img_cols), 37 batch_size=batch_size, 38 class_mode=‘categorical‘) 39 40 # self.train = train_generator 41 # self.valid = validation_generator 42 print(validation_generator.class_indices)