1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
import tensorflow as tf from tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D from tensorflow.keras import Model,Sequential import matplotlib.pyplot as plt import numpy as np
train_x = np.load("train_x.npy") train_y = np.load("train_y.npy")
plt.imshow(train_x[0]) plt.show() print(train_x.shape) print(train_y.shape)
model = Sequential() model.add(Conv2D(32, 3, activation='relu', input_shape=(224, 224, 3))) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(64, 3, activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(64, 3, activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(128, 3, activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(128, 3, activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Flatten()) model.add(Dropout(0.2)) model.add(Dense(1024,activation='relu')) model.add(Dense(1024,activation='relu')) model.add(Dense(3,activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = model.fit(train_x, train_y, batch_size=20, epochs=5)
l = np.array(history.history['loss']) a = np.array(history.history['accuracy']) step = np.linspace(1,50,50) plt.plot(step,l,label="Train Loss") plt.legend(loc='upper right') plt.title('epoch-loss') plt.xlim((0, 50)) plt.gca().set_ylim(bottom=0) plt.xlabel('epoch') plt.ylabel('loss') plt.show() plt.plot(step,a,label="Train Acc") plt.legend(loc='lower right') plt.title('epoch-acc') plt.xlim((0, 50)) plt.gca().set_ylim(bottom=0) plt.xlabel('epoch') plt.ylabel('acc') plt.show()
|