def main(): if DO_TEST: test() model = ResNet50(input_shape=(64, 64, 3), classes=6) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset( ) # Normalize image vectors X_train = X_train_orig / 255. X_test = X_test_orig / 255. # Convert training and test labels to one hot matrices 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)) model.fit(X_train, Y_train, epochs=7, batch_size=64) my_preds = model.evaluate(X_test, Y_test) print("My Loss = " + str(my_preds[0])) print("My Test Accuracy = " + str(my_preds[1])) model = load_model('../trained_model/ResNet50.h5') preds = model.evaluate(X_test, Y_test) print("Their Loss = " + str(preds[0])) print("Their Test Accuracy = " + str(preds[1]))
def Load_Model(themodel): model = load_model(themodel) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = resnets_utils.load_dataset() # Normalize image vectors X_train = X_train_orig / 255. X_test = X_test_orig / 255. # Convert training and test labels to one hot matrices Y_train = resnets_utils.convert_to_one_hot(Y_train_orig, 6).T Y_test = resnets_utils.convert_to_one_hot(Y_test_orig, 6).T # 评估模型 preds = model.evaluate(X_test, Y_test) print("误差值 = " + str(preds[0])) print("准确率 = " + str(preds[1])) return model
def Train_Model(): # print(datetime.datetime.now()) # 实体化并编译 model = ResNet50(input_shape=(64, 64, 3), classes=6) model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = resnets_utils.load_dataset() # Normalize image vectors X_train = X_train_orig / 255. X_test = X_test_orig / 255. # Convert training and test labels to one hot matrices Y_train = resnets_utils.convert_to_one_hot(Y_train_orig, 6).T Y_test = resnets_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)) """ number of training examples = 1080 number of test examples = 120 X_train shape: (1080, 64, 64, 3) Y_train shape: (1080, 6) X_test shape: (120, 64, 64, 3) Y_test shape: (120, 6) """ # 训练 model.fit(X_train, Y_train, epochs=40, batch_size=32) # 评估模型 preds = model.evaluate(X_test, Y_test) print("误差值 = " + str(preds[0])) print("准确率 = " + str(preds[1]))
model = Model(inputs=input, outputs=output) return model if __name__ == '__main__': X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = resnets_utils.load_dataset( ) # Normalize image vectors X_train = X_train_orig / 255. X_test = X_test_orig / 255. # Convert training and test labels to one hot matrices Y_train = resnets_utils.convert_to_one_hot(Y_train_orig, 6).T Y_test = resnets_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)) model = ResNet50(input_shape=(64, 64, 3), classes=6) model.summary() plot_model(model, to_file='model.png') SVG(model_to_dot(model).create(prog='dot', format='svg'))
return model model = ResNet50(input_shape=(64, 64, 3), classes=6) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset() # Normalize image vectors X_train = X_train_orig / 255. X_test = X_test_orig / 255. # Convert training and test labels to one hot matrices 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)) start_time = time.time() model.fit(X_train, Y_train, epochs=10, batch_size=32) end_time = time.time() print("训练模型所花的时间为:%d秒" % (end_time - start_time)) preds = model.evaluate(X_test, Y_test)