def init_dataset(X_train_orig, Y_train_orig, X_test_orig, Y_test_orig): X_train = X_train_orig / 255. X_test = X_test_orig / 255. Y_train = cnn_utils.convert_to_one_hot(Y_train_orig, 6).T Y_test = cnn_utils.convert_to_one_hot(Y_test_orig, 6).T # print ("number of training examples = " + str(X_train.shape[0])) # print ("number of test examples = " + str(X_test.shape[0])) # print ("X_train shape: " + str(X_train.shape)) # print ("Y_train shape: " + str(Y_train.shape)) # print ("X_test shape: " + str(X_test.shape)) # print ("Y_test shape: " + str(Y_test.shape)) return X_train, Y_train, X_test, Y_test
def main(): # Loading the data (signs) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset( ) # Example of a picture index = 6 plt.imshow(X_train_orig[index]) print("y = " + str(np.squeeze(Y_train_orig[:, index]))) if DISPLAY: plt.show() X_train = X_train_orig / 255. X_test = X_test_orig / 255. Y_train = convert_to_one_hot(Y_train_orig, 6).T Y_test = convert_to_one_hot(Y_test_orig, 6).T print("number of training examples = " + str(X_train.shape[0])) print("number of test examples = " + str(X_test.shape[0])) print("X_train shape: " + str(X_train.shape)) print("Y_train shape: " + str(Y_train.shape)) print("X_test shape: " + str(X_test.shape)) print("Y_test shape: " + str(Y_test.shape)) # tf.reset_default_graph() # with tf.Session() as sess: # np.random.seed(1) # X, Y = create_placeholders(64, 64, 3, 6) # parameters = initialize_parameters() # Z3 = forward_propagation(X, parameters) # init = tf.global_variables_initializer() # sess.run(init) # a = sess.run(Z3, {X: np.random.randn(2, 64, 64, 3), Y: np.random.randn(2, 6)}) # print("Z3 = " + str(a)) # # tf.reset_default_graph() # # with tf.Session() as sess: # np.random.seed(1) # X, Y = create_placeholders(64, 64, 3, 6) # parameters = initialize_parameters() # Z3 = forward_propagation(X, parameters) # cost = compute_cost(Z3, Y) # init = tf.global_variables_initializer() # sess.run(init) # a = sess.run(cost, {X: np.random.randn(4, 64, 64, 3), Y: np.random.randn(4, 6)}) # print("cost = " + str(a)) _, _, parameters = model(X_train, Y_train, X_test, Y_test, learning_rate=0.0015, num_epochs=150)
def train_model(image_path, learning_rate=0.001, num_epoch=100, batch_size=5): X_train = np.load(image_path + ".npy") Y_train = np.load(image_path + ".npy") X_train = X_train / 255 #Creating one-hot for label print(Y_train.shape) print(Y_train[0][1]) Y_train = convert_to_one_hot(Y_train, 10).T n_Y = Y_train.shape[1] x = tf.placeholder(tf.float32, [None, 227, 227, 3]) Y = tf.placeholder(tf.float32, [None, n_Y]) keep_prob = tf.placeholder(tf.float32) model = AlexNet(x, keep_prob, 10, []) score = model.fc8 cost = tf.reduce_mean( (tf.nn.softmax_cross_entropy_with_logits_v2(logits=score, labels=Y))) softmax = tf.nn.softmax(score) costs = [] optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(cost) init = tf.global_variables_initializer() [m, nH, nW, nCH] = X_train.shape seed = 0 #tf.reset_default_graph() with tf.Session() as sess: sess.run(init) sess.run(model.load_initial_weights(sess)) for i in range(num_epoch): minibatch_cost = 0 num_minibatch = int(m / batch_size) # number of batches seed = seed + 1 minibatches = random_mini_batches(X_train, Y_train, batch_size, seed) for minibatch in minibatches: X_batches, Y_batches = minibatch #Use cost and optimizer to run _, c = sess.run([optimizer, cost], { x: X_batches, Y: Y_batches, keep_prob: 1 }) minibatch_cost += c minibatch_cost = minibatch_cost / num_minibatch if i % 5 == 0: print("Cost===>" + str(c)) costs.append(c) plt.plot(np.squeeze(costs)) plt.ylabel('cost') plt.xlabel('epoch') plt.title('learning_rate' + str(learning_rate)) plt.show()
import os import tensorflow as tf from tensorflow.python.framework import ops try: from cnn_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict except ImportError: raise ImportError('The file is not found. Please check the file name!') os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Loading the data (signs) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset() X_train = X_train_orig / 255. X_test = X_test_orig / 255. Y_train = convert_to_one_hot(Y_train_orig, 6).T Y_test = convert_to_one_hot(Y_test_orig, 6).T print("number of training examples = " + str(X_train.shape[0])) print("number of test examples = " + str(X_test.shape[0])) print("X_train shape: " + str(X_train.shape)) print("Y_train shape: " + str(Y_train.shape)) print("X_test shape: " + str(X_test.shape)) print("Y_test shape: " + str(Y_test.shape)) def create_placeholders(n_H0, n_W0, n_C0, n_y): """ Creates the placeholders for the tensorflow session. Arguments: n_H0 -- scalar, height of an input image
train_accuracy = accuracy.eval({X: X_train, Y: Y_train}) test_accuary = accuracy.eval({X: X_test, Y: Y_test}) print("训练集准确度:" + str(train_accuracy)) print("测试集准确度:" + str(test_accuary)) return (train_accuracy, test_accuary, parameters) if __name__ == '__main__': X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = cnn_utils.load_dataset( ) index = 8 plt.imshow(X_train_orig[index]) plt.show() print("y = " + str(np.squeeze(Y_train_orig[:, index]))) X_train = X_train_orig / 255. X_test = X_test_orig / 255. Y_train = cnn_utils.convert_to_one_hot(Y_train_orig, 6).T Y_test = cnn_utils.convert_to_one_hot(Y_test_orig, 6).T print("number of training examples = " + str(X_train.shape[0])) print("number of test examples = " + str(X_test.shape[0])) print("X_train shape: " + str(X_train.shape)) print("Y_train shape: " + str(Y_train.shape)) print("X_test shape: " + str(X_test.shape)) print("Y_test shape: " + str(Y_test.shape)) conv_layers = {} _, _, parameters = model(X_train, Y_train, X_test, Y_test, num_epochs=150)
plt.xlabel('iterations (per tens)') plt.ylabel('cost') plt.title('Learning rate =' + str(learning_rate)) plt.show() # 计算预测的正确率 predict_op = tf.argmax(Z3, 1) correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1)) # 计算在train_Set 和 test_Set上的准确率 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy) train_accuracy = accuracy.eval({X: train_X, Y: train_Y}) test_accuracy = accuracy.eval({X: train_X, Y: train_Y}) print('Train Accuracy:', train_accuracy) print('Test Accuracy:', test_accuracy) return train_accuracy, test_accuracy, parameters if __name__ == '__main__': train_X, train_Y, test_X, test_Y, classes = cnn_utils.load_dataset() # 归一化数据集 train_X = train_X / 255 test_X = test_X / 255 # 将标签转换为one-hot编码 train_Y = cnn_utils.convert_to_one_hot(train_Y, 6).T test_Y = cnn_utils.convert_to_one_hot(test_Y, 6).T _, _, parameters = model(train_X, train_Y, test_X, test_Y, num_epochs=100)
# init = tf.global_variables_initializer() # sess_test.run(init) # a = sess_test.run(cost,{X:np.random.randn(4,64,64,3),Y:np.random.randn(4,6)}) # print('cost = '+str(a)) # # sess_test.close() ##2.1.4构建模型 X_train_orig , Y_train_orig , X_test_orig , Y_test_orig , classes = cnn_utils.load_dataset() # index = 6 # plt.imshow(X_train_orig[index]) # print ("y = " + str(np.squeeze(Y_train_orig[:, index]))) X_train = X_train_orig/255. X_test = X_test_orig/255. Y_train = cnn_utils.convert_to_one_hot(Y_train_orig, 6).T Y_test = cnn_utils.convert_to_one_hot(Y_test_orig, 6).T print ("number of training examples = " + str(X_train.shape[0])) print ("number of test examples = " + str(X_test.shape[0])) print ("X_train shape: " + str(X_train.shape)) print ("Y_train shape: " + str(Y_train.shape)) print ("X_test shape: " + str(X_test.shape)) print ("Y_test shape: " + str(Y_test.shape)) conv_layers = {} def model(X_train,Y_train,X_test,Y_test,learning_rate = 0.009, num_epochs = 100,minibatch_size = 64,print_cost = True,isPlot =True): ''' 使用TensorFlow实现三层的卷积神经网络 conv2d->relu->maxpool->>conv2d->relu->maxpool->flatten->fullyconnnected :param X_train: 训练数据,维度为(None,64,64,3)