# layer.trainable = False top_model = model.output top_model = Dropout(0.5, name='drop9')(top_model) top_model = Flatten()(top_model) top_model = Dense(600)(top_model) top_model = BatchNormalization()(top_model) top_model = Activation('relu', name='relu_conv9')(top_model) # top_model = Dense(600)(top_model) # top_model = Activation('relu', name='relu_conv10')(top_model) top_model = Dense(10)(top_model) top_model = BatchNormalization()(top_model) top_model = Activation('softmax', name='loss')(top_model) top_model = Model(model.input, top_model, name='top_net') top_model.load_weights(top_model_weights_path) #設定model參數 top_model.compile(loss=keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy']) #設定訓練紀錄 history = LossHistory(model, x_test, y_test) #開始訓練 top_model.fit_generator( generator=train_flow, steps_per_epoch=30000 // 20, verbose=2, validation_data=train_flow, validation_steps=5000 // 32, epochs=30, callbacks=[history]
def main2(X, X_te, y_te, words=None, tags=None, idx2word=None, idx2tag=None): from k.models import model_from_json from k.models import load_model # load json and create model # json_file = open('model_architecture.json', 'r') # loaded_model_json = json_file.read() # json_file.close() # loaded_model = model_from_json(loaded_model_json) # # load weights into new model # loaded_model.load_weights("model_weights.h5") # print("Loaded model from disk") # loaded_model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"]) # # load json and create model # with open('model.json', 'r') as json_file: # loaded_model_json = json_file.read() # loaded_model = model_from_json(loaded_model_json) # # load weights into new model # loaded_model.load_weights("model.h5") # # print("Loaded model from disk") # # Model reconstruction from JSON file # with open('model_architecture.json', 'r') as f: # loaded_model = model_from_json(f.read()) # Load weights into the new model # loaded_model.load_weights('model_weights.h5') # evaluate loaded model on test data # loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # loaded_model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"]) # loaded_model = load_model("My_Custom_Model3.h5") from keras_contrib.layers import CRF max_len = 50 n_tags = len(tags) n_words = len(words) word_input = Input(shape=(max_len, )) word_emb = Embedding(input_dim=n_words + 1, output_dim=20, input_length=max_len, mask_zero=True)(word_input) model = Dropout(0.1)(word_emb) model = Bidirectional( LSTM(50, return_sequences=True, recurrent_dropout=0.1))(model) model = TimeDistributed(Dense(50, activation="relu"))(model) crf = CRF(n_tags) out = crf(model) model = Model(inputs=word_input, outputs=out) model.compile(optimizer="rmsprop", loss=crf.loss_function, metrics=[crf.accuracy]) model.load_weights("model_weights.h5") """from keras.models import load_model loaded_model = load_model("My_Custom_Model.h5") print("Model Loaded.. Evaluating") # score = loaded_model.evaluate([X,second_input],Y, verbose=1) score = loaded_model.evaluate(X,Y, verbose=1) #print accuracy print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100)) if words is not None and tags is not None: i = 2318 p = loaded_model.predict(np.array([X[i]])) # p = loaded_model.predict([np.array(X[i]),second_input[i]]) p = np.argmax(p, axis=-1) print("{:15} ({:5}): {}".format("Word", "True", "Pred")) for w, pred in zip(X[i], p[0]): # p[0] = p[[1,3,4,5]] print("{:15}: {}".format(words[w], tags[pred])) # print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100)) # for x in score: # print(x) """ # from keras.models import load_model # loaded_model = load_model("My_Custom_Model3.h5") # loaded_model = "" # save_load_utils.load_all_weights(model, "Model_saved_using_contrib.h5") print("Model Loaded.. Evaluating again") score = model.evaluate(X_te, np.array(y_te), verbose=1) print("%s: %.2f%%" % (model.metrics_names[1], score[1] * 100)) # score = model.evaluate(X_te, np.array(y_te), verbose=1) # score = loaded_model.evaluate(X,Y, verbose=1) #print accuracy # print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100)) # if words is not None and tags is not None: # i = 2318 # p = loaded_model.predict(np.array([X[i]])) # # p = loaded_model.predict([np.array(X[i]),second_input[i]]) # p = np.argmax(p, axis=-1) # print("{:15} ({:5}): {}".format("Word", "True", "Pred")) # for w, pred in zip(X[i], p[0]): # p[0] => p[[1,3,4,5]] # print("{:15}: {}".format(words[w], tags[pred])) loaded_model = model i = 1 for x in X_te[i]: print(idx2word[x], end=" ") print(" ") if words is not None and tags is not None: p = loaded_model.predict(np.array([X_te[i]])) p = np.argmax(p, axis=-1) true = np.argmax(y_te[i], axis=-1) print("{:15} ({:5}): {}".format("Word", "True", "Pred")) print("{}th training example:".format(i)) for w, t, pred in zip(X_te[i], true, p[0]): if w != 0: print("{:15}: {:10} {}".format(idx2word[w - 1], idx2tag[t], idx2tag[pred])) print('*' * 50) print(p) print("len(p) = ", len(p))
def genderPredictionModel(h, w): inp_img = Input((h, w, 3)) model = ZeroPadding2D((1,1),input_shape=(224,224, 3))(inp_img) model = Convolution2D(64, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(64, (3, 3), activation='relu')(model) model = MaxPooling2D((2,2), strides=(2,2))(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(128, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(128, (3, 3), activation='relu')(model) model = MaxPooling2D((2,2), strides=(2,2))(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(256, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(256, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(256, (3, 3), activation='relu')(model) model = MaxPooling2D((2,2), strides=(2,2))(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(512, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(512, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(512, (3, 3), activation='relu')(model) model = MaxPooling2D((2,2), strides=(2,2))(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(512, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(512, (3, 3), activation='relu')(model) model = ZeroPadding2D((1,1))(model) model = Convolution2D(512, (3, 3), activation='relu')(model) model = MaxPooling2D((2,2), strides=(2,2))(model) model = Convolution2D(4096, (7, 7), activation='relu')(model) model = Dropout(0.5)(model) model = Convolution2D(4096, (1, 1), activation='relu')(model) model = Dropout(0.5)(model) model = Convolution2D(2622, (1, 1))(model) model = Flatten()(model) model = Activation('softmax')(model) model = Model(inp_img, model) # return Model(inp_img, model) model.load_weights('vgg_face_weights.h5') for layer in model.layers[:-7]: layer.trainable = False base_model_output = Sequential() base_model_output = Convolution2D(2, (1, 1), name='predictions')(model.layers[-4].output) base_model_output = Flatten()(base_model_output) base_model_output = Activation('softmax')(base_model_output) genderModel = Model(inputs=model.input, outputs=base_model_output) genderModel.load_weights("gender_model_weights.h5") return genderModel