def _train(self) -> Model: data, label = load_data0_cycle() train_data, test_data, train_label, test_label = train_test_split( data, label, test_size=0.2) train_data = np.reshape(train_data, train_data.shape + (1, )) train_label = to_categorical(train_label) test_data = np.reshape(test_data, test_data.shape + (1, )) test_label = to_categorical(test_label) network_input = Input(shape=(8, 200, 1)) # 如果这里修改了网络结构,记得去下面修改可视化函数里的参数 network = Conv2D(filters=20, kernel_size=(1, 10))(network_input) network = Conv2D(filters=40, kernel_size=(4, 10), activation=tanh)(network) network = MaxPool2D((2, 2))(network) network = Flatten()(network) network = Dense(units=40, activation=tanh)(network) network = Dense(units=10, activation=softmax)(network) network = Model(inputs=[network_input], outputs=[network]) network.compile(optimizer=RMSprop(), loss=categorical_crossentropy, metrics=[categorical_accuracy]) network.summary() self.train_history = network.fit(train_data, train_label, batch_size=32, epochs=16) self.evaluate_history = network.evaluate(test_data, test_label) return network
def model_fashion_vgg16(): filepath = './model/model_cifar_vgg16.hdf5' (X_train, Y_train), (X_test, Y_test) = cifar10.load_data() # 32*32 X_train = X_train.astype('float32').reshape(-1, 32, 32, 3) X_test = X_test.astype('float32').reshape(-1, 32, 32, 3) X_train /= 255 X_test /= 255 y_train = Y_train.reshape(-1) y_test = Y_test.reshape(-1) ### modify print('Train:{},Test:{}'.format(len(X_train), len(X_test))) nb_classes = 10 y_train = np_utils.to_categorical(y_train, nb_classes) y_test = np_utils.to_categorical(y_test, nb_classes) print('data success') # base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(28, 28, 1)) model_vgg = VGG16(include_top=False, weights='imagenet', input_shape=(32, 32, 3)) # for layer in model_vgg.layers: # 冻结权重 # layer.trainable = False model = Flatten()(model_vgg.output) model = Dense(1024, activation='relu', name='fc1')(model) # model = Dropout(0.5)(model) model = Dense(512, activation='relu', name='fc2')(model) # model = Dropout(0.5)(model) model = Dense(10, activation='softmax', name='prediction')(model) model = Model(inputs=model_vgg.input, outputs=model, name='vgg16_pretrain') model.summary() model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_accuracy', mode='auto', save_best_only='True') model.fit(X_train, y_train, batch_size=128, nb_epoch=30, validation_data=(X_test, y_test), callbacks=[checkpoint]) model = load_model(filepath) score = model.evaluate(X_test, y_test, verbose=0) print(score)
def model_fashion_vgg16(): filepath = './model/model_svhn_vgg16.hdf5' (X_train, y_train), (X_test, y_test) = SVNH_DatasetUtil.load_data() ### modify print('Train:{},Test:{}'.format(len(X_train), len(X_test))) model_vgg = VGG16(include_top=False, weights='imagenet', input_shape=(32, 32, 3)) # for layer in model_vgg.layers: # 冻结权重 # # layer.trainable = False # model_vgg = VGG16(include_top=False, weights='imagenet', input_shape=(32, 32, 3)) # model_vgg = VGG16(include_top=False, weights=None, input_shape=(32, 32, 3)) model = Flatten()(model_vgg.output) model = Dense(1024, activation='relu', name='fc1')(model) # model = Dropout(0.5)(model) model = Dense(512, activation='relu', name='fc2')(model) # model = Dropout(0.5)(model) model = Dense(10, activation='softmax', name='prediction')(model) model = Model(inputs=model_vgg.input, outputs=model, name='vgg16_pretrain') model.summary() model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_accuracy', mode='auto', save_best_only='True') model.fit(X_train, y_train, batch_size=64, nb_epoch=15, validation_data=(X_test, y_test), callbacks=[checkpoint]) model = load_model(filepath) score = model.evaluate(X_test, y_test, verbose=0) print(score)
model = Activation('relu')(model) model = BatchNormalization()(model) model = Dense(1)(model) model = Activation('sigmoid')(model) model = Model(inp, model) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.fit(x_train, Y_train, batch_size=16, epochs=10) score = model.evaluate(x_test, Y_test, batch_size=16) print x_test y_pred = model.predict(x_test, batch_size=64) Y_pred = [] print y_pred for i in range(0,len(y_pred)): if y_pred[i][0] >= y_pred[i][1]: a = 1 else: a = -1 Y_pred.append(a)
save_best_only=True), ] # Run training print('Training model') model.fit(x=X_train, y=y_train, batch_size=BATCH_SIZE, validation_data=(X_valid, y_valid), epochs=1000, callbacks=callbacks) # Reload best model model = load_model('output/{}.h5'.format(FILENAME)) finally: # Evaluate test dataset print('Evaluating test dataset') X_test = get_evaluated(X_test, 'test', BATCH_SIZE) loss, acc = model.evaluate(X_test, y_test, batch_size=BATCH_SIZE) prediction = model.predict(X_test) # Save results with open('output/{}.json'.format(FILENAME), 'w') as f: json.dump( { 'test_accuracy': acc, 'test_loss': loss, 'y_true': y_test.tolist(), 'y_pred': prediction.tolist(), }, f) print('\nTest Loss: {:.4} | Accuracy: {:.4}'.format(loss, acc))
train_y = to_categorical(train_y) train_X_feature = train_X_feature.reshape(train_X_feature.shape[0], 1, train_X_feature.shape[1], train_X_feature.shape[2]) model.fit([train_X_feature], train_y, nb_epoch=20, verbose=1, # validation_split=0.1, # validation_data=(validation_X, validation_y), shuffle=True, batch_size=32, # callbacks=[early_stop] ) print(model.evaluate(train_X_feature, train_y)) model.summary() quit() logging.debug('=' * 20) # **************************************************************** # ------------- region end : 3、构建onehot编码 ------------- # **************************************************************** logging.debug('=' * 20) # ------------------------------------------------------------------------------ # -------------- region end : 预测 ---------------
#model = MaxPooling2D(pool_size=(2,2), border_mode='valid')(model) model = Flatten()(model) model = Dense(output_dim=15, activation='softmax')(model) model = Dropout(p=0.65)(model) model = Dense(output_dim=10, activation='softmax')(model) model = Dropout(p=0.8)(model) # Apply new optimizer ADAM for the model model = Model(input=x, output=model) model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['categorical_accuracy']) batch_size = 25 batch_num = 0 test_images = np.concatenate([reshape(x) for x in mnist.test.images]) test_labels = mnist.test.labels for i in range(1, 10000): data = mnist.train.next_batch(batch_size) batch = np.concatenate([reshape(x) for x in data[0]]) label = data[1] batch_num += batch_size if i % 1000 == 0: print("Trained on " + str(batch_num) + " images") results = model.evaluate(x=test_images, y=test_labels, verbose=0, batch_size=batch_size) print("Cost Function: %s \n Accuracy: %s" % (results[0], results[1])) model.train_on_batch(batch, label)
output_label = Dense(1, activation='sigmoid')(model) model = Model(inputs=[question1, question2], outputs=output_label) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() callbacks = [ ModelCheckpoint(MODEL_WEIGHTS_FILE, monitor='val_acc', save_best_only=True) ] history = model.fit([q1_trainset, q2_trainset], Y_train, epochs=NB_EPOCHS, validation_split=VALIDATION_SPLIT, verbose=2, batch_size=BATCH_SIZE, callbacks=callbacks) import matplotlib.pyplot as plt plt.figure() plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) model.save(MODEL_WEIGHTS_FILE) model.load_weights(model_weight_file) loss, accuracy = model.evaluate([q1_testset, q2_testset], Y_test, verbose=0) print('loss = {0:.4f}, accuracy = {1:.4f}'.format(loss, accuracy))
Y_train = (np.arange(10) == y_train[:, None]).astype(int) # Y_test = (np.arange(10) == y_test[:, None]).astype(int) # Normalize to [0,1] X_train = X_train / 255 X_test = X_test / 255 start = datetime.now() history = model.fit(X_train, Y_train, batch_size=tBatchSize, epochs=5, shuffle=True, validation_split=0.3) duration = datetime.now() - start print("Training completed in time: ", duration) score = model.evaluate(X_test, Y_test, batch_size=tBatchSize) print('Test Loss:', score[0]) print('Test Accuracy:', score[1]) import matplotlib.pyplot as plt plt.plot(history.history["accuracy"]) plt.plot(history.history['val_accuracy']) plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title("Model accuracy") plt.ylabel("Accuracy") plt.xlabel("Epoch") plt.legend(["Accuracy", "Validation Accuracy", "Loss", "Validation Loss"]) plt.show()
#callbacks=callbacks_list #callbacks=[learning_rate_reduction] ) plt.figure(1) plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.legend(['loss', 'val_loss'], loc='upper right') plt.title('Learning curve for the training') plt.figure(2) plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.legend(['acc', 'val_acc'], loc='upper right') plt.title('Accuracy for the training') X_train = x_train / 255. X_val = x_val / 255. X_test = x_test / 255. scores = model.evaluate(X_train, y_train, batch_size=1, verbose=0) print("\n%s: %.2f%% (Train)" % (model.metrics_names[1], scores[1] * 100)) scores = model.evaluate(X_val, y_val, batch_size=1, verbose=0) print("\n%s: %.2f%% (Val)" % (model.metrics_names[1], scores[1] * 100)) scores = model.evaluate(X_test, y_test, batch_size=1, verbose=0) print("\n%s: %.2f%% (Test)" % (model.metrics_names[1], scores[1] * 100)) pred_test = np.argmax(model.predict(X_test, batch_size=1, verbose=0), axis=1) true_test = np.argmax(y_test, axis=1) print(confusion_matrix(true_test, pred_test)) plt.show()