This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MODEL: | |
| TYPE: generalized_rcnn | |
| CONV_BODY: FPN.add_fpn_ResNet50_conv5_body | |
| NUM_CLASSES: 4 | |
| FASTER_RCNN: True | |
| NUM_GPUS: 1 | |
| SOLVER: | |
| WEIGHT_DECAY: 0.0001 | |
| LR_POLICY: steps_with_decay | |
| BASE_LR: 0.0025 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # --*-- coding: utf-8 --*-- | |
| import numpy as np | |
| import json | |
| import matplotlib.pyplot as plt | |
| import cv2 | |
| from random import shuffle | |
| import os | |
| categories_dict ={1: 'upper', 2: 'upper', 3: 'upper', 4: 'upper', 5: 'upper', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| friendship | |
| beauty | |
| beautiful | |
| engage | |
| engaged | |
| engagement | |
| love | |
| lover | |
| romantic | |
| romance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Evaluation Function | |
| def evaluate(model, test_loader, version='title', threshold=0.5): | |
| y_pred = [] | |
| y_true = [] | |
| model.eval() | |
| with torch.no_grad(): | |
| for (labels, (title, title_len), (text, text_len), (titletext, titletext_len)), _ in test_loader: | |
| labels = labels.to(device) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train_loss_list, valid_loss_list, global_steps_list = load_metrics(destination_folder + '/metrics.pt') | |
| plt.plot(global_steps_list, train_loss_list, label='Train') | |
| plt.plot(global_steps_list, valid_loss_list, label='Valid') | |
| plt.xlabel('Global Steps') | |
| plt.ylabel('Loss') | |
| plt.legend() | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Training Function | |
| def train(model, | |
| optimizer, | |
| criterion = nn.BCELoss(), | |
| train_loader = train_iter, | |
| valid_loader = valid_iter, | |
| num_epochs = 5, | |
| eval_every = len(train_iter) // 2, | |
| file_path = destination_folder, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Save and Load Functions | |
| def save_checkpoint(save_path, model, optimizer, valid_loss): | |
| if save_path == None: | |
| return | |
| state_dict = {'model_state_dict': model.state_dict(), | |
| 'optimizer_state_dict': optimizer.state_dict(), | |
| 'valid_loss': valid_loss} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LSTM(nn.Module): | |
| def __init__(self, dimension=128): | |
| super(LSTM, self).__init__() | |
| self.embedding = nn.Embedding(len(text_field.vocab), 300) | |
| self.dimension = dimension | |
| self.lstm = nn.LSTM(input_size=300, | |
| hidden_size=dimension, | |
| num_layers=1, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Fields | |
| label_field = Field(sequential=False, use_vocab=False, batch_first=True, dtype=torch.float) | |
| text_field = Field(tokenize='spacy', lower=True, include_lengths=True, batch_first=True) | |
| fields = [('label', label_field), ('title', text_field), ('text', text_field), ('titletext', text_field)] | |
| # TabularDataset | |
| train, valid, test = TabularDataset.splits(path=source_folder, train='train.csv', validation='valid.csv', test='test.csv', | |
| format='CSV', fields=fields, skip_header=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Libraries | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import torch | |
| # Preliminaries | |
| from torchtext.data import Field, TabularDataset, BucketIterator |