when I run the code
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
SML=input("small,medium or large: ").lower()#I pick small
model_name = "microsoft/DialoGPT-"+SML
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
It downloads microsoft/DialoGPT-small from somewhere on the internet and downloads it to my computer but I don`t know where the saved file is. Anyone have any idea where this file is saved?
#EDIT#
I found the below code in \Lib\site-packages\transformers\models\dialogpt\convert_dialogpt_original_pytorch_checkpoint_to_pytorch.py but have no idea what it means.
def convert_dialogpt_checkpoint(checkpoint_path: str, pytorch_dump_folder_path: str):
d = torch.load(checkpoint_path)
d[NEW_KEY] = d.pop(OLD_KEY)
os.makedirs(pytorch_dump_folder_path, exist_ok=True)
torch.save(d, os.path.join(pytorch_dump_folder_path, WEIGHTS_NAME))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--dialogpt_path", default=".", type=str)
args = parser.parse_args()
for MODEL in DIALOGPT_MODELS:
checkpoint_path = os.path.join(args.dialogpt_path, f"{MODEL}_ft.pkl")
pytorch_dump_folder_path = f"./DialoGPT-{MODEL}"
convert_dialogpt_checkpoint(
checkpoint_path,
pytorch_dump_folder_path,
)
Related
I am trying to upload text files in streamlit and then am trying to load a ML model file that I have created to extract and display the entites in streamlit. I have given two choices 'Upload' and 'Entities Visualization'. However, after I have uploaded all the text files they are not being transferred to the Entites Visualization section. I have tried many different ways and getting the same output: Display the Entities. Please upload files first.
Can anyone provide any solution to this?
import streamlit as st
import os, random, spacy
from arcgis.learn import prepare_data
from arcgis.learn.text import EntityRecognizer
import tempfile
def color_gen():
random_number = random.randint(0,16777215) #16777215 ~= 256x256x256(R,G,B)
hex_number = format(random_number, 'x')
hex_number = '#' + hex_number
return hex_number
# Load spacy model
model_folder = os.path.join('models','invoice_reports')
nlp = spacy.load(model_folder)
def main():
uploaded_files = {}
choice = ""
with st.sidebar:
st.image("https://www.onepointltd.com/wp-content/uploads/2020/03/inno2.png")
st.title("Invoice-Extractor")
choice = st.radio("Navigation", ["Upload","Entities Visualization","Extract Entities", "Download"])
st.info("The model will extract entities and generate them in a csv file")
if choice == "Upload":
st.title("Upload your files for extraction!")
uploaded_file = st.file_uploader("Upload your dataset", accept_multiple_files=True)
if uploaded_file:
for f in uploaded_file:
uploaded_files[f.name] = f.read().decode("utf-8")
if choice == "Entities Visualization":
st.title("Display the Entities")
if not uploaded_files:
st.write("Please upload files first.")
else:
file_name = st.selectbox("Select a file", list(uploaded_files.keys()))
if file_name:
ner = EntityRecognizer(file_name, backbone='spacy')
txt = uploaded_files[file_name]
doc = nlp(txt)
colors = {ent.upper():color_gen() for ent in ner.entities}
options = {"ents":[ent.upper() for ent in ner.entities], "colors":colors}
st.write("Entity Extraction for:", file_name)
spacy.displacy.render(doc,jupyter=True, style='ent', options=options)
if choice == "Extract Entities":
pass
if choice == "Download":
pass
if __name__ == '__main__':
main()
I am training an image captioning model on Google Colab. While doing that, I can't open an image when it is there. I printed out the image path and found no faults there. What is the wrong I'm doing here?
Here's the image_path:
/content/gdrive/MyDrive/Thesis/BanglaImageCaptioning/image&caption/validation/7330.png
Here's the script:
!python predict.py --path image_path --checkpoint '/content/gdrive/MyDrive/Thesis/BanglaImageCaptioning/checkpoint.pth'
I can open the image with Image.open normally but it doesn't open with predict.py. I'm providing the predict.py below:
import torch
from transformers import BertTokenizer
from PIL import Image
import argparse
import matplotlib.pyplot as plt
from models import caption
from datasets import coco, utils
from configuration import Config
import os
parser = argparse.ArgumentParser(description='Image Captioning')
parser.add_argument('--path', type=str, help='path to image')
parser.add_argument('--checkpoint', type=str, help='checkpoint path')
args = parser.parse_args()
image_path = args.path
#version = args.v
checkpoint_path = args.checkpoint
config = Config()
# if version == 'v1':
# model = torch.hub.load('saahiluppal/catr', 'v1', pretrained=True)
# elif version == 'v2':
# model = torch.hub.load('saahiluppal/catr', 'v2', pretrained=True)
# elif version == 'v3':
# model = torch.hub.load('saahiluppal/catr', 'v3', pretrained=True)
# else:
print("Checking for checkpoint.")
if checkpoint_path is None:
raise NotImplementedError('No model to chose from!')
else:
if not os.path.exists(checkpoint_path):
raise NotImplementedError('Give valid checkpoint path')
print("Found checkpoint! Loading!")
model,_ = caption.build_model(config)
print("Loading Checkpoint...")
checkpoint = torch.load(checkpoint_path, map_location='cuda')
model.load_state_dict(checkpoint['model'])
tokenizer = BertTokenizer.from_pretrained("sagorsarker/bangla-bert-base")
start_token = tokenizer.convert_tokens_to_ids(tokenizer._cls_token)
end_token = tokenizer.convert_tokens_to_ids(tokenizer._sep_token)
image = Image.open(image_path)
image = coco.val_transform(image)
image = image.unsqueeze(0)
def create_caption_and_mask(start_token, max_length):
caption_template = torch.zeros((1, max_length), dtype=torch.long)
mask_template = torch.ones((1, max_length), dtype=torch.bool)
caption_template[:, 0] = start_token
mask_template[:, 0] = False
return caption_template, mask_template
caption, cap_mask = create_caption_and_mask(
start_token, config.max_position_embeddings)
#torch.no_grad()
def evaluate():
model.eval()
for i in range(config.max_position_embeddings - 1):
predictions = model(image, caption, cap_mask)
predictions = predictions[:, i, :]
predicted_id = torch.argmax(predictions, axis=-1)
if predicted_id[0] == 102:
return caption
caption[:, i+1] = predicted_id[0]
cap_mask[:, i+1] = False
return caption
output = evaluate()
######################################
#print(tokenizer.decode(output[0][2]))
for i in range(len(output[0][1:16])):
print(tokenizer.decode(output[0][i]))
########################################
result = tokenizer.decode(output[0].tolist(), skip_special_tokens=True)
#result = tokenizer.decode(output[0], skip_special_tokens=True)
print("predicted caption: {}".format(result))
Here's the error msg:
Checking for checkpoint.
Found checkpoint! Loading!
/usr/local/lib/python3.8/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
warnings.warn(
/usr/local/lib/python3.8/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet101_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet101_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
Loading Checkpoint...
Traceback (most recent call last):
File "predict.py", line 47, in <module>
image = Image.open(image_path)
File "/usr/local/lib/python3.8/dist-packages/PIL/Image.py", line 2843, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'image_path'
It looks like the issue is related to the way you're passing the image_path argument to your script.
In this line:
!python predict.py --path image_path --checkpoint '/content/gdrive/MyDrive/Thesis/BanglaImageCaptioning/checkpoint.pth'
You're passing the string "image_path" as the argument instead of the actual path.
It should be:
!python predict.py --path /content/gdrive/MyDrive/Thesis/BanglaImageCaptioning/image&caption/validation/7330.png --checkpoint '/content/gdrive/MyDrive/Thesis/BanglaImageCaptioning/checkpoint.pth'
So the full path to the image should be used in the command instead of a variable name
It would be a good idea to check if the image is there in the given path before trying to open it.
Also, the ! before python is not needed since it's running on the same runtime environment of the colab.
It will open the image correctly and perform the captioning task.
I have two python files, when I run "python downloader.py todownload" it downloads images from todownload.txt file, but when I run it indirectly (calling function of downloader.py via other python file) I don't get the images downloaded.
I tried debugging and printing what arguments are sent to function "download_all_images(args)" when I call it both ways, and the object args match.
downloader.py
import argparse
from concurrent import futures
import os
import re
import sys
import boto3
import botocore
import tqdm
BUCKET_NAME = 'open-images-dataset'
REGEX = r'(test|train|validation|challenge2018)/([a-fA-F0-9]*)'
def check_and_homogenize_one_image(image):
split, image_id = re.match(REGEX, image).groups()
yield split, image_id
def check_and_homogenize_image_list(image_list):
for line_number, image in enumerate(image_list):
try:
yield from check_and_homogenize_one_image(image)
except (ValueError, AttributeError):
raise ValueError(
f'ERROR in line {line_number} of the image list. The following image '
f'string is not recognized: "{image}".')
def read_image_list_file(image_list_file):
with open(image_list_file, 'r') as f:
for line in f:
yield line.strip().replace('.jpg', '')
def download_one_image(bucket, split, image_id, download_folder):
try:
bucket.download_file(f'{split}/{image_id}.jpg',
os.path.join(download_folder, f'{image_id}.jpg'))
except botocore.exceptions.ClientError as exception:
sys.exit(
f'ERROR when downloading image `{split}/{image_id}`: {str(exception)}')
def download_all_images(args):
print(type(args))
print(args)
"""Downloads all images specified in the input file."""
bucket = boto3.resource(
's3', config=botocore.config.Config(
signature_version=botocore.UNSIGNED)).Bucket(BUCKET_NAME)
download_folder = args['download_folder'] or os.getcwd()
if not os.path.exists(download_folder):
os.makedirs(download_folder)
try:
image_list = list(
check_and_homogenize_image_list(
read_image_list_file(args['image_list'])))
except ValueError as exception:
sys.exit(exception)
progress_bar = tqdm.tqdm(
total=len(image_list), desc='Downloading images', leave=True)
with futures.ThreadPoolExecutor(
max_workers=args['num_processes']) as executor:
all_futures = [
executor.submit(download_one_image, bucket, split, image_id,
download_folder) for (split, image_id) in image_list
]
for future in futures.as_completed(all_futures):
future.result()
progress_bar.update(1)
progress_bar.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'image_list',
type=str,
default=None,
help=('Filename that contains the split + image IDs of the images to '
'download. Check the document'))
parser.add_argument(
'--num_processes',
type=int,
default=5,
help='Number of parallel processes to use (default is 5).')
parser.add_argument(
'--download_folder',
type=str,
default=None,
help='Folder where to download the images.')
download_all_images(vars(parser.parse_args()))
imagegatherer.py
import pandas as pd
import argparse
from concurrent import futures
import os
import re
import sys
import boto3
import botocore
import tqdm
import downloader
labels = pd.read_csv('class-descriptions-boxable.csv', header=None)
labels.columns = ['id', 'name']
classified = pd.read_csv('train-annotations-human-imagelabels-boxable.csv', nrows=2500)
classes = ['Apple', 'Motorcycle', 'Snowman']
ids = labels[labels['name'].isin(classes)]
for entry in ids.iterrows():
id = entry[1][0]
name = entry[1][1]
good = classified.loc[classified['LabelName'] == id]['ImageID'].tolist()
with open('todownload.txt', 'w') as file:
for img in good:
file.write('train/' + img + '\n')
args = dict()
args['image_list'] = 'todownload.txt'
args['num_processes'] = 5
args['download_folder'] = None
downloader.download_all_images(args)
todownload.txt content that should download 4 images:
train/0000048549557964
train/000023aa04ab09ed
train/00002f4ff380c64c
train/000037c2dd414b46
I am referring (here) to freeze models into .pb file. My model is CNN for text classification I am using (Github) link to train CNN for text classification and exporting in form of models. I have trained models to 4 epoch and My checkpoints folders look as follows:
I want to freeze this model into (.pb file). For that I am using following script:
import os, argparse
import tensorflow as tf
# The original freeze_graph function
# from tensorflow.python.tools.freeze_graph import freeze_graph
dir = os.path.dirname(os.path.realpath(__file__))
def freeze_graph(model_dir, output_node_names):
"""Extract the sub graph defined by the output nodes and convert
all its variables into constant
Args:
model_dir: the root folder containing the checkpoint state file
output_node_names: a string, containing all the output node's names,
comma separated
"""
if not tf.gfile.Exists(model_dir):
raise AssertionError(
"Export directory doesn't exists. Please specify an export "
"directory: %s" % model_dir)
if not output_node_names:
print("You need to supply the name of a node to --output_node_names.")
return -1
# We retrieve our checkpoint fullpath
checkpoint = tf.train.get_checkpoint_state(model_dir)
input_checkpoint = checkpoint.model_checkpoint_path
# We precise the file fullname of our freezed graph
absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1])
output_graph = absolute_model_dir + "/frozen_model.pb"
# We clear devices to allow TensorFlow to control on which device it will load operations
clear_devices = True
# We start a session using a temporary fresh Graph
with tf.Session(graph=tf.Graph()) as sess:
# We import the meta graph in the current default Graph
saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)
# We restore the weights
saver.restore(sess, input_checkpoint)
# We use a built-in TF helper to export variables to constants
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess, # The session is used to retrieve the weights
tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes
output_node_names.split(",") # The output node names are used to select the usefull nodes
)
# Finally we serialize and dump the output graph to the filesystem
with tf.gfile.GFile(output_graph, "wb") as f:
f.write(output_graph_def.SerializeToString())
print("%d ops in the final graph." % len(output_graph_def.node))
return output_graph_def
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--model_dir", type=str, default="", help="Model folder to export")
parser.add_argument("--output_node_names", type=str, default="", help="The name of the output nodes, comma separated.")
args = parser.parse_args()
freeze_graph(args.model_dir, args.output_node_names)
I am using following argument parser to run the above code
python3 freeze_graph.py --model_dir /Users/path_to_checkpoints/ --output_node_names softmax
It is giving error
assert d in name_to_node_map, "%s is not in graph" % d
AssertionError: softmax is not in graph
My model is CNN for text classification. What should I write in output_node_names ? to produce a successful .pb file in the output
Use the below script to print the tensors... the last tensor would be the output tensor.
Original author: https://blog.metaflow.fr/tensorflow-how-to-freeze-a-model-and-serve-it-with-a-python-api-d4f3596b3adc
import argparse
import tensorflow as tf
def print_tensors(pb_file):
print('Model File: {}\n'.format(pb_file))
# read pb into graph_def
with tf.gfile.GFile(pb_file, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# import graph_def
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def)
# print operations
for op in graph.get_operations():
print(op.name + '\t' + str(op.values()))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--pb_file", type=str, required=True, help="Pb file")
args = parser.parse_args()
print_tensors(args.pb_file)
In task2.py , I am executing a python file like this:
text = os.system("python new_image.py --image_file " + "/home/roots/" + str(nc) + ".jpg --num_top_predictions 1")
The essential lines of new_image.py look like
def main(_):
maybe_download_and_extract()
image = (FLAGS.image_file if FLAGS.image_file else
os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
score = run_inference_on_image(image)
return score
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--model_dir',
type=str,
default='/tmp/imagenet',
help="""\
Path to classify_image_graph_def.pb,
imagenet_synset_to_human_label_map.txt, and
imagenet_2012_challenge_label_map_proto.pbtxt.\
"""
)
parser.add_argument(
'--image_file',
type=str,
default='',
help='Absolute path to image file.'
)
parser.add_argument(
'--num_top_predictions',
type=int,
default=5,
help='Display this many predictions.'
)
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
I have modified classify_image.py in tensorflow models imagenet such that run_inference_on_image() returns a list. I would now like to get this list in task2.py. How will I do this?
An easy way is to save the list as a temporary pickle file in new_image.py, and load the pickle file in task2.py. i.e.
# In new_image.py
import pickle
picklefile = open('temp.pkl', 'wb')
# Dump list to the pickle file
pickle.dump(listvariable, picklefile)
picklefile.close()
And,
# In task2.py
import pickle
picklefile = open('temp.pkl', 'rb')
newlist_variable = pickle.load(picklefile)
picklefile.close()
I don't know if there is a way to share dynamic variables between two python processes. But if there is, I'd love to know!