def load_input(): # load data next_feature, next_label = load_data(JSON_TRAIN, TRAIN_IMAGES_DIR, is_training=True, batch_size=BATCH_SIZE) x_test, y_test = load_data(JSON_VAL, VAL_IMAGES_DIR, is_training=False, batch_size=BATCH_SIZE) return next_feature, next_label, x_test, y_test
import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import Dense, Embedding, Input, Flatten, AveragePooling1D from keras.layers import LSTM, Bidirectional, Dropout, TimeDistributed from keras.preprocessing import text, sequence from keras.callbacks import EarlyStopping, ModelCheckpoint from sklearn.model_selection import train_test_split from input import load_data print('------- LOADING DATA... -------') x, y, vocabulary, vocabularyInv = load_data() print('------- SPLITING DATA... -------') xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size=0.2, random_state=42) sequence_length = x.shape[1] # 56 max_features = len(vocabularyInv) xTrain = sequence.pad_sequences(xTrain, maxlen=sequence_length) xTest = sequence.pad_sequences(xTest, maxlen=sequence_length) epochs = 2 batch_size = 30 model = Sequential() model.add(Embedding(max_features, 128, input_length=sequence_length)) model.add(Bidirectional(LSTM(64, recurrent_dropout=0.2,
def do_train(num_class, log_dir): with tf.device("/cpu:0"): # load data next_feature, next_label = load_data(JSON_TRAIN, TRAIN_IMAGES_DIR, is_training=True, batch_size=BATCH_SIZE, num_epochs=NUM_EPOCHS) x_test, y_test = load_data(JSON_VAL, VAL_IMAGES_DIR, is_training=False, batch_size=BATCH_SIZE, num_epochs=1) # 创建模型,定义输入输出 model = resnet_model_keras(num_class) model.load_weights(filepath="weights\\resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5", by_name=True) logging = TensorBoard(log_dir=log_dir) checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5', monitor='val_loss', verbose=1, save_weights_only=True, save_best_only=True, period=5) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1) early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1) # loss_fn = losses.sparse_categorical_crossentropy() adm = optimizers.Adam() # metrics_fn = metrics.sparse_categorical_crossentropy() # 定义优化器,loss function,评估函数metrics model.compile(optimizer=adm, loss="categorical_crossentropy", metrics=["categorical_accuracy"] ) # 训练模型 # 记录运行计算图一次的时间 # start_time = time.time() # math.ceil向上取整,一个epoch需要将数据集整个遍历一遍 # steps_per_epoch = math.ceil(STEPS_PER_EPOCH_FOR_TRAIN) # val_steps = math.ceil(STEPS_PER_EPOCH_FOR_EVAL) steps_per_epoch = math.ceil(500/BATCH_SIZE) val_steps = math.ceil(500/BATCH_SIZE) # validation_steps = int(320/BATCH_SIZE) print("steps_per_epoch:",steps_per_epoch) hist = model.fit(next_feature, next_label, batch_size=None, epochs=NUM_EPOCHS, validation_data=(x_test, y_test), verbose=1, callbacks=[checkpoint], steps_per_epoch=steps_per_epoch, validation_steps=val_steps) """ hist = model.fit(next_feature, next_label, batch_size=None, epochs=NUM_EPOCHS, verbose=1, callbacks=[logging, checkpoint, reduce_lr, early_stopping], steps_per_epoch=steps_per_epoch) """ # duration_time = time.time() - start_time print("history loss:") # print(hist.history.keys()) print(hist.history["loss"]) print("history val_loss:") print(hist.history["loss"]) plt.plot(hist.history["loss"]) plt.plot(hist.history["val_loss"]) plt.title("model loss") plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/res_model_loss.png") plt.show() # """ plt.plot(hist.history["categorical_accuracy"]) plt.plot(hist.history["val_categorical_accuracy"]) plt.title("model accuracy") plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/res_model_top1.png") plt.show() plt.plot(hist.history["top3_accuracy"]) plt.plot(hist.history["val_top3_accuracy"]) plt.title("model accuracy3") plt.ylabel('accuracy3') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/res_model_top3.png") plt.show() # """ # 评估模型 print("evaluate:") loss, accuracy = model.evaluate(x_test, y_test, verbose=1, steps=val_steps) print('Test loss:', loss) print('Test accuracy:', accuracy) # 保存模型 # model.save_weights(log_dir + "\scence_resnet_model.h5") return
from keras.utils import np_utils from keras.models import Sequential from keras.layers import Dense, Activation, Conv1D, MaxPooling1D, Flatten from keras.optimizers import RMSprop import input (X_train, y_train, frame, cate) = input.load_data("wav") (X_test, y_test, frame, cate) = input.load_data("wav") y_train = np_utils.to_categorical(y_train, num_classes=cate) y_test = np_utils.to_categorical(y_test, num_classes=cate) model = Sequential([ Dense(32, input_dim=frame * 14), Activation('relu'), Dense(cate), Activation('softmax'), ]) # Another way to define your optimizer rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0) # We add metrics to get more results you want to see model.compile(optimizer=rmsprop, loss='categorical_crossentropy', metrics=['accuracy']) print('Training ------------') model.fit(X_train, y_train, epochs=10, batch_size=32) print('\nTesting ------------')
from __future__ import division from __future__ import print_function # Imports import numpy as np import tensorflow as tf import input import train from tensorflow.contrib import learn from tensorflow.contrib.learn.python import SKCompat tf.logging.set_verbosity(tf.logging.INFO) input.load_data() train_data = np.load("train_data.npy") #train_label_super = np.load("train_label_super.npy") test_data = np.load("test_data.npy") #test_label_super = np.load("test_label_super.npy") train_label_sub = np.load("train_label_sub.npy") test_label_sub = np.load("test_label_sub.npy") print(train_data.shape) # Create the Estimator mnist_classifier = SKCompat( learn.Estimator(model_fn=train.cnn_model, model_dir="./model"))
def train(log_dir): # load data next_feature, next_label = load_data(JSON_TRAIN, TRAIN_IMAGES_DIR, is_training=True, batch_size=BATCH_SIZE, num_epochs=NUM_EPOCHS) x_test, y_test = load_data(JSON_VAL, VAL_IMAGES_DIR, is_training=False, batch_size=BATCH_SIZE, num_epochs=1) tl_model = resnet50_model() logging = TensorBoard(log_dir=log_dir) checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}.h5', monitor='loss', verbose=1, save_weights_only=True, save_best_only=True, period=5) reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.1, patience=3, verbose=1) early_stopping = EarlyStopping(monitor='loss', min_delta=0, patience=10, verbose=1) # math.ceil向上取整,一个epoch需要将数据集整个遍历一遍 steps_per_epoch = math.ceil(STEPS_PER_EPOCH_FOR_TRAIN) val_steps = math.ceil(STEPS_PER_EPOCH_FOR_EVAL) steps_per_epoch = math.ceil(500 / BATCH_SIZE) val_steps = math.ceil(500 / BATCH_SIZE) # validation_steps = int(320/BATCH_SIZE) print("steps_per_epoch:", steps_per_epoch) hist = tl_model.fit(next_feature, next_label, batch_size=None, epochs=NUM_EPOCHS, validation_data=(x_test, y_test), verbose=1, callbacks=[checkpoint], steps_per_epoch=steps_per_epoch, validation_steps=val_steps) # duration_time = time.time() - start_time print("history:") # print(hist.history.keys()) print(hist.history["loss"]) plt.plot(hist.history["loss"]) plt.plot(hist.history["val_loss"]) plt.title("model loss") plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(LOG_DIR + "/fig/model_loss.png") plt.show() # """ plt.plot(hist.history["categorical_accuracy"]) plt.plot(hist.history["val_categorical_accuracy"]) plt.title("model accuracy") plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(LOG_DIR + "/fig/model_top1.png") plt.show() plt.plot(hist.history["top3_accuracy"]) plt.plot(hist.history["val_top3_accuracy"]) plt.title("model accuracy3") plt.ylabel('accuracy3') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(LOG_DIR + "/fig/model_top3.png") plt.show() # """ # 评估模型 print("evaluate:") loss, accuracy, top3 = tl_model.evaluate(x_test, y_test, verbose=1, steps=val_steps) print('Test loss:', loss) print('Test accuracy:', accuracy) print('Test accuracy of top3:', top3) return
from __future__ import absolute_import from __future__ import division from __future__ import print_function from skimage import transform import tensorflow as tf import input # load the train and test data train_data, train_label = input.load_data('image/train.txt') test_data, test_label = input.load_data('image/val.txt') # resize rhe training and testing pictures train_data_m = [] for image in train_data: img64 = transform.resize(image, (64, 64, 3)) train_data_m.append(img64) test_data_m = [] for image in test_data: img64 = transform.resize(image, (64, 64, 3)) test_data_m.append(img64) # Create a graph to hold the model. graph = tf.Graph() # Create model in the graph. with graph.as_default(): # Placeholders for inputs and labels. images_ph = tf.placeholder(tf.float32, [None, 64, 64, 3])
import numpy as np from keras.utils import np_utils from keras.models import Sequential from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten from keras.optimizers import Adam import input (x1, y1, frame, cate) = input.load_data("group0") print(frame, cate) (x2, y2, frame, cate) = input.load_data("group1") print(frame, cate) (x3, y3, frame, cate) = input.load_data("group2") print(frame, cate) (x4, y4, frame, cate) = input.load_data("group3") print(frame, cate) (x5, y5, frame, cate) = input.load_data("group4") print(frame, cate) x = [] y = [] x.append(x1.reshape(-1, 1, frame, 20)) x.append(x2.reshape(-1, 1, frame, 20)) x.append(x3.reshape(-1, 1, frame, 20)) x.append(x4.reshape(-1, 1, frame, 20)) x.append(x5.reshape(-1, 1, frame, 20)) y.append(np_utils.to_categorical(y1, num_classes=cate)) y.append(np_utils.to_categorical(y2, num_classes=cate)) y.append(np_utils.to_categorical(y3, num_classes=cate)) y.append(np_utils.to_categorical(y4, num_classes=cate)) y.append(np_utils.to_categorical(y5, num_classes=cate)) f = open("5fold_test.txt", "w")
def do_train(nb_classes, log_dir): # load data next_feature, next_label = load_data(JSON_TRAIN, TRAIN_IMAGES_DIR, is_training=True, batch_size=BATCH_SIZE) x_test, y_test = load_data(JSON_VAL, VAL_IMAGES_DIR, is_training=False, batch_size=BATCH_SIZE) model = resnet50_model(nb_classes) # 可观察模型结构 model.summary() logging = TensorBoard(log_dir=log_dir) checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}.h5', monitor='val_loss', verbose=1, save_weights_only=True, save_best_only=True, period=5) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1) # math.ceil向上取整,一个epoch需要将数据集整个遍历一遍 steps_per_epoch = math.ceil(STEPS_PER_EPOCH_FOR_TRAIN) val_steps = math.ceil(STEPS_PER_EPOCH_FOR_EVAL) steps_per_epoch = math.ceil(500 / BATCH_SIZE) val_steps = math.ceil(500 / BATCH_SIZE) # validation_steps = int(320/BATCH_SIZE) print("steps_per_epoch:", steps_per_epoch) def top3_accuracy(y, predic): return metrics.top_k_categorical_accuracy(y, predic, k=3) # top3 = metrics.top_k_categorical_accuracy(labels, predic, k=3) adm = optimizers.Adam() # loss = losses.categorical_crossentropy() # Train with frozen layers first, to get a stable loss. # Adjust num epochs to your dataset. This step is enough to obtain a not bad model. if True: model.compile(optimizer=optimizers.Adam(lr=1e-3), loss="categorical_crossentropy", metrics=[metrics.categorical_accuracy, top3_accuracy]) hist1 = model.fit(next_feature, next_label, batch_size=None, epochs=NUM_EPOCHS_1, validation_data=(x_test, y_test), verbose=1, callbacks=[checkpoint], initial_epoch=0, steps_per_epoch=steps_per_epoch, validation_steps=val_steps) # duration_time = time.time() - start_time plt.plot(hist1.history["loss"]) plt.plot(hist1.history["val_loss"]) plt.title("model loss") plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/model_loss_stage1.png") plt.show() # """ plt.plot(hist1.history["categorical_accuracy"]) plt.plot(hist1.history["val_categorical_accuracy"]) plt.title("model accuracy") plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/model_top1_stage1.png") plt.show() plt.plot(hist1.history["top3_accuracy"]) plt.plot(hist1.history["val_top3_accuracy"]) plt.title("model accuracy3") plt.ylabel('accuracy3') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/model_top3_stage1.png") plt.show() model.save_weights(log_dir + 'trained_weights_stage1.h5') # Unfreeze and continue training, to fine-tune. # Train longer if the result is not good. if True: for i in range(len(model.layers)): model.layers[i].trainable = True # 可观察模型结构 model.summary() model.compile(optimizer=optimizers.Adam(lr=1e-4), loss="categorical_crossentropy", metrics=[metrics.categorical_accuracy, top3_accuracy]) hist2 = model.fit(next_feature, next_label, batch_size=None, epochs=NUM_EPOCHS_2, validation_data=(x_test, y_test), verbose=1, callbacks=[checkpoint, reduce_lr, early_stopping], initial_epoch=NUM_EPOCHS_1, steps_per_epoch=steps_per_epoch, validation_steps=val_steps) # duration_time = time.time() - start_time plt.plot(hist2.history["loss"]) plt.plot(hist2.history["val_loss"]) plt.title("model loss") plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/model_loss_stage2.png") plt.show() # """ plt.plot(hist2.history["categorical_accuracy"]) plt.plot(hist2.history["val_categorical_accuracy"]) plt.title("model accuracy") plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/model_top1_stage2.png") plt.show() plt.plot(hist2.history["top3_accuracy"]) plt.plot(hist2.history["val_top3_accuracy"]) plt.title("model accuracy3") plt.ylabel('accuracy3') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig(log_dir + "/fig/model_top3_stage2.png") plt.show() model.save_weights(log_dir + 'trained_weights_final.h5') # 评估模型 print("evaluate:") loss, accuracy, top3 = model.evaluate(x_test, y_test, verbose=1, steps=val_steps) print('Test loss:', loss) print('Test accuracy:', accuracy) print('Test accuracy of top3:', top3) return