python


2、读入自己的数据到keras

<pre><code> # coding: utf-8 # In[1]: import numpy as np from PIL import Image import os import pandas as pd from sklearn.model_selection import train_test_split from tensorflow.python.keras import backend as K from tensorflow import keras from tensorflow.python.keras.layers import Conv2D, MaxPooling2D from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Dropout, Flatten from tensorflow.python.keras.optimizers import RMSprop # In[2]: def read_image(img_name): im = Image.open(img_name).convert('L') data = np.array(im) return data # In[3]: images = [] # In[4]: def read_imgs_labels(): # 照片目录 file_dir="D:\\work\\jupyterNotebook\\baidu_dianshi\\data\\micro_height_data" # 读入标签数据 df = pd.DataFrame(pd.read_csv('./data/micro_height_data_lable.csv',header=0)) # 获取lables 和img_name lables = df['lables'] img_name = df['img_name'] ''' #输出TFRecord文件的地址 filename="./output.tfrecords" #创建一个writer来写TFRecord文件 writer=tf.python_io.TFRecordWriter(filename) ''' # 遍历文件夹内所有图片,读取图片存到img,遍历到名字,在img_name中查找 # 查找到后,获取当前行数,取出对应的label,并替换为1-6的整数存入label_name for file in os.listdir(file_dir): # print(file) img_path=file_dir+'\\'+file #每个图片的地址 # img=Image.open(img_path) i = 0 for file_name in img_name: if file_name == file: label = lables[i] # print(label) if label == 'MOUNTAIN': label_name = 1 elif label == 'OCEAN': label_name = 2 elif label == 'DESERT': #沙漠 label_name = 3 elif label == 'LAKE': label_name = 4 elif label == 'FARMLAND': #农田 label_name = 5 elif label == 'CITY': label_name = 6 # 把标签存入txt文件 label = label_name # print (label)#ord():查看字符asc码 doc= open('label.txt','a') print(label,file=doc) break else: i = i+1 images.append(read_image(img_path)) doc.close() X_img = np.array(images) Y_label = np.loadtxt('label.txt') return X_img,Y_label # In[5]: images = [] (X_img,Y_label) = read_imgs_labels() # if label == 'MOUNTAIN': label_name = 1 # # elif label == 'OCEAN': label_name = 2 # # elif label == 'DESERT': label_name = 3 #沙漠 # # elif label == 'LAKE': label_name = 4 # # elif label == 'FARMLAND': label_name = 5 #农田 # # elif label == 'CITY': label_name = 6 # In[6]: print(X_img.shape) print(Y_label.shape) # In[7]: x_train, x_test, y_train, y_test = train_test_split(X_img, Y_label, test_size = 0.2, random_state= 30) print (x_train.shape) print (x_test.shape) print (y_train.shape) print (y_test.shape) # In[8]: batch_size = 5 num_classes = 6 epochs = 3 # input image dimensions img_rows, img_cols = 256, 256 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) # In[9]: 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') # In[10]: # convert class vectors to binary class matrices # 由于之前分六类写为了1-6 这里需要减1 变为 0-5 y_train = keras.utils.to_categorical(y_train-1, num_classes) y_test = keras.utils.to_categorical(y_test-1, num_classes) print(y_test[0]) # In[11]: model = Sequential() model.add(Conv2D(8, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(16, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(32, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) # In[12]: model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) # In[13]: history = model.fit(x_train, y_train, batch_size=5, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) # In[16]: model.save('./model.hdf5') # In[17]: model.summary() # In[18]: from tensorflow.python.keras.utils import plot_model from tensorflow.python.keras.utils.vis_utils import model_to_dot from IPython.display import SVG SVG(model_to_dot(model,show_shapes=True).create(prog='dot', format='svg')) # In[20]: import matplotlib.pyplot as plt image_arr = np.expand_dims(x_train[1], axis=0) layer_1 = K.function([model.layers[0].input], [model.layers[0].output]) f1 = layer_1([image_arr])[0] for _ in range(8): show_img = f1[:, :, :, _] show_img.shape = [254, 254] #plt.subplot(4, 8, _ + 1) plt.subplot(1, 8, _ + 1) plt.imshow(show_img, cmap='gray') plt.axis('off') plt.show() </code></pre>

页面列表

ITEM_HTML