第四讲 网络八股拓展--自定义数据集加载
1 # -*- coding: utf-8 -*- 2 3 import tensorflow as tf 4 from PIL import Image 5 import numpy as np 6 import os 7 8 rt_path = '/home/ubuntu/Documents/中国大学MOOCTF笔记2.1共享给所有学习者/class4/MNIST_FC' 9 10 11 train_path = os.path.join(rt_path, 'mnist_image_label/mnist_train_jpg_60000/') 12 train_txt = os.path.join(rt_path, 'mnist_image_label/mnist_train_jpg_60000.txt') 13 x_train_savepath = os.path.join(rt_path, 'mnist_image_label/mnist_x_train.npy') 14 y_train_savepath = os.path.join(rt_path, 'mnist_image_label/mnist_y_train.npy') 15 16 test_path = os.path.join(rt_path, 'mnist_image_label/mnist_test_jpg_10000/') 17 test_txt = os.path.join(rt_path, 'mnist_image_label/mnist_test_jpg_10000.txt') 18 x_test_savepath = os.path.join(rt_path, 'mnist_image_label/mnist_x_test.npy') 19 y_test_savepath = os.path.join(rt_path, 'mnist_image_label/mnist_y_test.npy') 20 21 22 def generateds(path, txt): 23 with open(txt, 'r') as f: 24 contents = f.readlines() 25 x, y_ = [], [] 26 i = 0 27 j = len(contents) 28 for content in contents: 29 value = content.split() # 以空格分开,图片路径为value[0] , 标签为value[1] , 存入列表 30 img_path = path + value[0] # 拼出图片路径和文件名 31 img = Image.open(img_path) # 读入图片 32 img = np.array(img.convert('L')) # 图片变为8位宽灰度值的np.array格式 33 img = img / 255. # 数据归一化 (实现预处理) 34 x.append(img) 35 y_.append(value[1]) # 标签贴到列表y_ 36 print('loading %d of %d:'%(i, j) + content) # 打印状态提示 37 i += 1 38 39 x = np.array(x) 40 y_ = np.array(y_) 41 y_ = y_.astype(np.int64) 42 return x, y_ # 返回输入特征x,返回标签y_ 43 44 45 if os.path.exists(x_train_savepath) and os.path.exists(y_train_savepath) and os.path.exists( 46 x_test_savepath) and os.path.exists(y_test_savepath): 47 print('--------------------------------Load Datasets------------------------') 48 x_train_save = np.load(x_train_savepath) 49 y_train = np.load(y_train_savepath) 50 x_test_save = np.load(x_test_savepath) 51 y_test = np.load(y_test_savepath) 52 x_train = np.reshape(x_train_save, (len(x_train_save), 28, 28)) 53 x_test = np.reshape(x_test_save, (len(x_test_save), 28, 28)) 54 else: 55 print('-------------------------Generate Datasets-----------------------') 56 x_train, y_train = generateds(train_path, train_txt) 57 x_test, y_test = generateds(test_path, test_txt) 58 59 print('--------------------Save Datasets--------------------------') 60 x_train_save = np.reshape(x_train, (len(x_train), -1)) 61 x_test_save = np.reshape(x_test, (len(x_test), -1)) 62 np.save(x_train_savepath, x_train_save) 63 np.save(y_train_savepath, y_train) 64 np.save(x_test_savepath, x_test_save) 65 np.save(y_test_savepath, y_test) 66 67 68 model = tf.keras.models.Sequential([ 69 tf.keras.layers.Flatten(), 70 tf.keras.layers.Dense(128, activation='relu'), 71 tf.keras.layers.Dense(10, activation='softmax') 72 ]) 73 74 model.compile(optimizer='adam', 75 loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), 76 metrics=['sparse_categorical_accuracy']) 77 78 model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1) 79 model.summary() 80