def load_checkpoint(filepath, gpu=False, arch=None): """ Loads checkpoint and returns model representation according to checkpoint information :param filepath: str (path indicating location of checkpoint) :param gpu: bool (specifies if GPU should be used for Tensor mapping) :param arch: str (name of model architecture used as pre-trained model) :return: trained model and corresponding checkpoint dictionary """ device = torch.device("cuda" if gpu else "cpu") try: model_checkpoint = torch.load(filepath, map_location=device.type) except AssertionError: raise ModelLoadError # if no information on pre-trained model architecture: # 1. try to load from checkpoint # 2. if no information in checkpoint dict, used default checkpoint (vgg11, 3 hl [512, 256, 128]) if arch: arch = arch print(f"loading specified architecture ... {arch}") else: try: arch = model_checkpoint["arch"] print( f"loading architecture used from checkpoint dictionary ... {arch}" ) except KeyError: print( colored( f"NO ARCHITECTURE INFO PROVIDED AND NO INFORMATION IN CHECKPOINT... " f"load default (vgg11, 3 hl [512, 256, 128] ) {default_path_checkpoint}", "red")) arch = "vgg11" model_checkpoint = torch.load(default_path_checkpoint, map_location=device.type) # load used pre-trained model and freeze weights of pre-trained model # -> as previously, freeze weights of pre-trained model if further training of fc layers intended model = load_pretrained(arch) for param in model.parameters(): param.requires_grad = False # add fc network and load trained weights model.classifier = Classifier(model_checkpoint["input_size"], model_checkpoint["output_size"], model_checkpoint["hidden_layers"]) model.load_state_dict(model_checkpoint["state_dict"]) model.class_to_idx = model_checkpoint["class_to_idx"] return model, model_checkpoint
def main(): parser = argparse.ArgumentParser( description='Parser to evaluate Face Attribute Model') parser.add_argument('--mode', type=str, default='Test', help='Evaluation mode') parser.add_argument('--trained_model', type=str, required=True, help='Face-attr model to be evaluated') parser.add_argument( '--preprocessing', type=str, default='resnet50', help= 'Preprocessing method to be applied for the image, vgg16, inception_v3, or resnet50' ) parser.add_argument('--test', type=str, default='test.tfrecord', help='Test tfrecord file') parser.add_argument('--batch_size', type=int, default=32, help='Batch size') parser.add_argument('--img_size', type=int, default=224, help='Batch size') args = parser.parse_args() # Load Test data test_dataset = load_dataset(args.test, args.batch_size, (args.img_size, args.img_size), args.preprocessing) # Load model attr_model = load_pretrained(args.trained_model) # Define the training metrics metrics = define_metrics() # Compile the model using binary_crossentropy attr_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=metrics) scores = attr_model.evaluate(test_dataset) print('Test loss : {:.2%}'.format(scores[0])) print('Test accuracy : {:.2%}'.format(scores[1])) print('Test f1 : {:.2%}'.format(scores[2])) print('Test precision : {:.2%}'.format(scores[3])) print('Test recall : {:.2%}'.format(scores[4])) print('Test AUC : {:.2%}'.format(scores[5]))
def main(argv): options = argparser().parse_args(argv[1:]) logger.info(f'train.py arguments: {options}') # word_labels are the labels assigned to words in the original # data, token_labeler.labels() the labels assigned to tokens in # the tokenized data. The two are differentiated to allow distinct # labels to be added e.g. to continuation wordpieces. word_labels = load_labels(options.labels) token_labeler = IobesTokenLabeler(word_labels) num_labels = len(token_labeler.labels()) label_encoder = LabelEncoder(token_labeler.labels()) logger.info(f'token labels: {token_labeler.labels()}') logger.info('loading pretrained model') pretrained_model, tokenizer, config = load_pretrained( options.model_name, cache_dir=options.cache_dir) logger.info('pretrained model config:') logger.info(config) if options.max_seq_length > config.max_position_embeddings: raise ValueError(f'--max_seq_length {options.max_seq_length} not ' f'supported by model') seq_len = options.max_seq_length encode_tokens = lambda t: tokenizer.encode(t, add_special_tokens=False) document_loader = ConllLoader(tokenizer.tokenize, token_labeler.label_tokens, options.separator) example_generator = EXAMPLE_GENERATORS[options.examples]( seq_len, Token(tokenizer.cls_token, is_special=True, masked=False), Token(tokenizer.sep_token, is_special=True, masked=False), Token(tokenizer.pad_token, is_special=True, masked=True), encode_tokens, label_encoder.encode) train_documents = document_loader.load(options.train_data) dev_documents = document_loader.load(options.dev_data) # containers instead of generators for statistics train_documents = list(train_documents) dev_documents = list(dev_documents) log_dataset_statistics('train', train_documents) log_dataset_statistics('dev', dev_documents) decoder = ViterbiDecoder(label_encoder.label_map) decoder.estimate_probabilities(train_documents) logger.info(f'init_prob:\n{decoder.init_prob}') logger.info(f'trans_prob:\n{decoder.trans_prob}') train_examples = example_generator.examples(train_documents) dev_examples = example_generator.examples(dev_documents) # containers instead of generators for len() and logging train_examples = list(train_examples) dev_examples = list(dev_examples) num_train_examples = len(train_examples) log_examples(train_examples, count=2) train_x, train_y = examples_to_inputs(train_examples) dev_x, dev_y = examples_to_inputs(dev_examples) ner_model = build_ner_model(pretrained_model, num_labels, seq_len) optimizer, lr_schedule = get_optimizer( options.lr, options.num_train_epochs, options.batch_size, options.warmup_proportion, num_train_examples, ) ner_model.compile( optimizer=optimizer, loss='sparse_categorical_crossentropy', sample_weight_mode='temporal', # TODO is this necessary? metrics=['sparse_categorical_accuracy']) logger.info('ner model:') ner_model.summary(print_fn=logger.info) lr_history = LRHistory(lr_schedule) history = ner_model.fit(train_x, train_y, epochs=options.num_train_epochs, batch_size=options.batch_size, validation_data=(dev_x, dev_y), callbacks=[lr_history]) for k, v in history.history.items(): logger.info(f'{k} history: {v}') logger.info(f'lr history: {lr_history.by_epoch}') dev_predictions = ner_model.predict(dev_x, verbose=1, batch_size=options.batch_size) assert len(dev_examples) == len(dev_predictions) for example, preds in zip(dev_examples, dev_predictions): assert len(example.tokens) == len(preds) for pos, (token, pred) in enumerate(zip(example.tokens, preds)): token.predictions.append((pos, pred)) documents = unique(t.document for e in dev_examples for t in e.tokens if not t.is_special) check_predictions(documents) for n, r in evaluate_assign_labels_funcs(documents, label_encoder).items(): print(f'{n}: prec {r.prec:.2%} rec {r.rec:.2%} f {r.fscore:.2%}') summarize_predictions = PREDICTION_SUMMARIZERS[options.summarize_preds] assign_labels = LABEL_ASSIGNERS[options.assign_labels] for document in documents: summarize_predictions(document) assign_labels(document, label_encoder) for n, r in evaluate_viterbi(documents, decoder.init_prob, decoder.trans_prob, label_encoder).items(): print(f'{n}: prec {r.prec:.2%} rec {r.rec:.2%} f {r.fscore:.2%}') for document in documents: assign_labels(document, label_encoder) # greedy print(conlleval_report(documents)) if options.output_file is not None: with open(options.output_file, 'w') as out: write_conll(documents, out=out) if options.ner_model_dir is not None: save_ner_model(options.ner_model_dir, ner_model, decoder, tokenizer, word_labels, config) return 0
def main(): parser = argparse.ArgumentParser(description='Parser to train Face Attribute Model') parser.add_argument('--model', type=str, default='vgg16', help='Base model to use, vgg16, inception_v3, or resnet50') parser.add_argument('--model_dir', type=str, default='models', help='path to save model') parser.add_argument('--ckpt_path', type=str, default='checkpoint', help='path to save checkpoint') parser.add_argument('--pretrained', type=str, default=None, help='path to pretrained model') parser.add_argument('--train_tfrecord', type=str, required=True, help='Train tfrecord file') parser.add_argument('--val_tfrecord', type=str, required=True, help='Validation tfrecord file') parser.add_argument('--img_size', type=int, default=224, help='Size of image dataset') parser.add_argument('--num_classes', type=int, default=4, help='Number of classes') parser.add_argument('--batch_size', type=int, default=64, help='Size of image dataset') parser.add_argument('--epochs', type=int, default=40, help='Size of image dataset') parser.add_argument('--lr', type=float, default=1e-4, help='Learning rate') parser.add_argument('--summary', type=bool, default=True, help='Model summary will be shown if summary is True') args = parser.parse_args() base_model = args.model model_dir = args.model_dir train_tfrecord = args.train_tfrecord val_tfrecord = args.val_tfrecord num_classes = args.num_classes input_shape = (args.img_size, args.img_size, 3) img_size = (args.img_size, args.img_size) batch_size = args.batch_size num_epochs = args.epochs # Load train and validation data train_dataset = load_dataset(train_tfrecord, batch_size, img_size, base_model) val_dataset = load_dataset(val_tfrecord, batch_size, img_size, base_model) print('Dataset loaded') if args.pretrained: # Load pretrained model attr_model = load_pretrained(args.pretrained) print('Model loaded') else: # Build model attr_model = build_model(base_model, input_shape, num_classes) print('Model created') if args.summary: attr_model.summary() # Define the training metrics metrics = define_metrics() # Define callbacks callbacks = define_callbacks(args.ckpt_path) # Define the optimizer opt = Adam(learning_rate = args.lr) # Compile the model using binary_crossentropy attr_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=metrics) # Start training model attr_model.fit(train_dataset, epochs=num_epochs, validation_data=val_dataset, callbacks = callbacks, verbose=1) # Save the model model_filename = 'attr_model.h5' save_model(attr_model, model_dir, base_model)
def main(): parser = argparse.ArgumentParser( description='Parser for Predicting Face Attribute') parser.add_argument('--model_path', type=str, required=True, help='Path to model with .h5 extensions') parser.add_argument('--image', type=str, required=True, help='Image filename to be predicted') parser.add_argument( '--preprocessing', type=str, default='resnet50', help= 'Preprocessing method to be applied for the image, vgg16, inception_v3, or resnet50' ) parser.add_argument('--img_size', type=int, default=224, help='Image filename to be predicted') parser.add_argument('--save_image', type=bool, default=True, help='Image filename to be predicted') args = parser.parse_args() model_path = args.model_path preprocessing = args.preprocessing filename = args.image size = (args.img_size, args.img_size) results = None # Load model model = load_pretrained(model_path) # Load image img = np.array(load_img(filename)) # Detect faces using MTCNN face detector faces, bboxes = detect_face(img) if faces is not None: # Preprocess the image and expand its dimension trans_imgs = [ transform_image(face, size, preprocessing) for face in faces ] # Predict face attributes preds = model.predict(np.array(trans_imgs)) # Convert the output of predict and combine with its corresponding bbox results = convert_attribute(bboxes, preds) # Save the output image if args.save_image: img = draw_outputs(img, results) cv2.imwrite('out_{}'.format(filename), img) print('Output image saved at out_{}'.format(filename)) print(results)