def openAIGPTModel(*args, **kwargs): """ OpenAIGPTModel is the basic OpenAI GPT Transformer model based on identical stacked masked self-attention blocks and pre-trained on large scale dataset using language modeling signal. Example: # Load the tokenizer >>> import torch >>> tokenizer = torch.hub.load('huggingface/pytorch-pretrained-BERT', 'openAIGPTTokenizer', 'openai-gpt') # Prepare tokenized input >>> text = "Who was Jim Henson ? Jim Henson was a puppeteer" >>> tokenized_text = tokenizer.tokenize(text) >>> indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text) >>> tokens_tensor = torch.tensor([indexed_tokens]) # Load openAIGPTModel >>> model = torch.hub.load('huggingface/pytorch-pretrained-BERT', 'openAIGPTModel', 'openai-gpt') >>> model.eval() # Predict hidden states features for each layer >>> with torch.no_grad(): hidden_states = model(tokens_tensor) """ model = OpenAIGPTModel.from_pretrained(*args, **kwargs) return model
def extractOpenAI(): model = OpenAIGPTModel.from_pretrained(modelPath[args.model]) embeddings = model.tokens_embed print(embeddings.num_embeddings) print(embeddings.weight.size()) tokenizer = OpenAIGPTTokenizer.from_pretrained(modelPath[args.model]) weight = embeddings.weight.detach().numpy() #print(tokenizer.decoder) with open(programmingalpha.openAI768 + "embeddings.txt", "w") as f: for i in range(len(weight)): vec = weight[i] vec_str = list(map(lambda x: str(x), vec)) token = tokenizer.decoder[i] vec_str.insert(0, token) vec_str = " ".join(vec_str) f.writelines(vec_str + "\n") with open(programmingalpha.openAI768 + "vocab.txt", "w") as f: for i in range(len(weight)): token = tokenizer.decoder[i] f.write(token + "\n")
word_to_idx = pickle.load(f) train_val_dataset = FlowerDataset( img_folder=FLOWERS_DATA_ROOT + 'jpg', text_folder=FLOWERS_DATA_ROOT + 'train_val', word_to_idx=word_to_idx, idx_to_word=idx_to_word, ) if args.use_skip_thought: model = BayesianUniSkip('data/skip_thoughts', word_to_idx.keys()) elif args.use_bert: model = BertModel.from_pretrained('bert-base-uncased') model.eval() elif args.use_gpt: model = OpenAIGPTModel.from_pretrained('openai-gpt') model.eval() else: model = RnnEncoder(dict_size=len(word_to_idx), embed_size=args.embed_size, hidden_dim=args.hidden_dim, drop_prob=0.5) generator = Generator() discriminator = Discriminator() dataloader = torch.utils.data.DataLoader(train_val_dataset, batch_size=1, shuffle=True) model = model.to(device) generator = generator.to(device) discriminator = discriminator.to(device)
def inception_score(imgs, model_file, cuda=True, batch_size=32, resize=False, splits=1): """Computes the inception score of the generated images imgs imgs -- Torch dataset of (3xHxW) numpy images normalized in the range [-1, 1] cuda -- whether or not to run on GPU batch_size -- batch size for feeding into Inception v3 splits -- number of splits """ N = len(imgs) assert batch_size > 0 assert N > batch_size device = torch.device('cuda' if cuda else 'cpu') # Set up dtype if cuda: dtype = torch.cuda.FloatTensor else: if torch.cuda.is_available(): print( "WARNING: You have a CUDA device, so you should probably set cuda=True" ) dtype = torch.FloatTensor # Set up dataloader dataloader = torch.utils.data.DataLoader(imgs, batch_size=batch_size, drop_last=True) # Load generator and embeddings if args.use_skip_thought: model = BayesianUniSkip('data/skip_thoughts', imgs.word_to_idx.keys()) for param in model.parameters(): param.requires_grad = False elif args.use_bert: model = BertModel.from_pretrained('bert-base-uncased') model.eval() elif args.use_gpt: model = OpenAIGPTModel.from_pretrained('openai-gpt') model.eval() else: model = RnnEncoder(dict_size=len(word_to_idx), embed_size=args.embed_size, hidden_dim=args.rnn_hidden_dim, drop_prob=0.5) generator = Generator().to(device) trainer = Trainer(dataloader, model, generator, None, None, None, None, device, None) trainer.load_model(model_file) trainer.rnn_encoder.eval() trainer.generator.eval() # Load inception model inception_model = inception_v3(pretrained=True, transform_input=False).type(dtype) inception_model.eval() up = nn.Upsample(size=(299, 299), mode='bilinear').type(dtype) def get_pred(x): if resize: x = up(x) x = inception_model(x) return F.softmax(x).data.cpu().numpy() # Get predictions preds = np.zeros((N // batch_size, 1000)) for i, batch in enumerate(dataloader, 0): print("Calculating Inception Score... iter: {} / {} ".format( i, N // batch_size), end='\r') # batch = batch.type(dtype) # batchv = Variable(batch) imgs, caps, cap_lens, fake_caps, fake_cap_lens = trainer.prepare_data( batch) # Text embedding sent_emb, fake_sent_emb = trainer.embed_text(caps, cap_lens, fake_caps, fake_cap_lens, batch_size) batch_size_i = caps.size()[0] sampled = torch.randn((batch_size_i, generator.z_size)).to(device) batchv = generator(sent_emb, sampled) preds[i * batch_size:i * batch_size + batch_size_i] = get_pred(batchv) print() # Now compute the mean kl-div split_scores = [] for k in range(splits): part = preds[k * (N // splits):(k + 1) * (N // splits), :] py = np.mean(part, axis=0) scores = [] for i in range(part.shape[0]): pyx = part[i, :] scores.append(entropy(pyx, py)) split_scores.append(np.exp(np.mean(scores))) return np.mean(split_scores), np.std(split_scores)